Skip to content

Commit 0087eb1

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 1763649 commit 0087eb1

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,
@@ -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;
@@ -1296,6 +1300,12 @@ function formatBigInt(fn, value) {
12961300

12971301
function formatPrimitive(fn, value, ctx) {
12981302
if (typeof value === 'string') {
1303+
let trailer = '';
1304+
if (value.length > ctx.maxStringLength) {
1305+
const remaining = value.length - ctx.maxStringLength;
1306+
value = value.slice(0, ctx.maxStringLength);
1307+
trailer = `... ${remaining} more character${remaining > 1 ? 's' : ''}`;
1308+
}
12991309
if (ctx.compact !== true &&
13001310
// TODO(BridgeAR): Add unicode support. Use the readline getStringWidth
13011311
// function.
@@ -1304,9 +1314,9 @@ function formatPrimitive(fn, value, ctx) {
13041314
return value
13051315
.split(/(?<=\n)/)
13061316
.map((line) => fn(strEscape(line), 'string'))
1307-
.join(` +\n${' '.repeat(ctx.indentationLvl + 2)}`);
1317+
.join(` +\n${' '.repeat(ctx.indentationLvl + 2)}`) + trailer;
13081318
}
1309-
return fn(strEscape(value), 'string');
1319+
return fn(strEscape(value), 'string') + trailer;
13101320
}
13111321
if (typeof value === 'number')
13121322
return formatNumber(fn, value);

test/parallel/test-util-inspect.js

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

0 commit comments

Comments
 (0)