0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00

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.
This commit is contained in:
Benjamin Thomas 2010-02-27 08:32:55 +00:00 committed by Ryan Dahl
parent b021a845f7
commit 6034701f57
2 changed files with 22 additions and 11 deletions

View File

@ -95,11 +95,11 @@ exports.inspect = function (obj, showHidden, depth) {
return braces[0] + base + braces[1];
}
if( recurseTimes < 0 ) {
if (recurseTimes < 0) {
if (value instanceof RegExp) {
return '' + value;
} else {
return "[object Object]";
return "[Object]";
}
}
@ -129,10 +129,17 @@ exports.inspect = function (obj, showHidden, depth) {
else {
str = format(value[key], recurseTimes - 1);
}
if( str.indexOf('\n') > -1 ) {
str = '\n' + str.split('\n').map(function(line) {
if (str.indexOf('\n') > -1) {
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;
}).join('\n');
}
}
} else {
str = '[Circular]';
@ -143,7 +150,7 @@ exports.inspect = function (obj, showHidden, depth) {
return str;
}
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);
}
else {
@ -154,13 +161,17 @@ exports.inspect = function (obj, showHidden, depth) {
return name + ": " + str;
});
var numLinesEst = 0;
var length = output.reduce(function(prev, cur) {
numLinesEst++;
if( cur.indexOf('\n') >= 0 ) {
numLinesEst++;
}
return prev + cur.length + 1;
},0);
if( length > 50 ) {
output = braces[0] + (base === '' ? '' : base + '\n,') + ' ' + output.join('\n, ') + '\n' +braces[1];
if (length > 50) {
output = braces[0] + (base === '' ? '' : base + '\n,') + ' ' + output.join('\n, ') + (numLinesEst > 1 ? '\n' : ' ') + braces[1];
}
else {
output = braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];

View File

@ -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: {} }', inspect({'a': {}}));
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('[ 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: { b: [object Object] } }', inspect({'a': {'b': { 'c': 2}}},false,1));
assert.equal('{ a: [Object] }', inspect({'a': {'b': { 'c': 2}}},false,0));
assert.equal('{ a: { b: [Object] } }', inspect({'a': {'b': { 'c': 2}}},false,1));
assert.equal("{ visible: 1 }",
inspect(Object.create({}, {visible:{value:1,enumerable:true},hidden:{value:2}}))
);