diff --git a/lib/util.js b/lib/util.js index 8de905f4dc..c7fcbccc8a 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 83238f9815..b5592203a3 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%%');