Skip to content

Commit 900d3d8

Browse files
mklsljharb
authored andcommitted
[New] add indent option
Fixes #27.
1 parent 28b9179 commit 900d3d8

File tree

5 files changed

+346
-13
lines changed

5 files changed

+346
-13
lines changed

.eslintrc

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
"root": true,
33
"extends": "@ljharb",
44
"rules": {
5-
"complexity": 0,
6-
"func-style": [2, "declaration"],
5+
"complexity": 0,
6+
"func-style": [2, "declaration"],
77
"indent": [2, 4],
8+
"max-lines": 1,
89
"max-lines-per-function": 1,
910
"max-params": [2, 4],
10-
"max-statements": [2, 90],
11+
"max-statements": [2, 100],
1112
"max-statements-per-line": [2, { "max": 2 }],
1213
"no-magic-numbers": 0,
1314
"no-param-reassign": 1,
@@ -24,6 +25,7 @@
2425
"files": ["test/**", "test-*", "example/**"],
2526
"rules": {
2627
"array-bracket-newline": 0,
28+
"id-length": 0,
2729
"max-params": 0,
2830
"max-statements": 0,
2931
"max-statements-per-line": 0,

index.js

+68-10
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ module.exports = function inspect_(obj, options, depth, seen) {
3838
throw new TypeError('option "customInspect", if provided, must be `true` or `false`');
3939
}
4040

41+
if (
42+
has(opts, 'indent')
43+
&& opts.indent !== null
44+
&& opts.indent !== '\t'
45+
&& !(parseInt(opts.indent, 10) === opts.indent && opts.indent > 0)
46+
) {
47+
throw new TypeError('options "indent" must be "\\t", an integer > 0, or `null`');
48+
}
49+
4150
if (typeof obj === 'undefined') {
4251
return 'undefined';
4352
}
@@ -67,17 +76,28 @@ module.exports = function inspect_(obj, options, depth, seen) {
6776
return isArray(obj) ? '[Array]' : '[Object]';
6877
}
6978

79+
var indent = getIndent(opts, depth);
80+
7081
if (typeof seen === 'undefined') {
7182
seen = [];
7283
} else if (indexOf(seen, obj) >= 0) {
7384
return '[Circular]';
7485
}
7586

76-
function inspect(value, from) {
87+
function inspect(value, from, noIndent) {
7788
if (from) {
7889
seen = seen.slice();
7990
seen.push(from);
8091
}
92+
if (noIndent) {
93+
var newOpts = {
94+
depth: opts.depth
95+
};
96+
if (has(opts, 'quoteStyle')) {
97+
newOpts.quoteStyle = opts.quoteStyle;
98+
}
99+
return inspect_(value, newOpts, depth + 1, seen);
100+
}
81101
return inspect_(value, opts, depth + 1, seen);
82102
}
83103

@@ -102,7 +122,11 @@ module.exports = function inspect_(obj, options, depth, seen) {
102122
}
103123
if (isArray(obj)) {
104124
if (obj.length === 0) { return '[]'; }
105-
return '[ ' + arrObjKeys(obj, inspect).join(', ') + ' ]';
125+
var xs = arrObjKeys(obj, inspect);
126+
if (indent && !singleLineValues(xs)) {
127+
return '[' + indentedJoin(xs, indent) + ']';
128+
}
129+
return '[ ' + xs.join(', ') + ' ]';
106130
}
107131
if (isError(obj)) {
108132
var parts = arrObjKeys(obj, inspect);
@@ -119,16 +143,16 @@ module.exports = function inspect_(obj, options, depth, seen) {
119143
if (isMap(obj)) {
120144
var mapParts = [];
121145
mapForEach.call(obj, function (value, key) {
122-
mapParts.push(inspect(key, obj) + ' => ' + inspect(value, obj));
146+
mapParts.push(inspect(key, obj, true) + ' => ' + inspect(value, obj));
123147
});
124-
return collectionOf('Map', mapSize.call(obj), mapParts);
148+
return collectionOf('Map', mapSize.call(obj), mapParts, indent);
125149
}
126150
if (isSet(obj)) {
127151
var setParts = [];
128152
setForEach.call(obj, function (value) {
129153
setParts.push(inspect(value, obj));
130154
});
131-
return collectionOf('Set', setSize.call(obj), setParts);
155+
return collectionOf('Set', setSize.call(obj), setParts, indent);
132156
}
133157
if (isWeakMap(obj)) {
134158
return weakCollectionOf('WeakMap');
@@ -149,9 +173,12 @@ module.exports = function inspect_(obj, options, depth, seen) {
149173
return markBoxed(inspect(String(obj)));
150174
}
151175
if (!isDate(obj) && !isRegExp(obj)) {
152-
var xs = arrObjKeys(obj, inspect);
153-
if (xs.length === 0) { return '{}'; }
154-
return '{ ' + xs.join(', ') + ' }';
176+
var ys = arrObjKeys(obj, inspect);
177+
if (ys.length === 0) { return '{}'; }
178+
if (indent) {
179+
return '{' + indentedJoin(ys, indent) + '}';
180+
}
181+
return '{ ' + ys.join(', ') + ' }';
155182
}
156183
return String(obj);
157184
};
@@ -299,8 +326,39 @@ function weakCollectionOf(type) {
299326
return type + ' { ? }';
300327
}
301328

302-
function collectionOf(type, size, entries) {
303-
return type + ' (' + size + ') {' + entries.join(', ') + '}';
329+
function collectionOf(type, size, entries, indent) {
330+
var joinedEntries = indent ? indentedJoin(entries, indent) : entries.join(', ');
331+
return type + ' (' + size + ') {' + joinedEntries + '}';
332+
}
333+
334+
function singleLineValues(xs) {
335+
for (var i = 0; i < xs.length; i++) {
336+
if (indexOf(xs[i], '\n') >= 0) {
337+
return false;
338+
}
339+
}
340+
return true;
341+
}
342+
343+
function getIndent(opts, depth) {
344+
var baseIndent;
345+
if (opts.indent === '\t') {
346+
baseIndent = '\t';
347+
} else if (typeof opts.indent === 'number' && opts.indent > 0) {
348+
baseIndent = Array(opts.indent + 1).join(' ');
349+
} else {
350+
return null;
351+
}
352+
return {
353+
base: baseIndent,
354+
prev: Array(depth + 1).join(baseIndent)
355+
};
356+
}
357+
358+
function indentedJoin(xs, indent) {
359+
if (xs.length === 0) { return ''; }
360+
var lineJoiner = '\n' + indent.prev + indent.base;
361+
return lineJoiner + xs.join(',' + lineJoiner) + '\n' + indent.prev;
304362
}
305363

306364
function arrObjKeys(obj, inspect) {

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"aud": "^1.1.2",
99
"core-js": "^2.6.11",
1010
"eslint": "^7.1.0",
11+
"for-each": "^0.3.3",
1112
"nyc": "^10.3.2",
1213
"safe-publish-latest": "^1.1.4",
1314
"string.prototype.repeat": "^1.0.0",

readme.markdown

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Additional options:
4747
- `quoteStyle`: must be "single" or "double", if present. Default `'single'` for strings, `'double'` for HTML elements.
4848
- `maxStringLength`: must be `0`, a positive integer, `Infinity`, or `null`, if present. Default `Infinity`.
4949
- `customInspect`: When `true`, a custom inspect method function will be invoked. Default `true`.
50+
- `indent`: must be "\t", `null`, or a positive integer. Default `null`.
5051

5152
# install
5253

0 commit comments

Comments
 (0)