fn helper fixes

This commit is contained in:
Rowan 2024-11-22 05:51:22 -06:00
parent f0f607c2af
commit 808952646c

View file

@ -26,7 +26,7 @@ export const always = x => () => x
export const orDefault = curry((value, defaultValue) => value || defaultValue) export const orDefault = curry((value, defaultValue) => value || defaultValue)
export const orElse = curry((value, fn) => orDefault(value, fn())) export const orElse = curry((value, fn) => orDefault(value, fn()))
export const pipe = (...f) => v => reduce(applyTo, v, f) export const pipe = (...f) => v => reduce(applyTo, v, f)
export const apply = fn => args => fn.apply(null, args) export const apply = curry((fn, args) => fn.apply(null, args))
export const applyTo = curry((v, fn) => fn(v)) export const applyTo = curry((v, fn) => fn(v))
export const useWith = curry((fn, tfns) => export const useWith = curry((fn, tfns) =>
curryN( curryN(
@ -35,8 +35,8 @@ export const useWith = curry((fn, tfns) =>
)) ))
export const converge = (fn, tfns) => curryN( export const converge = (fn, tfns) => curryN(
pipe(length, map, max)(tfns), pipe(map(length), max)(tfns),
(...args) => fn(...map(tfn => tfn(...args), tfns)) (...args) => pipe(map(flip(apply)(args)), apply(fn))(tfns)
) )
export const diverge = (fn, tfns) => pipe(applyTo, flip(map)(tfns), apply(fn)) export const diverge = (fn, tfns) => pipe(applyTo, flip(map)(tfns), apply(fn))
@ -67,8 +67,12 @@ export const split = curry((delim, v) => v.split(delim))
// math // math
export const max = v => Math.max(...v) export const max = v => Math.max(...v)
export const min = v => Math.min(...v) export const min = v => Math.min(...v)
export const inc = v => v + 1 export const add = curry((x, y) => x + y)
export const dec = v => v - 1 export const sub = curry((x, y) => x - y)
export const mul = curry((x, y) => x * y)
export const div = curry((x, y) => x / y)
export const inc = add(1)
export const dec = sub(1)
// arrays // arrays
export const map = curry((fn, v) => v.map(fn)) export const map = curry((fn, v) => v.map(fn))
@ -92,6 +96,7 @@ export const append = curry((value, iter) => [...iter, value])
export const enumerate = value => value.map((v, i) => ([i, v])) export const enumerate = value => value.map((v, i) => ([i, v]))
export const flatten = curry((n, v) => v.flat(n)) export const flatten = curry((n, v) => v.flat(n))
export const update = curry((index, value, arr) => arr.toSpliced(index, 1, value)) export const update = curry((index, value, arr) => arr.toSpliced(index, 1, value))
export const sum = reduce(add, 0)
// objects // objects
const makePath = unless(isArray, split('.')) const makePath = unless(isArray, split('.'))
@ -103,18 +108,18 @@ export const assocPath = curry((key, value, obj) => pipe(
mergeRightDeep(obj), mergeRightDeep(obj),
)(key)) )(key))
export const mergeLeftDeep = curry((a, b) => export const mergeRightDeep = curry((a, b) =>
reduce((acc, key) => pipe( reduce((acc, key) => pipe(
ifElse( ifElse(
every(is(Object)), every(is(Object)),
apply(mergeLeftDeep), apply(mergeRightDeep),
last last
), ),
v => assoc(key, v, acc) v => assoc(key, v, acc)
)([acc[key], b[key]]), a, keys(b)) )([acc[key], b[key]]), a, keys(b))
) )
export const mergeRightDeep = flip(mergeLeftDeep) export const mergeLeftDeep = flip(mergeRightDeep)
export const prop = curry((key, obj) => obj && key && obj[key]) export const prop = curry((key, obj) => obj && key && obj[key])
export const path = curry((key, obj) => reduce(flip(prop), obj, makePath(key))) export const path = curry((key, obj) => reduce(flip(prop), obj, makePath(key)))