fx/internal/engine/stdlib.js

92 lines
2.1 KiB
JavaScript
Raw Normal View History

2024-03-13 22:16:23 +00:00
function apply(fn, ...args) {
if (typeof fn === 'function') return fn(...args)
return fn
}
function len(x) {
if (Array.isArray(x)) return x.length
if (typeof x === 'string') return x.length
if (typeof x === 'object' && x !== null) return Object.keys(x).length
2024-03-21 19:48:09 +00:00
throw new Error(`Cannot get length of ${typeof x}`)
2024-03-13 22:16:23 +00:00
}
function uniq(x) {
if (Array.isArray(x)) return [...new Set(x)]
2024-03-21 19:48:09 +00:00
throw new Error(`Cannot get unique values of ${typeof x}`)
2024-03-13 22:16:23 +00:00
}
function sort(x) {
if (Array.isArray(x)) return x.sort()
2024-03-21 19:48:09 +00:00
throw new Error(`Cannot sort ${typeof x}`)
2024-03-13 22:16:23 +00:00
}
function map(fn) {
return function (x) {
if (Array.isArray(x)) return x.map((v, i) => fn(v, i))
2024-03-21 19:48:09 +00:00
throw new Error(`Cannot map ${typeof x}`)
2024-03-13 22:16:23 +00:00
}
}
function sortBy(fn) {
return function (x) {
if (Array.isArray(x)) return x.sort((a, b) => {
const fa = fn(a)
const fb = fn(b)
return fa < fb ? -1 : fa > fb ? 1 : 0
})
2024-03-21 19:48:09 +00:00
throw new Error(`Cannot sort ${typeof x}`)
2024-03-13 22:16:23 +00:00
}
}
function groupBy(keyFn) {
return function (x) {
const grouped = {}
for (const item of x) {
const key = typeof keyFn === 'function' ? keyFn(item) : item[keyFn]
if (!grouped.hasOwnProperty(key)) grouped[key] = []
grouped[key].push(item)
}
return grouped
}
}
function chunk(size) {
return function (x) {
const res = []
let i = 0
while (i < x.length) {
res.push(x.slice(i, i += size))
}
return res
}
}
function zip(...x) {
const length = Math.min(...x.map(a => a.length))
const res = []
for (let i = 0; i < length; i++) {
res.push(x.map(a => a[i]))
}
return res
}
function flatten(x) {
if (Array.isArray(x)) return x.flat()
2024-03-21 19:48:09 +00:00
throw new Error(`Cannot flatten ${typeof x}`)
2024-03-13 22:16:23 +00:00
}
function reverse(x) {
if (Array.isArray(x)) return x.reverse()
2024-03-21 19:48:09 +00:00
throw new Error(`Cannot reverse ${typeof x}`)
2024-03-13 22:16:23 +00:00
}
function keys(x) {
if (typeof x === 'object' && x !== null) return Object.keys(x)
2024-03-21 19:48:09 +00:00
throw new Error(`Cannot get keys of ${typeof x}`)
2024-03-13 22:16:23 +00:00
}
function values(x) {
if (typeof x === 'object' && x !== null) return Object.values(x)
2024-03-21 19:48:09 +00:00
throw new Error(`Cannot get values of ${typeof x}`)
2024-03-13 22:16:23 +00:00
}