@railway-ts/pipelines 0.1.30 → 0.2.0
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 +21 -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
|
@@ -1,373 +0,0 @@
|
|
|
1
|
-
type MaybeAsync<T> = T | Promise<T>;
|
|
2
|
-
type UnknownFunction = (...params: unknown[]) => unknown;
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Pipes a value through a series of functions, from left to right.
|
|
6
|
-
* Each function receives the output of the previous function.
|
|
7
|
-
* Unlike flow, pipe immediately executes the functions with the provided initial value.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* // Basic usage
|
|
11
|
-
* const result = pipe(
|
|
12
|
-
* 5,
|
|
13
|
-
* (n) => n * 2, // 10
|
|
14
|
-
* (n) => n + 1, // 11
|
|
15
|
-
* (n) => n.toString() // "11"
|
|
16
|
-
* );
|
|
17
|
-
* // result: "11"
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* // With Option
|
|
21
|
-
* import { some, mapOption } from "@railway-ts/pipelines/option";
|
|
22
|
-
*
|
|
23
|
-
* const optionResult = pipe(
|
|
24
|
-
* some(5),
|
|
25
|
-
* (o) => mapOption(o, (n) => n * 2),
|
|
26
|
-
* (o) => mapOption(o, (n) => n.toString())
|
|
27
|
-
* );
|
|
28
|
-
* // optionResult: Option<string> containing "10"
|
|
29
|
-
*
|
|
30
|
-
* @example
|
|
31
|
-
* // With Result
|
|
32
|
-
* import { ok, mapResult } from "@railway-ts/pipelines/result";
|
|
33
|
-
*
|
|
34
|
-
* const resultValue = pipe(
|
|
35
|
-
* ok(5),
|
|
36
|
-
* (r) => mapResult(r, (n) => n * 2),
|
|
37
|
-
* (r) => mapResult(r, (n) => n.toString())
|
|
38
|
-
* );
|
|
39
|
-
* // resultValue: Result<string, never> containing "10"
|
|
40
|
-
*
|
|
41
|
-
* @param a - The initial value to pipe through the functions
|
|
42
|
-
* @param ab - The first function to apply to the initial value
|
|
43
|
-
* @param functions - Additional functions to apply in sequence
|
|
44
|
-
* @returns The final result after applying all functions
|
|
45
|
-
*/
|
|
46
|
-
declare function pipe<A, B>(a: A, ab: (this: void, a: A) => B): B;
|
|
47
|
-
declare function pipe<A, B, C>(a: A, ab: (this: void, a: A) => B, bc: (this: void, b: B) => C): C;
|
|
48
|
-
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;
|
|
49
|
-
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;
|
|
50
|
-
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;
|
|
51
|
-
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;
|
|
52
|
-
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;
|
|
53
|
-
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;
|
|
54
|
-
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;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Creates a new function that is the composition of the provided functions, applied from left to right.
|
|
58
|
-
* Unlike pipe, flow doesn't immediately execute the functions but returns a new function that,
|
|
59
|
-
* when called, will run the composed functions in sequence.
|
|
60
|
-
*
|
|
61
|
-
* @example
|
|
62
|
-
* // Basic usage
|
|
63
|
-
* const processNumber = flow(
|
|
64
|
-
* (n: number) => n * 2,
|
|
65
|
-
* (n) => n + 1,
|
|
66
|
-
* (n) => n.toString()
|
|
67
|
-
* );
|
|
68
|
-
*
|
|
69
|
-
* const result = processNumber(5); // "11"
|
|
70
|
-
*
|
|
71
|
-
* @example
|
|
72
|
-
* // With multiple arguments
|
|
73
|
-
* const formatName = flow(
|
|
74
|
-
* (first: string, last: string) => `${first} ${last}`,
|
|
75
|
-
* (name) => name.toUpperCase()
|
|
76
|
-
* );
|
|
77
|
-
*
|
|
78
|
-
* const name = formatName("John", "Doe"); // "JOHN DOE"
|
|
79
|
-
*
|
|
80
|
-
* @example
|
|
81
|
-
* // With Option type
|
|
82
|
-
* import { some, none, mapOption, flatMapOption, type Option } from "@railway-ts/pipelines/option";
|
|
83
|
-
*
|
|
84
|
-
* const safeParseInt = (radix: number) => (str: string): Option<number> => {
|
|
85
|
-
* const n = Number.parseInt(str, radix);
|
|
86
|
-
* return Number.isNaN(n) ? none() : some(n);
|
|
87
|
-
* };
|
|
88
|
-
*
|
|
89
|
-
* const processString = flow(
|
|
90
|
-
* (s: string) => some(s),
|
|
91
|
-
* (o) => mapOption(o, (s) => s + "0"),
|
|
92
|
-
* (o) => flatMapOption(o, safeParseInt(10))
|
|
93
|
-
* );
|
|
94
|
-
*
|
|
95
|
-
* const option = processString("42"); // Option<number> containing 420
|
|
96
|
-
*
|
|
97
|
-
* @example
|
|
98
|
-
* // With Result type
|
|
99
|
-
* import { ok, err, flatMapResult } from "@railway-ts/pipelines/result";
|
|
100
|
-
*
|
|
101
|
-
* const validatePositive = (n: number) => (n > 0 ? ok(n) : err("Not positive"));
|
|
102
|
-
*
|
|
103
|
-
* const validateNumber = flow(
|
|
104
|
-
* (n: number) => ok(n),
|
|
105
|
-
* (r) => flatMapResult(r, validatePositive),
|
|
106
|
-
* (r) => flatMapResult(r, (n) => ok(n * 2))
|
|
107
|
-
* );
|
|
108
|
-
*
|
|
109
|
-
* const result = validateNumber(5); // Result<number, string> containing 10
|
|
110
|
-
*
|
|
111
|
-
* @param ab - The first function in the composition
|
|
112
|
-
* @param functions - Additional functions to compose in sequence
|
|
113
|
-
* @returns A new function that applies all the composed functions in sequence
|
|
114
|
-
*/
|
|
115
|
-
declare function flow<A extends unknown[], B>(ab: (this: void, ...a: A) => B): (...a: A) => B;
|
|
116
|
-
declare function flow<A extends unknown[], B, C>(ab: (this: void, ...a: A) => B, bc: (this: void, b: B) => C): (...a: A) => C;
|
|
117
|
-
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;
|
|
118
|
-
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;
|
|
119
|
-
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;
|
|
120
|
-
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;
|
|
121
|
-
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;
|
|
122
|
-
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;
|
|
123
|
-
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;
|
|
124
|
-
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;
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Pipes a value through a series of potentially async functions, from left to right.
|
|
128
|
-
* Each function receives the awaited output of the previous function.
|
|
129
|
-
* Unlike pipe, pipeAsync awaits between each step, enabling async composition.
|
|
130
|
-
*
|
|
131
|
-
* @example
|
|
132
|
-
* // Basic async usage
|
|
133
|
-
* const result = await pipeAsync(
|
|
134
|
-
* 5,
|
|
135
|
-
* async (n) => n * 2, // 10
|
|
136
|
-
* (n) => n + 1, // 11
|
|
137
|
-
* async (n) => n.toString() // "11"
|
|
138
|
-
* );
|
|
139
|
-
* // result: "11"
|
|
140
|
-
*
|
|
141
|
-
* @example
|
|
142
|
-
* // With Result and curried helpers
|
|
143
|
-
* import { ok, flatMapWith, mapWith } from "@railway-ts/pipelines/result";
|
|
144
|
-
*
|
|
145
|
-
* const result = await pipeAsync(
|
|
146
|
-
* ok(5),
|
|
147
|
-
* flatMapWith(async (n) => ok(n * 2)),
|
|
148
|
-
* mapWith((n) => n.toString())
|
|
149
|
-
* );
|
|
150
|
-
* // result: Result<string, never> containing "10"
|
|
151
|
-
*
|
|
152
|
-
* @param a - The initial value (or Promise) to pipe through the functions
|
|
153
|
-
* @param ab - The first function to apply to the initial value
|
|
154
|
-
* @param functions - Additional functions to apply in sequence
|
|
155
|
-
* @returns A Promise of the final result after applying all functions
|
|
156
|
-
*/
|
|
157
|
-
declare function pipeAsync<A, B>(a: MaybeAsync<A>, ab: (this: void, a: A) => MaybeAsync<B>): Promise<B>;
|
|
158
|
-
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>;
|
|
159
|
-
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>;
|
|
160
|
-
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>;
|
|
161
|
-
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>;
|
|
162
|
-
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>;
|
|
163
|
-
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>;
|
|
164
|
-
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>;
|
|
165
|
-
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>;
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Creates a new function that is the async composition of the provided functions, applied from left to right.
|
|
169
|
-
* Unlike flow, flowAsync awaits between each step, enabling async composition.
|
|
170
|
-
* The returned function always returns a Promise.
|
|
171
|
-
*
|
|
172
|
-
* @example
|
|
173
|
-
* // Basic async usage
|
|
174
|
-
* const processNumber = flowAsync(
|
|
175
|
-
* async (n: number) => n * 2,
|
|
176
|
-
* (n) => n + 1,
|
|
177
|
-
* async (n) => n.toString()
|
|
178
|
-
* );
|
|
179
|
-
*
|
|
180
|
-
* const result = await processNumber(5); // "11"
|
|
181
|
-
*
|
|
182
|
-
* @example
|
|
183
|
-
* // With Result and curried helpers
|
|
184
|
-
* import { ok, flatMapWith, mapWith } from "@railway-ts/pipelines/result";
|
|
185
|
-
*
|
|
186
|
-
* const process = flowAsync(
|
|
187
|
-
* (n: number) => ok(n),
|
|
188
|
-
* flatMapWith(async (n) => ok(n * 2)),
|
|
189
|
-
* mapWith((n) => n.toString())
|
|
190
|
-
* );
|
|
191
|
-
*
|
|
192
|
-
* const result = await process(5); // Result<string, never> containing "10"
|
|
193
|
-
*
|
|
194
|
-
* @param ab - The first function in the composition
|
|
195
|
-
* @param functions - Additional functions to compose in sequence
|
|
196
|
-
* @returns A new function that applies all the composed functions in sequence, returning a Promise
|
|
197
|
-
*/
|
|
198
|
-
declare function flowAsync<A extends unknown[], B>(ab: (this: void, ...a: A) => MaybeAsync<B>): (...a: A) => Promise<B>;
|
|
199
|
-
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>;
|
|
200
|
-
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>;
|
|
201
|
-
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>;
|
|
202
|
-
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>;
|
|
203
|
-
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>;
|
|
204
|
-
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>;
|
|
205
|
-
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>;
|
|
206
|
-
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>;
|
|
207
|
-
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>;
|
|
208
|
-
|
|
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
|
-
declare function curry<A, B, R>(fn: (a: A, b: B) => R): (a: A) => (b: B) => R;
|
|
245
|
-
declare function curry<A, B, C, R>(fn: (a: A, b: B, c: C) => R): (a: A) => (b: B) => (c: C) => R;
|
|
246
|
-
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
|
-
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
|
-
/**
|
|
250
|
-
* Uncurries a curried unary function chain back into a multi-argument function.
|
|
251
|
-
* Inverse of `curry`. Useful when calling curried APIs with positional arguments.
|
|
252
|
-
*
|
|
253
|
-
* Note: Expects strictly unary nesting (the shape produced by `curry`).
|
|
254
|
-
*
|
|
255
|
-
* @example
|
|
256
|
-
* // 2-arity
|
|
257
|
-
* const addCurried = (a: number) => (b: number) => a + b;
|
|
258
|
-
* const add = uncurry(addCurried);
|
|
259
|
-
* add(5, 3); // 8
|
|
260
|
-
*
|
|
261
|
-
* @example
|
|
262
|
-
* // 3-arity
|
|
263
|
-
* const multiplyCurried = (a: number) => (b: number) => (c: number) => a * b * c;
|
|
264
|
-
* const multiply = uncurry(multiplyCurried);
|
|
265
|
-
* multiply(2, 3, 4); // 24
|
|
266
|
-
*
|
|
267
|
-
* @example
|
|
268
|
-
* // Round-trip with curry
|
|
269
|
-
* import { curry } from "@railway-ts/pipelines";
|
|
270
|
-
* const divide = (divisor: number, dividend: number) => dividend / divisor;
|
|
271
|
-
* const divideCurried = curry(divide);
|
|
272
|
-
* const divideUncurried = uncurry(divideCurried);
|
|
273
|
-
* divideUncurried(2, 100); // 50
|
|
274
|
-
*
|
|
275
|
-
* @param fn - A curried unary function chain (e.g., a => b => c => r)
|
|
276
|
-
* @returns A multi-argument function equivalent to applying the curried chain in sequence
|
|
277
|
-
*/
|
|
278
|
-
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
|
-
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;
|
|
280
|
-
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;
|
|
281
|
-
declare function uncurry<A, B, C, R>(fn: (a: A) => (b: B) => (c: C) => R): (a: A, b: B, c: C) => R;
|
|
282
|
-
declare function uncurry<A, B, R>(fn: (a: A) => (b: B) => R): (a: A, b: B) => R;
|
|
283
|
-
declare function uncurry<A, R>(fn: (a: A) => R): (a: A) => R;
|
|
284
|
-
|
|
285
|
-
/**
|
|
286
|
-
* Transforms a multi-argument function into one that accepts a single tuple argument.
|
|
287
|
-
* This makes multi-arg functions composable in `pipe`/`flow` when your data is a tuple.
|
|
288
|
-
*
|
|
289
|
-
* @example
|
|
290
|
-
* // Basic
|
|
291
|
-
* const add = (a: number, b: number) => a + b;
|
|
292
|
-
* const tupledAdd = tupled(add);
|
|
293
|
-
* tupledAdd([5, 3]); // 8
|
|
294
|
-
*
|
|
295
|
-
* @example
|
|
296
|
-
* // With pipe (tuple input)
|
|
297
|
-
* import { pipe } from "@/utils";
|
|
298
|
-
* const distance = (x: number, y: number) => Math.hypot(x, y);
|
|
299
|
-
* pipe([3, 4] as [number, number], tupled(distance)); // 5
|
|
300
|
-
*
|
|
301
|
-
* @example
|
|
302
|
-
* // With Result
|
|
303
|
-
* import { ok, err, flatMapResult, mapResult, type Result } from "@railway-ts/pipelines/result";
|
|
304
|
-
* const divide = (dividend: number, divisor: number): Result<number, string> =>
|
|
305
|
-
* divisor === 0 ? err("Division by zero") : ok(dividend / divisor);
|
|
306
|
-
* const tupledDivide = tupled(divide);
|
|
307
|
-
* pipe(ok<[number, number], string>([10, 2]),
|
|
308
|
-
* (r) => flatMapResult(r, tupledDivide),
|
|
309
|
-
* (r) => mapResult(r, (n) => n * 2)
|
|
310
|
-
* ); // ok(10)
|
|
311
|
-
*
|
|
312
|
-
* @example
|
|
313
|
-
* // With Option
|
|
314
|
-
* import { some, none, flatMapOption, type Option } from "@railway-ts/pipelines/option";
|
|
315
|
-
* const safeParse = (str: string, radix: number): Option<number> => {
|
|
316
|
-
* const n = Number.parseInt(str, radix);
|
|
317
|
-
* return Number.isNaN(n) ? none() : some(n);
|
|
318
|
-
* };
|
|
319
|
-
* const tupledParse = tupled(safeParse);
|
|
320
|
-
* flatMapOption(some(["FF", 16] as [string, number]), tupledParse); // some(255)
|
|
321
|
-
*
|
|
322
|
-
* @param fn - A function taking positional arguments to be adapted to a single tuple argument
|
|
323
|
-
* @returns A function that takes a tuple and applies it to `fn` as positional args
|
|
324
|
-
*/
|
|
325
|
-
declare function tupled<A, B, R>(fn: (a: A, b: B) => R): (args: [A, B]) => R;
|
|
326
|
-
declare function tupled<A, B, C, R>(fn: (a: A, b: B, c: C) => R): (args: [A, B, C]) => R;
|
|
327
|
-
declare function tupled<A, B, C, D, R>(fn: (a: A, b: B, c: C, d: D) => R): (args: [A, B, C, D]) => R;
|
|
328
|
-
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;
|
|
329
|
-
|
|
330
|
-
/**
|
|
331
|
-
* Transforms a function that accepts a tuple into a multi-argument function.
|
|
332
|
-
* Inverse of `tupled`. Useful when callers have positional arguments but your core API uses tuples.
|
|
333
|
-
*
|
|
334
|
-
* @example
|
|
335
|
-
* // Basic
|
|
336
|
-
* const tupledAdd = ([a, b]: [number, number]) => a + b;
|
|
337
|
-
* const add = untupled(tupledAdd);
|
|
338
|
-
* add(5, 3); // 8
|
|
339
|
-
*
|
|
340
|
-
* @example
|
|
341
|
-
* // 3-arity
|
|
342
|
-
* const sum3Tupled = ([a, b, c]: [number, number, number]) => a + b + c;
|
|
343
|
-
* const sum3 = untupled(sum3Tupled);
|
|
344
|
-
* sum3(1, 2, 3); // 6
|
|
345
|
-
*
|
|
346
|
-
* @example
|
|
347
|
-
* // With Result
|
|
348
|
-
* import { ok, err, flatMapResult, type Result } from "@railway-ts/pipelines/result";
|
|
349
|
-
* const validateRange = ([min, max, value]: [number, number, number]): Result<number, string> =>
|
|
350
|
-
* value >= min && value <= max ? ok(value) : err(`Value ${value} not between ${min} and ${max}`);
|
|
351
|
-
* const validate = untupled(validateRange);
|
|
352
|
-
* flatMapResult(ok(50), (n) => validate(0, 100, n)); // ok(50)
|
|
353
|
-
*
|
|
354
|
-
* @example
|
|
355
|
-
* // With Option
|
|
356
|
-
* import { some, none, type Option } from "@railway-ts/pipelines/option";
|
|
357
|
-
* const parsePair = ([x, y]: [string, string]): Option<number> => {
|
|
358
|
-
* const a = Number(x), b = Number(y);
|
|
359
|
-
* return Number.isNaN(a) || Number.isNaN(b) ? none() : some(a + b);
|
|
360
|
-
* };
|
|
361
|
-
* const parse = untupled(parsePair);
|
|
362
|
-
* parse("2", "3"); // some(5)
|
|
363
|
-
*
|
|
364
|
-
* @param fn - A function that accepts a tuple of arguments
|
|
365
|
-
* @returns A function that accepts the same arguments positionally
|
|
366
|
-
*/
|
|
367
|
-
declare function untupled<A, B, R>(fn: (args: [A, B]) => R): (a: A, b: B) => R;
|
|
368
|
-
declare function untupled<A, B, C, R>(fn: (args: [A, B, C]) => R): (a: A, b: B, c: C) => R;
|
|
369
|
-
declare function untupled<A, B, C, D, R>(fn: (args: [A, B, C, D]) => R): (a: A, b: B, c: C, d: D) => R;
|
|
370
|
-
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;
|
|
371
|
-
|
|
372
|
-
export { curry, flow, flowAsync, pipe, pipeAsync, tupled, uncurry, untupled };
|
|
373
|
-
export type { MaybeAsync, UnknownFunction };
|
package/dist/option/index.cjs
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// src/result/result.ts
|
|
4
|
-
var RESULT_BRAND = /* @__PURE__ */ Symbol("RESULT_BRAND");
|
|
5
|
-
function ok(value) {
|
|
6
|
-
return {
|
|
7
|
-
ok: true,
|
|
8
|
-
value,
|
|
9
|
-
[RESULT_BRAND]: "ok"
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
function err(error) {
|
|
13
|
-
return { ok: false, error, [RESULT_BRAND]: "error" };
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// src/option/option.ts
|
|
17
|
-
var OPTION_BRAND = /* @__PURE__ */ Symbol("OPTION_BRAND");
|
|
18
|
-
function some(value) {
|
|
19
|
-
return {
|
|
20
|
-
some: true,
|
|
21
|
-
value,
|
|
22
|
-
[OPTION_BRAND]: "some"
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
function none() {
|
|
26
|
-
return {
|
|
27
|
-
some: false,
|
|
28
|
-
[OPTION_BRAND]: "none"
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
function isSome(option) {
|
|
32
|
-
return option.some;
|
|
33
|
-
}
|
|
34
|
-
function isNone(option) {
|
|
35
|
-
return !option.some;
|
|
36
|
-
}
|
|
37
|
-
function map(option, fn) {
|
|
38
|
-
return option.some ? some(fn(option.value)) : none();
|
|
39
|
-
}
|
|
40
|
-
function flatMap(option, fn) {
|
|
41
|
-
return option.some ? fn(option.value) : none();
|
|
42
|
-
}
|
|
43
|
-
function bimap(option, someFn, noneFn) {
|
|
44
|
-
return option.some ? someFn(option.value) : noneFn();
|
|
45
|
-
}
|
|
46
|
-
function filter(option, predicate) {
|
|
47
|
-
return option.some && predicate(option.value) ? option : none();
|
|
48
|
-
}
|
|
49
|
-
function tap(option, fn) {
|
|
50
|
-
if (isSome(option)) {
|
|
51
|
-
const callback = fn;
|
|
52
|
-
callback(option.value);
|
|
53
|
-
}
|
|
54
|
-
return option;
|
|
55
|
-
}
|
|
56
|
-
function match(option, patterns) {
|
|
57
|
-
if (isSome(option)) {
|
|
58
|
-
const someFn = patterns.some;
|
|
59
|
-
return someFn(option.value);
|
|
60
|
-
}
|
|
61
|
-
const noneFn = patterns.none;
|
|
62
|
-
return noneFn();
|
|
63
|
-
}
|
|
64
|
-
function unwrap(option, errorMsg) {
|
|
65
|
-
if (option.some) {
|
|
66
|
-
return option.value;
|
|
67
|
-
}
|
|
68
|
-
throw new Error(errorMsg || "Cannot unwrap a none Option");
|
|
69
|
-
}
|
|
70
|
-
function unwrapOr(option, defaultValue) {
|
|
71
|
-
return option.some ? option.value : defaultValue;
|
|
72
|
-
}
|
|
73
|
-
function unwrapOrElse(option, defaultFn) {
|
|
74
|
-
return option.some ? option.value : defaultFn();
|
|
75
|
-
}
|
|
76
|
-
function combine(options) {
|
|
77
|
-
const values = [];
|
|
78
|
-
for (const option of options) {
|
|
79
|
-
if (!option.some) return none();
|
|
80
|
-
values.push(option.value);
|
|
81
|
-
}
|
|
82
|
-
return some(values);
|
|
83
|
-
}
|
|
84
|
-
var fromNullable = (value) => {
|
|
85
|
-
return value === null || value === void 0 ? none() : some(value);
|
|
86
|
-
};
|
|
87
|
-
function mapToResult(option, error) {
|
|
88
|
-
return option.some ? ok(option.value) : err(error);
|
|
89
|
-
}
|
|
90
|
-
function mapWith(fn) {
|
|
91
|
-
return (option) => map(option, fn);
|
|
92
|
-
}
|
|
93
|
-
function flatMapWith(fn) {
|
|
94
|
-
return (option) => {
|
|
95
|
-
if (!option.some) return option;
|
|
96
|
-
return fn(option.value);
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
function filterWith(predicate) {
|
|
100
|
-
return (option) => filter(option, predicate);
|
|
101
|
-
}
|
|
102
|
-
function tapWith(fn) {
|
|
103
|
-
return (option) => {
|
|
104
|
-
if (!option.some) return option;
|
|
105
|
-
const out = fn(option.value);
|
|
106
|
-
if (out instanceof Promise) return out.then(() => option);
|
|
107
|
-
return option;
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
function mapToResultWith(error) {
|
|
111
|
-
return (option) => mapToResult(option, error);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
exports.OPTION_BRAND = OPTION_BRAND;
|
|
115
|
-
exports.bimap = bimap;
|
|
116
|
-
exports.combine = combine;
|
|
117
|
-
exports.filter = filter;
|
|
118
|
-
exports.filterWith = filterWith;
|
|
119
|
-
exports.flatMap = flatMap;
|
|
120
|
-
exports.flatMapWith = flatMapWith;
|
|
121
|
-
exports.fromNullable = fromNullable;
|
|
122
|
-
exports.isNone = isNone;
|
|
123
|
-
exports.isSome = isSome;
|
|
124
|
-
exports.map = map;
|
|
125
|
-
exports.mapToResult = mapToResult;
|
|
126
|
-
exports.mapToResultWith = mapToResultWith;
|
|
127
|
-
exports.mapWith = mapWith;
|
|
128
|
-
exports.match = match;
|
|
129
|
-
exports.none = none;
|
|
130
|
-
exports.some = some;
|
|
131
|
-
exports.tap = tap;
|
|
132
|
-
exports.tapWith = tapWith;
|
|
133
|
-
exports.unwrap = unwrap;
|
|
134
|
-
exports.unwrapOr = unwrapOr;
|
|
135
|
-
exports.unwrapOrElse = unwrapOrElse;
|
|
136
|
-
//# sourceMappingURL=index.cjs.map
|
|
137
|
-
//# sourceMappingURL=index.cjs.map
|