-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ariana.js
57 lines (43 loc) · 1.49 KB
/
Ariana.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
"use strict";
const kimiNoNaWa = "Ariana";
//Type checking
const isObj = arg =>
arg != null && typeof arg === "object" && !Array.isArray(arg);
const isFun = arg => arg != null && typeof arg === "function";
const isStr = arg => arg != null && typeof arg === "string";
const msg = `${kimiNoNaWa} : the 'callbacks object' must be an Object with a function as value for each key.`;
const msg2 = `${kimiNoNaWa} : wtf : a wrapper is a read-only object!`;
//Proxy
const proxyfy = wrapper =>
new Proxy(wrapper, {
get: function(target, prop) {
if (prop === "child") return target.child;
if (prop === "parent") return target.parent;
if (prop === "val") return target.val;
if (prop === "eject") return target.eject;
return prop in target.val ? target.val[prop] : undefined;
},
set: function(target, name, value) {
throw msg2;
}
});
//Wrapper
class Wrapper {
constructor(callbacksObject, parent) {
if (!isObj(callbacksObject)) throw msg;
this.val = Object.assign({}, callbacksObject); //internal shallow copy
Object.keys(this.val).forEach(k => {
if (!(isStr(k) && isFun(this.val[k]))) throw msg;
});
this.parent = parent;
Object.freeze(this.val); //immutable internal callbacks handler!
Object.freeze(this); //immutable properties of this!
}
child(callbacksObject) {
return proxyfy(new Wrapper(callbacksObject, this));
}
eject() {
return Object.assign({}, this.val);
}
}
export default o => proxyfy(new Wrapper(o));