mirror of https://github.com/antonmedv/fx
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.1 KiB
JavaScript
92 lines
2.1 KiB
JavaScript
3 months ago
|
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
|
||
2 months ago
|
throw new Error(`Cannot get length of ${typeof x}`)
|
||
3 months ago
|
}
|
||
|
|
||
|
function uniq(x) {
|
||
|
if (Array.isArray(x)) return [...new Set(x)]
|
||
2 months ago
|
throw new Error(`Cannot get unique values of ${typeof x}`)
|
||
3 months ago
|
}
|
||
|
|
||
|
function sort(x) {
|
||
|
if (Array.isArray(x)) return x.sort()
|
||
2 months ago
|
throw new Error(`Cannot sort ${typeof x}`)
|
||
3 months ago
|
}
|
||
|
|
||
|
function map(fn) {
|
||
|
return function (x) {
|
||
|
if (Array.isArray(x)) return x.map((v, i) => fn(v, i))
|
||
2 months ago
|
throw new Error(`Cannot map ${typeof x}`)
|
||
3 months ago
|
}
|
||
|
}
|
||
|
|
||
|
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
|
||
|
})
|
||
2 months ago
|
throw new Error(`Cannot sort ${typeof x}`)
|
||
3 months ago
|
}
|
||
|
}
|
||
|
|
||
|
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()
|
||
2 months ago
|
throw new Error(`Cannot flatten ${typeof x}`)
|
||
3 months ago
|
}
|
||
|
|
||
|
function reverse(x) {
|
||
|
if (Array.isArray(x)) return x.reverse()
|
||
2 months ago
|
throw new Error(`Cannot reverse ${typeof x}`)
|
||
3 months ago
|
}
|
||
|
|
||
|
function keys(x) {
|
||
|
if (typeof x === 'object' && x !== null) return Object.keys(x)
|
||
2 months ago
|
throw new Error(`Cannot get keys of ${typeof x}`)
|
||
3 months ago
|
}
|
||
|
|
||
|
function values(x) {
|
||
|
if (typeof x === 'object' && x !== null) return Object.values(x)
|
||
2 months ago
|
throw new Error(`Cannot get values of ${typeof x}`)
|
||
3 months ago
|
}
|