2018-11-09 18:57:24 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
function reduce(json, code) {
|
2018-12-07 16:33:55 +00:00
|
|
|
if (/^\./.test(code)) {
|
|
|
|
const fx = eval(`function fn() {
|
|
|
|
return ${code === '.' ? 'this' : 'this' + code}
|
|
|
|
}; fn`)
|
|
|
|
return fx.call(json)
|
2018-11-09 18:57:24 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 16:33:55 +00:00
|
|
|
if ('?' === code) {
|
|
|
|
return Object.keys(json)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (/yield\*?\s/.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`)
|
2018-11-09 18:57:24 +00:00
|
|
|
return fx.call(json)
|
|
|
|
}
|
|
|
|
|
2018-12-01 17:29:44 +00:00
|
|
|
const fx = eval(`function fn() {
|
|
|
|
return ${code}
|
|
|
|
}; fn`)
|
2018-12-07 16:33:55 +00:00
|
|
|
|
|
|
|
const fn = fx.call(json)
|
|
|
|
if (typeof fn === 'function') {
|
|
|
|
return fn(json)
|
|
|
|
}
|
|
|
|
return fn
|
2018-11-09 18:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = reduce
|