GeezerVison is showing github.com — util: add `maxArrayLength` option to Set and Map · nodejs/node@71ca6d7
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/util.md +6-2Lines changed: 6 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -485,6 +485,9 @@ stream.write('With ES6'); | 485 | 485 | <!-- YAML | 486 | 486 | added: v0.3.0 | 487 | 487 | changes: | | 488 | + - version: REPLACEME | | 489 | + pr-url: https://github.com/nodejs/node/pull/43576 | | 490 | + description: add support for `maxArrayLength` when inspecting `Set` and `Map`. | 488 | 491 | - version: | 489 | 492 | - v17.3.0 | 490 | 493 | - v16.14.0 | @@ -586,8 +589,9 @@ changes: | 586 | 589 | * `showProxy` {boolean} If `true`, `Proxy` inspection includes | 587 | 590 | the [`target` and `handler`][] objects. **Default:** `false`. | 588 | 591 | * `maxArrayLength` {integer} Specifies the maximum number of `Array`, | 589 | | - [`TypedArray`][], [`WeakMap`][], and [`WeakSet`][] elements to include when | 590 | | - formatting. Set to `null` or `Infinity` to show all elements. Set to `0` or | | 592 | + [`TypedArray`][], [`Map`][], [`Set`][], [`WeakMap`][], | | 593 | + and [`WeakSet`][] elements to include when formatting. | | 594 | + Set to `null` or `Infinity` to show all elements. Set to `0` or | 591 | 595 | negative to show no elements. **Default:** `100`. | 592 | 596 | * `maxStringLength` {integer} Specifies the maximum number of characters to | 593 | 597 | include when formatting. Set to `null` or `Infinity` to show all elements. | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/util/inspect.js +25-6Lines changed: 25 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1553,6 +1553,8 @@ function addNumericSeparatorEnd(integerString) { | 1553 | 1553 | `${result}${integerString.slice(i)}`; | 1554 | 1554 | } | 1555 | 1555 |
| | 1556 | +const remainingText = (remaining) => `... ${remaining} more item${remaining > 1 ? 's' : ''}`; | | 1557 | + | 1556 | 1558 | function formatNumber(fn, number, numericSeparator) { | 1557 | 1559 | if (!numericSeparator) { | 1558 | 1560 | // 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) { | 1679 | 1681 | output.push(ctx.stylize(message, 'undefined')); | 1680 | 1682 | } | 1681 | 1683 | } else if (remaining > 0) { | 1682 | | - output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`); | | 1684 | + output.push(remainingText(remaining)); | 1683 | 1685 | } | 1684 | 1686 | return output; | 1685 | 1687 | } | @@ -1717,7 +1719,7 @@ function formatArray(ctx, value, recurseTimes) { | 1717 | 1719 | output.push(formatProperty(ctx, value, recurseTimes, i, kArrayType)); | 1718 | 1720 | } | 1719 | 1721 | if (remaining > 0) | 1720 | | - output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`); | | 1722 | + output.push(remainingText(remaining)); | 1721 | 1723 | return output; | 1722 | 1724 | } | 1723 | 1725 |
| @@ -1732,7 +1734,7 @@ function formatTypedArray(value, length, ctx, ignored, recurseTimes) { | 1732 | 1734 | output[i] = elementFormatter(ctx.stylize, value[i], ctx.numericSeparator); | 1733 | 1735 | } | 1734 | 1736 | if (remaining > 0) { | 1735 | | - output[maxLength] = `... ${remaining} more item${remaining > 1 ? 's' : ''}`; | | 1737 | + output[maxLength] = remainingText(remaining); | 1736 | 1738 | } | 1737 | 1739 | if (ctx.showHidden) { | 1738 | 1740 | // .buffer goes last, it's not a primitive like the others. | @@ -1754,22 +1756,40 @@ function formatTypedArray(value, length, ctx, ignored, recurseTimes) { | 1754 | 1756 | } | 1755 | 1757 |
| 1756 | 1758 | function formatSet(value, ctx, ignored, recurseTimes) { | | 1759 | + const length = value.size; | | 1760 | + const maxLength = MathMin(MathMax(0, ctx.maxArrayLength), length); | | 1761 | + const remaining = length - maxLength; | 1757 | 1762 | const output = []; | 1758 | 1763 | ctx.indentationLvl += 2; | | 1764 | + let i = 0; | 1759 | 1765 | for (const v of value) { | | 1766 | + if (i >= maxLength) break; | 1760 | 1767 | ArrayPrototypePush(output, formatValue(ctx, v, recurseTimes)); | | 1768 | + i++; | | 1769 | + } | | 1770 | + if (remaining > 0) { | | 1771 | + ArrayPrototypePush(output, remainingText(remaining)); | 1761 | 1772 | } | 1762 | 1773 | ctx.indentationLvl -= 2; | 1763 | 1774 | return output; | 1764 | 1775 | } | 1765 | 1776 |
| 1766 | 1777 | function formatMap(value, ctx, ignored, recurseTimes) { | | 1778 | + const length = value.size; | | 1779 | + const maxLength = MathMin(MathMax(0, ctx.maxArrayLength), length); | | 1780 | + const remaining = length - maxLength; | 1767 | 1781 | const output = []; | 1768 | 1782 | ctx.indentationLvl += 2; | | 1783 | + let i = 0; | 1769 | 1784 | for (const { 0: k, 1: v } of value) { | | 1785 | + if (i >= maxLength) break; | 1770 | 1786 | output.push( | 1771 | 1787 | `${formatValue(ctx, k, recurseTimes)} => ${formatValue(ctx, v, recurseTimes)}` | 1772 | 1788 | ); | | 1789 | + i++; | | 1790 | + } | | 1791 | + if (remaining > 0) { | | 1792 | + ArrayPrototypePush(output, remainingText(remaining)); | 1773 | 1793 | } | 1774 | 1794 | ctx.indentationLvl -= 2; | 1775 | 1795 | return output; | @@ -1792,8 +1812,7 @@ function formatSetIterInner(ctx, recurseTimes, entries, state) { | 1792 | 1812 | } | 1793 | 1813 | const remaining = entries.length - maxLength; | 1794 | 1814 | if (remaining > 0) { | 1795 | | - ArrayPrototypePush(output, | 1796 | | - `... ${remaining} more item${remaining > 1 ? 's' : ''}`); | | 1815 | + ArrayPrototypePush(output, remainingText(remaining)); | 1797 | 1816 | } | 1798 | 1817 | return output; | 1799 | 1818 | } | @@ -1831,7 +1850,7 @@ function formatMapIterInner(ctx, recurseTimes, entries, state) { | 1831 | 1850 | } | 1832 | 1851 | ctx.indentationLvl -= 2; | 1833 | 1852 | if (remaining > 0) { | 1834 | | - output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`); | | 1853 | + output.push(remainingText(remaining)); | 1835 | 1854 | } | 1836 | 1855 | return output; | 1837 | 1856 | } | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-util-inspect.js +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1172,6 +1172,7 @@ if (typeof Symbol !== 'undefined') { | 1172 | 1172 | { | 1173 | 1173 | assert.strictEqual(util.inspect(new Set()), 'Set(0) {}'); | 1174 | 1174 | assert.strictEqual(util.inspect(new Set([1, 2, 3])), 'Set(3) { 1, 2, 3 }'); | | 1175 | + assert.strictEqual(util.inspect(new Set([1, 2, 3]), { maxArrayLength: 1 }), 'Set(3) { 1, ... 2 more items }'); | 1175 | 1176 | const set = new Set(['foo']); | 1176 | 1177 | set.bar = 42; | 1177 | 1178 | assert.strictEqual( | @@ -1192,6 +1193,8 @@ if (typeof Symbol !== 'undefined') { | 1192 | 1193 | assert.strictEqual(util.inspect(new Map()), 'Map(0) {}'); | 1193 | 1194 | assert.strictEqual(util.inspect(new Map([[1, 'a'], [2, 'b'], [3, 'c']])), | 1194 | 1195 | "Map(3) { 1 => 'a', 2 => 'b', 3 => 'c' }"); | | 1196 | + assert.strictEqual(util.inspect(new Map([[1, 'a'], [2, 'b'], [3, 'c']]), { maxArrayLength: 1 }), | | 1197 | + "Map(3) { 1 => 'a', ... 2 more items }"); | 1195 | 1198 | const map = new Map([['foo', null]]); | 1196 | 1199 | map.bar = 42; | 1197 | 1200 | assert.strictEqual(util.inspect(map, true), | |
|