flowRight
util
This method is like flow except that it creates a function that invokes the given functions from right to left.
Installation
Import
import { flowRight } from '@tulx/utils';Source Code
Implementation
/**
* This method is like flow except that it creates a function that invokes the given functions from right to left.
*
* @param funcs - The functions to invoke.
* @returns Returns the new composite function.
*
* @example
* ```ts
* function square(n: number) {
* return n * n;
* }
* const addSquare = flowRight(square, add);
* addSquare(1, 2); // 9
* ```
*/
export function flowRight<TArgs extends unknown[], R>(
...funcs: readonly ((...args: unknown[]) => unknown)[]
): (...args: TArgs) => R {
return function (this: unknown, ...args: TArgs): R {
let result: unknown = funcs[funcs.length - 1].apply(this, args);
for (let i = funcs.length - 2; i >= 0; i--) {
result = funcs[i].call(this, result);
}
return result as R;
};
}
Example
import { flowRight } from '@tulx/utils';
function square(n: number) {
return n * n;
}
const addSquare = flowRight(square, add);
addSquare(1, 2); // 9Related Functions
attempt
Attempts to invoke func, returning either the result or the caught error object.
bindAll
Binds methods of an object to the object itself, overwriting the existing method.
cond
Creates a function that iterates over pairs and invokes the corresponding function of the first predicate to return truthy.
conforms
Creates a function that invokes the predicate properties of source with the corresponding property values of a given object.
constant
Creates a function that returns value.
defaultTo
Checks value to determine whether a default value should be returned in its place.