From ed3b057e9fb8fa0ca9d47bcd9ea831ae1e3382d0 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Mon, 23 Feb 2015 17:32:18 -0500 Subject: [PATCH] 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 --- lib/util.js | 2 +- test/parallel/test-util-format.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/util.js b/lib/util.js index 86f585f95217e4..fb5f7f4b31ab3d 100644 --- a/lib/util.js +++ b/lib/util.js @@ -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); diff --git a/test/parallel/test-util-format.js b/test/parallel/test-util-format.js index 446e11e98db594..715509419806af 100644 --- a/test/parallel/test-util-format.js +++ b/test/parallel/test-util-format.js @@ -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');