diff options
author | Benjamin Thomas <benjamin@benjaminthomas.org> | 2010-02-27 08:32:55 +0000 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2010-03-01 13:34:46 -0800 |
commit | 6034701f57354d7e44d7378d9d6f9860e49619fc (patch) | |
tree | 43b5e14f7882345b47eab18bc2a7fbf080facd9a /lib/sys.js | |
parent | b021a845f7d3712c6b821a30ebfe69156c708a28 (diff) | |
download | nodejs-6034701f57354d7e44d7378d9d6f9860e49619fc.tar.gz nodejs-6034701f57354d7e44d7378d9d6f9860e49619fc.tar.bz2 nodejs-6034701f57354d7e44d7378d9d6f9860e49619fc.zip |
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.
Diffstat (limited to 'lib/sys.js')
-rw-r--r-- | lib/sys.js | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/lib/sys.js b/lib/sys.js index 4066ab917..5ad68e492 100644 --- a/lib/sys.js +++ b/lib/sys.js @@ -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]; |