UNPKG

1.18 kBJavaScriptView Raw
1var assert = require('assert');
2var traverse = require('../');
3
4exports.siblings = function () {
5 var obj = { a : 1, b : 2, c : [ 4, 5, 6 ] };
6
7 var res = traverse(obj).reduce(function (acc, x) {
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 }
16 else {
17 acc[p] = {
18 siblings : [],
19 key : this.key,
20 index : -1
21 }
22 }
23 return acc;
24 }, {});
25
26 assert.deepEqual(res, {
27 '/' : { siblings : [], key : undefined, index : -1 },
28 '/a' : { siblings : [ 'a', 'b', 'c' ], key : 'a', index : 0 },
29 '/b' : { siblings : [ 'a', 'b', 'c' ], key : 'b', index : 1 },
30 '/c' : { siblings : [ 'a', 'b', 'c' ], key : 'c', index : 2 },
31 '/c/0' : { siblings : [ '0', '1', '2' ], key : '0', index : 0 },
32 '/c/1' : { siblings : [ '0', '1', '2' ], key : '1', index : 1 },
33 '/c/2' : { siblings : [ '0', '1', '2' ], key : '2', index : 2 }
34 });
35};