Browse Source

util: handle symbols properly in format()

Currently, if util.format() is called with a string as its first
argument, and a Symbol as one of the subsequent arguments, an
exception is thrown due to an attempted implicit string conversion.
This commit causes Symbols to be explicitly converted.

Fixes: https://github.com/iojs/io.js/issues/927
PR-URL: https://github.com/iojs/io.js/pull/931
Reviewed-By: Domenic Denicola <domenic@domenicdenicola.com>
v1.8.0-commit
cjihrig 10 years ago
parent
commit
ed3b057e9f
  1. 2
      lib/util.js
  2. 10
      test/parallel/test-util-format.js

2
lib/util.js

@ -30,7 +30,7 @@ exports.format = function(f) {
} }
}); });
for (var x = args[i]; i < len; x = args[++i]) { for (var x = args[i]; i < len; x = args[++i]) {
if (x === null || typeof x !== 'object') { if (x === null || (typeof x !== 'object' && typeof x !== 'symbol')) {
str += ' ' + x; str += ' ' + x;
} else { } else {
str += ' ' + inspect(x); str += ' ' + inspect(x);

10
test/parallel/test-util-format.js

@ -1,6 +1,7 @@
var common = require('../common'); var common = require('../common');
var assert = require('assert'); var assert = require('assert');
var util = require('util'); var util = require('util');
var symbol = Symbol('foo');
assert.equal(util.format(), ''); assert.equal(util.format(), '');
assert.equal(util.format(''), ''); assert.equal(util.format(''), '');
@ -14,6 +15,15 @@ assert.equal(util.format('test'), 'test');
// CHECKME this is for console.log() compatibility - but is it *right*? // CHECKME this is for console.log() compatibility - but is it *right*?
assert.equal(util.format('foo', 'bar', 'baz'), 'foo bar baz'); assert.equal(util.format('foo', 'bar', 'baz'), 'foo bar baz');
// ES6 Symbol handling
assert.equal(util.format(symbol), 'Symbol(foo)');
assert.equal(util.format('foo', symbol), 'foo Symbol(foo)');
assert.equal(util.format('%s', symbol), 'Symbol(foo)');
assert.equal(util.format('%j', symbol), 'undefined');
assert.throws(function() {
util.format('%d', symbol);
}, TypeError);
assert.equal(util.format('%d', 42.0), '42'); assert.equal(util.format('%d', 42.0), '42');
assert.equal(util.format('%d', 42), '42'); assert.equal(util.format('%d', 42), '42');
assert.equal(util.format('%s', 42), '42'); assert.equal(util.format('%s', 42), '42');

Loading…
Cancel
Save