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>
This commit is contained in:
parent
da730c76e9
commit
ed3b057e9f
@ -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);
|
||||
|
@ -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(''), '');
|
||||
@ -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');
|
||||
|
Loading…
x
Reference in New Issue
Block a user