forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.js
61 lines (53 loc) · 1.81 KB
/
path.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
describe('path', function() {
var deepObject = {a: {b: {c: 'c'}}, falseVal: false, nullVal: null, undefinedVal: undefined, arrayVal: ['arr']};
it('takes a path and an object and returns the value at the path or undefined', function() {
var obj = {
a: {
b: {
c: 100,
d: 200
},
e: {
f: [100, 101, 102],
g: 'G'
},
h: 'H'
},
i: 'I',
j: ['J']
};
eq(R.path(['a', 'b', 'c'], obj), 100);
eq(R.path([], obj), obj);
eq(R.path(['a', 'e', 'f', 1], obj), 101);
eq(R.path(['j', 0], obj), 'J');
eq(R.path(['j', 1], obj), undefined);
});
it('takes a path that contains indices into arrays', function() {
var obj = {
a: [[{}], [{x: 'first'}, {x: 'second'}, {x: 'third'}, {x: 'last'}]]
};
eq(R.path(['a', 0, 0], obj), {});
eq(R.path(['a', 0, 1], obj), undefined);
eq(R.path(['a', 1, 0, 'x'], obj), 'first');
eq(R.path(['a', 1, 1, 'x'], obj), 'second');
eq(R.path([0], ['A']), 'A');
});
it('takes a path that contains negative indices into arrays', function() {
eq(R.path(['x', -2], {x: ['a', 'b', 'c', 'd']}), 'c');
eq(R.path([-1, 'y'], [{x: 1, y: 99}, {x: 2, y: 98}, {x: 3, y: 97}]), 97);
});
it("gets a deep property's value from objects", function() {
eq(R.path(['a', 'b', 'c'], deepObject), 'c');
eq(R.path(['a'], deepObject), deepObject.a);
});
it('returns undefined for items not found', function() {
eq(R.path(['a', 'b', 'foo'], deepObject), undefined);
eq(R.path(['bar'], deepObject), undefined);
eq(R.path(['a', 'b'], {a: null}), undefined);
});
it('works with falsy items', function() {
eq(R.path(['toString'], false), Boolean.prototype.toString);
});
});