forked from VoliJS/NestedLink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
valuelink.js
295 lines (295 loc) · 9.68 KB
/
valuelink.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* Advanced React links for purely functional two-way data binding
*
* MIT License, (c) 2016 Vlad Balin, Volicon.
*/
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
// Main Link class. All links must extend it.
var Link = (function () {
// create
function Link(value) {
this.value = value;
}
// Create link to componen't state
Link.state = function (component, key) {
var value = component.state[key], cache = component.links || (component.links = {}), cached = cache[key];
return cached && cached.value === value ? cached : cache[key] = new StateLink(value, component, key);
};
;
// Ensure that listed links are cached. Return links cache.
Link.all = function (component) {
var state = component.state, links = component.links || (component.links = {});
for (var i = 1; i < arguments.length; i++) {
var key = arguments[i], value = state[key], cached = links[key];
if (!cached || cached.value !== value) {
links[key] = new StateLink(value, component, key);
}
}
return links;
};
// Create custom link to arbitrary value
Link.value = function (value, set) {
return new CustomLink(value, set);
};
Object.defineProperty(Link.prototype, "validationError", {
// DEPRECATED: Old error holder for backward compatibility with Volicon code base
get: function () { return this.error; },
enumerable: true,
configurable: true
});
Link.prototype.onChange = function (handler) {
var _this = this;
return new CloneLink(this, function (x) {
handler(x);
_this.set(x);
});
};
// DEPRECATED: Old React method for backward compatibility
Link.prototype.requestChange = function (x) {
this.set(x);
};
// Immediately update the link value using given transform function.
Link.prototype.update = function (transform, e) {
var next = transform(this.clone(), e);
next === void 0 || this.set(next);
};
// Create new link which applies transform function on set.
Link.prototype.pipe = function (handler) {
var _this = this;
return new CloneLink(this, function (x) {
var next = handler(x);
next === void 0 || _this.set(next);
});
};
// Create UI event handler function which will update the link with a given transform function.
Link.prototype.action = function (transform) {
var _this = this;
return function (e) { return _this.update(transform, e); };
};
Link.prototype.equals = function (truthyValue) {
return new EqualsLink(this, truthyValue);
};
// Array-only links methods
Link.prototype.contains = function (element) {
return new ContainsLink(this, element);
};
Link.prototype.push = function () {
var array = arrayHelpers.clone(this.value);
Array.prototype.push.apply(array, arguments);
this.set(array);
};
Link.prototype.unshift = function () {
var array = arrayHelpers.clone(this.value);
Array.prototype.unshift.apply(array, arguments);
this.set(array);
};
Link.prototype.splice = function () {
var array = arrayHelpers.clone(this.value);
Array.prototype.splice.apply(array, arguments);
this.set(array);
};
// Array and objects universal collection methods
Link.prototype.map = function (iterator) {
return helpers(this.value).map(this, iterator);
};
Link.prototype.remove = function (key) {
var value = this.value, _ = helpers(value);
this.set(_.remove(_.clone(value), key));
};
Link.prototype.at = function (key) {
return new ChainedLink(this, key);
};
Link.prototype.clone = function () {
var value = this.value;
return helpers(value).clone(value);
};
Link.prototype.pick = function () {
var links = {};
for (var i = 0; i < arguments.length; i++) {
var key = arguments[i];
links[key] = new ChainedLink(this, key);
}
return links;
};
/**
* Validate link with validness predicate and optional custom error object. Can be chained.
*/
Link.prototype.check = function (whenValid, error) {
if (!this.error && !whenValid(this.value)) {
this.error = error || whenValid.error || defaultError;
}
return this;
};
return Link;
}());
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = Link;
var CustomLink = (function (_super) {
__extends(CustomLink, _super);
function CustomLink(value, set) {
_super.call(this, value);
this.set = set;
}
CustomLink.prototype.set = function (x) { };
return CustomLink;
}(Link));
exports.CustomLink = CustomLink;
var CloneLink = (function (_super) {
__extends(CloneLink, _super);
function CloneLink(parent, set) {
_super.call(this, parent.value);
this.set = set;
var error = parent.error;
if (error)
this.error = error;
}
CloneLink.prototype.set = function (x) { };
return CloneLink;
}(Link));
exports.CloneLink = CloneLink;
var StateLink = (function (_super) {
__extends(StateLink, _super);
function StateLink(value, component, key) {
_super.call(this, value);
this.component = component;
this.key = key;
}
StateLink.prototype.set = function (x) {
this.component.setState((_a = {}, _a[this.key] = x, _a));
var _a;
};
return StateLink;
}(Link));
exports.StateLink = StateLink;
var EqualsLink = (function (_super) {
__extends(EqualsLink, _super);
function EqualsLink(parent, truthyValue) {
_super.call(this, parent.value === truthyValue);
this.parent = parent;
this.truthyValue = truthyValue;
}
EqualsLink.prototype.set = function (x) {
this.parent.set(x ? this.truthyValue : null);
};
return EqualsLink;
}(Link));
exports.EqualsLink = EqualsLink;
var ContainsLink = (function (_super) {
__extends(ContainsLink, _super);
function ContainsLink(parent, element) {
_super.call(this, parent.value.indexOf(element) >= 0);
this.parent = parent;
this.element = element;
}
ContainsLink.prototype.set = function (x) {
var _this = this;
var next = Boolean(x);
if (this.value !== next) {
var arr = this.parent.value, nextValue = x ? arr.concat(this.element) : arr.filter(function (el) { return el !== _this.element; });
this.parent.set(nextValue);
}
};
return ContainsLink;
}(Link));
exports.ContainsLink = ContainsLink;
var defaultError = 'Invalid value';
/**
* Link to array or object element enclosed in parent link.
* Performs purely functional update of the parent, shallow copying its value on `set`.
*/
var ChainedLink = (function (_super) {
__extends(ChainedLink, _super);
function ChainedLink(parent, key) {
_super.call(this, parent.value[key]);
this.parent = parent;
this.key = key;
}
ChainedLink.prototype.remove = function (key) {
if (key === void 0) {
this.parent.remove(this.key);
}
else {
_super.prototype.remove.call(this, key);
}
};
// Set new element value to parent array or object, performing purely functional update.
ChainedLink.prototype.set = function (x) {
var _this = this;
if (this.value !== x) {
this.parent.update(function (value) {
value[_this.key] = x;
return value;
});
}
};
;
return ChainedLink;
}(Link));
exports.ChainedLink = ChainedLink;
var ArrayProto = Array.prototype, ObjectProto = Object.prototype;
function helpers(value) {
if (value && typeof value === 'object') {
switch (Object.getPrototypeOf(value)) {
case ArrayProto: return arrayHelpers;
case ObjectProto: return objectHelpers;
}
}
return dummyHelpers;
}
// Do nothing for types other than Array and plain Object.
var dummyHelpers = {
clone: function (value) { return value; },
map: function (link, fun) { return []; },
remove: function (value) { return value; }
};
// `map` and `clone` for plain JS objects
var objectHelpers = {
// Map through the link to object
map: function (link, iterator) {
var hash = link.value;
var mapped = [];
for (var key in hash) {
var element = iterator(link.at(key), key);
element === void 0 || (mapped.push(element));
}
return mapped;
},
remove: function (object, key) {
delete object[key];
return object;
},
// Shallow clone plain JS object
clone: function (object) {
var cloned = {};
for (var key in object) {
cloned[key] = object[key];
}
return cloned;
}
};
// `map` and `clone` helpers for arrays.
var arrayHelpers = {
// Shallow clone array
clone: function (array) {
return array.slice();
},
remove: function (array, i) {
array.splice(i, 1);
return array;
},
// Map through the link to array
map: function (link, iterator) {
var mapped = [], array = link.value;
for (var i = 0; i < array.length; i++) {
var y = iterator(link.at(i), i);
y === void 0 || (mapped.push(y));
}
return mapped;
}
};
//# sourceMappingURL=valuelink.js.map