From d3d8f1b972e1fb99916b32647609ad58f6c16fd9 Mon Sep 17 00:00:00 2001 From: koichik Date: Sat, 30 Jul 2011 22:08:59 +0900 Subject: [PATCH] Add %% escape to util.format() Fixes #1273. --- lib/util.js | 3 ++- test/simple/test-util-format.js | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/util.js b/lib/util.js index 0630d94b895..d4e6bbf5cc8 100644 --- a/lib/util.js +++ b/lib/util.js @@ -22,7 +22,7 @@ var events = require('events'); -var formatRegExp = /%[sdj]/g; +var formatRegExp = /%[sdj%]/g; exports.format = function(f) { if (typeof f !== 'string') { var objects = []; @@ -39,6 +39,7 @@ exports.format = function(f) { case '%s': return String(args[i++]); case '%d': return Number(args[i++]); case '%j': return JSON.stringify(args[i++]); + case '%%': return '%'; default: return x; } diff --git a/test/simple/test-util-format.js b/test/simple/test-util-format.js index 2525680b64f..6a497089b19 100644 --- a/test/simple/test-util-format.js +++ b/test/simple/test-util-format.js @@ -43,4 +43,7 @@ assert.equal(util.format('%j', 42), '42'); assert.equal(util.format('%d', '42.0'), '42'); assert.equal(util.format('%d', '42'), '42'); assert.equal(util.format('%s', '42'), '42'); -assert.equal(util.format('%j', '42'), '"42"'); \ No newline at end of file +assert.equal(util.format('%j', '42'), '"42"'); + +assert.equal(util.format('%%s%s', 'foo'), '%sfoo'); +