Skip to content

Commit cd5be74

Browse files
committed
util: switch inspect "base" order
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}"
1 parent 843a5d5 commit cd5be74

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

lib/util.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ function formatValue(ctx, value, recurseTimes, ln) {
490490
const formatted = formatPrimitive(stylizeNoColor, raw, ctx);
491491
if (keyLength === raw.length)
492492
return ctx.stylize(`[String: ${formatted}]`, 'string');
493-
base = ` [String: ${formatted}]`;
493+
base = `[String: ${formatted}]`;
494494
// For boxed Strings, we have to remove the 0-n indexed entries,
495495
// since they just noisy up the output and are redundant
496496
// Make boxed primitive Strings look like such
@@ -512,25 +512,25 @@ function formatValue(ctx, value, recurseTimes, ln) {
512512
`${constructor || tag}${value.name ? `: ${value.name}` : ''}`;
513513
if (keyLength === 0)
514514
return ctx.stylize(`[${name}]`, 'special');
515-
base = ` [${name}]`;
515+
base = `[${name}]`;
516516
} else if (isRegExp(value)) {
517517
// Make RegExps say that they are RegExps
518518
if (keyLength === 0 || recurseTimes < 0)
519519
return ctx.stylize(regExpToString.call(value), 'regexp');
520-
base = ` ${regExpToString.call(value)}`;
520+
base = `${regExpToString.call(value)}`;
521521
} else if (isDate(value)) {
522522
if (keyLength === 0) {
523523
if (Number.isNaN(value.getTime()))
524524
return ctx.stylize(value.toString(), 'date');
525525
return ctx.stylize(dateToISOString.call(value), 'date');
526526
}
527527
// Make dates with properties first say the date
528-
base = ` ${dateToISOString.call(value)}`;
528+
base = `${dateToISOString.call(value)}`;
529529
} else if (isError(value)) {
530530
// Make error with message first say the error
531531
if (keyLength === 0)
532532
return formatError(value);
533-
base = ` ${formatError(value)}`;
533+
base = `${formatError(value)}`;
534534
} else if (isAnyArrayBuffer(value)) {
535535
// Fast path for ArrayBuffer and SharedArrayBuffer.
536536
// Can't do the same for DataView because it has a non-primitive
@@ -560,13 +560,13 @@ function formatValue(ctx, value, recurseTimes, ln) {
560560
const formatted = formatPrimitive(stylizeNoColor, raw);
561561
if (keyLength === 0)
562562
return ctx.stylize(`[Number: ${formatted}]`, 'number');
563-
base = ` [Number: ${formatted}]`;
563+
base = `[Number: ${formatted}]`;
564564
} else if (typeof raw === 'boolean') {
565565
// Make boxed primitive Booleans look like such
566566
const formatted = formatPrimitive(stylizeNoColor, raw);
567567
if (keyLength === 0)
568568
return ctx.stylize(`[Boolean: ${formatted}]`, 'boolean');
569-
base = ` [Boolean: ${formatted}]`;
569+
base = `[Boolean: ${formatted}]`;
570570
} else if (typeof raw === 'symbol') {
571571
const formatted = formatPrimitive(stylizeNoColor, raw);
572572
return ctx.stylize(`[Symbol: ${formatted}]`, 'symbol');
@@ -890,8 +890,7 @@ function reduceToSingleString(ctx, output, base, braces, addLn) {
890890
var i = 0;
891891
if (ctx.structured === true) {
892892
const indentation = ' '.repeat(ctx.indentationLvl);
893-
var res = braces[0];
894-
res += `${base}\n${indentation} `;
893+
var res = `${base ? `${base} ` : ''}${braces[0]}\n${indentation} `;
895894
for (; i < output.length - 1; i++) {
896895
res += `${output[i]},\n${indentation} `;
897896
}
@@ -908,15 +907,16 @@ function reduceToSingleString(ctx, output, base, braces, addLn) {
908907
}
909908
}
910909
if (length <= breakLength)
911-
return `${braces[0]}${base} ${join(output, ', ')} ${braces[1]}`;
910+
return `${braces[0]}${base ? ` ${base}` : ''} ${join(output, ', ')} ` +
911+
braces[1];
912912
}
913913
// If the opening "brace" is too large, like in the case of "Set {",
914914
// we need to force the first item to be on the next line or the
915915
// items will not line up correctly.
916916
const indentation = ' '.repeat(ctx.indentationLvl);
917917
const extraLn = addLn === true ? `\n${indentation}` : '';
918918
const ln = base === '' && braces[0].length === 1 ?
919-
' ' : `${base}\n${indentation} `;
919+
' ' : `${base ? ` ${base}` : base}\n${indentation} `;
920920
const str = join(output, `,\n${indentation} `);
921921
return `${extraLn}${braces[0]}${ln}${str} ${braces[1]}`;
922922
}

test/parallel/test-util-inspect.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,4 +1311,31 @@ assert.doesNotThrow(() => util.inspect(process));
13111311
' \'0\''
13121312
].join('\n');
13131313
assert.strictEqual(out, expect);
1314+
1315+
o.a = () => {};
1316+
o.b = new Number(3);
1317+
out = util.inspect(
1318+
o,
1319+
{ structured: true, breakLength: 3 });
1320+
expect = [
1321+
'{',
1322+
' a: [Function],',
1323+
' b: [Number: 3]',
1324+
'}'
1325+
].join('\n');
1326+
assert.strictEqual(out, expect);
1327+
1328+
out = util.inspect(
1329+
o,
1330+
{ structured: true, breakLength: 3, showHidden: 3 });
1331+
expect = [
1332+
'{',
1333+
' a: [Function] {',
1334+
' [length]: 0,',
1335+
" [name]: ''",
1336+
' },',
1337+
' b: [Number: 3]',
1338+
'}'
1339+
].join('\n');
1340+
assert.strictEqual(out, expect);
13141341
}

0 commit comments

Comments
 (0)