diff --git a/format.js b/format.js index b035911..772a797 100644 --- a/format.js +++ b/format.js @@ -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; diff --git a/test_format.js b/test_format.js index b7a7dee..9cd6754 100644 --- a/test_format.js +++ b/test_format.js @@ -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];