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"] }