stop.js 884 B

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