util: add inspect.defaultOptions · nodejs/node@1a6a69a

@@ -6,7 +6,16 @@ const internalUtil = require('internal/util');

66

const binding = process.binding('util');

7788

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+

});

10191120

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;

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

204215

inspect.colors = {

@@ -231,6 +242,7 @@ inspect.styles = {

231242

'regexp': 'red'

232243

};

233244245+

exports.inspect = inspect;

234246235247

function stylizeWithColor(str, styleType) {

236248

var style = inspect.styles[styleType];