From a2714be8b5c397700df4c80ba6f4abbaab073ea9 Mon Sep 17 00:00:00 2001 From: Benjamin Thomas Date: Thu, 25 Feb 2010 21:12:39 +0000 Subject: [PATCH] Add optional third argument sys.inpect to indicate how many times you want it to recurse --- doc/api.txt | 21 ++++++++++++++++----- lib/sys.js | 25 ++++++++++++++++++------- test/simple/test-sys.js | 2 ++ 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/doc/api.txt b/doc/api.txt index 13e70f4c49..c61564eb79 100644 --- a/doc/api.txt +++ b/doc/api.txt @@ -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 diff --git a/lib/sys.js b/lib/sys.js index cbdf0c5788..8d5404b048 100644 --- a/lib/sys.js +++ b/lib/sys.js @@ -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 () { diff --git a/test/simple/test-sys.js b/test/simple/test-sys.js index bff7d2704b..5236fb7eb0 100644 --- a/test/simple/test-sys.js +++ b/test/simple/test-sys.js @@ -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}})) );