diff options
-rw-r--r-- | doc/api/util.md | 6 | ||||
-rw-r--r-- | lib/internal/util/inspect.js | 14 | ||||
-rw-r--r-- | test/parallel/test-util-inspect.js | 8 |
3 files changed, 26 insertions, 2 deletions
diff --git a/doc/api/util.md b/doc/api/util.md index e1dadbf65f2..b702d5adba3 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -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 @@ -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`). diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index e25b599e8cc..f59e7db3a97 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -146,6 +146,7 @@ const inspectDefaultOptions = ObjectSeal({ customInspect: true, showProxy: false, maxArrayLength: 100, + maxStringLength: Infinity, breakLength: 80, compact: 3, sorted: false, @@ -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, @@ -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, @@ -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; @@ -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. @@ -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); diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 622171ea522..fab2a12a00d 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -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" + ); +} |