flowRight()
flowRight(): <
T>(x) =>T
flowRight<
A,B>(f): (a) =>B
flowRight<
A,B,C>(f,g): (a) =>C
flowRight<
A,B,C,D>(f,g,h): (a) =>D
Show 3 more signatures
flowRight<
A,B,C,D,E>(f,g,h,i): (a) =>E
flowRight<
A,B,C,D,E,F>(f,g,h,i,j): (a) =>F
flowRight<
A,B,C,D,E,F,G>(f,g,h,i,j,k): (a) =>G
Composes functions right-to-left, passing the result of each to the previous.
This is the opposite of pipe (also known as compose).
compose
Type Parametersโ
A: Aโ
B: Bโ
C: Cโ
D: Dโ
E: Eโ
F: Fโ
G: Gโ
Parametersโ
Overload 1:
f: (a) => Bโ
Overload 2:
f: (b) => Cโ
g: (a) => Bโ
Overload 3:
f: (c) => Dโ
g: (b) => Cโ
h: (a) => Bโ
Overload 4:
f: (d) => Eโ
g: (c) => Dโ
h: (b) => Cโ
i: (a) => Bโ
Show 2 more overloads
Returns: <T>(x): Tโ
The composed function.
Type Parametersโ
T: Tโ
x:
T
Returns:T
Sinceโ
2.0.0
Performanceโ
Uses reduce for composition. Overhead is minimal for typical use.
Also known asโ
compose (Ramda) ยท flowRight (Lodash, es-toolkit) ยท โ (Remeda, Radashi, Effect, Modern Dash, Antfu)
Exampleโ
const add10 = (x: number) => x + 10;
const multiply2 = (x: number) => x * 2;
const square = (x: number) => x * x;
// Right to left: square(multiply2(add10(5)))
const composed = flowRight(square, multiply2, add10);
composed(5); // => 900 (5 + 10 = 15, 15 * 2 = 30, 30 * 30 = 900)
// Compare with pipe (left to right)
// pipe would be: add10(multiply2(square(5)))
// String transformation
const trim = (s: string) => s.trim();
const upper = (s: string) => s.toUpperCase();
const exclaim = (s: string) => s + '!';
const shout = flowRight(exclaim, upper, trim);
shout(' hello '); // => 'HELLO!'
How it works?โ
Composes functions right-to-left (mathematical composition).
Also known as compose โ the rightmost function executes first.
Step-by-Stepโ
flowRight vs pipeโ
| flowRight | pipe | |
|---|---|---|
| Direction | Right โ Left | Left โ Right |
| Math notation | f(g(h(x))) | h then g then f |
| First executed | Last argument | First function |
Use Casesโ
Data Transformation pipelines (right-to-left) ๐โ
Compose functions from right to left (traditional compose). Essential for mathematical function composition.
import { flowRight } from "pithos/arkhe/function/flow-right";
const double = (x: number) => x * 2;
const addOne = (x: number) => x + 1;
const square = (x: number) => x * x;
// Reads right-to-left: square, then addOne, then double
const transform = flowRight(double, addOne, square);
console.log(transform(3)); // double(addOne(square(3))) = double(addOne(9)) = double(10) = 20
String Processing chains ๐โ
Compose string transformations in logical order. Critical for text processing pipelines.
import { flowRight } from "pithos/arkhe/function/flow-right";
const trim = (s: string) => s.trim();
const toLowerCase = (s: string) => s.toLowerCase();
const removeSpaces = (s: string) => s.replace(/\s+/g, "-");
// Create a slug generator: removeSpaces(toLowerCase(trim(input)))
const slugify = flowRight(removeSpaces, toLowerCase, trim);
console.log(slugify(" Hello World ")); // "hello-world"
console.log(slugify(" My Blog Post ")); // "my-blog-post"
Validation Chains compositionโ
Compose validators that run in sequence. Important for form and data validation.
import { flowRight } from "pithos/arkhe/function/flow-right";
type ValidationResult = { valid: boolean; errors: string[] };
const checkRequired = (field: string) => (result: ValidationResult) => ({
...result,
valid: result.valid && field.length > 0,
errors: field.length === 0 ? [...result.errors, "Required"] : result.errors,
});
const checkMinLength = (min: number) => (field: string) => (result: ValidationResult) => ({
...result,
valid: result.valid && field.length >= min,
errors: field.length < min ? [...result.errors, `Min ${min} chars`] : result.errors,
});
// Compose validators (right-to-left execution)
const validateUsername = (username: string) => {
const validate = flowRight(
checkMinLength(3)(username),
checkRequired(username)
);
return validate({ valid: true, errors: [] });
};
console.log(validateUsername("ab")); // { valid: false, errors: ["Min 3 chars"] }