Skip to content

Commit 1a6a69a

Browse files
committed
util: add inspect.defaultOptions
Adds util.inspect.defaultOptions which allows customization of the default util.inspect options, which is useful for functions like console.log or util.format which implicitly call into util.inspect. PR-URL: #8013 Fixes: #7566 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Evan Lucas <[email protected]>
1 parent a8438a0 commit 1a6a69a

File tree

3 files changed

+86
-15
lines changed

3 files changed

+86
-15
lines changed

doc/api/util.md

+18
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,23 @@ util.inspect(obj);
271271
// "{ bar: 'baz' }"
272272
```
273273

274+
### util.inspect.defaultOptions
275+
276+
The `defaultOptions` value allows customization of the default options used by
277+
`util.inspect`. This is useful for functions like `console.log` or
278+
`util.format` which implicitly call into `util.inspect`. It shall be set to an
279+
object containing one or more valid [`util.inspect()`][] options. Setting
280+
option properties directly is also supported.
281+
282+
```js
283+
const util = require('util');
284+
const arr = Array(101);
285+
286+
console.log(arr); // logs the truncated array
287+
util.inspect.defaultOptions.maxArrayLength = null;
288+
console.log(arr); // logs the full array
289+
```
290+
274291
## Deprecated APIs
275292

276293
The following APIs have been deprecated and should no longer be used. Existing
@@ -662,6 +679,7 @@ similar built-in functionality through [`Object.assign()`].
662679
[`Array.isArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
663680
[constructor]: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/constructor
664681
[semantically incompatible]: https://github.com/nodejs/node/issues/4179
682+
[`util.inspect()`]: #util_util_inspect_object_options
665683
[Customizing `util.inspect` colors]: #util_customizing_util_inspect_colors
666684
[`Error`]: errors.html#errors_class_error
667685
[`console.log()`]: console.html#console_console_log_data

lib/util.js

+27-15
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@ const internalUtil = require('internal/util');
66
const binding = process.binding('util');
77

88
const isError = internalUtil.isError;
9-
const kDefaultMaxLength = 100;
9+
10+
const inspectDefaultOptions = Object.seal({
11+
showHidden: false,
12+
depth: 2,
13+
colors: false,
14+
customInspect: true,
15+
showProxy: false,
16+
maxArrayLength: 100,
17+
breakLength: 60
18+
});
1019

1120
var Debug;
1221
var simdFormatters;
@@ -176,29 +185,31 @@ function inspect(obj, opts) {
176185
stylize: stylizeNoColor
177186
};
178187
// legacy...
179-
if (arguments.length >= 3) ctx.depth = arguments[2];
180-
if (arguments.length >= 4) ctx.colors = arguments[3];
188+
if (arguments[2] !== undefined) ctx.depth = arguments[2];
189+
if (arguments[3] !== undefined) ctx.colors = arguments[3];
181190
if (typeof opts === 'boolean') {
182191
// legacy...
183192
ctx.showHidden = opts;
184-
} else if (opts) {
185-
// got an "options" object
186-
exports._extend(ctx, opts);
187193
}
188-
// set default options
189-
if (ctx.showHidden === undefined) ctx.showHidden = false;
190-
if (ctx.depth === undefined) ctx.depth = 2;
191-
if (ctx.colors === undefined) ctx.colors = false;
192-
if (ctx.customInspect === undefined) ctx.customInspect = true;
193-
if (ctx.showProxy === undefined) ctx.showProxy = false;
194+
// Set default and user-specified options
195+
ctx = Object.assign({}, inspect.defaultOptions, ctx, opts);
194196
if (ctx.colors) ctx.stylize = stylizeWithColor;
195-
if (ctx.maxArrayLength === undefined) ctx.maxArrayLength = kDefaultMaxLength;
196197
if (ctx.maxArrayLength === null) ctx.maxArrayLength = Infinity;
197-
if (ctx.breakLength === undefined) ctx.breakLength = 60;
198198
return formatValue(ctx, obj, ctx.depth);
199199
}
200-
exports.inspect = inspect;
201200

201+
Object.defineProperty(inspect, 'defaultOptions', {
202+
get: function() {
203+
return inspectDefaultOptions;
204+
},
205+
set: function(options) {
206+
if (options === null || typeof options !== 'object') {
207+
throw new TypeError('"options" must be an object');
208+
}
209+
Object.assign(inspectDefaultOptions, options);
210+
return inspectDefaultOptions;
211+
}
212+
});
202213

203214
// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
204215
inspect.colors = {
@@ -231,6 +242,7 @@ inspect.styles = {
231242
'regexp': 'red'
232243
};
233244

245+
exports.inspect = inspect;
234246

235247
function stylizeWithColor(str, styleType) {
236248
var style = inspect.styles[styleType];

test/parallel/test-util-inspect.js

+41
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ assert.equal(util.inspect(Object.create({},
4343
'{ visible: 1 }'
4444
);
4545

46+
assert(/Object/.test(
47+
util.inspect({a: {a: {a: {a: {}}}}}, undefined, undefined, true)
48+
));
49+
assert(!/Object/.test(
50+
util.inspect({a: {a: {a: {a: {}}}}}, undefined, null, true)
51+
));
52+
4653
for (const showHidden of [true, false]) {
4754
const ab = new ArrayBuffer(4);
4855
const dv = new DataView(ab, 1, 2);
@@ -723,3 +730,37 @@ checkAlignment(new Map(big_array.map(function(y) { return [y, null]; })));
723730
assert.strictEqual(oneLine, util.inspect(obj, {breakLength: breakpoint + 1}));
724731
assert.strictEqual(twoLines, '{ foo: \'abc\',\n bar: \'xyz\' }');
725732
}
733+
734+
// util.inspect.defaultOptions tests
735+
{
736+
const arr = Array(101);
737+
const obj = {a: {a: {a: {a: 1}}}};
738+
739+
const oldOptions = Object.assign({}, util.inspect.defaultOptions);
740+
741+
// Set single option through property assignment
742+
util.inspect.defaultOptions.maxArrayLength = null;
743+
assert(!/1 more item/.test(util.inspect(arr)));
744+
util.inspect.defaultOptions.maxArrayLength = oldOptions.maxArrayLength;
745+
assert(/1 more item/.test(util.inspect(arr)));
746+
util.inspect.defaultOptions.depth = null;
747+
assert(!/Object/.test(util.inspect(obj)));
748+
util.inspect.defaultOptions.depth = oldOptions.depth;
749+
assert(/Object/.test(util.inspect(obj)));
750+
assert.strictEqual(
751+
JSON.stringify(util.inspect.defaultOptions),
752+
JSON.stringify(oldOptions)
753+
);
754+
755+
// Set multiple options through object assignment
756+
util.inspect.defaultOptions = {maxArrayLength: null, depth: null};
757+
assert(!/1 more item/.test(util.inspect(arr)));
758+
assert(!/Object/.test(util.inspect(obj)));
759+
util.inspect.defaultOptions = oldOptions;
760+
assert(/1 more item/.test(util.inspect(arr)));
761+
assert(/Object/.test(util.inspect(obj)));
762+
assert.strictEqual(
763+
JSON.stringify(util.inspect.defaultOptions),
764+
JSON.stringify(oldOptions)
765+
);
766+
}

0 commit comments

Comments
 (0)