Skip to content

Commit 1aa6e99

Browse files
BridgeARBethGriggs
authored andcommitted
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> Signed-off-by: Beth Griggs <Bethany.Griggs@uk.ibm.com>
1 parent fd6381b commit 1aa6e99

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
@@ -1156,27 +1156,28 @@ function formatMapIterInner(ctx, recurseTimes, entries, state) {
11561156
const remaining = len - maxArrayLength;
11571157
const maxLength = Math.min(maxArrayLength, len);
11581158
let output = new Array(maxLength);
1159-
let start = '';
1160-
let end = '';
1161-
let middle = ' => ';
11621159
let i = 0;
1163-
if (state === kMapEntries) {
1164-
start = '[ ';
1165-
end = ' ]';
1166-
middle = ', ';
1167-
}
11681160
ctx.indentationLvl += 2;
1169-
for (; i < maxLength; i++) {
1170-
const pos = i * 2;
1171-
output[i] = `${start}${formatValue(ctx, entries[pos], recurseTimes)}` +
1172-
`${middle}${formatValue(ctx, entries[pos + 1], recurseTimes)}${end}`;
1173-
}
1174-
ctx.indentationLvl -= 2;
11751161
if (state === kWeak) {
1162+
for (; i < maxLength; i++) {
1163+
const pos = i * 2;
1164+
output[i] = `${formatValue(ctx, entries[pos], recurseTimes)}` +
1165+
` => ${formatValue(ctx, entries[pos + 1], recurseTimes)}`;
1166+
}
11761167
// Sort all entries to have a halfway reliable output (if more entries
11771168
// than retrieved ones exist, we can not reliably return the same output).
11781169
output = output.sort();
1170+
} else {
1171+
for (; i < maxLength; i++) {
1172+
const pos = i * 2;
1173+
const res = [
1174+
formatValue(ctx, entries[pos], recurseTimes),
1175+
formatValue(ctx, entries[pos + 1], recurseTimes)
1176+
];
1177+
output[i] = reduceToSingleString(ctx, res, '', ['[', ']']);
1178+
}
11791179
}
1180+
ctx.indentationLvl -= 2;
11801181
if (remaining > 0) {
11811182
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
11821183
}

test/parallel/test-util-inspect.js

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

998998
// Test Set iterators.
999999
{
1000-
const aSet = new Set([1, 3]);
1000+
const aSet = new Set([1]);
1001+
assert.strictEqual(util.inspect(aSet.entries(), { compact: false }),
1002+
'[Set Entries] {\n [\n 1,\n 1\n ]\n}');
1003+
aSet.add(3);
10011004
assert.strictEqual(util.inspect(aSet.keys()), '[Set Iterator] { 1, 3 }');
10021005
assert.strictEqual(util.inspect(aSet.values()), '[Set Iterator] { 1, 3 }');
10031006
const setEntries = aSet.entries();

0 commit comments

Comments
 (0)