Skip to content

Commit

Permalink
[New] add indent option
Browse files Browse the repository at this point in the history
Fixes #27.
  • Loading branch information
mkls authored and ljharb committed Feb 20, 2017
1 parent 28b9179 commit 900d3d8
Show file tree
Hide file tree
Showing 5 changed files with 346 additions and 13 deletions.
8 changes: 5 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
"root": true,
"extends": "@ljharb",
"rules": {
"complexity": 0,
"func-style": [2, "declaration"],
"complexity": 0,
"func-style": [2, "declaration"],
"indent": [2, 4],
"max-lines": 1,
"max-lines-per-function": 1,
"max-params": [2, 4],
"max-statements": [2, 90],
"max-statements": [2, 100],
"max-statements-per-line": [2, { "max": 2 }],
"no-magic-numbers": 0,
"no-param-reassign": 1,
Expand All @@ -24,6 +25,7 @@
"files": ["test/**", "test-*", "example/**"],
"rules": {
"array-bracket-newline": 0,
"id-length": 0,
"max-params": 0,
"max-statements": 0,
"max-statements-per-line": 0,
Expand Down
78 changes: 68 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ module.exports = function inspect_(obj, options, depth, seen) {
throw new TypeError('option "customInspect", if provided, must be `true` or `false`');
}

if (
has(opts, 'indent')
&& opts.indent !== null
&& opts.indent !== '\t'
&& !(parseInt(opts.indent, 10) === opts.indent && opts.indent > 0)
) {
throw new TypeError('options "indent" must be "\\t", an integer > 0, or `null`');
}

if (typeof obj === 'undefined') {
return 'undefined';
}
Expand Down Expand Up @@ -67,17 +76,28 @@ module.exports = function inspect_(obj, options, depth, seen) {
return isArray(obj) ? '[Array]' : '[Object]';
}

var indent = getIndent(opts, depth);

if (typeof seen === 'undefined') {
seen = [];
} else if (indexOf(seen, obj) >= 0) {
return '[Circular]';
}

function inspect(value, from) {
function inspect(value, from, noIndent) {
if (from) {
seen = seen.slice();
seen.push(from);
}
if (noIndent) {
var newOpts = {
depth: opts.depth
};
if (has(opts, 'quoteStyle')) {
newOpts.quoteStyle = opts.quoteStyle;
}
return inspect_(value, newOpts, depth + 1, seen);
}
return inspect_(value, opts, depth + 1, seen);
}

Expand All @@ -102,7 +122,11 @@ module.exports = function inspect_(obj, options, depth, seen) {
}
if (isArray(obj)) {
if (obj.length === 0) { return '[]'; }
return '[ ' + arrObjKeys(obj, inspect).join(', ') + ' ]';
var xs = arrObjKeys(obj, inspect);
if (indent && !singleLineValues(xs)) {
return '[' + indentedJoin(xs, indent) + ']';
}
return '[ ' + xs.join(', ') + ' ]';
}
if (isError(obj)) {
var parts = arrObjKeys(obj, inspect);
Expand All @@ -119,16 +143,16 @@ module.exports = function inspect_(obj, options, depth, seen) {
if (isMap(obj)) {
var mapParts = [];
mapForEach.call(obj, function (value, key) {
mapParts.push(inspect(key, obj) + ' => ' + inspect(value, obj));
mapParts.push(inspect(key, obj, true) + ' => ' + inspect(value, obj));
});
return collectionOf('Map', mapSize.call(obj), mapParts);
return collectionOf('Map', mapSize.call(obj), mapParts, indent);
}
if (isSet(obj)) {
var setParts = [];
setForEach.call(obj, function (value) {
setParts.push(inspect(value, obj));
});
return collectionOf('Set', setSize.call(obj), setParts);
return collectionOf('Set', setSize.call(obj), setParts, indent);
}
if (isWeakMap(obj)) {
return weakCollectionOf('WeakMap');
Expand All @@ -149,9 +173,12 @@ module.exports = function inspect_(obj, options, depth, seen) {
return markBoxed(inspect(String(obj)));
}
if (!isDate(obj) && !isRegExp(obj)) {
var xs = arrObjKeys(obj, inspect);
if (xs.length === 0) { return '{}'; }
return '{ ' + xs.join(', ') + ' }';
var ys = arrObjKeys(obj, inspect);
if (ys.length === 0) { return '{}'; }
if (indent) {
return '{' + indentedJoin(ys, indent) + '}';
}
return '{ ' + ys.join(', ') + ' }';
}
return String(obj);
};
Expand Down Expand Up @@ -299,8 +326,39 @@ function weakCollectionOf(type) {
return type + ' { ? }';
}

function collectionOf(type, size, entries) {
return type + ' (' + size + ') {' + entries.join(', ') + '}';
function collectionOf(type, size, entries, indent) {
var joinedEntries = indent ? indentedJoin(entries, indent) : entries.join(', ');
return type + ' (' + size + ') {' + joinedEntries + '}';
}

function singleLineValues(xs) {
for (var i = 0; i < xs.length; i++) {
if (indexOf(xs[i], '\n') >= 0) {
return false;
}
}
return true;
}

function getIndent(opts, depth) {
var baseIndent;
if (opts.indent === '\t') {
baseIndent = '\t';
} else if (typeof opts.indent === 'number' && opts.indent > 0) {
baseIndent = Array(opts.indent + 1).join(' ');
} else {
return null;
}
return {
base: baseIndent,
prev: Array(depth + 1).join(baseIndent)
};
}

function indentedJoin(xs, indent) {
if (xs.length === 0) { return ''; }
var lineJoiner = '\n' + indent.prev + indent.base;
return lineJoiner + xs.join(',' + lineJoiner) + '\n' + indent.prev;
}

function arrObjKeys(obj, inspect) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"aud": "^1.1.2",
"core-js": "^2.6.11",
"eslint": "^7.1.0",
"for-each": "^0.3.3",
"nyc": "^10.3.2",
"safe-publish-latest": "^1.1.4",
"string.prototype.repeat": "^1.0.0",
Expand Down
1 change: 1 addition & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Additional options:
- `quoteStyle`: must be "single" or "double", if present. Default `'single'` for strings, `'double'` for HTML elements.
- `maxStringLength`: must be `0`, a positive integer, `Infinity`, or `null`, if present. Default `Infinity`.
- `customInspect`: When `true`, a custom inspect method function will be invoked. Default `true`.
- `indent`: must be "\t", `null`, or a positive integer. Default `null`.

# install

Expand Down
Loading

0 comments on commit 900d3d8

Please sign in to comment.