Add support for .[0] syntax

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

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

@ -1,22 +1,30 @@
'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 json
}
if ('?' === code) {
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}
}; fn`)
}; fn`).call(json)
const fn = fx.call(json)
if (typeof fn === 'function') {
return fn(json)
}

Loading…
Cancel
Save