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