This package provides a set of utilities to help you compose your functions in a more readable and functional way.
npm install @ze-ts/composition
Pipe is a function with arity n
that the first argument is a value and the rest are functions that will be applied to the value in sequence.
pipe: (value: any, ...fns: Function[]) => any;
import { pipe } from '@ze-ts/composition';
const add = (a: number) => (b: number) => a + b;
const multiply = (a: number) => (b: number) => a * b;
const result = pipe(5, add(3), multiply(2));
console.log(result); // 16
Flow is a function with arity n
that takes a list of functions and returns a new function that receives a value and applies the functions in sequence.
flow: (...fns: Function[]) =>
(value: any) =>
any;
import { flow } from '@ze-ts/composition';
const add = (a: number) => (b: number) => a + b;
const multiply = (a: number) => (b: number) => a * b;
const addAndMultiply = flow(add(3), multiply(2));
addAndMultiply(2); // 10
Compose is a function with arity n
that takes a list of functions and returns a new function that receives a value and applies the functions in reverse order (rigth-to-left).
// fns will be applied in reverse order
compose: (...fns: Function[]) =>
(value: any) =>
any;
import { compose } from '@ze-ts/composition';
const add = (a: number) => (b: number) => a + b;
const multiply = (a: number) => (b: number) => a * b;
const addAndMultiply = compose(multiply(2), add(3));
addAndMultiply(2); // 10