Skip to content

Commit

Permalink
simplifying the whole mess by just using node's inspect function for …
Browse files Browse the repository at this point in the history
…custom calls.
  • Loading branch information
martinheidegger committed May 26, 2022
1 parent 48b9d0e commit ab0b3e0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 33 deletions.
13 changes: 2 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,8 @@ module.exports = function inspect_(obj, options, depth, seen) {
return '{ [' + String(obj) + '] ' + $join.call(parts, ', ') + ' }';
}
if (typeof obj === 'object' && customInspect) {
if (inspectSymbol && typeof obj[inspectSymbol] === 'function') {
return obj[inspectSymbol](maxDepth - depth, {
breakLength: utilInspect.defaultOptions ? utilInspect.defaultOptions.breakLength : 60,
colors: utilInspect.defaultOptions ? utilInspect.defaultOptions.colors : 60,
customInspect: true,
depth: maxDepth,
maxArrayLength: utilInspect.defaultOptions ? utilInspect.defaultOptions.maxArrayLength : 60,
seen: seen,
showHidden: utilInspect.defaultOptions ? utilInspect.defaultOptions.showHidden : false,
showProxy: utilInspect.defaultOptions ? utilInspect.defaultOptions.showProxy : false
});
if (inspectSymbol && typeof obj[inspectSymbol] === 'function' && utilInspect) {
return utilInspect(obj, { depth: maxDepth - depth });
} else if (customInspect !== 'symbol' && typeof obj.inspect === 'function') {
return obj.inspect();
}
Expand Down
30 changes: 8 additions & 22 deletions test/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,23 @@ test('maxStringLength', function (t) {

test('inspect options', { skip: !utilInspect.custom }, function (t) {
var obj = {};
obj[utilInspect.custom] = function (depth, opts) {
return String(depth) + ':' + JSON.stringify(opts);
obj[utilInspect.custom] = function () {
return JSON.stringify(arguments);
};
t.equal(
inspect(obj),
'5:{"breakLength":60,"colors":false,"customInspect":true,"depth":5,"maxArrayLength":100,"seen":[],"showHidden":false,"showProxy":false}',
'direct inspect will fallback to default maxdepth = 5'
utilInspect(obj, { depth: 5 }),
'custom symbols will use node\'s inspect'
);
t.equal(
inspect(obj, { depth: 2 }),
'2:{"breakLength":60,"colors":false,"customInspect":true,"depth":2,"maxArrayLength":100,"seen":[],"showHidden":false,"showProxy":false}',
'custom depth will be passed to the inspect call'
utilInspect(obj, { depth: 2 }),
'a reduced depth will be passed to node\'s inspect'
);
t.equal(
inspect({ d1: obj }, { depth: 3 }),
'{ d1: 2:{"breakLength":60,"colors":false,"customInspect":true,"depth":3,"maxArrayLength":100,"seen":[{"d1":{}}],"showHidden":false,"showProxy":false} }',
'the passed-in depth is reduced if the object is deeper in the hierarchy'
'{ d1: ' + utilInspect(obj, { depth: 2 }) + ' }',
'deep objects will receive a reduced depth'
);
t.equal(
inspect({ d1: obj }, { depth: 1 }),
Expand All @@ -128,20 +128,6 @@ test('inspect options', { skip: !utilInspect.custom }, function (t) {
t.end();
});

test('inspect option defaults', { skip: !(utilInspect.custom && utilInspect.defaultOptions) }, function (t) {
var obj = {};
obj[utilInspect.custom] = function (depth, opts) {
return String(depth) + ':' + JSON.stringify(opts);
};
utilInspect.defaultOptions.showHidden = true;
t.equal(
inspect({ d1: obj }),
'{ d1: 4:{"breakLength":60,"colors":false,"customInspect":true,"depth":5,"maxArrayLength":100,"seen":[{"d1":{}}],"showHidden":true,"showProxy":false} }',
'unlike nodejs inspect, customInspect will not be used once the depth is exceeded.'
);
t.end();
});

test('inspect URL', { skip: typeof URL === 'undefined' }, function (t) {
t.match(
inspect(new URL('https://nodejs.org')),
Expand Down

0 comments on commit ab0b3e0

Please sign in to comment.