Browse Source

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 <trev.norris@gmail.com>
Reviewed-by: Chris Dickinson <christopher.s.dickinson@gmail.com>
v0.11.15-release
gyson 10 years ago
committed by Chris Dickinson
parent
commit
cb97bcd6f9
  1. 4
      lib/util.js
  2. 9
      test/simple/test-util-inspect.js

4
lib/util.js

@ -174,6 +174,7 @@ inspect.styles = {
'undefined': 'grey', 'undefined': 'grey',
'null': 'bold', 'null': 'bold',
'string': 'green', 'string': 'green',
'symbol': 'green',
'date': 'magenta', 'date': 'magenta',
// "name": intentionally not styling // "name": intentionally not styling
'regexp': 'red' 'regexp': 'red'
@ -388,6 +389,9 @@ function formatPrimitive(ctx, value) {
// For some reason typeof null is "object", so special case here. // For some reason typeof null is "object", so special case here.
if (isNull(value)) if (isNull(value))
return ctx.stylize('null', 'null'); return ctx.stylize('null', 'null');
// es6 symbol primitive
if (isSymbol(value))
return ctx.stylize(value.toString(), 'symbol');
} }

9
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); var num = new Number(13.37);
num.foo = 'bar'; num.foo = 'bar';
assert.equal(util.inspect(num), '{ [Number: 13.37] 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() }');
}

Loading…
Cancel
Save