fx/reduce.js

37 lines
682 B
JavaScript
Raw Normal View History

'use strict'
function reduce(json, code) {
if (/^\w+\s*=>/.test(code)) {
const fx = eval(code)
return fx(json)
}
if (/yield/.test(code)) {
2018-12-01 17:29:44 +00:00
const fx = eval(`function fn() {
const gen = (function*(){
${code.replace(/\\\n/g, '')}
}).call(this)
return [...gen]
}; fn`)
return fx.call(json)
}
if ('?' === code) {
return Object.keys(json)
}
if (/^\./.test(code)) {
2018-12-01 17:29:44 +00:00
const fx = eval(`function fn() {
return ${code === '.' ? 'this' : 'this' + code}
}; fn`)
return fx.call(json)
}
2018-12-01 17:29:44 +00:00
const fx = eval(`function fn() {
return ${code}
}; fn`)
return fx.call(json)
}
module.exports = reduce