Add optional third argument sys.inpect to indicate how many times you want it to recurse

This commit is contained in:
Benjamin Thomas 2010-02-25 21:12:39 +00:00 коммит произвёл Ryan Dahl
Родитель b1b84960ce
Коммит a2714be8b5
3 изменённых файлов: 36 добавлений и 12 удалений

Просмотреть файл

@ -201,17 +201,29 @@ These function are in the module +"sys"+. Use +require("sys")+ to access
them.
+puts(string)+::
Outputs the +string+ and a trailing new-line to +stdout+.
Outputs +string+ and a trailing new-line to +stdout+.
+print(string)+::
Like +puts()+ but without the trailing new-line.
+debug(string)+::
A synchronous output function. Will block the process and
output the string immediately to stdout.
output +string+ immediately to +stdout+.
+inspect(object, showHidden, depth)+ ::
Return a string representation of +object+. (For debugging.)
+
If +showHidden+ is +true+, then the object's non-enumerable properties will be
shown too.
+
If +depth+ is provided, it tells +inspect+ how many times to recurse while
formatting the object. This is useful for inspecting large complicated objects.
The defualt is to recurse indefinitely.
+inspect(object, showHidden)+ ::
Return a string representation of the +object+. (For debugging.) If showHidden is true, then the object's non-enumerable properties will be shown too.
+exec(command, callback)+::
Executes the command as a child process, buffers the output and returns it
@ -230,7 +242,6 @@ will be +null+. On error +err+ will be an instance of +Error+ and +err.code+
will be the exit code of the child process.
== Events
Many objects in Node emit events: a TCP server emits an event each time

Просмотреть файл

@ -29,9 +29,9 @@ exports.error = function (x) {
* @param {Object} value The object to print out
* @param {Boolean} showHidden Flag that shows hidden (not enumerable) properties of objects.
*/
exports.inspect = function (obj, showHidden) {
exports.inspect = function (obj, showHidden, depth) {
var seen = [];
function format(value) {
function format(value, recurseTimes) {
// Primitive types cannot have properties
switch (typeof value) {
case 'undefined': return 'undefined';
@ -45,9 +45,12 @@ exports.inspect = function (obj, showHidden) {
}
// Look up the keys of the object.
var keys = showHidden ? Object.getOwnPropertyNames(value).map(function (key) {
return '' + key;
}) : Object.keys(value);
if (showHidden) {
var keys = Object.getOwnPropertyNames(value).map(function (key) { return '' + key; });
} else {
var keys = Object.keys(value);
}
var visible_keys = Object.keys(value);
// Functions without properties can be shortcutted.
@ -112,7 +115,15 @@ exports.inspect = function (obj, showHidden) {
}
if (!str) {
if (seen.indexOf(value[key]) < 0) {
str = format(value[key]);
if (typeof recurseTimes === 'undefined' || recurseTimes === null) {
str = format(value[key]);
}
else if (recurseTimes > 0) {
str = format(value[key], recurseTimes - 1);
}
else {
str = value[key];
}
} else {
str = '[Circular]';
}
@ -129,7 +140,7 @@ exports.inspect = function (obj, showHidden) {
return ' ' + line;
}).join('\n') + "\n" + braces[1];
}
return format(obj);
return format(obj, depth);
};
exports.p = function () {

Просмотреть файл

@ -27,6 +27,8 @@ 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}}));
assert.equal('[\n 1,\n 2,\n 3,\n [length]: 3\n]', inspect([1,2,3], true));
assert.equal('{\n \"a\": [object Object]\n}', inspect({'a': {'b': { 'c': 2}}},false,0));
assert.equal('{\n \"a\": {\n \"b\": [object Object]\n }\n}', inspect({'a': {'b': { 'c': 2}}},false,1));
assert.equal("{\n \"visible\": 1\n}",
inspect(Object.create({}, {visible:{value:1,enumerable:true},hidden:{value:2}}))
);