Skip to content

Commit

Permalink
test older node and util-less operation
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasq committed Mar 28, 2021
1 parent 1d01e16 commit 4820653
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
5 changes: 3 additions & 2 deletions qprintf.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports.lib = {
countTrailingZeros: countTrailingZeros,
};

var util = tryCall(function() { return require("util") });
var util = tryCall(function() { return !process.env.TEST_WITHOUT_UTIL && require("util") });

var nodeVersion = parseFloat(process.versions.node);
var maxToFixedPrecision = tryCall(function() { return (1).toFixed(100) && 100 }) || 20;
Expand Down Expand Up @@ -335,6 +335,7 @@ function convertFloat( width, padChar, rightPad, signChar, v, precision ) {
return padNumber(width, padChar, rightPad, signChar, formatFloat(v, precision));
}

// format the exponent like C: "e+00" with 'e' or 'E', sign, and at least two digits
function _formatExp( exp, e ) {
return (
(exp <= -10) ? e+"-" + -exp :
Expand Down Expand Up @@ -452,7 +453,7 @@ function formatFloat( v, precision ) {
}

/*
* format a %N.Mg float with optional rounding and trailing zero-decimal trimming
* format a %N.Mg float with optional trailing decimal-zero trimming and optional rounding
*
* note: both C and PHP render ("%5.2f", 1.275) as "1.27", because of the IEEE representation
* works for positive values only, but thats all we use
Expand Down
65 changes: 64 additions & 1 deletion test-qprintf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the Apache License, Version 2.0
*/

qprintf = require('./qprintf');
qprintf = require('./');
vsprintf = qprintf.vsprintf;
sprintf = qprintf.sprintf;

Expand Down Expand Up @@ -807,6 +807,69 @@ module.exports = {
},
},

'should work with node-v0.8': {
before: function(done) {
this.nodeVersion = process.versions.node;
// delete first, assignment does not overwrite
delete process.versions.node;
Object.defineProperty(process.versions, 'node', {value: '0.8.28'});
process.versions.node = '0.8.28';
done();
},

after: function(done) {
process.versions.node = this.nodeVersion;
done();
},

'formatFloat uses native precision': function(t) {
// verify manually by looking at code coverage
t.disrequire('./');
var qprintf = require('./');
t.equal(qprintf.lib.formatFloat(1.25, 5), '1.25000');
t.done();
},

'can format an object': function(t) {
t.disrequire('./');
var qprintf = require('./');
t.equal(qprintf.sprintf("%2.1O", {a: 1, b: {c: {d: 2}}}), '{ a: 1, b: { c: [Object] } }');
t.done();
},

'can format an array': function(t) {
t.disrequire('./');
var qprintf = require('./');
t.equal(qprintf.sprintf("%2.1A", [1, 2, 3]), '[ 1, 2, ... ]');
t.done();
},
},

'should work without util': {
setUp: function(done) {
process.env.TEST_WITHOUT_UTIL = '1';
done();
},
tearDown: function(done) {
delete process.env.TEST_WITHOUT_UTIL;
done();
},

'can format an object': function(t) {
t.disrequire('./');
var qprintf = require('./');
t.equal(qprintf.sprintf("%2.1O", {a: 1, b: {c: {d: 2}}}), '[Object]');
t.done();
},

'can format an array': function(t) {
t.disrequire('./');
var qprintf = require('./');
t.equal(qprintf.sprintf("%2.1A", [1, 2, 3]), '[Array]');
t.done();
},
},

'speed of 10k string+num': function(t) {
var t1 = Date.now();
for (var i=0; i<100000; i++) {
Expand Down

0 comments on commit 4820653

Please sign in to comment.