Skip to content

Commit

Permalink
util: add maxStrLength option to inspect function
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
rosaxxny authored and targos committed Apr 11, 2020
1 parent 1763649 commit 0087eb1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
6 changes: 6 additions & 0 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ stream.write('With ES6');
<!-- YAML
added: v0.3.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/32392
description: The `maxStringLength` option is supported now.
- version: v13.5.0
pr-url: https://github.com/nodejs/node/pull/30768
description: User defined prototype properties are inspected in case
Expand Down Expand Up @@ -483,6 +486,9 @@ changes:
[`TypedArray`][], [`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.
Set to `0` or negative to show no characters. **Default:** `Infinity`.
* `breakLength` {integer} The length at which input values are split across
multiple lines. Set to `Infinity` to format the input as a single line
(in combination with `compact` set to `true` or any number >= `1`).
Expand Down
14 changes: 12 additions & 2 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ const inspectDefaultOptions = ObjectSeal({
customInspect: true,
showProxy: false,
maxArrayLength: 100,
maxStringLength: Infinity,
breakLength: 80,
compact: 3,
sorted: false,
Expand Down Expand Up @@ -213,6 +214,7 @@ function getUserOptions(ctx) {
customInspect: ctx.customInspect,
showProxy: ctx.showProxy,
maxArrayLength: ctx.maxArrayLength,
maxStringLength: ctx.maxStringLength,
breakLength: ctx.breakLength,
compact: ctx.compact,
sorted: ctx.sorted,
Expand Down Expand Up @@ -243,6 +245,7 @@ function inspect(value, opts) {
customInspect: inspectDefaultOptions.customInspect,
showProxy: inspectDefaultOptions.showProxy,
maxArrayLength: inspectDefaultOptions.maxArrayLength,
maxStringLength: inspectDefaultOptions.maxStringLength,
breakLength: inspectDefaultOptions.breakLength,
compact: inspectDefaultOptions.compact,
sorted: inspectDefaultOptions.sorted,
Expand Down Expand Up @@ -280,6 +283,7 @@ function inspect(value, opts) {
}
if (ctx.colors) ctx.stylize = stylizeWithColor;
if (ctx.maxArrayLength === null) ctx.maxArrayLength = Infinity;
if (ctx.maxStringLength === null) ctx.maxStringLength = Infinity;
return formatValue(ctx, value, 0);
}
inspect.custom = customInspectSymbol;
Expand Down Expand Up @@ -1296,6 +1300,12 @@ function formatBigInt(fn, value) {

function formatPrimitive(fn, value, ctx) {
if (typeof value === 'string') {
let trailer = '';
if (value.length > ctx.maxStringLength) {
const remaining = value.length - ctx.maxStringLength;
value = value.slice(0, ctx.maxStringLength);
trailer = `... ${remaining} more character${remaining > 1 ? 's' : ''}`;
}
if (ctx.compact !== true &&
// TODO(BridgeAR): Add unicode support. Use the readline getStringWidth
// function.
Expand All @@ -1304,9 +1314,9 @@ function formatPrimitive(fn, value, ctx) {
return value
.split(/(?<=\n)/)
.map((line) => fn(strEscape(line), 'string'))
.join(` +\n${' '.repeat(ctx.indentationLvl + 2)}`);
.join(` +\n${' '.repeat(ctx.indentationLvl + 2)}`) + trailer;
}
return fn(strEscape(value), 'string');
return fn(strEscape(value), 'string') + trailer;
}
if (typeof value === 'number')
return formatNumber(fn, value);
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2780,3 +2780,11 @@ assert.strictEqual(
v8.setFlagsFromString('--no-allow-natives-syntax');
assert.strictEqual(inspect(undetectable), '{}');
}

{
const x = 'a'.repeat(1e6);
assert.strictEqual(
util.inspect(x, { maxStringLength: 4 }),
"'aaaa'... 999996 more characters"
);
}

0 comments on commit 0087eb1

Please sign in to comment.