Add keys() and values()

This commit is contained in:
Anton Medvedev 2023-09-26 21:32:49 +02:00
parent c861b448fd
commit 6181342e08
No known key found for this signature in database

View File

@ -82,9 +82,6 @@ async function transform(json, code) {
if ('.' === code) if ('.' === code)
return json return json
if ('?' === code)
return Object.keys(json)
if (/^(\.\w*)+\[]/.test(code)) if (/^(\.\w*)+\[]/.test(code))
return eval(`(function () { return eval(`(function () {
return (${fold(code.split('[]'))})(this) return (${fold(code.split('[]'))})(this)
@ -100,15 +97,17 @@ async function transform(json, code) {
if (/^\.\[/.test(code)) if (/^\.\[/.test(code))
return eval(`(function () { return eval(`(function () {
const x = this
return this${code.substring(1)} return this${code.substring(1)}
})`).call(json) })`).call(json)
if (/^\./.test(code)) if (/^\./.test(code))
return eval(`(function () { return eval(`(function () {
const x = this
return this${code} return this${code}
})`).call(json) })`).call(json)
if (/^[a-z]+\(.+?\)$/i.test(code)) { if (/^map\(.+?\)$/i.test(code)) {
let s = code.substring(4, code.length - 1) let s = code.substring(4, code.length - 1)
if (s[0] === '.') s = 'x' + s if (s[0] === '.') s = 'x' + s
return eval(`(function () { return eval(`(function () {
@ -121,13 +120,13 @@ async function transform(json, code) {
return ${code} return ${code}
})`).call(json) })`).call(json)
return apply(fn, json)
function apply(fn, ...args) { function apply(fn, ...args) {
if (typeof fn === 'function') return fn(...args) if (typeof fn === 'function') return fn(...args)
return fn return fn
} }
return apply(fn, json)
function len(x) { function len(x) {
if (Array.isArray(x)) return x.length if (Array.isArray(x)) return x.length
if (typeof x === 'string') return x.length if (typeof x === 'string') return x.length
@ -145,11 +144,18 @@ async function transform(json, code) {
throw new Error(`Cannot sort ${typeof x}`) throw new Error(`Cannot sort ${typeof x}`)
} }
function sortBy(func) { function map(fn) {
return function (x) {
if (Array.isArray(x)) return x.map((v, i) => fn(v, i))
throw new Error(`Cannot map ${typeof x}`)
}
}
function sortBy(fn) {
return function (x) { return function (x) {
if (Array.isArray(x)) return x.sort((a, b) => { if (Array.isArray(x)) return x.sort((a, b) => {
const fa = func(a) const fa = fn(a)
const fb = func(b) const fb = fn(b)
return fa < fb ? -1 : fa > fb ? 1 : 0 return fa < fb ? -1 : fa > fb ? 1 : 0
}) })
throw new Error(`Cannot sort ${typeof x}`) throw new Error(`Cannot sort ${typeof x}`)
@ -160,11 +166,8 @@ async function transform(json, code) {
return function (x) { return function (x) {
const grouped = {} const grouped = {}
for (const item of x) { for (const item of x) {
const key = typeof keyFn === 'function' const key = typeof keyFn === 'function' ? keyFn(item) : item[keyFn]
? keyFn(item) if (!grouped.hasOwnProperty(key)) grouped[key] = []
: item[keyFn]
if (!grouped.hasOwnProperty(key))
grouped[key] = []
grouped[key].push(item) grouped[key].push(item)
} }
return grouped return grouped
@ -200,6 +203,16 @@ async function transform(json, code) {
if (Array.isArray(x)) return x.reverse() if (Array.isArray(x)) return x.reverse()
throw new Error(`Cannot reverse ${typeof x}`) throw new Error(`Cannot reverse ${typeof x}`)
} }
function keys(x) {
if (typeof x === 'object' && x !== null) return Object.keys(x)
throw new Error(`Cannot get keys of ${typeof x}`)
}
function values(x) {
if (typeof x === 'object' && x !== null) return Object.values(x)
throw new Error(`Cannot get values of ${typeof x}`)
}
} }
async function read(fd = 0) { async function read(fd = 0) {