stop.js 1000 B

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