Merge pull request #7 from chocolateboy/json

add JSON support (%j)
This commit is contained in:
Sami Samhuri 2015-08-30 16:54:40 -07:00
commit 4f89809675
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];