@railway-ts/pipelines 0.1.30 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/dist/composition/index.d.mts +367 -0
- package/dist/composition/index.mjs +37 -59
- package/dist/composition/index.mjs.map +1 -1
- package/dist/{result/index.d.cts → index-DDkNG41b.d.mts} +63 -139
- package/dist/option/index.d.mts +7 -0
- package/dist/option/index.mjs +1 -114
- package/dist/option-BQCaaEBP.mjs +909 -0
- package/dist/option-BQCaaEBP.mjs.map +1 -0
- package/dist/result/index.d.mts +7 -0
- package/dist/result/index.mjs +1 -185
- package/dist/schema/{index.d.cts → index.d.mts} +227 -259
- package/dist/schema/index.mjs +1646 -978
- package/dist/schema/index.mjs.map +1 -1
- package/docs/GETTING_STARTED.md +1 -1
- package/docs/RECIPES.md +3 -3
- package/package.json +26 -70
- package/dist/composition/index.cjs +0 -94
- package/dist/composition/index.cjs.map +0 -1
- package/dist/composition/index.d.cts +0 -373
- package/dist/composition/index.d.ts +0 -373
- package/dist/option/index.cjs +0 -137
- package/dist/option/index.cjs.map +0 -1
- package/dist/option/index.d.cts +0 -1
- package/dist/option/index.d.ts +0 -1
- package/dist/option/index.mjs.map +0 -1
- package/dist/result/index.cjs +0 -219
- package/dist/result/index.cjs.map +0 -1
- package/dist/result/index.d.ts +0 -1063
- package/dist/result/index.mjs.map +0 -1
- package/dist/schema/index.cjs +0 -1165
- package/dist/schema/index.cjs.map +0 -1
- package/dist/schema/index.d.ts +0 -1266
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @railway-ts/pipelines
|
|
2
2
|
|
|
3
|
-
[](https://www.npmjs.com/package/@railway-ts/pipelines) [](https://www.npmjs.com/package/@railway-ts/pipelines) [](https://github.com/sakobu/railway-ts/actions/workflows/ci.yml) [](https://codecov.io/gh/sakobu/railway-ts) [](https://opensource.org/licenses/MIT) [](https://www.typescriptlang.org/)
|
|
4
4
|
|
|
5
5
|
Railway-oriented programming for TypeScript. Typed `Result<T, E>` pipelines with first-class validation and optional `Option<T>` support -- small, modular, and fully tree-shakable.
|
|
6
6
|
|
|
@@ -44,7 +44,7 @@ See how it compares at [schemabenchmarks.dev](https://schemabenchmarks.dev) —
|
|
|
44
44
|
bun add @railway-ts/pipelines # or npm, pnpm, yarn
|
|
45
45
|
```
|
|
46
46
|
|
|
47
|
-
Requires TypeScript 5.0+ and Node.js 18+.
|
|
47
|
+
ESM-only. Requires TypeScript 5.0+ and Node.js 18+. CommonJS code can `require()` it on Node.js 20.19+ or 22.12+ (with TypeScript, use `module: nodenext` and TypeScript 5.8+).
|
|
48
48
|
|
|
49
49
|
## Quick Start
|
|
50
50
|
|
|
@@ -82,9 +82,9 @@ Validate at boundaries, chain operations, branch once at the end. Errors propaga
|
|
|
82
82
|
Cross-field validation + async operations:
|
|
83
83
|
|
|
84
84
|
```typescript
|
|
85
|
-
import { object, required, chain, string, minLength, refineAt, validate } from '@railway-ts/pipelines/schema';
|
|
86
85
|
import { pipeAsync } from '@railway-ts/pipelines/composition';
|
|
87
86
|
import { flatMapWith, tapWith } from '@railway-ts/pipelines/result';
|
|
87
|
+
import { object, required, chain, string, minLength, refineAt, validate } from '@railway-ts/pipelines/schema';
|
|
88
88
|
|
|
89
89
|
const signupSchema = chain(
|
|
90
90
|
object({
|
|
@@ -227,7 +227,7 @@ Sync and async composition share the same mental model.
|
|
|
227
227
|
|
|
228
228
|
## Ecosystem
|
|
229
229
|
|
|
230
|
-
- **[@railway-ts/use-form](https://github.com/sakobu/railway-ts
|
|
230
|
+
- **[@railway-ts/use-form](https://github.com/sakobu/railway-ts/tree/main/packages/use-form)** -- Type-safe React form hook built directly on the Schema layer. Define a schema once -- use it for validation, type inference, and form state.
|
|
231
231
|
|
|
232
232
|
## Documentation
|
|
233
233
|
|
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for functional programming
|
|
3
|
+
*
|
|
4
|
+
* @module composition
|
|
5
|
+
*/
|
|
6
|
+
export type MaybeAsync<T> = T | Promise<T>;
|
|
7
|
+
export type UnknownFunction = (...params: unknown[]) => unknown;
|
|
8
|
+
/**
|
|
9
|
+
* Pipes a value through a series of functions, from left to right.
|
|
10
|
+
* Each function receives the output of the previous function.
|
|
11
|
+
* Unlike flow, pipe immediately executes the functions with the provided initial value.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* // Basic usage
|
|
15
|
+
* const result = pipe(
|
|
16
|
+
* 5,
|
|
17
|
+
* (n) => n * 2, // 10
|
|
18
|
+
* (n) => n + 1, // 11
|
|
19
|
+
* (n) => n.toString() // "11"
|
|
20
|
+
* );
|
|
21
|
+
* // result: "11"
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* // With Option
|
|
25
|
+
* import { some, mapOption } from "@railway-ts/pipelines/option";
|
|
26
|
+
*
|
|
27
|
+
* const optionResult = pipe(
|
|
28
|
+
* some(5),
|
|
29
|
+
* (o) => mapOption(o, (n) => n * 2),
|
|
30
|
+
* (o) => mapOption(o, (n) => n.toString())
|
|
31
|
+
* );
|
|
32
|
+
* // optionResult: Option<string> containing "10"
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* // With Result
|
|
36
|
+
* import { ok, mapResult } from "@railway-ts/pipelines/result";
|
|
37
|
+
*
|
|
38
|
+
* const resultValue = pipe(
|
|
39
|
+
* ok(5),
|
|
40
|
+
* (r) => mapResult(r, (n) => n * 2),
|
|
41
|
+
* (r) => mapResult(r, (n) => n.toString())
|
|
42
|
+
* );
|
|
43
|
+
* // resultValue: Result<string, never> containing "10"
|
|
44
|
+
*
|
|
45
|
+
* @param a - The initial value to pipe through the functions
|
|
46
|
+
* @param ab - The first function to apply to the initial value
|
|
47
|
+
* @param functions - Additional functions to apply in sequence
|
|
48
|
+
* @returns The final result after applying all functions
|
|
49
|
+
*/
|
|
50
|
+
export declare function pipe<A, B>(a: A, ab: (this: void, a: A) => B): B;
|
|
51
|
+
export declare function pipe<A, B, C>(a: A, ab: (this: void, a: A) => B, bc: (this: void, b: B) => C): C;
|
|
52
|
+
export declare function pipe<A, B, C, D>(a: A, ab: (this: void, a: A) => B, bc: (this: void, b: B) => C, cd: (this: void, c: C) => D): D;
|
|
53
|
+
export declare function pipe<A, B, C, D, E>(a: A, ab: (this: void, a: A) => B, bc: (this: void, b: B) => C, cd: (this: void, c: C) => D, de: (this: void, d: D) => E): E;
|
|
54
|
+
export declare function pipe<A, B, C, D, E, F>(a: A, ab: (this: void, a: A) => B, bc: (this: void, b: B) => C, cd: (this: void, c: C) => D, de: (this: void, d: D) => E, ef: (this: void, e: E) => F): F;
|
|
55
|
+
export declare function pipe<A, B, C, D, E, F, G>(a: A, ab: (this: void, a: A) => B, bc: (this: void, b: B) => C, cd: (this: void, c: C) => D, de: (this: void, d: D) => E, ef: (this: void, e: E) => F, fg: (this: void, f: F) => G): G;
|
|
56
|
+
export declare function pipe<A, B, C, D, E, F, G, H>(a: A, ab: (this: void, a: A) => B, bc: (this: void, b: B) => C, cd: (this: void, c: C) => D, de: (this: void, d: D) => E, ef: (this: void, e: E) => F, fg: (this: void, f: F) => G, gh: (this: void, g: G) => H): H;
|
|
57
|
+
export declare function pipe<A, B, C, D, E, F, G, H, I>(a: A, ab: (this: void, a: A) => B, bc: (this: void, b: B) => C, cd: (this: void, c: C) => D, de: (this: void, d: D) => E, ef: (this: void, e: E) => F, fg: (this: void, f: F) => G, gh: (this: void, g: G) => H, hi: (this: void, h: H) => I): I;
|
|
58
|
+
export declare function pipe<A, B, C, D, E, F, G, H, I, J>(a: A, ab: (this: void, a: A) => B, bc: (this: void, b: B) => C, cd: (this: void, c: C) => D, de: (this: void, d: D) => E, ef: (this: void, e: E) => F, fg: (this: void, f: F) => G, gh: (this: void, g: G) => H, hi: (this: void, h: H) => I, ij: (this: void, i: I) => J): J;
|
|
59
|
+
/**
|
|
60
|
+
* Creates a new function that is the composition of the provided functions, applied from left to right.
|
|
61
|
+
* Unlike pipe, flow doesn't immediately execute the functions but returns a new function that,
|
|
62
|
+
* when called, will run the composed functions in sequence.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* // Basic usage
|
|
66
|
+
* const processNumber = flow(
|
|
67
|
+
* (n: number) => n * 2,
|
|
68
|
+
* (n) => n + 1,
|
|
69
|
+
* (n) => n.toString()
|
|
70
|
+
* );
|
|
71
|
+
*
|
|
72
|
+
* const result = processNumber(5); // "11"
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* // With multiple arguments
|
|
76
|
+
* const formatName = flow(
|
|
77
|
+
* (first: string, last: string) => `${first} ${last}`,
|
|
78
|
+
* (name) => name.toUpperCase()
|
|
79
|
+
* );
|
|
80
|
+
*
|
|
81
|
+
* const name = formatName("John", "Doe"); // "JOHN DOE"
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* // With Option type
|
|
85
|
+
* import { some, none, mapOption, flatMapOption, type Option } from "@railway-ts/pipelines/option";
|
|
86
|
+
*
|
|
87
|
+
* const safeParseInt = (radix: number) => (str: string): Option<number> => {
|
|
88
|
+
* const n = Number.parseInt(str, radix);
|
|
89
|
+
* return Number.isNaN(n) ? none() : some(n);
|
|
90
|
+
* };
|
|
91
|
+
*
|
|
92
|
+
* const processString = flow(
|
|
93
|
+
* (s: string) => some(s),
|
|
94
|
+
* (o) => mapOption(o, (s) => s + "0"),
|
|
95
|
+
* (o) => flatMapOption(o, safeParseInt(10))
|
|
96
|
+
* );
|
|
97
|
+
*
|
|
98
|
+
* const option = processString("42"); // Option<number> containing 420
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* // With Result type
|
|
102
|
+
* import { ok, err, flatMapResult } from "@railway-ts/pipelines/result";
|
|
103
|
+
*
|
|
104
|
+
* const validatePositive = (n: number) => (n > 0 ? ok(n) : err("Not positive"));
|
|
105
|
+
*
|
|
106
|
+
* const validateNumber = flow(
|
|
107
|
+
* (n: number) => ok(n),
|
|
108
|
+
* (r) => flatMapResult(r, validatePositive),
|
|
109
|
+
* (r) => flatMapResult(r, (n) => ok(n * 2))
|
|
110
|
+
* );
|
|
111
|
+
*
|
|
112
|
+
* const result = validateNumber(5); // Result<number, string> containing 10
|
|
113
|
+
*
|
|
114
|
+
* @param ab - The first function in the composition
|
|
115
|
+
* @param functions - Additional functions to compose in sequence
|
|
116
|
+
* @returns A new function that applies all the composed functions in sequence
|
|
117
|
+
*/
|
|
118
|
+
export declare function flow<A extends unknown[], B>(ab: (this: void, ...a: A) => B): (...a: A) => B;
|
|
119
|
+
export declare function flow<A extends unknown[], B, C>(ab: (this: void, ...a: A) => B, bc: (this: void, b: B) => C): (...a: A) => C;
|
|
120
|
+
export declare function flow<A extends unknown[], B, C, D>(ab: (this: void, ...a: A) => B, bc: (this: void, b: B) => C, cd: (this: void, c: C) => D): (...a: A) => D;
|
|
121
|
+
export declare function flow<A extends unknown[], B, C, D, E>(ab: (this: void, ...a: A) => B, bc: (this: void, b: B) => C, cd: (this: void, c: C) => D, de: (this: void, d: D) => E): (...a: A) => E;
|
|
122
|
+
export declare function flow<A extends unknown[], B, C, D, E, F>(ab: (this: void, ...a: A) => B, bc: (this: void, b: B) => C, cd: (this: void, c: C) => D, de: (this: void, d: D) => E, ef: (this: void, e: E) => F): (...a: A) => F;
|
|
123
|
+
export declare function flow<A extends unknown[], B, C, D, E, F, G>(ab: (this: void, ...a: A) => B, bc: (this: void, b: B) => C, cd: (this: void, c: C) => D, de: (this: void, d: D) => E, ef: (this: void, e: E) => F, fg: (this: void, f: F) => G): (...a: A) => G;
|
|
124
|
+
export declare function flow<A extends unknown[], B, C, D, E, F, G, H>(ab: (this: void, ...a: A) => B, bc: (this: void, b: B) => C, cd: (this: void, c: C) => D, de: (this: void, d: D) => E, ef: (this: void, e: E) => F, fg: (this: void, f: F) => G, gh: (this: void, g: G) => H): (...a: A) => H;
|
|
125
|
+
export declare function flow<A extends unknown[], B, C, D, E, F, G, H, I>(ab: (this: void, ...a: A) => B, bc: (this: void, b: B) => C, cd: (this: void, c: C) => D, de: (this: void, d: D) => E, ef: (this: void, e: E) => F, fg: (this: void, f: F) => G, gh: (this: void, g: G) => H, hi: (this: void, h: H) => I): (...a: A) => I;
|
|
126
|
+
export declare function flow<A extends unknown[], B, C, D, E, F, G, H, I, J>(ab: (this: void, ...a: A) => B, bc: (this: void, b: B) => C, cd: (this: void, c: C) => D, de: (this: void, d: D) => E, ef: (this: void, e: E) => F, fg: (this: void, f: F) => G, gh: (this: void, g: G) => H, hi: (this: void, h: H) => I, ij: (this: void, i: I) => J): (...a: A) => J;
|
|
127
|
+
export declare function flow<A extends unknown[], B, C, D, E, F, G, H, I, J, K>(ab: (this: void, ...a: A) => B, bc: (this: void, b: B) => C, cd: (this: void, c: C) => D, de: (this: void, d: D) => E, ef: (this: void, e: E) => F, fg: (this: void, f: F) => G, gh: (this: void, g: G) => H, hi: (this: void, h: H) => I, ij: (this: void, i: I) => J, jk: (this: void, j: J) => K): (...a: A) => K;
|
|
128
|
+
/**
|
|
129
|
+
* Pipes a value through a series of potentially async functions, from left to right.
|
|
130
|
+
* Each function receives the awaited output of the previous function.
|
|
131
|
+
* Unlike pipe, pipeAsync awaits between each step, enabling async composition.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* // Basic async usage
|
|
135
|
+
* const result = await pipeAsync(
|
|
136
|
+
* 5,
|
|
137
|
+
* async (n) => n * 2, // 10
|
|
138
|
+
* (n) => n + 1, // 11
|
|
139
|
+
* async (n) => n.toString() // "11"
|
|
140
|
+
* );
|
|
141
|
+
* // result: "11"
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* // With Result and curried helpers
|
|
145
|
+
* import { ok, flatMapWith, mapWith } from "@railway-ts/pipelines/result";
|
|
146
|
+
*
|
|
147
|
+
* const result = await pipeAsync(
|
|
148
|
+
* ok(5),
|
|
149
|
+
* flatMapWith(async (n) => ok(n * 2)),
|
|
150
|
+
* mapWith((n) => n.toString())
|
|
151
|
+
* );
|
|
152
|
+
* // result: Result<string, never> containing "10"
|
|
153
|
+
*
|
|
154
|
+
* @param a - The initial value (or Promise) to pipe through the functions
|
|
155
|
+
* @param ab - The first function to apply to the initial value
|
|
156
|
+
* @param functions - Additional functions to apply in sequence
|
|
157
|
+
* @returns A Promise of the final result after applying all functions
|
|
158
|
+
*/
|
|
159
|
+
export declare function pipeAsync<A, B>(a: MaybeAsync<A>, ab: (this: void, a: A) => MaybeAsync<B>): Promise<B>;
|
|
160
|
+
export declare function pipeAsync<A, B, C>(a: MaybeAsync<A>, ab: (this: void, a: A) => MaybeAsync<B>, bc: (this: void, b: B) => MaybeAsync<C>): Promise<C>;
|
|
161
|
+
export declare function pipeAsync<A, B, C, D>(a: MaybeAsync<A>, ab: (this: void, a: A) => MaybeAsync<B>, bc: (this: void, b: B) => MaybeAsync<C>, cd: (this: void, c: C) => MaybeAsync<D>): Promise<D>;
|
|
162
|
+
export declare function pipeAsync<A, B, C, D, E>(a: MaybeAsync<A>, ab: (this: void, a: A) => MaybeAsync<B>, bc: (this: void, b: B) => MaybeAsync<C>, cd: (this: void, c: C) => MaybeAsync<D>, de: (this: void, d: D) => MaybeAsync<E>): Promise<E>;
|
|
163
|
+
export declare function pipeAsync<A, B, C, D, E, F>(a: MaybeAsync<A>, ab: (this: void, a: A) => MaybeAsync<B>, bc: (this: void, b: B) => MaybeAsync<C>, cd: (this: void, c: C) => MaybeAsync<D>, de: (this: void, d: D) => MaybeAsync<E>, ef: (this: void, e: E) => MaybeAsync<F>): Promise<F>;
|
|
164
|
+
export declare function pipeAsync<A, B, C, D, E, F, G>(a: MaybeAsync<A>, ab: (this: void, a: A) => MaybeAsync<B>, bc: (this: void, b: B) => MaybeAsync<C>, cd: (this: void, c: C) => MaybeAsync<D>, de: (this: void, d: D) => MaybeAsync<E>, ef: (this: void, e: E) => MaybeAsync<F>, fg: (this: void, f: F) => MaybeAsync<G>): Promise<G>;
|
|
165
|
+
export declare function pipeAsync<A, B, C, D, E, F, G, H>(a: MaybeAsync<A>, ab: (this: void, a: A) => MaybeAsync<B>, bc: (this: void, b: B) => MaybeAsync<C>, cd: (this: void, c: C) => MaybeAsync<D>, de: (this: void, d: D) => MaybeAsync<E>, ef: (this: void, e: E) => MaybeAsync<F>, fg: (this: void, f: F) => MaybeAsync<G>, gh: (this: void, g: G) => MaybeAsync<H>): Promise<H>;
|
|
166
|
+
export declare function pipeAsync<A, B, C, D, E, F, G, H, I>(a: MaybeAsync<A>, ab: (this: void, a: A) => MaybeAsync<B>, bc: (this: void, b: B) => MaybeAsync<C>, cd: (this: void, c: C) => MaybeAsync<D>, de: (this: void, d: D) => MaybeAsync<E>, ef: (this: void, e: E) => MaybeAsync<F>, fg: (this: void, f: F) => MaybeAsync<G>, gh: (this: void, g: G) => MaybeAsync<H>, hi: (this: void, h: H) => MaybeAsync<I>): Promise<I>;
|
|
167
|
+
export declare function pipeAsync<A, B, C, D, E, F, G, H, I, J>(a: MaybeAsync<A>, ab: (this: void, a: A) => MaybeAsync<B>, bc: (this: void, b: B) => MaybeAsync<C>, cd: (this: void, c: C) => MaybeAsync<D>, de: (this: void, d: D) => MaybeAsync<E>, ef: (this: void, e: E) => MaybeAsync<F>, fg: (this: void, f: F) => MaybeAsync<G>, gh: (this: void, g: G) => MaybeAsync<H>, hi: (this: void, h: H) => MaybeAsync<I>, ij: (this: void, i: I) => MaybeAsync<J>): Promise<J>;
|
|
168
|
+
/**
|
|
169
|
+
* Creates a new function that is the async composition of the provided functions, applied from left to right.
|
|
170
|
+
* Unlike flow, flowAsync awaits between each step, enabling async composition.
|
|
171
|
+
* The returned function always returns a Promise.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* // Basic async usage
|
|
175
|
+
* const processNumber = flowAsync(
|
|
176
|
+
* async (n: number) => n * 2,
|
|
177
|
+
* (n) => n + 1,
|
|
178
|
+
* async (n) => n.toString()
|
|
179
|
+
* );
|
|
180
|
+
*
|
|
181
|
+
* const result = await processNumber(5); // "11"
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* // With Result and curried helpers
|
|
185
|
+
* import { ok, flatMapWith, mapWith } from "@railway-ts/pipelines/result";
|
|
186
|
+
*
|
|
187
|
+
* const process = flowAsync(
|
|
188
|
+
* (n: number) => ok(n),
|
|
189
|
+
* flatMapWith(async (n) => ok(n * 2)),
|
|
190
|
+
* mapWith((n) => n.toString())
|
|
191
|
+
* );
|
|
192
|
+
*
|
|
193
|
+
* const result = await process(5); // Result<string, never> containing "10"
|
|
194
|
+
*
|
|
195
|
+
* @param ab - The first function in the composition
|
|
196
|
+
* @param functions - Additional functions to compose in sequence
|
|
197
|
+
* @returns A new function that applies all the composed functions in sequence, returning a Promise
|
|
198
|
+
*/
|
|
199
|
+
export declare function flowAsync<A extends unknown[], B>(ab: (this: void, ...a: A) => MaybeAsync<B>): (...a: A) => Promise<B>;
|
|
200
|
+
export declare function flowAsync<A extends unknown[], B, C>(ab: (this: void, ...a: A) => MaybeAsync<B>, bc: (this: void, b: B) => MaybeAsync<C>): (...a: A) => Promise<C>;
|
|
201
|
+
export declare function flowAsync<A extends unknown[], B, C, D>(ab: (this: void, ...a: A) => MaybeAsync<B>, bc: (this: void, b: B) => MaybeAsync<C>, cd: (this: void, c: C) => MaybeAsync<D>): (...a: A) => Promise<D>;
|
|
202
|
+
export declare function flowAsync<A extends unknown[], B, C, D, E>(ab: (this: void, ...a: A) => MaybeAsync<B>, bc: (this: void, b: B) => MaybeAsync<C>, cd: (this: void, c: C) => MaybeAsync<D>, de: (this: void, d: D) => MaybeAsync<E>): (...a: A) => Promise<E>;
|
|
203
|
+
export declare function flowAsync<A extends unknown[], B, C, D, E, F>(ab: (this: void, ...a: A) => MaybeAsync<B>, bc: (this: void, b: B) => MaybeAsync<C>, cd: (this: void, c: C) => MaybeAsync<D>, de: (this: void, d: D) => MaybeAsync<E>, ef: (this: void, e: E) => MaybeAsync<F>): (...a: A) => Promise<F>;
|
|
204
|
+
export declare function flowAsync<A extends unknown[], B, C, D, E, F, G>(ab: (this: void, ...a: A) => MaybeAsync<B>, bc: (this: void, b: B) => MaybeAsync<C>, cd: (this: void, c: C) => MaybeAsync<D>, de: (this: void, d: D) => MaybeAsync<E>, ef: (this: void, e: E) => MaybeAsync<F>, fg: (this: void, f: F) => MaybeAsync<G>): (...a: A) => Promise<G>;
|
|
205
|
+
export declare function flowAsync<A extends unknown[], B, C, D, E, F, G, H>(ab: (this: void, ...a: A) => MaybeAsync<B>, bc: (this: void, b: B) => MaybeAsync<C>, cd: (this: void, c: C) => MaybeAsync<D>, de: (this: void, d: D) => MaybeAsync<E>, ef: (this: void, e: E) => MaybeAsync<F>, fg: (this: void, f: F) => MaybeAsync<G>, gh: (this: void, g: G) => MaybeAsync<H>): (...a: A) => Promise<H>;
|
|
206
|
+
export declare function flowAsync<A extends unknown[], B, C, D, E, F, G, H, I>(ab: (this: void, ...a: A) => MaybeAsync<B>, bc: (this: void, b: B) => MaybeAsync<C>, cd: (this: void, c: C) => MaybeAsync<D>, de: (this: void, d: D) => MaybeAsync<E>, ef: (this: void, e: E) => MaybeAsync<F>, fg: (this: void, f: F) => MaybeAsync<G>, gh: (this: void, g: G) => MaybeAsync<H>, hi: (this: void, h: H) => MaybeAsync<I>): (...a: A) => Promise<I>;
|
|
207
|
+
export declare function flowAsync<A extends unknown[], B, C, D, E, F, G, H, I, J>(ab: (this: void, ...a: A) => MaybeAsync<B>, bc: (this: void, b: B) => MaybeAsync<C>, cd: (this: void, c: C) => MaybeAsync<D>, de: (this: void, d: D) => MaybeAsync<E>, ef: (this: void, e: E) => MaybeAsync<F>, fg: (this: void, f: F) => MaybeAsync<G>, gh: (this: void, g: G) => MaybeAsync<H>, hi: (this: void, h: H) => MaybeAsync<I>, ij: (this: void, i: I) => MaybeAsync<J>): (...a: A) => Promise<J>;
|
|
208
|
+
export declare function flowAsync<A extends unknown[], B, C, D, E, F, G, H, I, J, K>(ab: (this: void, ...a: A) => MaybeAsync<B>, bc: (this: void, b: B) => MaybeAsync<C>, cd: (this: void, c: C) => MaybeAsync<D>, de: (this: void, d: D) => MaybeAsync<E>, ef: (this: void, e: E) => MaybeAsync<F>, fg: (this: void, f: F) => MaybeAsync<G>, gh: (this: void, g: G) => MaybeAsync<H>, hi: (this: void, h: H) => MaybeAsync<I>, ij: (this: void, i: I) => MaybeAsync<J>, jk: (this: void, j: J) => MaybeAsync<K>): (...a: A) => Promise<K>;
|
|
209
|
+
/**
|
|
210
|
+
* Curries a multi-parameter function into a sequence of unary functions.
|
|
211
|
+
* Enables partial application at any depth and composes cleanly with `pipe`.
|
|
212
|
+
*
|
|
213
|
+
* Note: This curry is unary-only at each step to ensure compatibility with `pipe` and `flow`.
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* // Basic (2-arity)
|
|
217
|
+
* const add = (a: number, b: number) => a + b;
|
|
218
|
+
* const add5 = curry(add)(5);
|
|
219
|
+
* add5(3); // 8
|
|
220
|
+
*
|
|
221
|
+
* @example
|
|
222
|
+
* // 3-arity and partial application
|
|
223
|
+
* const multiply = (a: number, b: number, c: number) => a * b * c;
|
|
224
|
+
* const curriedMultiply = curry(multiply);
|
|
225
|
+
* curriedMultiply(2)(3)(4); // 24
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* // With pipe
|
|
229
|
+
* import { pipe } from "@/utils";
|
|
230
|
+
* const divide = (divisor: number, dividend: number) => dividend / divisor;
|
|
231
|
+
* const result = pipe(100, curry(divide)(2), (n) => n + 1); // 51
|
|
232
|
+
*
|
|
233
|
+
* @example
|
|
234
|
+
* // With Result validators
|
|
235
|
+
* import { ok, err, flatMap, type Result } from "@/result";
|
|
236
|
+
* const between = (min: number, max: number, n: number): Result<number, string> =>
|
|
237
|
+
* n >= min && n <= max ? ok(n) : err(`Value must be between ${min} and ${max}`);
|
|
238
|
+
* const validateAge = curry(between)(0)(120);
|
|
239
|
+
* pipe(ok(25), (r) => flatMap(r, validateAge)); // ok(25)
|
|
240
|
+
*
|
|
241
|
+
* @param fn - The multi-argument function to transform into a unary curried chain
|
|
242
|
+
* @returns A curried function that takes one argument at each application step
|
|
243
|
+
*/
|
|
244
|
+
export declare function curry<A, B, R>(fn: (a: A, b: B) => R): (a: A) => (b: B) => R;
|
|
245
|
+
export declare function curry<A, B, C, R>(fn: (a: A, b: B, c: C) => R): (a: A) => (b: B) => (c: C) => R;
|
|
246
|
+
export declare function curry<A, B, C, D, R>(fn: (a: A, b: B, c: C, d: D) => R): (a: A) => (b: B) => (c: C) => (d: D) => R;
|
|
247
|
+
export declare function curry<A, B, C, D, E, R>(fn: (a: A, b: B, c: C, d: D, e: E) => R): (a: A) => (b: B) => (c: C) => (d: D) => (e: E) => R;
|
|
248
|
+
/**
|
|
249
|
+
* Uncurries a curried unary function chain back into a multi-argument function.
|
|
250
|
+
* Inverse of `curry`. Useful when calling curried APIs with positional arguments.
|
|
251
|
+
*
|
|
252
|
+
* Note: Expects strictly unary nesting (the shape produced by `curry`).
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
* // 2-arity
|
|
256
|
+
* const addCurried = (a: number) => (b: number) => a + b;
|
|
257
|
+
* const add = uncurry(addCurried);
|
|
258
|
+
* add(5, 3); // 8
|
|
259
|
+
*
|
|
260
|
+
* @example
|
|
261
|
+
* // 3-arity
|
|
262
|
+
* const multiplyCurried = (a: number) => (b: number) => (c: number) => a * b * c;
|
|
263
|
+
* const multiply = uncurry(multiplyCurried);
|
|
264
|
+
* multiply(2, 3, 4); // 24
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* // Round-trip with curry
|
|
268
|
+
* import { curry } from "@railway-ts/pipelines";
|
|
269
|
+
* const divide = (divisor: number, dividend: number) => dividend / divisor;
|
|
270
|
+
* const divideCurried = curry(divide);
|
|
271
|
+
* const divideUncurried = uncurry(divideCurried);
|
|
272
|
+
* divideUncurried(2, 100); // 50
|
|
273
|
+
*
|
|
274
|
+
* @param fn - A curried unary function chain (e.g., a => b => c => r)
|
|
275
|
+
* @returns A multi-argument function equivalent to applying the curried chain in sequence
|
|
276
|
+
*/
|
|
277
|
+
export declare function uncurry<A, B, C, D, E, R>(fn: (a: A) => (b: B) => (c: C) => (d: D) => (e: E) => R): (a: A, b: B, c: C, d: D, e: E) => R;
|
|
278
|
+
export declare function uncurry<A, B, C, D, E, R>(fn: (a: A) => (b: B) => (c: C) => (d: D) => (e: E) => R): (a: A, b: B, c: C, d: D, e: E) => R;
|
|
279
|
+
export declare function uncurry<A, B, C, D, R>(fn: (a: A) => (b: B) => (c: C) => (d: D) => R): (a: A, b: B, c: C, d: D) => R;
|
|
280
|
+
export declare function uncurry<A, B, C, R>(fn: (a: A) => (b: B) => (c: C) => R): (a: A, b: B, c: C) => R;
|
|
281
|
+
export declare function uncurry<A, B, R>(fn: (a: A) => (b: B) => R): (a: A, b: B) => R;
|
|
282
|
+
export declare function uncurry<A, R>(fn: (a: A) => R): (a: A) => R;
|
|
283
|
+
/**
|
|
284
|
+
* Transforms a multi-argument function into one that accepts a single tuple argument.
|
|
285
|
+
* This makes multi-arg functions composable in `pipe`/`flow` when your data is a tuple.
|
|
286
|
+
*
|
|
287
|
+
* @example
|
|
288
|
+
* // Basic
|
|
289
|
+
* const add = (a: number, b: number) => a + b;
|
|
290
|
+
* const tupledAdd = tupled(add);
|
|
291
|
+
* tupledAdd([5, 3]); // 8
|
|
292
|
+
*
|
|
293
|
+
* @example
|
|
294
|
+
* // With pipe (tuple input)
|
|
295
|
+
* import { pipe } from "@/utils";
|
|
296
|
+
* const distance = (x: number, y: number) => Math.hypot(x, y);
|
|
297
|
+
* pipe([3, 4] as [number, number], tupled(distance)); // 5
|
|
298
|
+
*
|
|
299
|
+
* @example
|
|
300
|
+
* // With Result
|
|
301
|
+
* import { ok, err, flatMapResult, mapResult, type Result } from "@railway-ts/pipelines/result";
|
|
302
|
+
* const divide = (dividend: number, divisor: number): Result<number, string> =>
|
|
303
|
+
* divisor === 0 ? err("Division by zero") : ok(dividend / divisor);
|
|
304
|
+
* const tupledDivide = tupled(divide);
|
|
305
|
+
* pipe(ok<[number, number], string>([10, 2]),
|
|
306
|
+
* (r) => flatMapResult(r, tupledDivide),
|
|
307
|
+
* (r) => mapResult(r, (n) => n * 2)
|
|
308
|
+
* ); // ok(10)
|
|
309
|
+
*
|
|
310
|
+
* @example
|
|
311
|
+
* // With Option
|
|
312
|
+
* import { some, none, flatMapOption, type Option } from "@railway-ts/pipelines/option";
|
|
313
|
+
* const safeParse = (str: string, radix: number): Option<number> => {
|
|
314
|
+
* const n = Number.parseInt(str, radix);
|
|
315
|
+
* return Number.isNaN(n) ? none() : some(n);
|
|
316
|
+
* };
|
|
317
|
+
* const tupledParse = tupled(safeParse);
|
|
318
|
+
* flatMapOption(some(["FF", 16] as [string, number]), tupledParse); // some(255)
|
|
319
|
+
*
|
|
320
|
+
* @param fn - A function taking positional arguments to be adapted to a single tuple argument
|
|
321
|
+
* @returns A function that takes a tuple and applies it to `fn` as positional args
|
|
322
|
+
*/
|
|
323
|
+
export declare function tupled<A, B, R>(fn: (a: A, b: B) => R): (args: [A, B]) => R;
|
|
324
|
+
export declare function tupled<A, B, C, R>(fn: (a: A, b: B, c: C) => R): (args: [A, B, C]) => R;
|
|
325
|
+
export declare function tupled<A, B, C, D, R>(fn: (a: A, b: B, c: C, d: D) => R): (args: [A, B, C, D]) => R;
|
|
326
|
+
export declare function tupled<A, B, C, D, E, R>(fn: (a: A, b: B, c: C, d: D, e: E) => R): (args: [A, B, C, D, E]) => R;
|
|
327
|
+
/**
|
|
328
|
+
* Transforms a function that accepts a tuple into a multi-argument function.
|
|
329
|
+
* Inverse of `tupled`. Useful when callers have positional arguments but your core API uses tuples.
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* // Basic
|
|
333
|
+
* const tupledAdd = ([a, b]: [number, number]) => a + b;
|
|
334
|
+
* const add = untupled(tupledAdd);
|
|
335
|
+
* add(5, 3); // 8
|
|
336
|
+
*
|
|
337
|
+
* @example
|
|
338
|
+
* // 3-arity
|
|
339
|
+
* const sum3Tupled = ([a, b, c]: [number, number, number]) => a + b + c;
|
|
340
|
+
* const sum3 = untupled(sum3Tupled);
|
|
341
|
+
* sum3(1, 2, 3); // 6
|
|
342
|
+
*
|
|
343
|
+
* @example
|
|
344
|
+
* // With Result
|
|
345
|
+
* import { ok, err, flatMapResult, type Result } from "@railway-ts/pipelines/result";
|
|
346
|
+
* const validateRange = ([min, max, value]: [number, number, number]): Result<number, string> =>
|
|
347
|
+
* value >= min && value <= max ? ok(value) : err(`Value ${value} not between ${min} and ${max}`);
|
|
348
|
+
* const validate = untupled(validateRange);
|
|
349
|
+
* flatMapResult(ok(50), (n) => validate(0, 100, n)); // ok(50)
|
|
350
|
+
*
|
|
351
|
+
* @example
|
|
352
|
+
* // With Option
|
|
353
|
+
* import { some, none, type Option } from "@railway-ts/pipelines/option";
|
|
354
|
+
* const parsePair = ([x, y]: [string, string]): Option<number> => {
|
|
355
|
+
* const a = Number(x), b = Number(y);
|
|
356
|
+
* return Number.isNaN(a) || Number.isNaN(b) ? none() : some(a + b);
|
|
357
|
+
* };
|
|
358
|
+
* const parse = untupled(parsePair);
|
|
359
|
+
* parse("2", "3"); // some(5)
|
|
360
|
+
*
|
|
361
|
+
* @param fn - A function that accepts a tuple of arguments
|
|
362
|
+
* @returns A function that accepts the same arguments positionally
|
|
363
|
+
*/
|
|
364
|
+
export declare function untupled<A, B, R>(fn: (args: [A, B]) => R): (a: A, b: B) => R;
|
|
365
|
+
export declare function untupled<A, B, C, R>(fn: (args: [A, B, C]) => R): (a: A, b: B, c: C) => R;
|
|
366
|
+
export declare function untupled<A, B, C, D, R>(fn: (args: [A, B, C, D]) => R): (a: A, b: B, c: C, d: D) => R;
|
|
367
|
+
export declare function untupled<A, B, C, D, E, R>(fn: (args: [A, B, C, D, E]) => R): (a: A, b: B, c: C, d: D, e: E) => R;
|
|
@@ -1,85 +1,63 @@
|
|
|
1
1
|
// src/composition/pipe.ts
|
|
2
2
|
function pipe(value, ...fns) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
return result;
|
|
3
|
+
let result = value;
|
|
4
|
+
for (const fn of fns) result = fn(result);
|
|
5
|
+
return result;
|
|
8
6
|
}
|
|
9
|
-
|
|
10
7
|
// src/composition/flow.ts
|
|
11
8
|
function flow(...fns) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
result = fn(result);
|
|
20
|
-
}
|
|
21
|
-
return result;
|
|
22
|
-
};
|
|
9
|
+
if (fns.length === 1) return fns[0];
|
|
10
|
+
const [first, ...rest] = fns;
|
|
11
|
+
return (...args) => {
|
|
12
|
+
let result = first(...args);
|
|
13
|
+
for (const fn of rest) result = fn(result);
|
|
14
|
+
return result;
|
|
15
|
+
};
|
|
23
16
|
}
|
|
24
|
-
|
|
25
17
|
// src/composition/pipe-async.ts
|
|
26
18
|
async function pipeAsync(a, ...fns) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
return result;
|
|
19
|
+
let result = await a;
|
|
20
|
+
for (const fn of fns) result = await fn(result);
|
|
21
|
+
return result;
|
|
32
22
|
}
|
|
33
|
-
|
|
34
23
|
// src/composition/flow-async.ts
|
|
35
24
|
function flowAsync(first, ...fns) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
return result;
|
|
42
|
-
};
|
|
25
|
+
return async (...args) => {
|
|
26
|
+
let result = await first(...args);
|
|
27
|
+
for (const fn of fns) result = await fn(result);
|
|
28
|
+
return result;
|
|
29
|
+
};
|
|
43
30
|
}
|
|
44
|
-
|
|
45
31
|
// src/composition/curry.ts
|
|
46
32
|
function curry(fn) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
};
|
|
55
|
-
};
|
|
33
|
+
const arity = fn.length;
|
|
34
|
+
return function curried(...args) {
|
|
35
|
+
if (args.length >= arity) return fn(...args);
|
|
36
|
+
return function(...nextArgs) {
|
|
37
|
+
return curried(...args, ...nextArgs);
|
|
38
|
+
};
|
|
39
|
+
};
|
|
56
40
|
}
|
|
57
|
-
|
|
58
41
|
// src/composition/uncurry.ts
|
|
59
42
|
function uncurry(fn) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
return result;
|
|
66
|
-
};
|
|
43
|
+
return function uncurried(...args) {
|
|
44
|
+
let result = fn;
|
|
45
|
+
for (const arg of args) result = result(arg);
|
|
46
|
+
return result;
|
|
47
|
+
};
|
|
67
48
|
}
|
|
68
|
-
|
|
69
49
|
// src/composition/tupled.ts
|
|
70
50
|
function tupled(fn) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
51
|
+
return function tupledFn(args) {
|
|
52
|
+
return fn(...args);
|
|
53
|
+
};
|
|
74
54
|
}
|
|
75
|
-
|
|
76
55
|
// src/composition/untupled.ts
|
|
77
56
|
function untupled(fn) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
57
|
+
return function untupledFn(...args) {
|
|
58
|
+
return fn(args);
|
|
59
|
+
};
|
|
81
60
|
}
|
|
82
|
-
|
|
83
61
|
export { curry, flow, flowAsync, pipe, pipeAsync, tupled, uncurry, untupled };
|
|
84
|
-
|
|
62
|
+
|
|
85
63
|
//# sourceMappingURL=index.mjs.map
|