Skip to content

Commit

Permalink
[New] add support for arrays with additional object keys
Browse files Browse the repository at this point in the history
(node’s util.format does this as well)
  • Loading branch information
ljharb committed Jul 31, 2017
1 parent 8fdeb49 commit 0d19937
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
60 changes: 25 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,10 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
}
if (isArray(obj)) {
if (obj.length === 0) return '[]';
var xs = Array(obj.length);
for (var i = 0; i < obj.length; i++) {
xs[i] = has(obj, i) ? inspect(obj[i], obj) : '';
}
return '[ ' + xs.join(', ') + ' ]';
return '[ ' + arrObjKeys(obj, inspect).join(', ') + ' ]';
}
if (isError(obj)) {
var parts = [];
for (var key in obj) {
if (!has(obj, key)) continue;

if (/[^\w$]/.test(key)) {
parts.push(inspect(key) + ': ' + inspect(obj[key]));
}
else {
parts.push(key + ': ' + inspect(obj[key]));
}
}
var parts = arrObjKeys(obj, inspect);
if (parts.length === 0) return '[' + String(obj) + ']';
return '{ [' + String(obj) + '] ' + parts.join(', ') + ' }';
}
Expand Down Expand Up @@ -119,30 +105,13 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
return markBoxed(inspect(String(obj)));
}
if (!isDate(obj) && !isRegExp(obj)) {
var xs = [];
var keys = objectKeys(obj);
keys.sort();
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (/[^\w$]/.test(key)) {
xs.push(inspect(key) + ': ' + inspect(obj[key], obj));
}
else xs.push(key + ': ' + inspect(obj[key], obj));
}
var xs = arrObjKeys(obj, inspect);
if (xs.length === 0) return '{}';
return '{ ' + xs.join(', ') + ' }';
}
return String(obj);
};

function objectKeys(obj) {
var keys = [];
for (var key in obj) {
if (has(obj, key)) keys.push(key);
}
return keys;
}

function quote (s) {
return String(s).replace(/"/g, '&quot;');
}
Expand Down Expand Up @@ -227,6 +196,27 @@ function markBoxed (str) {
return 'Object(' + str + ')';
}

function collectionOf(type, size, entries) {
function collectionOf (type, size, entries) {
return type + ' (' + size + ') {' + entries.join(', ') + '}';
}

function arrObjKeys (obj, inspect) {
var isArr = isArray(obj);
var xs = [];
if (isArr) {
xs.length = obj.length;
for (var i = 0; i < obj.length; i++) {
xs[i] = has(obj, i) ? inspect(obj[i], obj) : '';
}
}
for (var key in obj) {
if (!has(obj, key)) continue;
if (isArr && String(Number(key)) === key && key < obj.length) continue;
if (/[^\w$]/.test(key)) {
xs.push(inspect(key, obj) + ': ' + inspect(obj[key], obj));
} else {
xs.push(key + ': ' + inspect(obj[key], obj));
}
}
return xs;
}
10 changes: 10 additions & 0 deletions test/values.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ test('values', function (t) {
t.equal(inspect(obj), '[ {}, [], { \'a-b\': 5 } ]');
});

test('arrays with properties', function (t) {
t.plan(1);
var arr = [3];
arr.foo = 'bar';
var obj = [1, 2, arr];
obj.baz = 'quux';
obj.index = -1;
t.equal(inspect(obj), '[ 1, 2, [ 3, foo: \'bar\' ], baz: \'quux\', index: -1 ]');
});

test('has', function (t) {
t.plan(1);
var has = Object.prototype.hasOwnProperty;
Expand Down

0 comments on commit 0d19937

Please sign in to comment.