-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlens.js
126 lines (114 loc) · 2.89 KB
/
lens.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"use strict";
// A lens represents a property getter and setter,
// designed to work with immutable data structures.
// It is an object with the following structure:
// {
// get: function(target) {
// return <value-from-target>;
// },
// set: function(target, newValue) {
// return <new-target>;
// }
// }
function compose(outerLens, innerLens) {
return {
get: function(outerValue) {
var innerValue = outerLens.get(outerValue);
return innerLens.get(innerValue);
},
set: function(outerValue, newInnerValue) {
return update(
outerValue,
outerLens,
function(innerTarget) {
return innerLens.set(innerTarget, newInnerValue);
}
);
}
};
}
function update(target, lens, updateFn) {
var currentValue = lens.get(target);
var newValue = updateFn(currentValue);
return lens.set(target, newValue);
}
function composeLenses(/* lens0, lens1, ... */) {
var lenses = arguments[0] instanceof Array ? arguments[0] : arguments;
return Array.prototype.reduceRight.call(
lenses,
function(inner, outer){
return compose(outer, inner);
}
);
}
function prop(key) {
return {
get: function(immutableObject) {
return immutableObject[key];
},
set: function(immutableObject, value) {
return immutableObject.set(key, value);
}
};
}
function arrayItem(index) {
return {
get: function(array) {
return array[index];
},
set: function(array, newItem) {
return array
.slice(0, index)
.concat(newItem)
.concat(array.slice(index + 1));
}
};
}
function build(type, path) {
path = path || [];
if (type instanceof Array) {
return function(arrayIndex) {
var pathToItem = path.concat(arrayItem(arrayIndex));
var arrayItemLens = composeLenses(pathToItem);
var itemType = type[0];
if (itemType) {
// Allow access to child item properties.
// e.g. items(0).text
extend(arrayItemLens, build(itemType, pathToItem));
}
return arrayItemLens;
}
} else if (typeof type === "object") {
var result = {};
Object.keys(type).forEach(function(key) {
var propLens = prop(key);
var pathToProp = path.concat(propLens);
var x = build(type[key], pathToProp);
if (typeof x === "function") {
result[key] = x;
var p = composeLenses(pathToProp);
result[key].get = p.get;
result[key].set = p.set;
} else {
result[key] = composeLenses(pathToProp);
extend(result[key], x);
}
});
return result;
} else {
return null;
}
};
function extend(target, props) {
if (!props) return;
for (var key in props) {
if (props.hasOwnProperty(key)) {
target[key] = props[key];
}
}
}
exports.arrayItem = arrayItem;
exports.prop = prop;
exports.compose = composeLenses;
exports.update = update;
exports.build = build;