util: add inspect.defaultOptions · nodejs/node@1a6a69a
@@ -6,7 +6,16 @@ const internalUtil = require('internal/util');
66const binding = process.binding('util');
7788const 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+});
10191120var Debug;
1221var simdFormatters;
@@ -176,29 +185,31 @@ function inspect(obj, opts) {
176185stylize: 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];
181190if (typeof opts === 'boolean') {
182191// legacy...
183192ctx.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);
194196if (ctx.colors) ctx.stylize = stylizeWithColor;
195-if (ctx.maxArrayLength === undefined) ctx.maxArrayLength = kDefaultMaxLength;
196197if (ctx.maxArrayLength === null) ctx.maxArrayLength = Infinity;
197-if (ctx.breakLength === undefined) ctx.breakLength = 60;
198198return formatValue(ctx, obj, ctx.depth);
199199}
200-exports.inspect = inspect;
201200201+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+});
202213203214// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
204215inspect.colors = {
@@ -231,6 +242,7 @@ inspect.styles = {
231242'regexp': 'red'
232243};
233244245+exports.inspect = inspect;
234246235247function stylizeWithColor(str, styleType) {
236248var style = inspect.styles[styleType];