Skip to content

Commit 944d862

Browse files
rosaxxnyaddaleax
authored andcommitted
util: add maxStrLength option to inspect function
Refs: #25478 PR-URL: #32392 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Matheus Marchini <mat@mmarchini.me> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 9b46e3f commit 944d862

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

doc/api/util.md

+6
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: v13.5.0
402405
pr-url: https://github.com/nodejs/node/pull/30768
403406
description: User defined prototype properties are inspected in case
@@ -483,6 +486,9 @@ changes:
483486
[`TypedArray`][], [`WeakMap`][] and [`WeakSet`][] elements to include when
484487
formatting. Set to `null` or `Infinity` to show all elements. Set to `0` or
485488
negative to show no elements. **Default:** `100`.
489+
* `maxStringLength` {integer} Specifies the maximum number of characters to
490+
include when formatting. Set to `null` or `Infinity` to show all elements.
491+
Set to `0` or negative to show no characters. **Default:** `Infinity`.
486492
* `breakLength` {integer} The length at which input values are split across
487493
multiple lines. Set to `Infinity` to format the input as a single line
488494
(in combination with `compact` set to `true` or any number >= `1`).

lib/internal/util/inspect.js

+12-2
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,
@@ -215,6 +216,7 @@ function getUserOptions(ctx) {
215216
customInspect: ctx.customInspect,
216217
showProxy: ctx.showProxy,
217218
maxArrayLength: ctx.maxArrayLength,
219+
maxStringLength: ctx.maxStringLength,
218220
breakLength: ctx.breakLength,
219221
compact: ctx.compact,
220222
sorted: ctx.sorted,
@@ -245,6 +247,7 @@ function inspect(value, opts) {
245247
customInspect: inspectDefaultOptions.customInspect,
246248
showProxy: inspectDefaultOptions.showProxy,
247249
maxArrayLength: inspectDefaultOptions.maxArrayLength,
250+
maxStringLength: inspectDefaultOptions.maxStringLength,
248251
breakLength: inspectDefaultOptions.breakLength,
249252
compact: inspectDefaultOptions.compact,
250253
sorted: inspectDefaultOptions.sorted,
@@ -282,6 +285,7 @@ function inspect(value, opts) {
282285
}
283286
if (ctx.colors) ctx.stylize = stylizeWithColor;
284287
if (ctx.maxArrayLength === null) ctx.maxArrayLength = Infinity;
288+
if (ctx.maxStringLength === null) ctx.maxStringLength = Infinity;
285289
return formatValue(ctx, value, 0);
286290
}
287291
inspect.custom = customInspectSymbol;
@@ -1301,6 +1305,12 @@ function formatBigInt(fn, value) {
13011305

13021306
function formatPrimitive(fn, value, ctx) {
13031307
if (typeof value === 'string') {
1308+
let trailer = '';
1309+
if (value.length > ctx.maxStringLength) {
1310+
const remaining = value.length - ctx.maxStringLength;
1311+
value = value.slice(0, ctx.maxStringLength);
1312+
trailer = `... ${remaining} more character${remaining > 1 ? 's' : ''}`;
1313+
}
13041314
if (ctx.compact !== true &&
13051315
// TODO(BridgeAR): Add unicode support. Use the readline getStringWidth
13061316
// function.
@@ -1309,9 +1319,9 @@ function formatPrimitive(fn, value, ctx) {
13091319
return value
13101320
.split(/(?<=\n)/)
13111321
.map((line) => fn(strEscape(line), 'string'))
1312-
.join(` +\n${' '.repeat(ctx.indentationLvl + 2)}`);
1322+
.join(` +\n${' '.repeat(ctx.indentationLvl + 2)}`) + trailer;
13131323
}
1314-
return fn(strEscape(value), 'string');
1324+
return fn(strEscape(value), 'string') + trailer;
13151325
}
13161326
if (typeof value === 'number')
13171327
return formatNumber(fn, value);

test/parallel/test-util-inspect.js

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

0 commit comments

Comments
 (0)