util: add maxArrayLength
option to Set and Map
PR-URL: https://github.com/nodejs/node/pull/43576 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
parent
48d4e3d0b2
commit
71ca6d7d6a
@ -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
|
||||
@ -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.
|
||||
|
@ -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.
|
||||
@ -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;
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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.
|
||||
@ -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;
|
||||
@ -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;
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
|
@ -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(
|
||||
@ -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),
|
||||
|
Loading…
x
Reference in New Issue
Block a user