add JSON support (%j)

This commit is contained in:
chocolateboy 2015-08-17 08:29:28 +01:00
parent 35d4cf5f4f
commit a2f2c59869
2 changed files with 6 additions and 1 deletions

View file

@ -97,6 +97,9 @@
tmp = String(parseFloat(nextArg()).toFixed(precision || 6));
result += leadingZero ? tmp : tmp.replace(/^0/, '');
break;
case 'j': // JSON
result += JSON.stringify(nextArg());
break;
case 'o': // number in octal
result += '0' + parseInt(nextArg(), 10).toString(8);
break;

View file

@ -40,7 +40,9 @@ var tests = [
[['%.2f', 3.14159], '3.14'],
[['%0.2f', 3.14159], '3.14'],
[['%.2f', 0.1234], '.12'],
[['%0.2f', 0.1234], '0.12']
[['%0.2f', 0.1234], '0.12'],
[['foo %j', 42], 'foo 42'],
[['foo %j', '42'], 'foo "42"']
];
tests.forEach(function(spec) {
var args = spec[0];