From 578ba76e6c0e16827bf9e471bdef500aedf3b09e Mon Sep 17 00:00:00 2001 From: ssuda Date: Fri, 2 Mar 2012 21:51:26 +0530 Subject: [PATCH] util: fix util.format() formatting of %% --- lib/util.js | 2 +- test/simple/test-util-format.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/util.js b/lib/util.js index 8de905f4dc9..c7fcbccc8a9 100644 --- a/lib/util.js +++ b/lib/util.js @@ -33,12 +33,12 @@ exports.format = function(f) { var args = arguments; var len = args.length; var str = String(f).replace(formatRegExp, function(x) { + if (x === '%%') return '%'; if (i >= len) return x; switch (x) { 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 83238f98150..b5592203a39 100644 --- a/test/simple/test-util-format.js +++ b/test/simple/test-util-format.js @@ -58,4 +58,5 @@ assert.equal(util.format('%s:%s', undefined), 'undefined:%s'); assert.equal(util.format('%s:%s', 'foo'), 'foo:%s'); assert.equal(util.format('%s:%s', 'foo', 'bar'), 'foo:bar'); assert.equal(util.format('%s:%s', 'foo', 'bar', 'baz'), 'foo:bar baz'); - +assert.equal(util.format('%%%s%%', 'hi'), '%hi%'); +assert.equal(util.format('%%%s%%%%', 'hi'), '%hi%%');