mirror of https://github.com/nodejs/node.git
util: pass opts to custom inspect functions
Objects with custom inpsect functions should get the options that were passed to `util.inspect()` fixes #5822 fixes #6098pull/41362/head
parent
fa89cf545c
commit
546ae2eef9
|
@ -98,8 +98,8 @@ formatted string:
|
||||||
- `colors` - if `true`, then the output will be styled with ANSI color codes.
|
- `colors` - if `true`, then the output will be styled with ANSI color codes.
|
||||||
Defaults to `false`. Colors are customizable, see below.
|
Defaults to `false`. Colors are customizable, see below.
|
||||||
|
|
||||||
- `customInspect` - if `false`, then custom `inspect()` functions defined on the
|
- `customInspect` - if `false`, then custom `inspect(depth, opts)` functions
|
||||||
objects being inspected won't be called. Defaults to `true`.
|
defined on the objects being inspected won't be called. Defaults to `true`.
|
||||||
|
|
||||||
Example of inspecting all properties of the `util` object:
|
Example of inspecting all properties of the `util` object:
|
||||||
|
|
||||||
|
@ -107,6 +107,10 @@ Example of inspecting all properties of the `util` object:
|
||||||
|
|
||||||
console.log(util.inspect(util, { showHidden: true, depth: null }));
|
console.log(util.inspect(util, { showHidden: true, depth: null }));
|
||||||
|
|
||||||
|
Values may supply their own custom `inspect(depth, opts)` functions, when
|
||||||
|
called they receive the current depth in the recursive inspection, as well as
|
||||||
|
the options object passed to `util.inspect()`.
|
||||||
|
|
||||||
### Customizing `util.inspect` colors
|
### Customizing `util.inspect` colors
|
||||||
|
|
||||||
<!-- type=misc -->
|
<!-- type=misc -->
|
||||||
|
|
|
@ -218,7 +218,7 @@ function formatValue(ctx, value, recurseTimes) {
|
||||||
value.inspect !== exports.inspect &&
|
value.inspect !== exports.inspect &&
|
||||||
// Also filter out any prototype objects using the circular check.
|
// Also filter out any prototype objects using the circular check.
|
||||||
!(value.constructor && value.constructor.prototype === value)) {
|
!(value.constructor && value.constructor.prototype === value)) {
|
||||||
var ret = value.inspect(recurseTimes);
|
var ret = value.inspect(recurseTimes, ctx);
|
||||||
if (!isString(ret)) {
|
if (!isString(ret)) {
|
||||||
ret = formatValue(ctx, ret, recurseTimes);
|
ret = formatValue(ctx, ret, recurseTimes);
|
||||||
}
|
}
|
||||||
|
|
|
@ -161,6 +161,12 @@ subject.inspect = function() { return { foo: 'bar' }; };
|
||||||
|
|
||||||
assert.equal(util.inspect(subject), '{ foo: \'bar\' }');
|
assert.equal(util.inspect(subject), '{ foo: \'bar\' }');
|
||||||
|
|
||||||
|
subject.inspect = function(depth, opts) {
|
||||||
|
assert.strictEqual(opts.customInspectOptions, true);
|
||||||
|
};
|
||||||
|
|
||||||
|
util.inspect(subject, { customInspectOptions: true });
|
||||||
|
|
||||||
// util.inspect with "colors" option should produce as many lines as without it
|
// util.inspect with "colors" option should produce as many lines as without it
|
||||||
function test_lines(input) {
|
function test_lines(input) {
|
||||||
var count_lines = function(str) {
|
var count_lines = function(str) {
|
||||||
|
|
Loading…
Reference in New Issue