Skip to content

Commit ed3b057

Browse files
committed
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: #927 PR-URL: #931 Reviewed-By: Domenic Denicola <[email protected]>
1 parent da730c7 commit ed3b057

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/util.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ exports.format = function(f) {
3030
}
3131
});
3232
for (var x = args[i]; i < len; x = args[++i]) {
33-
if (x === null || typeof x !== 'object') {
33+
if (x === null || (typeof x !== 'object' && typeof x !== 'symbol')) {
3434
str += ' ' + x;
3535
} else {
3636
str += ' ' + inspect(x);

test/parallel/test-util-format.js

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var common = require('../common');
22
var assert = require('assert');
33
var util = require('util');
4+
var symbol = Symbol('foo');
45

56
assert.equal(util.format(), '');
67
assert.equal(util.format(''), '');
@@ -14,6 +15,15 @@ assert.equal(util.format('test'), 'test');
1415
// CHECKME this is for console.log() compatibility - but is it *right*?
1516
assert.equal(util.format('foo', 'bar', 'baz'), 'foo bar baz');
1617

18+
// ES6 Symbol handling
19+
assert.equal(util.format(symbol), 'Symbol(foo)');
20+
assert.equal(util.format('foo', symbol), 'foo Symbol(foo)');
21+
assert.equal(util.format('%s', symbol), 'Symbol(foo)');
22+
assert.equal(util.format('%j', symbol), 'undefined');
23+
assert.throws(function() {
24+
util.format('%d', symbol);
25+
}, TypeError);
26+
1727
assert.equal(util.format('%d', 42.0), '42');
1828
assert.equal(util.format('%d', 42), '42');
1929
assert.equal(util.format('%s', 42), '42');

0 commit comments

Comments
 (0)