Skip to content

Commit

Permalink
util: switch inspect "base" order
Browse files Browse the repository at this point in the history
In case the structured option is used, it will now print

  "[Function: foo] {\n  property: 'data'\n}"

instead of

  "{ [Function: foo]\n  property: 'data'\n}"
  • Loading branch information
BridgeAR committed Dec 10, 2017
1 parent 843a5d5 commit cd5be74
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
22 changes: 11 additions & 11 deletions lib/util.js
Expand Up @@ -490,7 +490,7 @@ function formatValue(ctx, value, recurseTimes, ln) {
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
Expand All @@ -512,25 +512,25 @@ 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()))
return ctx.stylize(value.toString(), 'date');
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
Expand Down Expand Up @@ -560,13 +560,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');
Expand Down Expand Up @@ -890,8 +890,7 @@ function reduceToSingleString(ctx, output, base, braces, addLn) {
var i = 0;
if (ctx.structured === true) {
const indentation = ' '.repeat(ctx.indentationLvl);
var res = braces[0];
res += `${base}\n${indentation} `;
var res = `${base ? `${base} ` : ''}${braces[0]}\n${indentation} `;
for (; i < output.length - 1; i++) {
res += `${output[i]},\n${indentation} `;
}
Expand All @@ -908,15 +907,16 @@ 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
// items will not line up correctly.
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]}`;
}
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-util-inspect.js
Expand Up @@ -1311,4 +1311,31 @@ assert.doesNotThrow(() => util.inspect(process));
' \'0\''
].join('\n');
assert.strictEqual(out, expect);

o.a = () => {};
o.b = new Number(3);
out = util.inspect(
o,
{ structured: true, breakLength: 3 });
expect = [
'{',
' a: [Function],',
' b: [Number: 3]',
'}'
].join('\n');
assert.strictEqual(out, expect);

out = util.inspect(
o,
{ structured: true, breakLength: 3, showHidden: 3 });
expect = [
'{',
' a: [Function] {',
' [length]: 0,',
" [name]: ''",
' },',
' b: [Number: 3]',
'}'
].join('\n');
assert.strictEqual(out, expect);
}

0 comments on commit cd5be74

Please sign in to comment.