diff --git a/.eslintrc b/.eslintrc index c137b67..6c0a4de 100644 --- a/.eslintrc +++ b/.eslintrc @@ -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": { diff --git a/index.js b/index.js index 1394e53..32b2bd0 100644 --- a/index.js +++ b/index.js @@ -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'; @@ -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); diff --git a/package.json b/package.json index 477f958..a663bb1 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/readme.markdown b/readme.markdown index a476d2f..855a5a6 100644 --- a/readme.markdown +++ b/readme.markdown @@ -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 diff --git a/test/inspect.js b/test/inspect.js index 4f202c2..39694c3 100644 --- a/test/inspect.js +++ b/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('..'); @@ -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(); +});