Ai
1 Star 0 Fork 0

子安/fx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
reduce.js 679 Bytes
一键复制 编辑 原始数据 按行查看 历史
Anton Medvedev 提交于 2018-12-08 00:33 +08:00 . Add better detection of functions in reduce
'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
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/andrewgithub/fx.git
git@gitee.com:andrewgithub/fx.git
andrewgithub
fx
fx
master

搜索帮助