Skip to content

Commit

Permalink
add stringify to examples
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Sep 4, 2010
1 parent 6de18e5 commit 6683529
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
35 changes: 35 additions & 0 deletions examples/stringify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env node
var Traverse = require('traverse');
var sys = require('sys');

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

var s = '';
Traverse(obj).forEach(function (node) {
if (Array.isArray(node)) {
this.before(function () { s += '[' });
this.post(function (child) {
if (!child.isLast) s += ',';
});
this.after(function () { s += ']' });
}
else if (typeof node == 'object') {
this.before(function () { s += '{' });
this.pre(function (x, key) {
s += '"' + key + '"' + ':';
});
this.post(function (child) {
if (!child.isLast) s += ',';
});
this.after(function () { s += '}' });
}
else if (typeof node == 'function') {
s += 'null';
}
else {
s += node.toString();
}
});

console.log('JSON.stringify: ' + JSON.stringify(obj));
console.log('this stringify: ' + s);
3 changes: 1 addition & 2 deletions test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ var Traverse = require('traverse');
var sys = require('sys');

exports.stringify = function (assert) {
//var obj = [ 5, 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ];
var obj = [ 5, 6, -3, [ 7, 8, -2, 11 ], { f : 10, g : -13 } ];
var obj = [ 5, 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ];

var s = '';
Traverse(obj).forEach(function (node) {
Expand Down

0 comments on commit 6683529

Please sign in to comment.