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>
This commit is contained in:
parent
83d7d9e6d8
commit
cb97bcd6f9
@ -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');
|
||||
}
|
||||
|
||||
|
||||
|
@ -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() }');
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user