UNPKG

985 BJavaScriptView Raw
1var assert = require('assert');
2var traverse = require('../');
3
4exports.stop = function () {
5 var visits = 0;
6 traverse('abcdefghij'.split('')).forEach(function (node) {
7 if (typeof node === 'string') {
8 visits ++;
9 if (node === 'e') this.stop()
10 }
11 });
12
13 assert.equal(visits, 5);
14};
15
16exports.stopMap = function () {
17 var s = traverse('abcdefghij'.split('')).map(function (node) {
18 if (typeof node === 'string') {
19 if (node === 'e') this.stop()
20 return node.toUpperCase();
21 }
22 }).join('');
23
24 assert.equal(s, 'ABCDEfghij');
25};
26
27exports.stopReduce = function () {
28 var obj = {
29 a : [ 4, 5 ],
30 b : [ 6, [ 7, 8, 9 ] ]
31 };
32 var xs = traverse(obj).reduce(function (acc, node) {
33 if (this.isLeaf) {
34 if (node === 7) this.stop();
35 else acc.push(node)
36 }
37 return acc;
38 }, []);
39
40 assert.deepEqual(xs, [ 4, 5, 6 ]);
41};