Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
util: custom inspect() method may return an Object
Browse files Browse the repository at this point in the history
This is more like how `JSON.stringify()` works.
Closes #2711.
  • Loading branch information
TooTallNate committed Mar 12, 2013
1 parent da8b0ee commit 66280de
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
14 changes: 14 additions & 0 deletions doc/api/util.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ Predefined color codes are: `white`, `grey`, `black`, `blue`, `cyan`,
`green`, `magenta`, `red` and `yellow`.
There are also `bold`, `italic`, `underline` and `inverse` codes.

### Custom `inpect()` function on Objects

This comment has been minimized.

Copy link
@mscdex

mscdex Mar 12, 2013

s/inpect/inspect

This comment has been minimized.

Copy link
@TooTallNate

TooTallNate Mar 12, 2013

Author

Whoops! Thanks Brian, fixed in 3288bc9.


Objects also may define their own `inspect(depth)` function which `util.inspect()`
will invoke and use the result of when inspecting the object:

Expand All @@ -127,6 +129,18 @@ will invoke and use the result of when inspecting the object:
util.inspect(obj);
// "{nate}"

You may also return another Object entirely, and the returned String will be
formatted according to the returned Object. This is similar to how
`JSON.stringify()` works:

var obj = { foo: 'this will not show up in the inspect() output' };
obj.inspect = function(depth) {
return { bar: 'baz' };
};

util.inspect(obj);
// "{ bar: 'baz' }"


## util.isArray(object)

Expand Down
6 changes: 5 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,11 @@ function formatValue(ctx, value, recurseTimes) {
value.inspect !== exports.inspect &&
// Also filter out any prototype objects using the circular check.
!(value.constructor && value.constructor.prototype === value)) {
return String(value.inspect(recurseTimes));
var ret = value.inspect(recurseTimes);
if ('string' !== typeof ret) {
ret = formatValue(ctx, ret, recurseTimes);
}
return ret;
}

// Primitive types cannot have properties
Expand Down
5 changes: 5 additions & 0 deletions test/simple/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,8 @@ assert(util.inspect(subject, { customInspect: true }).indexOf('123') !== -1);
assert(util.inspect(subject, { customInspect: true }).indexOf('inspect') === -1);
assert(util.inspect(subject, { customInspect: false }).indexOf('123') === -1);
assert(util.inspect(subject, { customInspect: false }).indexOf('inspect') !== -1);

// custom inspect() functions should be able to return other Objects
subject.inspect = function() { return { foo: 'bar' }; };

assert.equal(util.inspect(subject), '{ foo: \'bar\' }');

4 comments on commit 66280de

@domenic
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a chance of back-porting this to 0.10?

@bajtos
Copy link

@bajtos bajtos commented on 66280de Jan 12, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for back-porting this change to v0.10.

@TooTallNate
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "returning object" behavior is new behavior, so that can't be backported.

The documentation (sans the "returning object" part) could be backported however.

@bajtos
Copy link

@bajtos bajtos commented on 66280de Jan 12, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "returning object" behavior is new behavior, so that can't be backported.

Well, I'd say it depends on the point of view. It fixes the bug that it's not possible to implement a custom inspect functions that honours the option color: true/false.

That's my two cents, I understand your POV too. If it did not take two years to release v0.12, I would absolutely agree with you.

Please sign in to comment.