util: fix util.format() formatting of %%

pull/24503/head
ssuda 2012-03-02 21:51:26 +05:30 committed by Ben Noordhuis
parent d6f0ecc622
commit 578ba76e6c
2 changed files with 3 additions and 2 deletions

View File

@ -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;
}

View File

@ -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%%');