Skip to content

Commit de15214

Browse files
Benjamin Thomasry
authored andcommitted
Make the output of sys.inspect a lot more compact.
See: http://groups.google.com/group/nodejs/browse_thread/thread/ac060521e27fef65 By default now, sys.inspect doesn't recurse more than two times. Pass in null as the third parameter to make it recurse indefinitely.
1 parent a2714be commit de15214

3 files changed

Lines changed: 75 additions & 36 deletions

File tree

‎doc/api.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ shown too.
222222
+
223223
If +depth+ is provided, it tells +inspect+ how many times to recurse while
224224
formatting the object. This is useful for inspecting large complicated objects.
225-
The defualt is to recurse indefinitely.
225+
The default is to only recurse twice. To make it recurse indefinitely, pass
226+
in +null+ for +depth+.
226227

227228

228229
+exec(command, callback)+::

‎lib/sys.js‎

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ exports.inspect = function (obj, showHidden, depth) {
3535
// Primitive types cannot have properties
3636
switch (typeof value) {
3737
case 'undefined': return 'undefined';
38-
case 'string': return JSON.stringify(value);
38+
case 'string': return JSON.stringify(value).replace(/'/g, "\\'").replace(/\\"/g, '"').replace(/(^"|"$)/g, "'");
3939
case 'number': return '' + value;
4040
case 'boolean': return '' + value;
4141
}
@@ -95,7 +95,15 @@ exports.inspect = function (obj, showHidden, depth) {
9595
return braces[0] + base + braces[1];
9696
}
9797

98-
return braces[0] + base + "\n" + (keys.map(function (key) {
98+
if( recurseTimes < 0 ) {
99+
if (value instanceof RegExp) {
100+
return '' + value;
101+
} else {
102+
return "[object Object]";
103+
}
104+
}
105+
106+
output = keys.map(function (key) {
99107
var name, str;
100108
if (value.__lookupGetter__) {
101109
if (value.__lookupGetter__(key)) {
@@ -115,14 +123,16 @@ exports.inspect = function (obj, showHidden, depth) {
115123
}
116124
if (!str) {
117125
if (seen.indexOf(value[key]) < 0) {
118-
if (typeof recurseTimes === 'undefined' || recurseTimes === null) {
126+
if ( recurseTimes === null) {
119127
str = format(value[key]);
120128
}
121-
else if (recurseTimes > 0) {
129+
else {
122130
str = format(value[key], recurseTimes - 1);
123131
}
124-
else {
125-
str = value[key];
132+
if( str.indexOf('\n') > -1 ) {
133+
str = '\n' + str.split('\n').map(function(line) {
134+
return ' ' + line;
135+
}).join('\n');
126136
}
127137
} else {
128138
str = '[Circular]';
@@ -133,14 +143,32 @@ exports.inspect = function (obj, showHidden, depth) {
133143
return str;
134144
}
135145
name = JSON.stringify('' + key);
146+
if( name.match(/^"([a-zA-Z_0-9]+)"$/) ) {
147+
name = name.substr(1, name.length-2);
148+
}
149+
else {
150+
name = name.replace(/'/g, "\\'").replace(/\\"/g, '"').replace(/(^"|"$)/g, "'");
151+
}
136152
}
137153

138154
return name + ": " + str;
139-
}).join(",\n")).split("\n").map(function (line) {
140-
return ' ' + line;
141-
}).join('\n') + "\n" + braces[1];
155+
});
156+
157+
158+
var length = output.reduce(function(prev, cur) {
159+
return prev + cur.length + 1;
160+
},0);
161+
162+
if( length > 50 ) {
163+
output = braces[0] + (base === '' ? '' : base + '\n,') + ' ' + output.join('\n, ') + '\n' +braces[1];
164+
}
165+
else {
166+
output = braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
167+
}
168+
169+
return output;
142170
}
143-
return format(obj, depth);
171+
return format(obj, (typeof depth === 'undefined' ? 2 : depth));
144172
};
145173

146174
exports.p = function () {

‎test/simple/test-sys.js‎

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,90 +4,100 @@ process.mixin(require("sys"));
44
assert.equal("0", inspect(0));
55
assert.equal("1", inspect(1));
66
assert.equal("false", inspect(false));
7-
assert.equal('""', inspect(""));
8-
assert.equal('"hello"', inspect("hello"));
7+
assert.equal("''", inspect(""));
8+
assert.equal("'hello'", inspect("hello"));
99
assert.equal("[Function]", inspect(function() {}));
1010
assert.equal('undefined', inspect(undefined));
1111
assert.equal('null', inspect(null));
1212
assert.equal('/foo(bar\\n)?/gi', inspect(/foo(bar\n)?/gi));
1313
assert.equal('Sun, 14 Feb 2010 11:48:40 GMT',
1414
inspect(new Date("Sun, 14 Feb 2010 11:48:40 GMT")));
1515

16-
assert.equal("\"\\n\\u0001\"", inspect("\n\u0001"));
16+
assert.equal("'\\n\\u0001'", inspect("\n\u0001"));
1717

1818
assert.equal('[]', inspect([]));
1919
assert.equal('[]', inspect(Object.create([])));
20-
assert.equal('[\n 1,\n 2\n]', inspect([1, 2]));
21-
assert.equal('[\n 1,\n [\n 2,\n 3\n ]\n]', inspect([1, [2, 3]]));
20+
assert.equal('[ 1, 2 ]', inspect([1, 2]));
21+
assert.equal('[ 1, [ 2, 3 ] ]', inspect([1, [2, 3]]));
2222

2323
assert.equal('{}', inspect({}));
24-
assert.equal('{\n "a": 1\n}', inspect({a: 1}));
25-
assert.equal('{\n "a": [Function]\n}', inspect({a: function() {}}));
26-
assert.equal('{\n "a": 1,\n "b": 2\n}', inspect({a: 1, b: 2}));
27-
assert.equal('{\n "a": {}\n}', inspect({'a': {}}));
28-
assert.equal('{\n "a": {\n "b": 2\n }\n}', inspect({'a': {'b': 2}}));
29-
assert.equal('[\n 1,\n 2,\n 3,\n [length]: 3\n]', inspect([1,2,3], true));
30-
assert.equal('{\n \"a\": [object Object]\n}', inspect({'a': {'b': { 'c': 2}}},false,0));
31-
assert.equal('{\n \"a\": {\n \"b\": [object Object]\n }\n}', inspect({'a': {'b': { 'c': 2}}},false,1));
32-
assert.equal("{\n \"visible\": 1\n}",
24+
assert.equal('{ a: 1 }', inspect({a: 1}));
25+
assert.equal('{ a: [Function] }', inspect({a: function() {}}));
26+
assert.equal('{ a: 1, b: 2 }', inspect({a: 1, b: 2}));
27+
assert.equal('{ a: {} }', inspect({'a': {}}));
28+
assert.equal('{ a: { b: 2 } }', inspect({'a': {'b': 2}}));
29+
assert.equal('{ a: { b: { c: [object Object] } } }', inspect({'a': {'b': { 'c': { 'd': 2 }}}}));
30+
assert.equal('{ a: { b: { c: { d: 2 } } } }', inspect({'a': {'b': { 'c': { 'd': 2 }}}}, false, null));
31+
assert.equal('[ 1, 2, 3, [length]: 3 ]', inspect([1,2,3], true));
32+
assert.equal('{ a: [object Object] }', inspect({'a': {'b': { 'c': 2}}},false,0));
33+
assert.equal('{ a: { b: [object Object] } }', inspect({'a': {'b': { 'c': 2}}},false,1));
34+
assert.equal("{ visible: 1 }",
3335
inspect(Object.create({}, {visible:{value:1,enumerable:true},hidden:{value:2}}))
3436
);
35-
assert.equal("{\n [hidden]: 2,\n \"visible\": 1\n}",
37+
assert.equal("{ [hidden]: 2, visible: 1 }",
3638
inspect(Object.create({}, {visible:{value:1,enumerable:true},hidden:{value:2}}), true)
3739
);
3840

3941
// Objects without prototype
4042
assert.equal(
41-
"{\n [hidden]: \"secret\",\n \"name\": \"Tim\"\n}",
43+
"{ [hidden]: 'secret', name: 'Tim' }",
4244
inspect(Object.create(null, {name: {value: "Tim", enumerable: true}, hidden: {value: "secret"}}), true)
4345
);
4446
assert.equal(
45-
"{\n \"name\": \"Tim\"\n}",
47+
"{ name: 'Tim' }",
4648
inspect(Object.create(null, {name: {value: "Tim", enumerable: true}, hidden: {value: "secret"}}))
4749
);
4850

4951

5052
// Dynamic properties
5153
assert.equal(
52-
"{\n \"readonly\": [Getter],\n \"readwrite\": [Getter/Setter],\n \"writeonly\": [Setter]\n}",
53-
inspect({get readonly(){},get readwrite(){},set readwrite(val){},set writeonly(val){}})
54+
"{ readonly: [Getter] }",
55+
inspect({get readonly(){}})
56+
);
57+
assert.equal(
58+
"{ readwrite: [Getter/Setter] }",
59+
inspect({get readwrite(){},set readwrite(val){}})
60+
);
61+
assert.equal(
62+
"{ writeonly: [Setter] }",
63+
inspect({set writeonly(val){}})
5464
);
5565

5666
var value = {};
5767
value['a'] = value;
58-
assert.equal('{\n "a": [Circular]\n}', inspect(value));
68+
assert.equal('{ a: [Circular] }', inspect(value));
5969
value = Object.create([]);
6070
value.push(1);
61-
assert.equal("[\n 1,\n \"length\": 1\n]", inspect(value));
71+
assert.equal("[ 1, length: 1 ]", inspect(value));
6272

6373
// Array with dynamic properties
6474
value = [1,2,3];
6575
value.__defineGetter__('growingLength', function () { this.push(true); return this.length; });
6676
assert.equal(
67-
"[\n 1,\n 2,\n 3,\n \"growingLength\": [Getter]\n]",
77+
"[ 1, 2, 3, growingLength: [Getter] ]",
6878
inspect(value)
6979
);
7080

7181
// Function with properties
7282
value = function () {};
7383
value.aprop = 42;
7484
assert.equal(
75-
"{ [Function]\n \"aprop\": 42\n}",
85+
"{ [Function] aprop: 42 }",
7686
inspect(value)
7787
);
7888

7989
// Regular expressions with properties
8090
value = /123/ig;
8191
value.aprop = 42;
8292
assert.equal(
83-
"{ /123/gi\n \"aprop\": 42\n}",
93+
"{ /123/gi aprop: 42 }",
8494
inspect(value)
8595
);
8696

8797
// Dates with properties
8898
value = new Date("Sun, 14 Feb 2010 11:48:40 GMT");
8999
value.aprop = 42;
90100
assert.equal(
91-
"{ Sun, 14 Feb 2010 11:48:40 GMT\n \"aprop\": 42\n}",
101+
"{ Sun, 14 Feb 2010 11:48:40 GMT aprop: 42 }",
92102
inspect(value)
93103
);

0 commit comments

Comments
 (0)