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:
884 bytes
|
Rev | Line | |
---|
[d24f17c] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var test = require('tape');
|
---|
| 4 | var traverse = require('../');
|
---|
| 5 |
|
---|
| 6 | test('stop', function (t) {
|
---|
| 7 | var visits = 0;
|
---|
| 8 | traverse('abcdefghij'.split('')).forEach(function (node) {
|
---|
| 9 | if (typeof node === 'string') {
|
---|
| 10 | visits += 1;
|
---|
| 11 | if (node === 'e') { this.stop(); }
|
---|
| 12 | }
|
---|
| 13 | });
|
---|
| 14 |
|
---|
| 15 | t.equal(visits, 5);
|
---|
| 16 | t.end();
|
---|
| 17 | });
|
---|
| 18 |
|
---|
| 19 | test('stopMap', function (t) {
|
---|
| 20 | var s = traverse('abcdefghij'.split('')).map(function (node) {
|
---|
| 21 | if (typeof node === 'string') {
|
---|
| 22 | if (node === 'e') { this.stop(); }
|
---|
| 23 | return node.toUpperCase();
|
---|
| 24 | }
|
---|
| 25 | return void undefined;
|
---|
| 26 | }).join('');
|
---|
| 27 |
|
---|
| 28 | t.equal(s, 'ABCDEfghij');
|
---|
| 29 | t.end();
|
---|
| 30 | });
|
---|
| 31 |
|
---|
| 32 | test('stopReduce', function (t) {
|
---|
| 33 | var obj = {
|
---|
| 34 | a: [4, 5],
|
---|
| 35 | b: [6, [7, 8, 9]],
|
---|
| 36 | };
|
---|
| 37 | var xs = traverse(obj).reduce(function (acc, node) {
|
---|
| 38 | if (this.isLeaf) {
|
---|
| 39 | if (node === 7) { this.stop(); } else { acc.push(node); }
|
---|
| 40 | }
|
---|
| 41 | return acc;
|
---|
| 42 | }, []);
|
---|
| 43 |
|
---|
| 44 | t.same(xs, [4, 5, 6]);
|
---|
| 45 | t.end();
|
---|
| 46 | });
|
---|
Note:
See
TracBrowser
for help on using the repository browser.