From 6034701f57354d7e44d7378d9d6f9860e49619fc Mon Sep 17 00:00:00 2001 From: Benjamin Thomas Date: Sat, 27 Feb 2010 08:32:55 +0000 Subject: 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. --- lib/sys.js | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'lib/sys.js') 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]; -- cgit v1.2.3