1 | var assert = require('assert');
|
2 | var Traverse = require('../');
|
3 |
|
4 | exports.stringify = function () {
|
5 | var obj = [ 5, 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ];
|
6 |
|
7 | var s = '';
|
8 | Traverse(obj).forEach(function (node) {
|
9 | if (Array.isArray(node)) {
|
10 | this.before(function () { s += '[' });
|
11 | this.post(function (child) {
|
12 | if (!child.isLast) s += ',';
|
13 | });
|
14 | this.after(function () { s += ']' });
|
15 | }
|
16 | else if (typeof node == 'object') {
|
17 | this.before(function () { s += '{' });
|
18 | this.pre(function (x, key) {
|
19 | s += '"' + key + '"' + ':';
|
20 | });
|
21 | this.post(function (child) {
|
22 | if (!child.isLast) s += ',';
|
23 | });
|
24 | this.after(function () { s += '}' });
|
25 | }
|
26 | else if (typeof node == 'function') {
|
27 | s += 'null';
|
28 | }
|
29 | else {
|
30 | s += node.toString();
|
31 | }
|
32 | });
|
33 |
|
34 | assert.equal(s, JSON.stringify(obj));
|
35 | }
|
36 |
|