diff options
author | Xavier Shay <xavier@rhnh.net> | 2009-12-01 21:20:14 +1100 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2009-12-06 12:19:23 +0100 |
commit | 34c02357ff8e00c35e493754943f599dba0b0f29 (patch) | |
tree | 7003b983dbf541015b0273a5f01f6666bebe129e /lib/sys.js | |
parent | 4d818f1fd3454f972ef6e0a4f32ed26585804dd5 (diff) | |
download | android-node-v8-34c02357ff8e00c35e493754943f599dba0b0f29.tar.gz android-node-v8-34c02357ff8e00c35e493754943f599dba0b0f29.tar.bz2 android-node-v8-34c02357ff8e00c35e493754943f599dba0b0f29.zip |
sys.inspect is totally more awesome now
- No longer relies on JSON.stringify, so it can output nulls and functions
- Handles circular references better
- Has tests
Diffstat (limited to 'lib/sys.js')
-rw-r--r-- | lib/sys.js | 84 |
1 files changed, 64 insertions, 20 deletions
diff --git a/lib/sys.js b/lib/sys.js index f755d71638..9c4dc6d944 100644 --- a/lib/sys.js +++ b/lib/sys.js @@ -17,26 +17,11 @@ exports.error = function (x) { /** * Echos the value of a value. Trys to print the value out * in the best way possible given the different types. - * + * * @param {Object} value The object to print out */ exports.inspect = function (value) { - if (value === 0) return "0"; - if (value === false) return "false"; - if (value === "") return '""'; - if (typeof(value) == "function") return "[Function]"; - if (value === undefined) return; - - try { - return JSON.stringify(value, undefined, 1); - } catch (e) { - // TODO make this recusrive and do a partial JSON output of object. - if (e.message.search("circular")) { - return "[Circular Object]"; - } else { - throw e; - } - } + return formatter(value, '', []); }; exports.p = function (x) { @@ -50,13 +35,13 @@ exports.exec = function (command) { var promise = new process.Promise(); child.addListener("output", function (chunk) { - if (chunk) stdout += chunk; + if (chunk) stdout += chunk; }); child.addListener("error", function (chunk) { - if (chunk) stderr += chunk; + if (chunk) stderr += chunk; }); - + child.addListener("exit", function (code) { if (code == 0) { promise.emitSuccess(stdout, stderr); @@ -82,3 +67,62 @@ exports.exec = function (command) { * @param {function} superCtor Constructor function to inherit prototype from */ exports.inherits = process.inherits; + +/** + * A recursive function to format an object - used by inspect. + * + * @param {Object} value + * the value to format + * @param {String} indent + * the indent level of any nested objects, since they are formatted over + * more than one line + * @param {Array} parents + * contains all objects above the current one in the heirachy, used to + * prevent getting stuck in a loop on circular references + */ +var formatter = function(value, indent, parents) { + switch(typeof(value)) { + case 'string': return '"' + value + '"'; + case 'number': return '' + value; + case 'function': return '[Function]'; + case 'boolean': return '' + value; + case 'undefined': return 'undefined'; + case 'object': + if (value == null) return 'null'; + if (parents.indexOf(value) >= 0) return '[Circular]'; + parents.push(value); + + if (value instanceof Array) { + return formatObject(value, indent, parents, '[]', function(x, f) { + return f(value[x]); + }); + } else { + return formatObject(value, indent, parents, '{}', function(x, f) { + return f(x) + ': ' + f(value[x]); + }); + } + return buffer; + default: + throw('inspect unimplemented for ' + typeof(value)); + } +} + +/** + * Helper function for formatting either an array or an object, used internally by formatter + */ +var formatObject = function(obj, indent, parents, parenthesis, entryFormatter) { + var buffer = parenthesis[0]; + var values = []; + + var localFormatter = function(value) { + return formatter(value, indent + ' ', parents); + }; + for (x in obj) { + values.push(indent + ' ' + entryFormatter(x, localFormatter)); + } + if (values.length > 0) { + buffer += "\n" + values.join(",\n") + "\n" + indent; + } + buffer += parenthesis[1]; + return buffer; +} |