Skip to content

Commit

Permalink
Add optional third argument sys.inpect to indicate how many times you…
Browse files Browse the repository at this point in the history
… want it to recurse
  • Loading branch information
Benjamin Thomas authored and ry committed Feb 25, 2010
1 parent b1b8496 commit a2714be
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
21 changes: 16 additions & 5 deletions doc/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
25 changes: 18 additions & 7 deletions lib/sys.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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.
Expand Down Expand Up @@ -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]';
}
Expand All @@ -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 () {
Expand Down
2 changes: 2 additions & 0 deletions test/simple/test-sys.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}}))
);
Expand Down

0 comments on commit a2714be

Please sign in to comment.