Skip to content

Commit

Permalink
Add node.inspect() and deprecate p().
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Sep 27, 2009
1 parent 9cd7864 commit 4f69871
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 60 deletions.
6 changes: 3 additions & 3 deletions doc/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ <h3 id="_helpers">Helpers</h3><div style="clear:left"></div>
</p>
</dd>
<dt class="hdlist1">
<tt>p(object)</tt>
<tt>node.inspect(object)</tt>
</dt>
<dd>
<p>
Print the JSON representation of <tt>object</tt> to the standard output.
Return a string representation of the <tt>object</tt>. (For debugging.)
</p>
</dd>
<dt class="hdlist1">
Expand Down Expand Up @@ -1944,7 +1944,7 @@ <h2 id="_extension_api">Extension API</h2>
<div id="footer">
<div id="footer-text">
Version 0.1.12<br />
Last updated 2009-09-24 13:47:32 CEST
Last updated 2009-09-27 12:27:16 CEST
</div>
</div>
</body>
Expand Down
4 changes: 2 additions & 2 deletions doc/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)+::
Expand Down
11 changes: 5 additions & 6 deletions doc/node.1
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
.\" Title: node
.\" Author:
.\" Generator: DocBook XSL Stylesheets v1.73.2 <http://docbook.sf.net/>
.\" 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)
Expand Down Expand Up @@ -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)
Expand Down
43 changes: 1 addition & 42 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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;
}
}
33 changes: 26 additions & 7 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
};

0 comments on commit 4f69871

Please sign in to comment.