UNPKG

1 kBJavaScriptView Raw
1var test = require('tap').test;
2var traverse = require('../');
3
4test('stop', function (t) {
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 t.equal(visits, 5);
14 t.end();
15});
16
17test('stopMap', function (t) {
18 var s = traverse('abcdefghij'.split('')).map(function (node) {
19 if (typeof node === 'string') {
20 if (node === 'e') this.stop()
21 return node.toUpperCase();
22 }
23 }).join('');
24
25 t.equal(s, 'ABCDEfghij');
26 t.end();
27});
28
29test('stopReduce', function (t) {
30 var obj = {
31 a : [ 4, 5 ],
32 b : [ 6, [ 7, 8, 9 ] ]
33 };
34 var xs = traverse(obj).reduce(function (acc, node) {
35 if (this.isLeaf) {
36 if (node === 7) this.stop();
37 else acc.push(node)
38 }
39 return acc;
40 }, []);
41
42 t.same(xs, [ 4, 5, 6 ]);
43 t.end();
44});