forked from VimCommando/fx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reduce.js
44 lines (35 loc) · 801 Bytes
/
reduce.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
'use strict'
function reduce(json, code) {
if (process.env.FX_APPLY) {
return global[process.env.FX_APPLY](code)(json)
}
if ('.' === code) {
return json
}
if ('?' === code) {
return Object.keys(json)
}
if (/^@/.test(code)) {
return eval(`function fn() {
return Object.values(this).map(x => x${code.substring(1)})
}; fn`).call(json)
}
if (/^\.\[/.test(code)) {
return eval(`function fn() {
return this${code.substring(1)}
}; fn`).call(json)
}
if (/^\./.test(code)) {
return eval(`function fn() {
return this${code}
}; fn`).call(json)
}
const fn = eval(`function fn() {
return ${code}
}; fn`).call(json)
if (typeof fn === 'function') {
return fn(json)
}
return fn
}
module.exports = reduce