Skip to content

Commit

Permalink
util: add es6 Symbol support for util.inspect
Browse files Browse the repository at this point in the history
* `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 <[email protected]>
Reviewed-by: Chris Dickinson <[email protected]>
  • Loading branch information
gyson authored and chrisdickinson committed Oct 3, 2014
1 parent 83d7d9e commit cb97bcd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ inspect.styles = {
'undefined': 'grey',
'null': 'bold',
'string': 'green',
'symbol': 'green',
'date': 'magenta',
// "name": intentionally not styling
'regexp': 'red'
Expand Down Expand Up @@ -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');
}


Expand Down
9 changes: 9 additions & 0 deletions test/simple/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() }');
}

0 comments on commit cb97bcd

Please sign in to comment.