Skip to content

Commit

Permalink
more correct string behavior in stringify example
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Sep 9, 2010
1 parent b9e6db5 commit b9750ff
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 2 additions & 0 deletions examples/hash.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env node

var Hash = require('traverse/hash');
var sys = require('sys');

Expand Down
10 changes: 7 additions & 3 deletions examples/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
var Traverse = require('traverse');
var sys = require('sys');

var obj = [ 5, 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ];
var obj = [ 'five', 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ];

var s = '';
Traverse(obj).forEach(function (node) {
Traverse(obj).forEach(function to_s (node) {
if (Array.isArray(node)) {
this.before(function () { s += '[' });
this.post(function (child) {
Expand All @@ -16,13 +16,17 @@ Traverse(obj).forEach(function (node) {
else if (typeof node == 'object') {
this.before(function () { s += '{' });
this.pre(function (x, key) {
s += '"' + key + '"' + ':';
to_s(key);
s += ':';
});
this.post(function (child) {
if (!child.isLast) s += ',';
});
this.after(function () { s += '}' });
}
else if (typeof node == 'string') {
s += '"' + node.toString().replace(/"/g, '\\"') + '"';
}
else if (typeof node == 'function') {
s += 'null';
}
Expand Down

0 comments on commit b9750ff

Please sign in to comment.