-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (37 loc) · 1.26 KB
/
index.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
/*
created by Jon Lasley
https://github.com/jclasley/computed-props
*/
const compute = function(computedObj) {
return function(staticObj) {
let source = {...staticObj};
const recurseProxies = function(source, props) {
if (!props.length) { return source; }
const prop = props[0][0];
const cb = props[0][1].bind(source);
// set the source prop so it exists after execution, otherwise only exists on lookup
source[prop] = cb.call(source);
let handler = {
get: function(target, property, receiver) {
if (property === prop) {
target[prop] = cb();
delete receiver[prop]; // prevent from performing actions on itself
return target[prop] = cb();
}
return Reflect.get(...arguments);
},
set: function(target, property, value, receiver) {
if (property === prop) {
console.log(`Attempted to override ${cb} with ${value} -- prevented`);
return true;
}
return Reflect.set(...arguments);
}
}
source = new Proxy(source, handler);
return recurseProxies(source, props.slice(1));
}
return recurseProxies(source, Object.entries(computedObj));
}
}
module.exports = compute;