forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlenses.js
75 lines (55 loc) · 2.04 KB
/
lenses.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var they = it;
var alice = {
name: 'Alice Jones',
address: ['22 Walnut St', 'San Francisco', 'CA'],
pets: {dog: 'joker', cat: 'batman'}
};
var nameLens = R.lens(R.prop('name'), R.assoc('name'));
var addressLens = R.lensProp('address');
var headLens = R.lensIndex(0);
var dogLens = R.lensPath(['pets', 'dog']);
describe('view, over, and set', function() {
they('may be applied to a lens created by `lensPath`', function() {
eq(R.view(dogLens, alice), 'joker');
});
they('may be applied to a lens created by `lensProp`', function() {
eq(R.view(nameLens, alice), 'Alice Jones');
eq(R.over(nameLens, R.toUpper, alice), {
name: 'ALICE JONES',
address: ['22 Walnut St', 'San Francisco', 'CA'],
pets: {dog: 'joker', cat: 'batman'}
});
eq(R.set(nameLens, 'Alice Smith', alice), {
name: 'Alice Smith',
address: ['22 Walnut St', 'San Francisco', 'CA'],
pets: {dog: 'joker', cat: 'batman'}
});
});
they('may be applied to a lens created by `lensIndex`', function() {
eq(R.view(headLens, alice.address), '22 Walnut St');
eq(R.over(headLens, R.toUpper, alice.address),
['22 WALNUT ST', 'San Francisco', 'CA']
);
eq(R.set(headLens, '52 Crane Ave', alice.address),
['52 Crane Ave', 'San Francisco', 'CA']
);
});
they('may be applied to composed lenses', function() {
var streetLens = R.compose(addressLens, headLens);
var dogLens = R.compose(R.lensPath(['pets']), R.lensPath(['dog']));
eq(R.view(dogLens, alice), R.view(R.lensPath(['pets', 'dog']), alice));
eq(R.view(streetLens, alice), '22 Walnut St');
eq(R.over(streetLens, R.toUpper, alice), {
name: 'Alice Jones',
address: ['22 WALNUT ST', 'San Francisco', 'CA'],
pets: {dog: 'joker', cat: 'batman'}
});
eq(R.set(streetLens, '52 Crane Ave', alice), {
name: 'Alice Jones',
address: ['52 Crane Ave', 'San Francisco', 'CA'],
pets: {dog: 'joker', cat: 'batman'}
});
});
});