Browse Source

Stop sys.inspect from adding extra new lines for deep objects that are elements in an array.

A couple other small fixes:

If the keys of an object were all numeric they should be quoted. This
way, you can now hypothetically copy and paste the output into your code
(if the object doesn't contain any circular objects, deeply nested
objects, Dates, RegExps or functions. I think).

If a nested object isn't being recursed into, output "[Object]" as
opposed to "[object Object]".

If an object is longer than the max width but it is one line no matter
what, then don't put the closing brace on a new line.

Fix some formatting issues to try and match Node's style guidelines.
v0.7.4-release
Benjamin Thomas 15 years ago
committed by Ryan Dahl
parent
commit
6034701f57
  1. 27
      lib/sys.js
  2. 6
      test/simple/test-sys.js

27
lib/sys.js

@ -95,11 +95,11 @@ exports.inspect = function (obj, showHidden, depth) {
return braces[0] + base + braces[1]; return braces[0] + base + braces[1];
} }
if( recurseTimes < 0 ) { if (recurseTimes < 0) {
if (value instanceof RegExp) { if (value instanceof RegExp) {
return '' + value; return '' + value;
} else { } else {
return "[object Object]"; return "[Object]";
} }
} }
@ -129,10 +129,17 @@ exports.inspect = function (obj, showHidden, depth) {
else { else {
str = format(value[key], recurseTimes - 1); str = format(value[key], recurseTimes - 1);
} }
if( str.indexOf('\n') > -1 ) { if (str.indexOf('\n') > -1) {
str = '\n' + str.split('\n').map(function(line) { if (value instanceof Array) {
str = str.split('\n').map(function(line) {
return ' ' + line;
}).join('\n').substr(2);
}
else {
str = '\n' + str.split('\n').map(function(line) {
return ' ' + line; return ' ' + line;
}).join('\n'); }).join('\n');
}
} }
} else { } else {
str = '[Circular]'; str = '[Circular]';
@ -143,7 +150,7 @@ exports.inspect = function (obj, showHidden, depth) {
return str; return str;
} }
name = JSON.stringify('' + key); name = JSON.stringify('' + key);
if( name.match(/^"([a-zA-Z_0-9]+)"$/) ) { if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
name = name.substr(1, name.length-2); name = name.substr(1, name.length-2);
} }
else { else {
@ -154,13 +161,17 @@ exports.inspect = function (obj, showHidden, depth) {
return name + ": " + str; return name + ": " + str;
}); });
var numLinesEst = 0;
var length = output.reduce(function(prev, cur) { var length = output.reduce(function(prev, cur) {
numLinesEst++;
if( cur.indexOf('\n') >= 0 ) {
numLinesEst++;
}
return prev + cur.length + 1; return prev + cur.length + 1;
},0); },0);
if( length > 50 ) { if (length > 50) {
output = braces[0] + (base === '' ? '' : base + '\n,') + ' ' + output.join('\n, ') + '\n' +braces[1]; output = braces[0] + (base === '' ? '' : base + '\n,') + ' ' + output.join('\n, ') + (numLinesEst > 1 ? '\n' : ' ') + braces[1];
} }
else { else {
output = braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1]; output = braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];

6
test/simple/test-sys.js

@ -26,11 +26,11 @@ assert.equal('{ a: [Function] }', inspect({a: function() {}}));
assert.equal('{ a: 1, b: 2 }', inspect({a: 1, b: 2})); assert.equal('{ a: 1, b: 2 }', inspect({a: 1, b: 2}));
assert.equal('{ a: {} }', inspect({'a': {}})); assert.equal('{ a: {} }', inspect({'a': {}}));
assert.equal('{ a: { b: 2 } }', inspect({'a': {'b': 2}})); assert.equal('{ a: { b: 2 } }', inspect({'a': {'b': 2}}));
assert.equal('{ a: { b: { c: [object Object] } } }', inspect({'a': {'b': { 'c': { 'd': 2 }}}})); assert.equal('{ a: { b: { c: [Object] } } }', inspect({'a': {'b': { 'c': { 'd': 2 }}}}));
assert.equal('{ a: { b: { c: { d: 2 } } } }', inspect({'a': {'b': { 'c': { 'd': 2 }}}}, false, null)); assert.equal('{ a: { b: { c: { d: 2 } } } }', inspect({'a': {'b': { 'c': { 'd': 2 }}}}, false, null));
assert.equal('[ 1, 2, 3, [length]: 3 ]', inspect([1,2,3], true)); assert.equal('[ 1, 2, 3, [length]: 3 ]', inspect([1,2,3], true));
assert.equal('{ a: [object Object] }', inspect({'a': {'b': { 'c': 2}}},false,0)); assert.equal('{ a: [Object] }', inspect({'a': {'b': { 'c': 2}}},false,0));
assert.equal('{ a: { b: [object Object] } }', inspect({'a': {'b': { 'c': 2}}},false,1)); assert.equal('{ a: { b: [Object] } }', inspect({'a': {'b': { 'c': 2}}},false,1));
assert.equal("{ visible: 1 }", assert.equal("{ visible: 1 }",
inspect(Object.create({}, {visible:{value:1,enumerable:true},hidden:{value:2}})) inspect(Object.create({}, {visible:{value:1,enumerable:true},hidden:{value:2}}))
); );

Loading…
Cancel
Save