Skip to content

Commit

Permalink
[New] add customInspect option, to disable custom inspect methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed May 27, 2020
1 parent 7cb5c65 commit 28b9179
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ module.exports = function inspect_(obj, options, depth, seen) {
) {
throw new TypeError('option "maxStringLength", if provided, must be a positive integer, Infinity, or `null`');
}
var customInspect = has(opts, 'customInspect') ? opts.customInspect : true;
if (typeof customInspect !== 'boolean') {
throw new TypeError('option "customInspect", if provided, must be `true` or `false`');
}

if (typeof obj === 'undefined') {
return 'undefined';
Expand Down Expand Up @@ -105,7 +109,7 @@ module.exports = function inspect_(obj, options, depth, seen) {
if (parts.length === 0) { return '[' + String(obj) + ']'; }
return '{ [' + String(obj) + '] ' + parts.join(', ') + ' }';
}
if (typeof obj === 'object') {
if (typeof obj === 'object' && customInspect) {
if (inspectSymbol && typeof obj[inspectSymbol] === 'function') {
return obj[inspectSymbol]();
} else if (typeof obj.inspect === 'function') {
Expand Down
5 changes: 3 additions & 2 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ var inspect = require('object-inspect')
Return a string `s` with the string representation of `obj` up to a depth of `opts.depth`.

Additional options:
- `quoteStyle`: must be "single" or "double", if present
- `maxStringLength`: must be `0`, a positive integer, `Infinity`, or `null`, if present
- `quoteStyle`: must be "single" or "double", if present. Default `'single'` for strings, `'double'` for HTML elements.
- `maxStringLength`: must be `0`, a positive integer, `Infinity`, or `null`, if present. Default `Infinity`.
- `customInspect`: When `true`, a custom inspect method function will be invoked. Default `true`.

# install

Expand Down
16 changes: 10 additions & 6 deletions test/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ var repeat = require('string.prototype.repeat');
var inspect = require('..');

test('inspect', function (t) {
t.plan(1);
var obj = [{ inspect: function () { return '!XYZ¡'; } }, []];
t.plan(3);
var obj = [{ inspect: function xyzInspect() { return '!XYZ¡'; } }, []];
t.equal(inspect(obj), '[ !XYZ¡, [] ]');
t.equal(inspect(obj, { customInspect: true }), '[ !XYZ¡, [] ]');
t.equal(inspect(obj, { customInspect: false }), '[ { inspect: [Function: xyzInspect] }, [] ]');
});

test('inspect custom symbol', { skip: !hasSymbols || !utilInspect }, function (t) {
t.plan(1);
test('inspect custom symbol', { skip: !hasSymbols || !utilInspect || !utilInspect.custom }, function (t) {
t.plan(3);

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

t.equal(inspect([obj, []]), '[ ' + (utilInspect.custom ? 'symbol' : 'string') + ', [] ]');
t.equal(inspect([obj, []], { customInspect: true }), '[ ' + (utilInspect.custom ? 'symbol' : 'string') + ', [] ]');
t.equal(inspect([obj, []], { customInspect: false }), '[ { inspect: [Function: stringInspect] }, [] ]');
});

test('maxStringLength', function (t) {
Expand Down

0 comments on commit 28b9179

Please sign in to comment.