Files
Bubberstation/tgui/packages/common/fp.ts
SkyratBot c98d76bca5 [MIRROR] Tgui: More common components in ts (#27674)
* Tgui: More common components in ts (#83098)

## About The Pull Request
Converts more of common into typescript, with some tests
## Why It's Good For The Game
Typescript,,,,

* Tgui: More common components in ts

---------

Co-authored-by: Jeremiah <42397676+jlsnow301@users.noreply.github.com>
2024-05-15 00:57:58 +02:00

39 lines
979 B
TypeScript

/**
* @file
* @copyright 2020 Aleksej Komarov
* @license MIT
*/
type Func = (...args: any[]) => any;
/**
* Creates a function that returns the result of invoking the given
* functions, where each successive invocation is supplied the return
* value of the previous.
*
* @example
* ```tsx
* const add2 = (x) => x + 2;
* const multiplyBy3 = (x) => x * 3;
* const subtract5 = (x) => x - 5;
*
* const composedFunction = flow(add2, multiplyBy3, subtract5); // ((4 + 2) * 3) - 5 = 13
* const composedFunction2 = flow([add2, multiplyBy3], subtract5); // ((4 + 2) * 3) - 5 = 13
*
*/
export const flow =
(...funcs: Array<Func | Func[]>) =>
(input: any, ...rest: any[]): any => {
let output = input;
for (let func of funcs) {
// Recurse into the array of functions
if (Array.isArray(func)) {
output = flow(...func)(output, ...rest);
} else if (func) {
output = func(output, ...rest);
}
}
return output;
};