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> </p>
</dd> </dd>
<dt class="hdlist1"> <dt class="hdlist1">
<tt>p(object)</tt> <tt>node.inspect(object)</tt>
</dt> </dt>
<dd> <dd>
<p> <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> </p>
</dd> </dd>
<dt class="hdlist1"> <dt class="hdlist1">
@ -1944,7 +1944,7 @@ init (Handle&lt;Object&gt; target)
<div id="footer"> <div id="footer">
<div id="footer-text"> <div id="footer-text">
Version 0.1.12<br /> 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>
</div> </div>
</body> </body>

4
doc/api.txt

@ -62,8 +62,8 @@ A synchronous output function. Will block the process and
output the string immediately to stdout. 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.)
+print(string)+:: +print(string)+::

11
doc/node.1

@ -1,11 +1,11 @@
.\" Title: node .\" Title: node
.\" Author: .\" Author:
.\" Generator: DocBook XSL Stylesheets v1.73.2 <http://docbook.sf.net/> .\" Generator: DocBook XSL Stylesheets v1.73.2 <http://docbook.sf.net/>
.\" Date: 09/24/2009 .\" Date: 09/27/2009
.\" Manual: .\" Manual:
.\" Source: .\" Source:
.\" .\"
.TH "NODE" "1" "09/24/2009" "" "" .TH "NODE" "1" "09/27/2009" "" ""
.\" disable hyphenation .\" disable hyphenation
.nh .nh
.\" disable justification (adjust text to left margin only) .\" 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\. A synchronous output function\. Will block the process and output the string immediately to stdout\.
.RE .RE
.PP .PP
p(object) node\.inspect(object)
.RS 4 .RS 4
Print the JSON representation of Return a string representation of the
object object\. (For debugging\.)
to the standard output\.
.RE .RE
.PP .PP
print(string) print(string)

43
lib/repl.js

@ -42,7 +42,7 @@ function readline (cmd) {
with (exports.scope) { with (exports.scope) {
var ret = eval(buffered_cmd); var ret = eval(buffered_cmd);
exports.scope['_'] = ret; exports.scope['_'] = ret;
printValue(ret); puts(node.inspect(ret));
} }
buffered_cmd = ''; buffered_cmd = '';
@ -137,44 +137,3 @@ function convertToScope (cmd) {
return 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"); node.stdio.writeError(x.toString() + "\n");
}; };
p = function (x) { /**
if (x === null) { * Echos the value of a value. Trys to print the value out
node.error("null"); * in the best way possible given the different types.
} else if (x === NaN) { *
node.error("NaN"); * @param {Object} value The object to print out
} else { */
node.error(JSON.stringify(x) || "undefined"); 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