Skip to content

Commit

Permalink
util: add maxArrayLength option to Set and Map
Browse files Browse the repository at this point in the history
PR-URL: #43576
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
cola119 committed Jul 14, 2022
1 parent 48d4e3d commit 71ca6d7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
8 changes: 6 additions & 2 deletions doc/api/util.md
Expand Up @@ -485,6 +485,9 @@ stream.write('With ES6');
<!-- YAML
added: v0.3.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/43576
description: add support for `maxArrayLength` when inspecting `Set` and `Map`.
- version:
- v17.3.0
- v16.14.0
Expand Down Expand Up @@ -586,8 +589,9 @@ changes:
* `showProxy` {boolean} If `true`, `Proxy` inspection includes
the [`target` and `handler`][] objects. **Default:** `false`.
* `maxArrayLength` {integer} Specifies the maximum number of `Array`,
[`TypedArray`][], [`WeakMap`][], and [`WeakSet`][] elements to include when
formatting. Set to `null` or `Infinity` to show all elements. Set to `0` or
[`TypedArray`][], [`Map`][], [`Set`][], [`WeakMap`][],
and [`WeakSet`][] elements to include when formatting.
Set to `null` or `Infinity` to show all elements. Set to `0` or
negative to show no elements. **Default:** `100`.
* `maxStringLength` {integer} Specifies the maximum number of characters to
include when formatting. Set to `null` or `Infinity` to show all elements.
Expand Down
31 changes: 25 additions & 6 deletions lib/internal/util/inspect.js
Expand Up @@ -1553,6 +1553,8 @@ function addNumericSeparatorEnd(integerString) {
`${result}${integerString.slice(i)}`;
}

const remainingText = (remaining) => `... ${remaining} more item${remaining > 1 ? 's' : ''}`;

function formatNumber(fn, number, numericSeparator) {
if (!numericSeparator) {
// Format -0 as '-0'. Checking `number === -0` won't distinguish 0 from -0.
Expand Down Expand Up @@ -1679,7 +1681,7 @@ function formatSpecialArray(ctx, value, recurseTimes, maxLength, output, i) {
output.push(ctx.stylize(message, 'undefined'));
}
} else if (remaining > 0) {
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
output.push(remainingText(remaining));
}
return output;
}
Expand Down Expand Up @@ -1717,7 +1719,7 @@ function formatArray(ctx, value, recurseTimes) {
output.push(formatProperty(ctx, value, recurseTimes, i, kArrayType));
}
if (remaining > 0)
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
output.push(remainingText(remaining));
return output;
}

Expand All @@ -1732,7 +1734,7 @@ function formatTypedArray(value, length, ctx, ignored, recurseTimes) {
output[i] = elementFormatter(ctx.stylize, value[i], ctx.numericSeparator);
}
if (remaining > 0) {
output[maxLength] = `... ${remaining} more item${remaining > 1 ? 's' : ''}`;
output[maxLength] = remainingText(remaining);
}
if (ctx.showHidden) {
// .buffer goes last, it's not a primitive like the others.
Expand All @@ -1754,22 +1756,40 @@ function formatTypedArray(value, length, ctx, ignored, recurseTimes) {
}

function formatSet(value, ctx, ignored, recurseTimes) {
const length = value.size;
const maxLength = MathMin(MathMax(0, ctx.maxArrayLength), length);
const remaining = length - maxLength;
const output = [];
ctx.indentationLvl += 2;
let i = 0;
for (const v of value) {
if (i >= maxLength) break;
ArrayPrototypePush(output, formatValue(ctx, v, recurseTimes));
i++;
}
if (remaining > 0) {
ArrayPrototypePush(output, remainingText(remaining));
}
ctx.indentationLvl -= 2;
return output;
}

function formatMap(value, ctx, ignored, recurseTimes) {
const length = value.size;
const maxLength = MathMin(MathMax(0, ctx.maxArrayLength), length);
const remaining = length - maxLength;
const output = [];
ctx.indentationLvl += 2;
let i = 0;
for (const { 0: k, 1: v } of value) {
if (i >= maxLength) break;
output.push(
`${formatValue(ctx, k, recurseTimes)} => ${formatValue(ctx, v, recurseTimes)}`
);
i++;
}
if (remaining > 0) {
ArrayPrototypePush(output, remainingText(remaining));
}
ctx.indentationLvl -= 2;
return output;
Expand All @@ -1792,8 +1812,7 @@ function formatSetIterInner(ctx, recurseTimes, entries, state) {
}
const remaining = entries.length - maxLength;
if (remaining > 0) {
ArrayPrototypePush(output,
`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
ArrayPrototypePush(output, remainingText(remaining));
}
return output;
}
Expand Down Expand Up @@ -1831,7 +1850,7 @@ function formatMapIterInner(ctx, recurseTimes, entries, state) {
}
ctx.indentationLvl -= 2;
if (remaining > 0) {
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
output.push(remainingText(remaining));
}
return output;
}
Expand Down
3 changes: 3 additions & 0 deletions test/parallel/test-util-inspect.js
Expand Up @@ -1172,6 +1172,7 @@ if (typeof Symbol !== 'undefined') {
{
assert.strictEqual(util.inspect(new Set()), 'Set(0) {}');
assert.strictEqual(util.inspect(new Set([1, 2, 3])), 'Set(3) { 1, 2, 3 }');
assert.strictEqual(util.inspect(new Set([1, 2, 3]), { maxArrayLength: 1 }), 'Set(3) { 1, ... 2 more items }');
const set = new Set(['foo']);
set.bar = 42;
assert.strictEqual(
Expand All @@ -1192,6 +1193,8 @@ if (typeof Symbol !== 'undefined') {
assert.strictEqual(util.inspect(new Map()), 'Map(0) {}');
assert.strictEqual(util.inspect(new Map([[1, 'a'], [2, 'b'], [3, 'c']])),
"Map(3) { 1 => 'a', 2 => 'b', 3 => 'c' }");
assert.strictEqual(util.inspect(new Map([[1, 'a'], [2, 'b'], [3, 'c']]), { maxArrayLength: 1 }),
"Map(3) { 1 => 'a', ... 2 more items }");
const map = new Map([['foo', null]]);
map.bar = 42;
assert.strictEqual(util.inspect(map, true),
Expand Down

0 comments on commit 71ca6d7

Please sign in to comment.