forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaths.js
43 lines (37 loc) · 1.21 KB
/
paths.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
describe('paths', function() {
var obj = {
a: {
b: {
c: 1,
d: 2
}
},
p: [{q: 3}, 'Hi'],
x: {
y: 'Alice',
z: [[{}]]
}
};
it('takes paths and returns values at those paths', function() {
eq(R.paths([['a', 'b', 'c'], ['x', 'y']], obj), [1, 'Alice']);
eq(R.paths([['a', 'b', 'd'], ['p', 'q']], obj), [2, undefined]);
});
it('takes a paths that contains indices into arrays', function() {
eq(R.paths([['p', 0, 'q'], ['x', 'z', 0, 0]], obj), [3, {}]);
eq(R.paths([['p', 0, 'q'], ['x', 'z', 2, 1]], obj), [3, undefined]);
});
it('takes a path that contains negative indices into arrays', function() {
eq(R.paths([['p', -2, 'q'], ['p', -1]], obj), [3, 'Hi']);
eq(R.paths([['p', -4, 'q'], ['x', 'z', -1, 0]], obj), [undefined, {}]);
});
it("gets a deep property's value from objects", function() {
eq(R.paths([['a', 'b']], obj), [obj.a.b]);
eq(R.paths([['p', 0]], obj), [obj.p[0]]);
});
it('returns undefined for items not found', function() {
eq(R.paths([['a', 'x', 'y']], obj), [undefined]);
eq(R.paths([['p', 2]], obj), [undefined]);
});
});