summaryrefslogtreecommitdiff
path: root/lib/sys.js
diff options
context:
space:
mode:
authorBenjamin Thomas <benjamin@benjaminthomas.org>2010-02-26 01:41:02 +0000
committerRyan Dahl <ry@tinyclouds.org>2010-02-25 18:23:11 -0800
commitde1521413ef53bd63c87a9698eb3611790bc5a56 (patch)
tree9aa1dd696a746e7efde95d82f4ef3fabc0a50fe1 /lib/sys.js
parenta2714be8b5c397700df4c80ba6f4abbaab073ea9 (diff)
downloadnode-new-de1521413ef53bd63c87a9698eb3611790bc5a56.tar.gz
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.
Diffstat (limited to 'lib/sys.js')
-rw-r--r--lib/sys.js48
1 files changed, 38 insertions, 10 deletions
diff --git a/lib/sys.js b/lib/sys.js
index 8d5404b048..9c05342e48 100644
--- a/lib/sys.js
+++ b/lib/sys.js
@@ -35,7 +35,7 @@ exports.inspect = function (obj, showHidden, depth) {
// Primitive types cannot have properties
switch (typeof value) {
case 'undefined': return 'undefined';
- case 'string': return JSON.stringify(value);
+ case 'string': return JSON.stringify(value).replace(/'/g, "\\'").replace(/\\"/g, '"').replace(/(^"|"$)/g, "'");
case 'number': return '' + value;
case 'boolean': return '' + value;
}
@@ -95,7 +95,15 @@ exports.inspect = function (obj, showHidden, depth) {
return braces[0] + base + braces[1];
}
- return braces[0] + base + "\n" + (keys.map(function (key) {
+ if( recurseTimes < 0 ) {
+ if (value instanceof RegExp) {
+ return '' + value;
+ } else {
+ return "[object Object]";
+ }
+ }
+
+ output = keys.map(function (key) {
var name, str;
if (value.__lookupGetter__) {
if (value.__lookupGetter__(key)) {
@@ -115,14 +123,16 @@ exports.inspect = function (obj, showHidden, depth) {
}
if (!str) {
if (seen.indexOf(value[key]) < 0) {
- if (typeof recurseTimes === 'undefined' || recurseTimes === null) {
+ if ( recurseTimes === null) {
str = format(value[key]);
}
- else if (recurseTimes > 0) {
+ else {
str = format(value[key], recurseTimes - 1);
}
- else {
- str = value[key];
+ if( str.indexOf('\n') > -1 ) {
+ str = '\n' + str.split('\n').map(function(line) {
+ return ' ' + line;
+ }).join('\n');
}
} else {
str = '[Circular]';
@@ -133,14 +143,32 @@ exports.inspect = function (obj, showHidden, depth) {
return str;
}
name = JSON.stringify('' + key);
+ if( name.match(/^"([a-zA-Z_0-9]+)"$/) ) {
+ name = name.substr(1, name.length-2);
+ }
+ else {
+ name = name.replace(/'/g, "\\'").replace(/\\"/g, '"').replace(/(^"|"$)/g, "'");
+ }
}
return name + ": " + str;
- }).join(",\n")).split("\n").map(function (line) {
- return ' ' + line;
- }).join('\n') + "\n" + braces[1];
+ });
+
+
+ var length = output.reduce(function(prev, cur) {
+ return prev + cur.length + 1;
+ },0);
+
+ if( length > 50 ) {
+ output = braces[0] + (base === '' ? '' : base + '\n,') + ' ' + output.join('\n, ') + '\n' +braces[1];
+ }
+ else {
+ output = braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
+ }
+
+ return output;
}
- return format(obj, depth);
+ return format(obj, (typeof depth === 'undefined' ? 2 : depth));
};
exports.p = function () {