Browse Source

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

v0.9.1-release
ssuda 13 years ago
committed by Ben Noordhuis
parent
commit
578ba76e6c
  1. 2
      lib/util.js
  2. 3
      test/simple/test-util-format.js

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

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

Loading…
Cancel
Save