Skip to content

Commit

Permalink
[New] add maxStringLength option
Browse files Browse the repository at this point in the history
Added to `util.inspect` in nodejs/node#32392, in node v12.17, v13.13, v14+
  • Loading branch information
ljharb committed May 26, 2020
1 parent 7c204f2 commit b3995cb
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
7 changes: 4 additions & 3 deletions .eslintrc
Expand Up @@ -3,14 +3,15 @@
"extends": "@ljharb",
"rules": {
"complexity": 0,
"func-style": [2, 'declaration'],
"func-style": [2, "declaration"],
"indent": [2, 4],
"max-lines-per-function": [2, 130],
"max-lines-per-function": 1,
"max-params": [2, 4],
"max-statements": [2, 90],
"max-statements-per-line": [2, { "max": 2 }],
"no-magic-numbers": 0,
"no-param-reassign": 1,
"no-param-reassign": 1,
"operator-linebreak": [2, "before"],
"strict": 0, // TODO
},
"globals": {
Expand Down
13 changes: 13 additions & 0 deletions index.js
Expand Up @@ -24,6 +24,14 @@ module.exports = function inspect_(obj, options, depth, seen) {
if (has(opts, 'quoteStyle') && (opts.quoteStyle !== 'single' && opts.quoteStyle !== 'double')) {
throw new TypeError('option "quoteStyle" must be "single" or "double"');
}
if (
has(opts, 'maxStringLength') && (typeof opts.maxStringLength === 'number'
? opts.maxStringLength < 0 && opts.maxStringLength !== Infinity
: opts.maxStringLength !== null
)
) {
throw new TypeError('option "maxStringLength", if provided, must be a positive integer, Infinity, or `null`');
}

if (typeof obj === 'undefined') {
return 'undefined';
Expand Down Expand Up @@ -259,6 +267,11 @@ function isElement(x) {
}

function inspectString(str, opts) {
if (str.length > opts.maxStringLength) {
var remaining = str.length - opts.maxStringLength;
var trailer = '... ' + remaining + ' more character' + (remaining > 1 ? 's' : '');
return inspectString(str.slice(0, opts.maxStringLength), opts) + trailer;
}
// eslint-disable-next-line no-control-regex
var s = str.replace(/(['\\])/g, '\\$1').replace(/[\x00-\x1f]/g, lowbyte);
return wrapQuotes(s, 'single', opts);
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -10,6 +10,7 @@
"eslint": "^7.1.0",
"nyc": "^10.3.2",
"safe-publish-latest": "^1.1.4",
"string.prototype.repeat": "^1.0.0",
"tape": "^5.0.1"
},
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions readme.markdown
Expand Up @@ -45,6 +45,7 @@ Return a string `s` with the string representation of `obj` up to a depth of `op

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

# install

Expand Down
11 changes: 11 additions & 0 deletions test/inspect.js
@@ -1,6 +1,7 @@
var test = require('tape');
var hasSymbols = require('has-symbols')();
var utilInspect = require('../util.inspect');
var repeat = require('string.prototype.repeat');

var inspect = require('..');

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

t.equal(inspect([obj, []]), '[ ' + (utilInspect.custom ? 'symbol' : 'string') + ', [] ]');
});

test('maxStringLength', function (t) {
t.equal(
inspect([repeat('a', 1e8)], { maxStringLength: 10 }),
'[ \'aaaaaaaaaa\'... 99999990 more characters ]',
'maxStringLength option limits output'
);

t.end();
});

0 comments on commit b3995cb

Please sign in to comment.