summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2017-12-09 05:27:21 -0200
committerRuben Bridgewater <ruben@bridgewater.de>2017-12-21 03:45:25 -0300
commitc2203cb4dd240a6644177f04d0b40f40090b4c33 (patch)
tree9e31254f84b72f5fb05178dcc30db112d507f8a5 /lib
parent2d374916ebbaaa83aaf68577b75d41c6bb6ca9b8 (diff)
downloadandroid-node-v8-c2203cb4dd240a6644177f04d0b40f40090b4c33.tar.gz
android-node-v8-c2203cb4dd240a6644177f04d0b40f40090b4c33.tar.bz2
android-node-v8-c2203cb4dd240a6644177f04d0b40f40090b4c33.zip
util: add util.inspect compact option
The current default formatting is not ideal and this improves the situation by formatting the output more intuitiv. 1) All object keys are now indented by 2 characters instead of sometimes 2 and sometimes 3 characters. 2) Each object key will now use an individual line instead of sharing a line potentially with multiple object keys. 3) Long strings will now be split into multiple lines in case they exceed the "lineBreak" option length (including the current indentation). 4) Opening braces are now directly behind a object property instead of using a new line. 5) Switch inspect "base" order. In case the compact option is set to `false`, inspect will now print "[Function: foo] {\n property: 'data'\n}" instead of "{ [Function: foo]\n property: 'data'\n}". PR-URL: https://github.com/nodejs/node/pull/17576 Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'lib')
-rw-r--r--lib/util.js84
1 files changed, 67 insertions, 17 deletions
diff --git a/lib/util.js b/lib/util.js
index 0a7af758f4..c64b8ea40a 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -69,7 +69,8 @@ const inspectDefaultOptions = Object.seal({
customInspect: true,
showProxy: false,
maxArrayLength: 100,
- breakLength: 60
+ breakLength: 60,
+ compact: true
});
const propertyIsEnumerable = Object.prototype.propertyIsEnumerable;
@@ -87,6 +88,10 @@ const keyStrRegExp = /^[a-zA-Z_][a-zA-Z_0-9]*$/;
const colorRegExp = /\u001b\[\d\d?m/g;
const numberRegExp = /^(0|[1-9][0-9]*)$/;
+const readableRegExps = {};
+
+const MIN_LINE_LENGTH = 16;
+
// Escaped special characters. Use empty strings to fill up unused entries.
const meta = [
'\\u0000', '\\u0001', '\\u0002', '\\u0003', '\\u0004',
@@ -277,7 +282,8 @@ function inspect(obj, opts) {
showProxy: inspectDefaultOptions.showProxy,
maxArrayLength: inspectDefaultOptions.maxArrayLength,
breakLength: inspectDefaultOptions.breakLength,
- indentationLvl: 0
+ indentationLvl: 0,
+ compact: inspectDefaultOptions.compact
};
// Legacy...
if (arguments.length > 2) {
@@ -363,7 +369,7 @@ function stylizeNoColor(str, styleType) {
function formatValue(ctx, value, recurseTimes, ln) {
// Primitive types cannot have properties
if (typeof value !== 'object' && typeof value !== 'function') {
- return formatPrimitive(ctx.stylize, value);
+ return formatPrimitive(ctx.stylize, value, ctx);
}
if (value === null) {
return ctx.stylize('null', 'null');
@@ -485,10 +491,10 @@ function formatValue(ctx, value, recurseTimes, ln) {
} catch (e) { /* ignore */ }
if (typeof raw === 'string') {
- const formatted = formatPrimitive(stylizeNoColor, raw);
+ const formatted = formatPrimitive(stylizeNoColor, raw, ctx);
if (keyLength === raw.length)
return ctx.stylize(`[String: ${formatted}]`, 'string');
- base = ` [String: ${formatted}]`;
+ base = `[String: ${formatted}]`;
// For boxed Strings, we have to remove the 0-n indexed entries,
// since they just noisy up the output and are redundant
// Make boxed primitive Strings look like such
@@ -510,12 +516,12 @@ function formatValue(ctx, value, recurseTimes, ln) {
`${constructor || tag}${value.name ? `: ${value.name}` : ''}`;
if (keyLength === 0)
return ctx.stylize(`[${name}]`, 'special');
- base = ` [${name}]`;
+ base = `[${name}]`;
} else if (isRegExp(value)) {
// Make RegExps say that they are RegExps
if (keyLength === 0 || recurseTimes < 0)
return ctx.stylize(regExpToString.call(value), 'regexp');
- base = ` ${regExpToString.call(value)}`;
+ base = `${regExpToString.call(value)}`;
} else if (isDate(value)) {
if (keyLength === 0) {
if (Number.isNaN(value.getTime()))
@@ -523,12 +529,12 @@ function formatValue(ctx, value, recurseTimes, ln) {
return ctx.stylize(dateToISOString.call(value), 'date');
}
// Make dates with properties first say the date
- base = ` ${dateToISOString.call(value)}`;
+ base = `${dateToISOString.call(value)}`;
} else if (isError(value)) {
// Make error with message first say the error
if (keyLength === 0)
return formatError(value);
- base = ` ${formatError(value)}`;
+ base = `${formatError(value)}`;
} else if (isAnyArrayBuffer(value)) {
// Fast path for ArrayBuffer and SharedArrayBuffer.
// Can't do the same for DataView because it has a non-primitive
@@ -558,13 +564,13 @@ function formatValue(ctx, value, recurseTimes, ln) {
const formatted = formatPrimitive(stylizeNoColor, raw);
if (keyLength === 0)
return ctx.stylize(`[Number: ${formatted}]`, 'number');
- base = ` [Number: ${formatted}]`;
+ base = `[Number: ${formatted}]`;
} else if (typeof raw === 'boolean') {
// Make boxed primitive Booleans look like such
const formatted = formatPrimitive(stylizeNoColor, raw);
if (keyLength === 0)
return ctx.stylize(`[Boolean: ${formatted}]`, 'boolean');
- base = ` [Boolean: ${formatted}]`;
+ base = `[Boolean: ${formatted}]`;
} else if (typeof raw === 'symbol') {
const formatted = formatPrimitive(stylizeNoColor, raw);
return ctx.stylize(`[Symbol: ${formatted}]`, 'symbol');
@@ -607,9 +613,42 @@ function formatNumber(fn, value) {
return fn(`${value}`, 'number');
}
-function formatPrimitive(fn, value) {
- if (typeof value === 'string')
+function formatPrimitive(fn, value, ctx) {
+ if (typeof value === 'string') {
+ if (ctx.compact === false &&
+ value.length > MIN_LINE_LENGTH &&
+ ctx.indentationLvl + value.length > ctx.breakLength) {
+ // eslint-disable-next-line max-len
+ const minLineLength = Math.max(ctx.breakLength - ctx.indentationLvl, MIN_LINE_LENGTH);
+ // eslint-disable-next-line max-len
+ const averageLineLength = Math.ceil(value.length / Math.ceil(value.length / minLineLength));
+ const divisor = Math.max(averageLineLength, MIN_LINE_LENGTH);
+ var res = '';
+ if (readableRegExps[divisor] === undefined) {
+ // Build a new RegExp that naturally breaks text into multiple lines.
+ //
+ // Rules
+ // 1. Greedy match all text up the max line length that ends with a
+ // whitespace or the end of the string.
+ // 2. If none matches, non-greedy match any text up to a whitespace or
+ // the end of the string.
+ //
+ // eslint-disable-next-line max-len, no-unescaped-regexp-dot
+ readableRegExps[divisor] = new RegExp(`(.|\\n){1,${divisor}}(\\s|$)|(\\n|.)+?(\\s|$)`, 'gm');
+ }
+ const indent = ' '.repeat(ctx.indentationLvl);
+ const matches = value.match(readableRegExps[divisor]);
+ if (matches.length > 1) {
+ res += `${fn(strEscape(matches[0]), 'string')} +\n`;
+ for (var i = 1; i < matches.length - 1; i++) {
+ res += `${indent} ${fn(strEscape(matches[i]), 'string')} +\n`;
+ }
+ res += `${indent} ${fn(strEscape(matches[i]), 'string')}`;
+ return res;
+ }
+ }
return fn(strEscape(value), 'string');
+ }
if (typeof value === 'number')
return formatNumber(fn, value);
if (typeof value === 'boolean')
@@ -820,7 +859,7 @@ function formatProperty(ctx, value, recurseTimes, key, array) {
const desc = Object.getOwnPropertyDescriptor(value, key) ||
{ value: value[key], enumerable: true };
if (desc.value !== undefined) {
- const diff = array === 0 ? 3 : 2;
+ const diff = array !== 0 || ctx.compact === false ? 2 : 3;
ctx.indentationLvl += diff;
str = formatValue(ctx, desc.value, recurseTimes, array === 0);
ctx.indentationLvl -= diff;
@@ -853,9 +892,19 @@ function formatProperty(ctx, value, recurseTimes, key, array) {
function reduceToSingleString(ctx, output, base, braces, addLn) {
const breakLength = ctx.breakLength;
+ var i = 0;
+ if (ctx.compact === false) {
+ const indentation = ' '.repeat(ctx.indentationLvl);
+ var res = `${base ? `${base} ` : ''}${braces[0]}\n${indentation} `;
+ for (; i < output.length - 1; i++) {
+ res += `${output[i]},\n${indentation} `;
+ }
+ res += `${output[i]}\n${indentation}${braces[1]}`;
+ return res;
+ }
if (output.length * 2 <= breakLength) {
var length = 0;
- for (var i = 0; i < output.length && length <= breakLength; i++) {
+ for (; i < output.length && length <= breakLength; i++) {
if (ctx.colors) {
length += output[i].replace(colorRegExp, '').length + 1;
} else {
@@ -863,7 +912,8 @@ function reduceToSingleString(ctx, output, base, braces, addLn) {
}
}
if (length <= breakLength)
- return `${braces[0]}${base} ${join(output, ', ')} ${braces[1]}`;
+ return `${braces[0]}${base ? ` ${base}` : ''} ${join(output, ', ')} ` +
+ braces[1];
}
// If the opening "brace" is too large, like in the case of "Set {",
// we need to force the first item to be on the next line or the
@@ -871,7 +921,7 @@ function reduceToSingleString(ctx, output, base, braces, addLn) {
const indentation = ' '.repeat(ctx.indentationLvl);
const extraLn = addLn === true ? `\n${indentation}` : '';
const ln = base === '' && braces[0].length === 1 ?
- ' ' : `${base}\n${indentation} `;
+ ' ' : `${base ? ` ${base}` : base}\n${indentation} `;
const str = join(output, `,\n${indentation} `);
return `${extraLn}${braces[0]}${ln}${str} ${braces[1]}`;
}