Skip to content

Commit ee60c03

Browse files
committed
[New] add enumerable own Symbols to plain object output
1 parent f3c2074 commit ee60c03

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ var objectToString = Object.prototype.toString;
1515
var functionToString = Function.prototype.toString;
1616
var match = String.prototype.match;
1717
var bigIntValueOf = typeof BigInt === 'function' ? BigInt.prototype.valueOf : null;
18+
var gOPS = Object.getOwnPropertySymbols;
1819
var symToString = typeof Symbol === 'function' ? Symbol.prototype.toString : null;
20+
var isEnumerable = Object.prototype.propertyIsEnumerable;
1921

2022
var inspectCustom = require('./util.inspect').custom;
2123
var inspectSymbol = inspectCustom && isSymbol(inspectCustom) ? inspectCustom : null;
@@ -380,5 +382,13 @@ function arrObjKeys(obj, inspect) {
380382
xs.push(key + ': ' + inspect(obj[key], obj));
381383
}
382384
}
385+
if (typeof gOPS === 'function') {
386+
var syms = gOPS(obj);
387+
for (var j = 0; j < syms.length; j++) {
388+
if (isEnumerable.call(obj, syms[j])) {
389+
xs.push('[' + inspect(syms[j]) + ']: ' + inspect(obj[syms[j]], obj));
390+
}
391+
}
392+
}
383393
return xs;
384394
}

test/inspect.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,25 @@ test('inspect custom symbol', { skip: !hasSymbols || !utilInspect || !utilInspec
2121

2222
t.equal(inspect([obj, []]), '[ ' + (utilInspect.custom ? 'symbol' : 'string') + ', [] ]');
2323
t.equal(inspect([obj, []], { customInspect: true }), '[ ' + (utilInspect.custom ? 'symbol' : 'string') + ', [] ]');
24-
t.equal(inspect([obj, []], { customInspect: false }), '[ { inspect: [Function: stringInspect] }, [] ]');
24+
t.equal(
25+
inspect([obj, []], { customInspect: false }),
26+
'[ { inspect: [Function: stringInspect]' + (utilInspect.custom ? ', [' + inspect(utilInspect.custom) + ']: [Function: custom]' : '') + ' }, [] ]'
27+
);
28+
});
29+
30+
test('symbols', { skip: !hasSymbols }, function (t) {
31+
t.plan(2);
32+
33+
var obj = { a: 1 };
34+
obj[Symbol('test')] = 2;
35+
obj[Symbol.iterator] = 3;
36+
Object.defineProperty(obj, Symbol('non-enum'), {
37+
enumerable: false,
38+
value: 4
39+
});
40+
41+
t.equal(inspect(obj), '{ a: 1, [Symbol(test)]: 2, [Symbol(Symbol.iterator)]: 3 }', 'object with symbols');
42+
t.equal(inspect([obj, []]), '[ { a: 1, [Symbol(test)]: 2, [Symbol(Symbol.iterator)]: 3 }, [] ]', 'object with symbols');
2543
});
2644

2745
test('maxStringLength', function (t) {

0 commit comments

Comments
 (0)