From 4f69871c17bbc14d947be5447aedb94be86fa563 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Sun, 27 Sep 2009 12:33:44 +0200 Subject: [PATCH] Add node.inspect() and deprecate p(). --- doc/api.html | 6 +++--- doc/api.txt | 4 ++-- doc/node.1 | 11 +++++------ lib/repl.js | 43 +------------------------------------------ src/util.js | 33 ++++++++++++++++++++++++++------- 5 files changed, 37 insertions(+), 60 deletions(-) diff --git a/doc/api.html b/doc/api.html index de644d6b9b..c303993214 100644 --- a/doc/api.html +++ b/doc/api.html @@ -84,11 +84,11 @@ output the string immediately to stdout.

-p(object) +node.inspect(object)

-Print the JSON representation of object to the standard output. +Return a string representation of the object. (For debugging.)

@@ -1944,7 +1944,7 @@ init (Handle<Object> target) diff --git a/doc/api.txt b/doc/api.txt index 89ab6fbe6c..fbc80ea57a 100644 --- a/doc/api.txt +++ b/doc/api.txt @@ -62,8 +62,8 @@ A synchronous output function. Will block the process and output the string immediately to stdout. -+p(object)+ :: -Print the JSON representation of +object+ to the standard output. ++node.inspect(object)+ :: +Return a string representation of the +object+. (For debugging.) +print(string)+:: diff --git a/doc/node.1 b/doc/node.1 index 734719af61..1a982822ea 100644 --- a/doc/node.1 +++ b/doc/node.1 @@ -1,11 +1,11 @@ .\" Title: node .\" Author: .\" Generator: DocBook XSL Stylesheets v1.73.2 -.\" Date: 09/24/2009 +.\" Date: 09/27/2009 .\" Manual: .\" Source: .\" -.TH "NODE" "1" "09/24/2009" "" "" +.TH "NODE" "1" "09/27/2009" "" "" .\" disable hyphenation .nh .\" disable justification (adjust text to left margin only) @@ -65,11 +65,10 @@ node\.debug(string) A synchronous output function\. Will block the process and output the string immediately to stdout\. .RE .PP -p(object) +node\.inspect(object) .RS 4 -Print the JSON representation of -object -to the standard output\. +Return a string representation of the +object\. (For debugging\.) .RE .PP print(string) diff --git a/lib/repl.js b/lib/repl.js index 23c70d4585..a55df4ef76 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -42,7 +42,7 @@ function readline (cmd) { with (exports.scope) { var ret = eval(buffered_cmd); exports.scope['_'] = ret; - printValue(ret); + puts(node.inspect(ret)); } buffered_cmd = ''; @@ -137,44 +137,3 @@ function convertToScope (cmd) { return cmd; } - -/** - * 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 - */ -function printValue (value) { - if (value === 0) { - puts("0"); - return; - } - - if (value === false) { - puts("false"); - return; - } - - if (value === "") { - puts('""'); - return; - } - - if (typeof(value) == "function") { - puts("[Function]"); - return; - } - - if (value === undefined) { - return; - } - - try { - puts(JSON.stringify(value)); - } catch (e) { - if (e.message.search("circular")) - puts("[Circular Object]"); - else - throw e; - } -} diff --git a/src/util.js b/src/util.js index 9850f6eef8..a90e9a93c0 100644 --- a/src/util.js +++ b/src/util.js @@ -80,12 +80,31 @@ node.error = function (x) { node.stdio.writeError(x.toString() + "\n"); }; -p = function (x) { - if (x === null) { - node.error("null"); - } else if (x === NaN) { - node.error("NaN"); - } else { - node.error(JSON.stringify(x) || "undefined"); +/** + * 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 + */ +node.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); + } 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; + } } }; + +p = function (x) { + node.error(node.inspect(x)); +};