Skip to content

Commit 0d19937

Browse files
committed
[New] add support for arrays with additional object keys
(node’s util.format does this as well)
1 parent 8fdeb49 commit 0d19937

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

index.js

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,10 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
7171
}
7272
if (isArray(obj)) {
7373
if (obj.length === 0) return '[]';
74-
var xs = Array(obj.length);
75-
for (var i = 0; i < obj.length; i++) {
76-
xs[i] = has(obj, i) ? inspect(obj[i], obj) : '';
77-
}
78-
return '[ ' + xs.join(', ') + ' ]';
74+
return '[ ' + arrObjKeys(obj, inspect).join(', ') + ' ]';
7975
}
8076
if (isError(obj)) {
81-
var parts = [];
82-
for (var key in obj) {
83-
if (!has(obj, key)) continue;
84-
85-
if (/[^\w$]/.test(key)) {
86-
parts.push(inspect(key) + ': ' + inspect(obj[key]));
87-
}
88-
else {
89-
parts.push(key + ': ' + inspect(obj[key]));
90-
}
91-
}
77+
var parts = arrObjKeys(obj, inspect);
9278
if (parts.length === 0) return '[' + String(obj) + ']';
9379
return '{ [' + String(obj) + '] ' + parts.join(', ') + ' }';
9480
}
@@ -119,30 +105,13 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
119105
return markBoxed(inspect(String(obj)));
120106
}
121107
if (!isDate(obj) && !isRegExp(obj)) {
122-
var xs = [];
123-
var keys = objectKeys(obj);
124-
keys.sort();
125-
for (var i = 0; i < keys.length; i++) {
126-
var key = keys[i];
127-
if (/[^\w$]/.test(key)) {
128-
xs.push(inspect(key) + ': ' + inspect(obj[key], obj));
129-
}
130-
else xs.push(key + ': ' + inspect(obj[key], obj));
131-
}
108+
var xs = arrObjKeys(obj, inspect);
132109
if (xs.length === 0) return '{}';
133110
return '{ ' + xs.join(', ') + ' }';
134111
}
135112
return String(obj);
136113
};
137114

138-
function objectKeys(obj) {
139-
var keys = [];
140-
for (var key in obj) {
141-
if (has(obj, key)) keys.push(key);
142-
}
143-
return keys;
144-
}
145-
146115
function quote (s) {
147116
return String(s).replace(/"/g, '&quot;');
148117
}
@@ -227,6 +196,27 @@ function markBoxed (str) {
227196
return 'Object(' + str + ')';
228197
}
229198

230-
function collectionOf(type, size, entries) {
199+
function collectionOf (type, size, entries) {
231200
return type + ' (' + size + ') {' + entries.join(', ') + '}';
232201
}
202+
203+
function arrObjKeys (obj, inspect) {
204+
var isArr = isArray(obj);
205+
var xs = [];
206+
if (isArr) {
207+
xs.length = obj.length;
208+
for (var i = 0; i < obj.length; i++) {
209+
xs[i] = has(obj, i) ? inspect(obj[i], obj) : '';
210+
}
211+
}
212+
for (var key in obj) {
213+
if (!has(obj, key)) continue;
214+
if (isArr && String(Number(key)) === key && key < obj.length) continue;
215+
if (/[^\w$]/.test(key)) {
216+
xs.push(inspect(key, obj) + ': ' + inspect(obj[key], obj));
217+
} else {
218+
xs.push(key + ': ' + inspect(obj[key], obj));
219+
}
220+
}
221+
return xs;
222+
}

test/values.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ test('values', function (t) {
77
t.equal(inspect(obj), '[ {}, [], { \'a-b\': 5 } ]');
88
});
99

10+
test('arrays with properties', function (t) {
11+
t.plan(1);
12+
var arr = [3];
13+
arr.foo = 'bar';
14+
var obj = [1, 2, arr];
15+
obj.baz = 'quux';
16+
obj.index = -1;
17+
t.equal(inspect(obj), '[ 1, 2, [ 3, foo: \'bar\' ], baz: \'quux\', index: -1 ]');
18+
});
19+
1020
test('has', function (t) {
1121
t.plan(1);
1222
var has = Object.prototype.hasOwnProperty;

0 commit comments

Comments
 (0)