UNPKG

es-next-tools

Version:

A comprehensive utility library for JavaScript and TypeScript that provides a wide range of functions for common programming tasks, including mathematical operations, date manipulations, array and object handling, string utilities, and more.

31 lines (30 loc) 1.15 kB
export * from './array'; export * from './structures'; export * from './date'; export * from './function'; export * from './math'; export * from './object'; export * from './promise'; export * from './string'; /** * Composes functions from left to right. * @param {...AnyFunction} fns - The functions to compose. * @returns {AnyFunction} A function that runs the composed functions in sequence. * @example * const addOne = (x: number) => x + 1; * const double = (x: number) => x * 2; * const addOneThenDouble = pipe(addOne, double); * console.log(addOneThenDouble(3)); // 8 */ export const pipe = (...fns) => (value) => fns.reduce((acc, fn) => fn(acc), value); /** * Composes functions from right to left. * @param {...AnyFunction} fns - The functions to compose. * @returns {AnyFunction} A function that runs the composed functions in reverse sequence. * @example * const addOne = (x: number) => x + 1; * const double = (x: number) => x * 2; * const doubleAndAddOne = compose(addOne, double); * console.log(doubleAndAddOne(3)); // 7 */ export const compose = (...fns) => (value) => fns.reduceRight((acc, fn) => fn(acc), value);