Skip to content

Commit

Permalink
util: handle symbols properly in format()
Browse files Browse the repository at this point in the history
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 <domenic@domenicdenicola.com>
  • Loading branch information
cjihrig committed Feb 24, 2015
1 parent da730c7 commit ed3b057
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ exports.format = function(f) {
}
});
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;
} else {
str += ' ' + inspect(x);
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-util-format.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var common = require('../common');
var assert = require('assert');
var util = require('util');
var symbol = Symbol('foo');

assert.equal(util.format(), '');
assert.equal(util.format(''), '');
Expand All @@ -14,6 +15,15 @@ assert.equal(util.format('test'), 'test');
// CHECKME this is for console.log() compatibility - but is it *right*?
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), '42');
assert.equal(util.format('%s', 42), '42');
Expand Down

0 comments on commit ed3b057

Please sign in to comment.