siblings.js 968 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. var test = require('tape');
  3. var traverse = require('../');
  4. test('siblings', function (t) {
  5. var obj = { a: 1, b: 2, c: [4, 5, 6] };
  6. var res = traverse(obj).reduce(function (acc) {
  7. /* eslint no-param-reassign: 0 */
  8. var p = '/' + this.path.join('/');
  9. if (this.parent) {
  10. acc[p] = {
  11. siblings: this.parent.keys,
  12. key: this.key,
  13. index: this.parent.keys.indexOf(this.key),
  14. };
  15. } else {
  16. acc[p] = {
  17. siblings: [],
  18. key: this.key,
  19. index: -1,
  20. };
  21. }
  22. return acc;
  23. }, {});
  24. t.same(res, {
  25. '/': { siblings: [], key: undefined, index: -1 },
  26. '/a': { siblings: ['a', 'b', 'c'], key: 'a', index: 0 },
  27. '/b': { siblings: ['a', 'b', 'c'], key: 'b', index: 1 },
  28. '/c': { siblings: ['a', 'b', 'c'], key: 'c', index: 2 },
  29. '/c/0': { siblings: ['0', '1', '2'], key: '0', index: 0 },
  30. '/c/1': { siblings: ['0', '1', '2'], key: '1', index: 1 },
  31. '/c/2': { siblings: ['0', '1', '2'], key: '2', index: 2 },
  32. });
  33. t.end();
  34. });