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: nodejs/node#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 authored and guangwong committed Jan 3, 2023
1 parent 9e8a7f6 commit fa3904e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
11 changes: 8 additions & 3 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,11 @@ stream.write('With ES6');
<!-- YAML
added: v0.3.0
changes:
- version: v16.14.0
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/43576
description: add support for `maxArrayLength` when inspecting `Set` and `Map`.
- version:
- v16.14.0
pr-url: https://github.com/nodejs/node/pull/41003
description: The `numericSeparator` option is supported now.
- version:
Expand Down Expand Up @@ -582,8 +586,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
Original file line number Diff line number Diff line change
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 @@ -1680,7 +1682,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 @@ -1718,7 +1720,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 @@ -1733,7 +1735,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 @@ -1755,22 +1757,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 @@ -1793,8 +1813,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 @@ -1832,7 +1851,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
Original file line number Diff line number Diff line change
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 fa3904e

Please sign in to comment.