Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: add maxStringLength option to inspect function #32392

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/api/util.md
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
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 @@ -215,6 +216,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 @@ -245,6 +247,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 @@ -282,6 +285,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 @@ -1301,6 +1305,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 @@ -1309,9 +1319,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
Expand Up @@ -2779,3 +2779,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"
);
}