source: node_modules/traverse/test/stringify.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 831 bytes
Line 
1'use strict';
2
3var test = require('tape');
4var traverse = require('../');
5
6test('stringify', function (t) {
7 var obj = [5, 6, -3, [7, 8, -2, 1], { f: 10, g: -13 }];
8
9 var s = '';
10 traverse(obj).forEach(function (node) {
11 if (Array.isArray(node)) {
12 this.before(function () { s += '['; });
13 this.post(function (child) {
14 if (!child.isLast) { s += ','; }
15 });
16 this.after(function () { s += ']'; });
17 } else if (typeof node === 'object') {
18 this.before(function () { s += '{'; });
19 this.pre(function (x, key) {
20 s += '"' + key + '":';
21 });
22 this.post(function (child) {
23 if (!child.isLast) { s += ','; }
24 });
25 this.after(function () { s += '}'; });
26 } else if (typeof node === 'function') {
27 s += 'null';
28 } else {
29 s += node.toString();
30 }
31 });
32
33 t.equal(s, JSON.stringify(obj));
34 t.end();
35});
Note: See TracBrowser for help on using the repository browser.