Add support for .[0] syntax

js-version
Anton Medvedev 5 years ago
parent ed6130907e
commit 290c2a0185

@ -46,7 +46,7 @@
"node": ">=8" "node": ">=8"
}, },
"dependencies": { "dependencies": {
"@medv/blessed": "^2.0.0", "@medv/blessed": "^2.0.1",
"chalk": "^3.0.0", "chalk": "^3.0.0",
"indent-string": "^4.0.0", "indent-string": "^4.0.0",
"lossless-json": "^1.0.3", "lossless-json": "^1.0.3",
@ -54,7 +54,7 @@
}, },
"devDependencies": { "devDependencies": {
"ava": "^2.4.0", "ava": "^2.4.0",
"pkg": "^4.4.1", "pkg": "^4.4.2",
"release-it": "^12.4.3" "release-it": "^12.4.3"
} }
} }

@ -1,22 +1,30 @@
'use strict' 'use strict'
function reduce(json, code) { function reduce(json, code) {
if (/^\./.test(code)) { if ('.' === code) {
const fx = eval(`function fn() { return json
return ${code === '.' ? 'this' : 'this' + code}
}; fn`)
return fx.call(json)
} }
if ('?' === code) { if ('?' === code) {
return Object.keys(json) return Object.keys(json)
} }
const fx = eval(`function fn() { 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} return ${code}
}; fn`) }; fn`).call(json)
const fn = fx.call(json)
if (typeof fn === 'function') { if (typeof fn === 'function') {
return fn(json) return fn(json)
} }

Loading…
Cancel
Save