npm install @psxcode/compose
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'constadd4=(a: number)=>a+4constmult2=(a: number)=>a*2constcomp=compose(mult2,add4)comp(2)// (2 + 4) * 2 => 12composeAsync accepts both sync and async functions
import{composeAsync}from'@psxcode/compose'constadd4Async=async(arg: number)=>arg+4constmult2=(a: number)=>a*2// (number) => Promise<number>constcomp=composeAsync(mult2,add4Async)awaitcomp(2)// (2 + 4) * 2 => 12The types are properly preserved
// (number) => stringcompose((val: any)=>`${val}`,(val: number)=>val*2)// (string[]) => booleancompose((val: number)=>val%2===0,compose((val: number[])=>val[0]||0,(val: string[])=>val.map(v=>v.length)))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'constadd4=(a: number)=>a+4constmult2=(a: number)=>a*2// (number) => numberconstcomp=pipe(mult2,add4)comp(2)// (2 * 2) + 4 => 8pipeAsync accepts both sync and async functions.
import{pipeAsync}from'@psxcode/compose'constadd4=(a: number)=>a+4constmult2=(a: number)=>a*2// (number) => Promise<number>constcomp=pipeAsync(mult2,add4)awaitcomp(2)// (2 * 2) + 4 => 8Parallel function composition, passing the initial value to all functions, and returning an array of results.
import{all}from'@psxcode/compose'constadd4=(a: number)=>a+4constmult2=(a: number)=>a*2consttoString=(a: number)=>`${a}`// (number) => [number, number, string]constcomp=all(mult2,add4,toString)comp(2)// [8, 6, '2']allAsync accepts both sync and async functions
import{allAsync}from'@psxcode/compose'constadd4=(a: number)=>a+4constmult2=async(a: number)=>a*2consttoString=(a: number)=>`${a}`// (number) => Promise<[number, number, string]>constcomp=allAsync(mult2,add4,toString)awaitcomp(2)// [8, 6, '2']