Add more functions

This commit is contained in:
Anton Medvedev 2023-09-25 17:24:15 +02:00
parent 8caeacff62
commit 85f07837f3
No known key found for this signature in database

View File

@ -90,6 +90,14 @@ async function transform(json, code) {
return (${fold(code.split('[]'))})(this) return (${fold(code.split('[]'))})(this)
})`).call(json) })`).call(json)
function fold(s) {
if (s.length === 1)
return 'x => x' + s[0]
let obj = s.shift()
obj = obj === '.' ? 'x' : 'x' + obj
return `x => Object.values(${obj}).flatMap(${fold(s)})`
}
if (/^\.\[/.test(code)) if (/^\.\[/.test(code))
return eval(`(function () { return eval(`(function () {
return this${code.substring(1)} return this${code.substring(1)}
@ -100,7 +108,7 @@ async function transform(json, code) {
return this${code} return this${code}
})`).call(json) })`).call(json)
if (/^map\(.+?\)$/.test(code)) { if (/^[a-z]+\(.+?\)$/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 () {
@ -113,40 +121,48 @@ 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
} }
function fold(s) { return apply(fn, json)
if (s.length === 1)
return 'x => x' + s[0] function len(x) {
let obj = s.shift() if (Array.isArray(x)) return x.length
obj = obj === '.' ? 'x' : 'x' + obj if (typeof x === 'string') return x.length
return `x => Object.values(${obj}).flatMap(${fold(s)})` if (typeof x === 'object' && x !== null) return Object.keys(x).length
throw new Error(`Cannot get length of ${typeof x}`)
} }
function len(array) { function uniq(x) {
return array.length if (Array.isArray(x)) return [...new Set(x)]
throw new Error(`Cannot get unique values of ${typeof x}`)
} }
function uniq(array) { function sort(x) {
return [...new Set(array)] if (Array.isArray(x)) return x.sort()
throw new Error(`Cannot sort ${typeof x}`)
} }
function sort(array) { function sortBy(func) {
return array.sort() return function (x) {
if (Array.isArray(x)) return x.sort((a, b) => {
const fa = func(a)
const fb = func(b)
return fa < fb ? -1 : fa > fb ? 1 : 0
})
throw new Error(`Cannot sort ${typeof x}`)
}
} }
function groupBy(keyOrFunction) { function groupBy(keyFn) {
return array => { return function (x) {
const grouped = {} const grouped = {}
for (const item of array) { for (const item of x) {
const key = typeof keyOrFunction === 'function' const key = typeof keyFn === 'function'
? keyOrFunction(item) ? keyFn(item)
: item[keyOrFunction] : item[keyFn]
if (!grouped.hasOwnProperty(key)) if (!grouped.hasOwnProperty(key))
grouped[key] = [] grouped[key] = []
grouped[key].push(item) grouped[key].push(item)
@ -156,24 +172,34 @@ async function transform(json, code) {
} }
function chunk(size) { function chunk(size) {
return function (arr) { return function (x) {
const res = [] const res = []
let i = 0 let i = 0
while (i < arr.length) { while (i < x.length) {
res.push(arr.slice(i, i += size)) res.push(x.slice(i, i += size))
} }
return res return res
} }
} }
function zip(...arrays) { function zip(...x) {
const length = Math.min(...arrays.map(a => a.length)) const length = Math.min(...x.map(a => a.length))
const res = [] const res = []
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {
res.push(arrays.map(a => a[i])) res.push(x.map(a => a[i]))
} }
return res return res
} }
function flatten(x) {
if (Array.isArray(x)) return x.flat()
throw new Error(`Cannot flatten ${typeof x}`)
}
function reverse(x) {
if (Array.isArray(x)) return x.reverse()
throw new Error(`Cannot reverse ${typeof x}`)
}
} }
async function read(fd = 0) { async function read(fd = 0) {