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
This commit is contained in:
parent
4d818f1fd3
commit
34c02357ff
76
lib/sys.js
76
lib/sys.js
@ -21,22 +21,7 @@ exports.error = function (x) {
|
|||||||
* @param {Object} value The object to print out
|
* @param {Object} value The object to print out
|
||||||
*/
|
*/
|
||||||
exports.inspect = function (value) {
|
exports.inspect = function (value) {
|
||||||
if (value === 0) return "0";
|
return formatter(value, '', []);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.p = function (x) {
|
exports.p = function (x) {
|
||||||
@ -82,3 +67,62 @@ exports.exec = function (command) {
|
|||||||
* @param {function} superCtor Constructor function to inherit prototype from
|
* @param {function} superCtor Constructor function to inherit prototype from
|
||||||
*/
|
*/
|
||||||
exports.inherits = process.inherits;
|
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;
|
||||||
|
}
|
||||||
|
26
test/mjsunit/test-sys.js
Normal file
26
test/mjsunit/test-sys.js
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
process.mixin(require("./common"));
|
||||||
|
process.mixin(require("sys"));
|
||||||
|
|
||||||
|
assert.equal("0", inspect(0));
|
||||||
|
assert.equal("1", inspect(1));
|
||||||
|
assert.equal("false", inspect(false));
|
||||||
|
assert.equal('""', inspect(""));
|
||||||
|
assert.equal('"hello"', inspect("hello"));
|
||||||
|
assert.equal("[Function]", inspect(function() {}));
|
||||||
|
assert.equal('undefined', inspect(undefined));
|
||||||
|
assert.equal('null', inspect(null));
|
||||||
|
|
||||||
|
assert.equal('[]', inspect([]));
|
||||||
|
assert.equal('[\n 1,\n 2\n]', inspect([1, 2]));
|
||||||
|
assert.equal('[\n 1,\n [\n 2,\n 3\n ]\n]', inspect([1, [2, 3]]));
|
||||||
|
|
||||||
|
assert.equal('{}', inspect({}));
|
||||||
|
assert.equal('{\n "a": 1\n}', inspect({a: 1}));
|
||||||
|
assert.equal('{\n "a": [Function]\n}', inspect({a: function() {}}));
|
||||||
|
assert.equal('{\n "a": 1,\n "b": 2\n}', inspect({a: 1, b: 2}));
|
||||||
|
assert.equal('{\n "a": {}\n}', inspect({'a': {}}));
|
||||||
|
assert.equal('{\n "a": {\n "b": 2\n }\n}', inspect({'a': {'b': 2}}));
|
||||||
|
|
||||||
|
var value = {}
|
||||||
|
value['a'] = value;
|
||||||
|
assert.equal('{\n "a": [Circular]\n}', inspect(value));
|
Loading…
x
Reference in New Issue
Block a user