-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcompiler.js
More file actions
67 lines (53 loc) · 1.41 KB
/
compiler.js
File metadata and controls
67 lines (53 loc) · 1.41 KB
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
var { Cache, normalizePath, split, forEach } = require('./')
var setCache = new Cache(512),
getCache = new Cache(512)
function makeSafe(path, param) {
var result = param,
parts = split(path),
isLast
forEach(parts, function (part, isBracket, isArray, idx, parts) {
isLast = idx === parts.length - 1
part = isBracket || isArray ? '[' + part + ']' : '.' + part
result += part + (!isLast ? ' || {})' : ')')
})
return new Array(parts.length + 1).join('(') + result
}
function expr(expression, safe, param) {
expression = expression || ''
if (typeof safe === 'string') {
param = safe
safe = false
}
param = param || 'data'
if (expression && expression.charAt(0) !== '[') expression = '.' + expression
return safe ? makeSafe(expression, param) : param + expression
}
module.exports = {
expr,
setter: function (path) {
if (
path.indexOf('__proto__') !== -1 ||
path.indexOf('constructor') !== -1 ||
path.indexOf('prototype') !== -1
) {
return (obj) => obj
}
return (
setCache.get(path) ||
setCache.set(
path,
new Function('data, value', expr(path, 'data') + ' = value')
)
)
},
getter: function (path, safe) {
var key = path + '_' + safe
return (
getCache.get(key) ||
getCache.set(
key,
new Function('data', 'return ' + expr(path, safe, 'data'))
)
)
},
}