From cb97bcd6f982b35a6fe4d41d884354f72905365d Mon Sep 17 00:00:00 2001 From: gyson Date: Wed, 1 Oct 2014 10:44:39 +0800 Subject: [PATCH] util: add es6 Symbol support for `util.inspect` * `util.inspect` cannot accept es6 symbol primitive * It will throw exception if do `util.inspect(Symbol())` * This also affects repl, console.log, etc. Reviewed-by: Trevor Norris Reviewed-by: Chris Dickinson --- lib/util.js | 4 ++++ test/simple/test-util-inspect.js | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/util.js b/lib/util.js index 2de944586f..e97820c9af 100644 --- a/lib/util.js +++ b/lib/util.js @@ -174,6 +174,7 @@ inspect.styles = { 'undefined': 'grey', 'null': 'bold', 'string': 'green', + 'symbol': 'green', 'date': 'magenta', // "name": intentionally not styling 'regexp': 'red' @@ -388,6 +389,9 @@ function formatPrimitive(ctx, value) { // For some reason typeof null is "object", so special case here. if (isNull(value)) return ctx.stylize('null', 'null'); + // es6 symbol primitive + if (isSymbol(value)) + return ctx.stylize(value.toString(), 'symbol'); } diff --git a/test/simple/test-util-inspect.js b/test/simple/test-util-inspect.js index 8651a2df01..2a7c2c4fcb 100644 --- a/test/simple/test-util-inspect.js +++ b/test/simple/test-util-inspect.js @@ -235,3 +235,12 @@ assert.equal(util.inspect(bool), '{ [Boolean: true] foo: \'bar\' }'); var num = new Number(13.37); num.foo = 'bar'; assert.equal(util.inspect(num), '{ [Number: 13.37] foo: \'bar\' }'); + +// test es6 Symbol +if (typeof Symbol !== 'undefined') { + assert.equal(util.inspect(Symbol()), 'Symbol()'); + assert.equal(util.inspect(Symbol(123)), 'Symbol(123)'); + assert.equal(util.inspect(Symbol('hi')), 'Symbol(hi)'); + assert.equal(util.inspect([Symbol()]), '[ Symbol() ]'); + assert.equal(util.inspect({ foo: Symbol() }), '{ foo: Symbol() }'); +}