Skip to content

Commit b3995cb

Browse files
committed
[New] add maxStringLength option
Added to `util.inspect` in nodejs/node#32392, in node v12.17, v13.13, v14+
1 parent 7c204f2 commit b3995cb

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

.eslintrc

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
"extends": "@ljharb",
44
"rules": {
55
"complexity": 0,
6-
"func-style": [2, 'declaration'],
6+
"func-style": [2, "declaration"],
77
"indent": [2, 4],
8-
"max-lines-per-function": [2, 130],
8+
"max-lines-per-function": 1,
99
"max-params": [2, 4],
1010
"max-statements": [2, 90],
1111
"max-statements-per-line": [2, { "max": 2 }],
1212
"no-magic-numbers": 0,
13-
"no-param-reassign": 1,
13+
"no-param-reassign": 1,
14+
"operator-linebreak": [2, "before"],
1415
"strict": 0, // TODO
1516
},
1617
"globals": {

index.js

+13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ module.exports = function inspect_(obj, options, depth, seen) {
2424
if (has(opts, 'quoteStyle') && (opts.quoteStyle !== 'single' && opts.quoteStyle !== 'double')) {
2525
throw new TypeError('option "quoteStyle" must be "single" or "double"');
2626
}
27+
if (
28+
has(opts, 'maxStringLength') && (typeof opts.maxStringLength === 'number'
29+
? opts.maxStringLength < 0 && opts.maxStringLength !== Infinity
30+
: opts.maxStringLength !== null
31+
)
32+
) {
33+
throw new TypeError('option "maxStringLength", if provided, must be a positive integer, Infinity, or `null`');
34+
}
2735

2836
if (typeof obj === 'undefined') {
2937
return 'undefined';
@@ -259,6 +267,11 @@ function isElement(x) {
259267
}
260268

261269
function inspectString(str, opts) {
270+
if (str.length > opts.maxStringLength) {
271+
var remaining = str.length - opts.maxStringLength;
272+
var trailer = '... ' + remaining + ' more character' + (remaining > 1 ? 's' : '');
273+
return inspectString(str.slice(0, opts.maxStringLength), opts) + trailer;
274+
}
262275
// eslint-disable-next-line no-control-regex
263276
var s = str.replace(/(['\\])/g, '\\$1').replace(/[\x00-\x1f]/g, lowbyte);
264277
return wrapQuotes(s, 'single', opts);

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"eslint": "^7.1.0",
1111
"nyc": "^10.3.2",
1212
"safe-publish-latest": "^1.1.4",
13+
"string.prototype.repeat": "^1.0.0",
1314
"tape": "^5.0.1"
1415
},
1516
"scripts": {

readme.markdown

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Return a string `s` with the string representation of `obj` up to a depth of `op
4545

4646
Additional options:
4747
- `quoteStyle`: must be "single" or "double", if present
48+
- `maxStringLength`: must be `0`, a positive integer, `Infinity`, or `null`, if present
4849

4950
# install
5051

test/inspect.js

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var test = require('tape');
22
var hasSymbols = require('has-symbols')();
33
var utilInspect = require('../util.inspect');
4+
var repeat = require('string.prototype.repeat');
45

56
var inspect = require('..');
67

@@ -18,3 +19,13 @@ test('inspect custom symbol', { skip: !hasSymbols || !utilInspect }, function (t
1819

1920
t.equal(inspect([obj, []]), '[ ' + (utilInspect.custom ? 'symbol' : 'string') + ', [] ]');
2021
});
22+
23+
test('maxStringLength', function (t) {
24+
t.equal(
25+
inspect([repeat('a', 1e8)], { maxStringLength: 10 }),
26+
'[ \'aaaaaaaaaa\'... 99999990 more characters ]',
27+
'maxStringLength option limits output'
28+
);
29+
30+
t.end();
31+
});

0 commit comments

Comments
 (0)