interface.js 996 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. var test = require('tape');
  2. var traverse = require('../');
  3. test('interface map', function (t) {
  4. var obj = { a : [ 5,6,7 ], b : { c : [8] } };
  5. t.same(
  6. traverse.paths(obj)
  7. .sort()
  8. .map(function (path) { return path.join('/') })
  9. .slice(1)
  10. .join(' ')
  11. ,
  12. 'a a/0 a/1 a/2 b b/c b/c/0'
  13. );
  14. t.same(
  15. traverse.nodes(obj),
  16. [
  17. { a: [ 5, 6, 7 ], b: { c: [ 8 ] } },
  18. [ 5, 6, 7 ], 5, 6, 7,
  19. { c: [ 8 ] }, [ 8 ], 8
  20. ]
  21. );
  22. t.same(
  23. traverse.map(obj, function (node) {
  24. if (typeof node == 'number') {
  25. return node + 1000;
  26. }
  27. else if (Array.isArray(node)) {
  28. return node.join(' ');
  29. }
  30. }),
  31. { a: '5 6 7', b: { c: '8' } }
  32. );
  33. var nodes = 0;
  34. traverse.forEach(obj, function (node) { nodes ++ });
  35. t.same(nodes, 8);
  36. t.end();
  37. });