Skip to content

Commit 6aa3869

Browse files
rosaxxnytargos
authored andcommitted
util: add maxStrLength option to inspect function
Refs: #25478 PR-URL: #32392 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Matheus Marchini <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent c0d3fc2 commit 6aa3869

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

doc/api/util.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,9 @@ stream.write('With ES6');
398398
<!-- YAML
399399
added: v0.3.0
400400
changes:
401+
- version: REPLACEME
402+
pr-url: https://github.com/nodejs/node/pull/32392
403+
description: The `maxStringLength` option is supported now.
401404
- version: v12.16.0
402405
pr-url: https://github.com/nodejs/node/pull/30768
403406
description: User defined prototype properties are inspected in case
@@ -480,6 +483,9 @@ changes:
480483
[`TypedArray`][], [`WeakMap`][] and [`WeakSet`][] elements to include when
481484
formatting. Set to `null` or `Infinity` to show all elements. Set to `0` or
482485
negative to show no elements. **Default:** `100`.
486+
* `maxStringLength` {integer} Specifies the maximum number of characters to
487+
include when formatting. Set to `null` or `Infinity` to show all elements.
488+
Set to `0` or negative to show no characters. **Default:** `Infinity`.
483489
* `breakLength` {integer} The length at which input values are split across
484490
multiple lines. Set to `Infinity` to format the input as a single line
485491
(in combination with `compact` set to `true` or any number >= `1`).

lib/internal/util/inspect.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ const inspectDefaultOptions = ObjectSeal({
146146
customInspect: true,
147147
showProxy: false,
148148
maxArrayLength: 100,
149+
maxStringLength: Infinity,
149150
breakLength: 80,
150151
compact: 3,
151152
sorted: false,
@@ -213,6 +214,7 @@ function getUserOptions(ctx) {
213214
customInspect: ctx.customInspect,
214215
showProxy: ctx.showProxy,
215216
maxArrayLength: ctx.maxArrayLength,
217+
maxStringLength: ctx.maxStringLength,
216218
breakLength: ctx.breakLength,
217219
compact: ctx.compact,
218220
sorted: ctx.sorted,
@@ -243,6 +245,7 @@ function inspect(value, opts) {
243245
customInspect: inspectDefaultOptions.customInspect,
244246
showProxy: inspectDefaultOptions.showProxy,
245247
maxArrayLength: inspectDefaultOptions.maxArrayLength,
248+
maxStringLength: inspectDefaultOptions.maxStringLength,
246249
breakLength: inspectDefaultOptions.breakLength,
247250
compact: inspectDefaultOptions.compact,
248251
sorted: inspectDefaultOptions.sorted,
@@ -280,6 +283,7 @@ function inspect(value, opts) {
280283
}
281284
if (ctx.colors) ctx.stylize = stylizeWithColor;
282285
if (ctx.maxArrayLength === null) ctx.maxArrayLength = Infinity;
286+
if (ctx.maxStringLength === null) ctx.maxStringLength = Infinity;
283287
return formatValue(ctx, value, 0);
284288
}
285289
inspect.custom = customInspectSymbol;
@@ -1293,6 +1297,12 @@ function formatBigInt(fn, value) {
12931297

12941298
function formatPrimitive(fn, value, ctx) {
12951299
if (typeof value === 'string') {
1300+
let trailer = '';
1301+
if (value.length > ctx.maxStringLength) {
1302+
const remaining = value.length - ctx.maxStringLength;
1303+
value = value.slice(0, ctx.maxStringLength);
1304+
trailer = `... ${remaining} more character${remaining > 1 ? 's' : ''}`;
1305+
}
12961306
if (ctx.compact !== true &&
12971307
// TODO(BridgeAR): Add unicode support. Use the readline getStringWidth
12981308
// function.
@@ -1301,9 +1311,9 @@ function formatPrimitive(fn, value, ctx) {
13011311
return value
13021312
.split(/(?<=\n)/)
13031313
.map((line) => fn(strEscape(line), 'string'))
1304-
.join(` +\n${' '.repeat(ctx.indentationLvl + 2)}`);
1314+
.join(` +\n${' '.repeat(ctx.indentationLvl + 2)}`) + trailer;
13051315
}
1306-
return fn(strEscape(value), 'string');
1316+
return fn(strEscape(value), 'string') + trailer;
13071317
}
13081318
if (typeof value === 'number')
13091319
return formatNumber(fn, value);

test/parallel/test-util-inspect.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2778,3 +2778,11 @@ assert.strictEqual(
27782778
v8.setFlagsFromString('--no-allow-natives-syntax');
27792779
assert.strictEqual(inspect(undetectable), '{}');
27802780
}
2781+
2782+
{
2783+
const x = 'a'.repeat(1e6);
2784+
assert.strictEqual(
2785+
util.inspect(x, { maxStringLength: 4 }),
2786+
"'aaaa'... 999996 more characters"
2787+
);
2788+
}

0 commit comments

Comments
 (0)