Compose
Install
npm install @psxcode/compose
Usage
compose
/ composeAsync
Sequential function composition, passing return value of the previous function as an argument to the next one. Functions are being invoked in reverse order.
import { compose } from '@psxcode/compose'
const add4 = (a: number) => a + 4
const mult2 = (a: number) => a * 2
const comp = compose(mult2, add4)
comp(2)
composeAsync
accepts both sync
and async
functions
import { composeAsync } from '@psxcode/compose'
const add4Async = async (arg: number) => arg + 4
const mult2 = (a: number) => a * 2
const comp = composeAsync(mult2, add4Async)
await comp(2)
The types are properly preserved
compose(
(val: any) => `${val}`,
(val: number) => val * 2
)
compose(
(val: number) => val % 2 === 0,
compose(
(val: number[]) => val[0] || 0,
(val: string[]) => val.map(v => v.length)
)
)
pipe
/ pipeAsync
Sequential function composition, passing return value of the previous function as an argument to the next one. Functions are being invoked in direct order.
import { pipe } from '@psxcode/compose'
const add4 = (a: number) => a + 4
const mult2 = (a: number) => a * 2
const comp = pipe(mult2, add4)
comp(2)
pipeAsync
accepts both sync
and async
functions.
import { pipeAsync } from '@psxcode/compose'
const add4 = (a: number) => a + 4
const mult2 = (a: number) => a * 2
const comp = pipeAsync(mult2, add4)
await comp(2)
all
/ allAsync
Parallel function composition, passing the initial value to all functions, and returning an array of results.
import { all } from '@psxcode/compose'
const add4 = (a: number) => a + 4
const mult2 = (a: number) => a * 2
const toString = (a: number) => `${a}`
const comp = all(mult2, add4, toString)
comp(2)
allAsync
accepts both sync
and async
functions
import { allAsync } from '@psxcode/compose'
const add4 = (a: number) => a + 4
const mult2 = async (a: number) => a * 2
const toString = (a: number) => `${a}`
const comp = allAsync(mult2, add4, toString)
await comp(2)