-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (54 loc) · 1.16 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
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
var path = "f8fd710e-05cb-46b9-a711-e3ad7c342064";
function id(v) {
return v;
}
function compileValue(v) {
if (v === yassium) {
return id;
}
if (v && v[path]) {
return function compiled(obj) {
var r = obj;
var i = 0;
while (i < v[path].length) {
r = r[v[path][i++]];
}
return r;
};
}
return v;
}
function yassium(strings) {
var values = Array.prototype.splice.call(arguments, 1);
var compiledValues = values.map(compileValue);
return function yassiumRender(obj) {
var result = [strings[0]];
var expr;
for (var i = 0; i < compiledValues.length; i++) {
expr = compiledValues[i];
result.push(String(expr(obj)), strings[i + 1]);
}
expr = null;
return result.join("");
};
}
Object.defineProperty(yassium, path, {
value: [],
enumerable: false,
});
function proxyGet(obj, prop) {
if (prop === path) {
return obj[path];
}
var target = {};
Object.defineProperty(target, path, {
value: obj[path].concat(prop),
enumerable: false,
});
return new Proxy(target, {
get: proxyGet,
});
}
module.exports = new Proxy(yassium, {
get: proxyGet,
});