interface.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. var assert = require('assert');
  2. var Traverse = require('traverse');
  3. exports['interface map'] = function () {
  4. var obj = { a : [ 5,6,7 ], b : { c : [8] } };
  5. assert.deepEqual(
  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. assert.deepEqual(
  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. assert.deepEqual(
  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. assert.deepEqual(nodes, 8);
  36. };