mirror of
https://github.com/antonmedv/fx
synced 2024-11-09 07:10:38 +00:00
37 lines
679 B
JavaScript
37 lines
679 B
JavaScript
'use strict'
|
|
|
|
function reduce(json, code) {
|
|
if (/^\./.test(code)) {
|
|
const fx = eval(`function fn() {
|
|
return ${code === '.' ? 'this' : 'this' + code}
|
|
}; fn`)
|
|
return fx.call(json)
|
|
}
|
|
|
|
if ('?' === code) {
|
|
return Object.keys(json)
|
|
}
|
|
|
|
if (/yield\*?\s/.test(code)) {
|
|
const fx = eval(`function fn() {
|
|
const gen = (function*(){
|
|
${code.replace(/\\\n/g, '')}
|
|
}).call(this)
|
|
return [...gen]
|
|
}; fn`)
|
|
return fx.call(json)
|
|
}
|
|
|
|
const fx = eval(`function fn() {
|
|
return ${code}
|
|
}; fn`)
|
|
|
|
const fn = fx.call(json)
|
|
if (typeof fn === 'function') {
|
|
return fn(json)
|
|
}
|
|
return fn
|
|
}
|
|
|
|
module.exports = reduce
|