From a2f2c598697c5b709d096a355a32616478db25bc Mon Sep 17 00:00:00 2001 From: chocolateboy Date: Mon, 17 Aug 2015 08:29:28 +0100 Subject: [PATCH] add JSON support (%j) --- format.js | 3 +++ test_format.js | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) 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];