Skip to content

Commit cb97bcd

Browse files
gysonchrisdickinson
authored andcommitted
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>
1 parent 83d7d9e commit cb97bcd

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

lib/util.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ inspect.styles = {
174174
'undefined': 'grey',
175175
'null': 'bold',
176176
'string': 'green',
177+
'symbol': 'green',
177178
'date': 'magenta',
178179
// "name": intentionally not styling
179180
'regexp': 'red'
@@ -388,6 +389,9 @@ function formatPrimitive(ctx, value) {
388389
// For some reason typeof null is "object", so special case here.
389390
if (isNull(value))
390391
return ctx.stylize('null', 'null');
392+
// es6 symbol primitive
393+
if (isSymbol(value))
394+
return ctx.stylize(value.toString(), 'symbol');
391395
}
392396

393397

test/simple/test-util-inspect.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,12 @@ assert.equal(util.inspect(bool), '{ [Boolean: true] foo: \'bar\' }');
235235
var num = new Number(13.37);
236236
num.foo = 'bar';
237237
assert.equal(util.inspect(num), '{ [Number: 13.37] foo: \'bar\' }');
238+
239+
// test es6 Symbol
240+
if (typeof Symbol !== 'undefined') {
241+
assert.equal(util.inspect(Symbol()), 'Symbol()');
242+
assert.equal(util.inspect(Symbol(123)), 'Symbol(123)');
243+
assert.equal(util.inspect(Symbol('hi')), 'Symbol(hi)');
244+
assert.equal(util.inspect([Symbol()]), '[ Symbol() ]');
245+
assert.equal(util.inspect({ foo: Symbol() }), '{ foo: Symbol() }');
246+
}

0 commit comments

Comments
 (0)