Skip to content

Commit e973a6e

Browse files
committed
[New] customInspect: add symbol option, to mimic modern util.inspect behavior
1 parent 45c20d6 commit e973a6e

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ module.exports = function inspect_(obj, options, depth, seen) {
4949
throw new TypeError('option "maxStringLength", if provided, must be a positive integer, Infinity, or `null`');
5050
}
5151
var customInspect = has(opts, 'customInspect') ? opts.customInspect : true;
52-
if (typeof customInspect !== 'boolean') {
53-
throw new TypeError('option "customInspect", if provided, must be `true` or `false`');
52+
if (typeof customInspect !== 'boolean' && customInspect !== 'symbol') {
53+
throw new TypeError('option "customInspect", if provided, must be `true`, `false`, or `\'symbol\'`');
5454
}
5555

5656
if (
@@ -152,7 +152,7 @@ module.exports = function inspect_(obj, options, depth, seen) {
152152
if (typeof obj === 'object' && customInspect) {
153153
if (inspectSymbol && typeof obj[inspectSymbol] === 'function') {
154154
return obj[inspectSymbol]();
155-
} else if (typeof obj.inspect === 'function') {
155+
} else if (customInspect !== 'symbol' && typeof obj.inspect === 'function') {
156156
return obj.inspect();
157157
}
158158
}

readme.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Return a string `s` with the string representation of `obj` up to a depth of `op
5353
Additional options:
5454
- `quoteStyle`: must be "single" or "double", if present. Default `'single'` for strings, `'double'` for HTML elements.
5555
- `maxStringLength`: must be `0`, a positive integer, `Infinity`, or `null`, if present. Default `Infinity`.
56-
- `customInspect`: When `true`, a custom inspect method function will be invoked. Default `true`.
56+
- `customInspect`: When `true`, a custom inspect method function will be invoked (either undere the `util.inspect.custom` symbol, or the `inspect` property). When the string `'symbol'`, only the symbol method will be invoked. Default `true`.
5757
- `indent`: must be "\t", `null`, or a positive integer. Default `null`.
5858

5959
# install

test/inspect.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,40 @@ var repeat = require('string.prototype.repeat');
66
var inspect = require('..');
77

88
test('inspect', function (t) {
9-
t.plan(4);
9+
t.plan(5);
10+
1011
var obj = [{ inspect: function xyzInspect() { return '!XYZ¡'; } }, []];
11-
t.equal(inspect(obj), '[ !XYZ¡, [] ]');
12-
t.equal(inspect(obj, { customInspect: true }), '[ !XYZ¡, [] ]');
13-
t.equal(inspect(obj, { customInspect: false }), '[ { inspect: [Function: xyzInspect] }, [] ]');
12+
var stringResult = '[ !XYZ¡, [] ]';
13+
var falseResult = '[ { inspect: [Function: xyzInspect] }, [] ]';
14+
15+
t.equal(inspect(obj), stringResult);
16+
t.equal(inspect(obj, { customInspect: true }), stringResult);
17+
t.equal(inspect(obj, { customInspect: 'symbol' }), falseResult);
18+
t.equal(inspect(obj, { customInspect: false }), falseResult);
1419
t['throws'](
15-
function () { inspect(obj, { customInspect: 'not a boolean' }); },
20+
function () { inspect(obj, { customInspect: 'not a boolean or "symbol"' }); },
1621
TypeError,
17-
'`customInspect` must be a boolean'
22+
'`customInspect` must be a boolean or the string "symbol"'
1823
);
1924
});
2025

2126
test('inspect custom symbol', { skip: !hasSymbols || !utilInspect || !utilInspect.custom }, function (t) {
22-
t.plan(3);
27+
t.plan(4);
2328

2429
var obj = { inspect: function stringInspect() { return 'string'; } };
2530
obj[utilInspect.custom] = function custom() { return 'symbol'; };
2631

27-
t.equal(inspect([obj, []]), '[ ' + (utilInspect.custom ? 'symbol' : 'string') + ', [] ]');
28-
t.equal(inspect([obj, []], { customInspect: true }), '[ ' + (utilInspect.custom ? 'symbol' : 'string') + ', [] ]');
29-
t.equal(
30-
inspect([obj, []], { customInspect: false }),
31-
'[ { inspect: [Function: stringInspect]' + (utilInspect.custom ? ', [' + inspect(utilInspect.custom) + ']: [Function: custom]' : '') + ' }, [] ]'
32-
);
32+
var symbolResult = '[ symbol, [] ]';
33+
var stringResult = '[ string, [] ]';
34+
var falseResult = '[ { inspect: [Function: stringInspect]' + (utilInspect.custom ? ', [' + inspect(utilInspect.custom) + ']: [Function: custom]' : '') + ' }, [] ]';
35+
36+
var symbolStringFallback = utilInspect.custom ? symbolResult : stringResult;
37+
var symbolFalseFallback = utilInspect.custom ? symbolResult : falseResult;
38+
39+
t.equal(inspect([obj, []]), symbolStringFallback);
40+
t.equal(inspect([obj, []], { customInspect: true }), symbolStringFallback);
41+
t.equal(inspect([obj, []], { customInspect: 'symbol' }), symbolFalseFallback);
42+
t.equal(inspect([obj, []], { customInspect: false }), falseResult);
3343
});
3444

3545
test('symbols', { skip: !hasSymbols }, function (t) {

0 commit comments

Comments
 (0)