Skip to content

Commit 520b3e6

Browse files
committed
util: fix map entries inspection
This makes sure the arrays returned by Map#entries() are handled as any other array instead of just visualizing the entries as array. Therefore options should have an impact on the arrays. PR-URL: #26918 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent f9da55c commit 520b3e6

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

lib/internal/util/inspect.js

+15-14
Original file line numberDiff line numberDiff line change
@@ -1180,27 +1180,28 @@ function formatMapIterInner(ctx, recurseTimes, entries, state) {
11801180
const remaining = len - maxArrayLength;
11811181
const maxLength = Math.min(maxArrayLength, len);
11821182
let output = new Array(maxLength);
1183-
let start = '';
1184-
let end = '';
1185-
let middle = ' => ';
11861183
let i = 0;
1187-
if (state === kMapEntries) {
1188-
start = '[ ';
1189-
end = ' ]';
1190-
middle = ', ';
1191-
}
11921184
ctx.indentationLvl += 2;
1193-
for (; i < maxLength; i++) {
1194-
const pos = i * 2;
1195-
output[i] = `${start}${formatValue(ctx, entries[pos], recurseTimes)}` +
1196-
`${middle}${formatValue(ctx, entries[pos + 1], recurseTimes)}${end}`;
1197-
}
1198-
ctx.indentationLvl -= 2;
11991185
if (state === kWeak) {
1186+
for (; i < maxLength; i++) {
1187+
const pos = i * 2;
1188+
output[i] = `${formatValue(ctx, entries[pos], recurseTimes)}` +
1189+
` => ${formatValue(ctx, entries[pos + 1], recurseTimes)}`;
1190+
}
12001191
// Sort all entries to have a halfway reliable output (if more entries
12011192
// than retrieved ones exist, we can not reliably return the same output).
12021193
output = output.sort();
1194+
} else {
1195+
for (; i < maxLength; i++) {
1196+
const pos = i * 2;
1197+
const res = [
1198+
formatValue(ctx, entries[pos], recurseTimes),
1199+
formatValue(ctx, entries[pos + 1], recurseTimes)
1200+
];
1201+
output[i] = reduceToSingleString(ctx, res, '', ['[', ']']);
1202+
}
12031203
}
1204+
ctx.indentationLvl -= 2;
12041205
if (remaining > 0) {
12051206
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
12061207
}

test/parallel/test-util-inspect.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,10 @@ if (typeof Symbol !== 'undefined') {
10171017

10181018
// Test Set iterators.
10191019
{
1020-
const aSet = new Set([1, 3]);
1020+
const aSet = new Set([1]);
1021+
assert.strictEqual(util.inspect(aSet.entries(), { compact: false }),
1022+
'[Set Entries] {\n [\n 1,\n 1\n ]\n}');
1023+
aSet.add(3);
10211024
assert.strictEqual(util.inspect(aSet.keys()), '[Set Iterator] { 1, 3 }');
10221025
assert.strictEqual(util.inspect(aSet.values()), '[Set Iterator] { 1, 3 }');
10231026
const setEntries = aSet.entries();

0 commit comments

Comments
 (0)