super-utils-plus
Version:
A superior alternative to Lodash with improved performance, TypeScript support, and developer experience
53 lines (52 loc) • 1.26 kB
JavaScript
/**
* Composes functions from right to left.
*
* @param funcs - The functions to compose
* @returns A function that composes the functions
*
* @example
* ```ts
* const add1 = (x: number) => x + 1;
* const multiply2 = (x: number) => x * 2;
* const subtract3 = (x: number) => x - 3;
*
* const composed = compose(subtract3, multiply2, add1);
* composed(5);
* // => ((5 + 1) * 2) - 3 = 9
* ```
*/
export function compose(...funcs) {
if (funcs.length === 0) {
return (arg) => arg;
}
if (funcs.length === 1) {
return funcs[0];
}
return funcs.reduce((a, b) => (...args) => a(b(...args)));
}
/**
* Composes functions from left to right.
*
* @param funcs - The functions to compose
* @returns A function that composes the functions
*
* @example
* ```ts
* const add1 = (x: number) => x + 1;
* const multiply2 = (x: number) => x * 2;
* const subtract3 = (x: number) => x - 3;
*
* const piped = pipe(add1, multiply2, subtract3);
* piped(5);
* // => ((5 + 1) * 2) - 3 = 9
* ```
*/
export function pipe(...funcs) {
if (funcs.length === 0) {
return (arg) => arg;
}
if (funcs.length === 1) {
return funcs[0];
}
return funcs.reduce((a, b) => (...args) => b(a(...args)));
}