Browse Source

Add node.inspect() and deprecate p().

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
4f69871c17
  1. 6
      doc/api.html
  2. 4
      doc/api.txt
  3. 11
      doc/node.1
  4. 43
      lib/repl.js
  5. 33
      src/util.js

6
doc/api.html

@ -84,11 +84,11 @@ output the string immediately to stdout.
</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">
@ -1944,7 +1944,7 @@ init (Handle&lt;Object&gt; target)
<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>

4
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)+::

11
doc/node.1

@ -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)
@ -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)

43
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;
}
}

33
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));
};

Loading…
Cancel
Save