functype 0.18.0 → 0.20.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 +13 -11
- package/README.processed.md +2 -11
- package/dist/Brand-B-0nKo7I.d.ts +55 -0
- package/dist/Brand-Cfr5zy8F.js +2 -0
- package/dist/Brand-Cfr5zy8F.js.map +1 -0
- package/dist/Tuple-CKxIyX7l.d.ts +178 -0
- package/dist/Tuple-CgX4p79w.js +2 -0
- package/dist/Tuple-CgX4p79w.js.map +1 -0
- package/dist/branded/index.d.ts +2 -54
- package/dist/branded/index.js +1 -0
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +660 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/do/index.d.ts +4 -240
- package/dist/do/index.js +1 -0
- package/dist/either/index.d.ts +4 -2
- package/dist/either/index.js +1 -0
- package/dist/fpromise/index.d.ts +4 -373
- package/dist/fpromise/index.js +1 -0
- package/dist/index-Bnjlo4cT.d.ts +3575 -0
- package/dist/index.d.ts +4 -1934
- package/dist/index.js +1 -0
- package/dist/list/index.d.ts +4 -2
- package/dist/list/index.js +1 -0
- package/dist/map/index.d.ts +4 -58
- package/dist/map/index.js +1 -0
- package/dist/option/index.d.ts +4 -2
- package/dist/option/index.js +1 -0
- package/dist/set/index.d.ts +4 -2
- package/dist/set/index.js +1 -0
- package/dist/src-BOxI0-73.js +20 -0
- package/dist/src-BOxI0-73.js.map +1 -0
- package/dist/try/index.d.ts +4 -2
- package/dist/try/index.js +1 -0
- package/dist/tuple/index.d.ts +2 -75
- package/dist/tuple/index.js +1 -0
- package/package.json +31 -42
- package/dist/Either-Ccg0S1uS.d.ts +0 -1015
- package/dist/Typeable-DiGVtDnq.d.ts +0 -96
- package/dist/branded/index.mjs +0 -2
- package/dist/branded/index.mjs.map +0 -1
- package/dist/chunk-6ZIR6EKC.mjs +0 -38
- package/dist/chunk-6ZIR6EKC.mjs.map +0 -1
- package/dist/chunk-7JJIUQXK.mjs +0 -3
- package/dist/chunk-7JJIUQXK.mjs.map +0 -1
- package/dist/chunk-OR6V4TCO.mjs +0 -2
- package/dist/chunk-OR6V4TCO.mjs.map +0 -1
- package/dist/do/index.mjs +0 -2
- package/dist/do/index.mjs.map +0 -1
- package/dist/either/index.mjs +0 -2
- package/dist/either/index.mjs.map +0 -1
- package/dist/fpromise/index.mjs +0 -2
- package/dist/fpromise/index.mjs.map +0 -1
- package/dist/index.mjs +0 -2
- package/dist/index.mjs.map +0 -1
- package/dist/list/index.mjs +0 -2
- package/dist/list/index.mjs.map +0 -1
- package/dist/map/index.mjs +0 -2
- package/dist/map/index.mjs.map +0 -1
- package/dist/option/index.mjs +0 -2
- package/dist/option/index.mjs.map +0 -1
- package/dist/set/index.mjs +0 -2
- package/dist/set/index.mjs.map +0 -1
- package/dist/try/index.mjs +0 -2
- package/dist/try/index.mjs.map +0 -1
- package/dist/tuple/index.mjs +0 -2
- package/dist/tuple/index.mjs.map +0 -1
|
@@ -0,0 +1,3575 @@
|
|
|
1
|
+
import { t as Brand } from "./Brand-B-0nKo7I.js";
|
|
2
|
+
import { c as Pipe, l as Foldable, o as Serializable, r as Typeable, t as Tuple, u as Type } from "./Tuple-CKxIyX7l.js";
|
|
3
|
+
|
|
4
|
+
//#region src/do/protocol.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Protocol definitions for Do-notation
|
|
8
|
+
* Separated from main Do module to avoid circular dependencies
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Result type for Do-notation unwrapping
|
|
12
|
+
* Indicates whether unwrapping succeeded and provides the value or error
|
|
13
|
+
*/
|
|
14
|
+
type DoResult<T$1> = {
|
|
15
|
+
ok: true;
|
|
16
|
+
value: T$1;
|
|
17
|
+
} | {
|
|
18
|
+
ok: false;
|
|
19
|
+
empty: true;
|
|
20
|
+
} | {
|
|
21
|
+
ok: false;
|
|
22
|
+
empty: false;
|
|
23
|
+
error: unknown;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Interface for types that support Do-notation
|
|
27
|
+
* Implementing this interface allows a type to be yielded in Do-comprehensions
|
|
28
|
+
*
|
|
29
|
+
* The doUnwrap method should return a DoResult indicating success/failure
|
|
30
|
+
* and the contained value or error information
|
|
31
|
+
*/
|
|
32
|
+
interface Doable<T$1> {
|
|
33
|
+
doUnwrap(): DoResult<T$1>;
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/extractable/Extractable.d.ts
|
|
37
|
+
/**
|
|
38
|
+
* Interface for operations that can fail with exceptions.
|
|
39
|
+
* These methods should be used with care as they can throw at runtime.
|
|
40
|
+
*
|
|
41
|
+
* @interface Unsafe
|
|
42
|
+
* @template T The type of value that can be extracted
|
|
43
|
+
*/
|
|
44
|
+
interface Unsafe<T$1 extends Type> {
|
|
45
|
+
/**
|
|
46
|
+
* Extract the value or throw an error
|
|
47
|
+
* @param error Optional custom error to throw. If not provided, uses type-appropriate default error
|
|
48
|
+
* @throws {Error} The specified error, container's error, or a sensible default
|
|
49
|
+
* @returns The contained value
|
|
50
|
+
*/
|
|
51
|
+
orThrow(error?: Error): T$1;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Extractable type class for data structures that can extract their values
|
|
55
|
+
* with various fallback strategies.
|
|
56
|
+
*
|
|
57
|
+
* This interface is implemented by Option, Either, and other types that
|
|
58
|
+
* wrap values and need both safe and fallible extraction methods.
|
|
59
|
+
*
|
|
60
|
+
* Extends Unsafe to provide exception-throwing operations alongside safe alternatives.
|
|
61
|
+
*/
|
|
62
|
+
interface Extractable<T$1 extends Type> extends Unsafe<T$1> {
|
|
63
|
+
/**
|
|
64
|
+
* Returns the contained value or a default value
|
|
65
|
+
* @param defaultValue - The value to return if extraction fails
|
|
66
|
+
* @returns The contained value or defaultValue
|
|
67
|
+
*/
|
|
68
|
+
orElse(defaultValue: T$1): T$1;
|
|
69
|
+
/**
|
|
70
|
+
* Returns this container if it has a value, otherwise returns the alternative container
|
|
71
|
+
* @param alternative - The alternative container
|
|
72
|
+
* @returns This container or the alternative
|
|
73
|
+
*/
|
|
74
|
+
or(alternative: Extractable<T$1>): Extractable<T$1>;
|
|
75
|
+
/**
|
|
76
|
+
* Returns the contained value or null
|
|
77
|
+
* @returns The contained value or null
|
|
78
|
+
*/
|
|
79
|
+
orNull(): T$1 | null;
|
|
80
|
+
/**
|
|
81
|
+
* Returns the contained value or undefined
|
|
82
|
+
* @returns The contained value or undefined
|
|
83
|
+
*/
|
|
84
|
+
orUndefined(): T$1 | undefined;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Type guard to check if a value implements Extractable
|
|
88
|
+
*/
|
|
89
|
+
declare function isExtractable<T$1 extends Type>(value: unknown): value is Extractable<T$1>;
|
|
90
|
+
//#endregion
|
|
91
|
+
//#region src/list/LazyList.d.ts
|
|
92
|
+
/**
|
|
93
|
+
* LazyList provides lazy evaluation for list operations.
|
|
94
|
+
* Operations are deferred until the list is materialized.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* // Basic lazy evaluation
|
|
98
|
+
* const result = LazyList([1, 2, 3, 4, 5])
|
|
99
|
+
* .map(x => x * 2)
|
|
100
|
+
* .filter(x => x > 5)
|
|
101
|
+
* .toArray() // [6, 8, 10]
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* // Infinite sequences with take
|
|
105
|
+
* const fibonacci = LazyList.iterate([0, 1], ([a, b]) => [b, a + b])
|
|
106
|
+
* .map(([a]) => a)
|
|
107
|
+
* .take(10)
|
|
108
|
+
* .toArray() // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
|
|
109
|
+
*/
|
|
110
|
+
interface LazyList<A extends Type> extends Foldable<A>, Pipe<LazyList<A>>, Serializable<LazyList<A>>, Typeable<"LazyList"> {
|
|
111
|
+
[Symbol.iterator](): Iterator<A>;
|
|
112
|
+
map<B extends Type>(f: (a: A) => B): LazyList<B>;
|
|
113
|
+
flatMap<B extends Type>(f: (a: A) => LazyList<B>): LazyList<B>;
|
|
114
|
+
filter(predicate: (a: A) => boolean): LazyList<A>;
|
|
115
|
+
take(n: number): LazyList<A>;
|
|
116
|
+
drop(n: number): LazyList<A>;
|
|
117
|
+
takeWhile(predicate: (a: A) => boolean): LazyList<A>;
|
|
118
|
+
dropWhile(predicate: (a: A) => boolean): LazyList<A>;
|
|
119
|
+
concat(other: LazyList<A>): LazyList<A>;
|
|
120
|
+
zip<B extends Type>(other: LazyList<B>): LazyList<[A, B]>;
|
|
121
|
+
toList(): List<A>;
|
|
122
|
+
toArray(): A[];
|
|
123
|
+
forEach(f: (a: A) => void): void;
|
|
124
|
+
reduce<B extends Type>(f: (acc: B, a: A) => B, initial: B): B;
|
|
125
|
+
find(predicate: (a: A) => boolean): Option<A>;
|
|
126
|
+
some(predicate: (a: A) => boolean): boolean;
|
|
127
|
+
every(predicate: (a: A) => boolean): boolean;
|
|
128
|
+
count(): number;
|
|
129
|
+
first(): Option<A>;
|
|
130
|
+
last(): Option<A>;
|
|
131
|
+
toString(): string;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Lazy list implementation for efficient deferred computation
|
|
135
|
+
* @example
|
|
136
|
+
* // Process large datasets efficiently
|
|
137
|
+
* const result = LazyList.range(1, 1000000)
|
|
138
|
+
* .filter(x => x % 2 === 0)
|
|
139
|
+
* .map(x => x * x)
|
|
140
|
+
* .take(5)
|
|
141
|
+
* .toArray() // [4, 16, 36, 64, 100]
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* // Infinite sequences
|
|
145
|
+
* const primes = LazyList.iterate(2, n => n + 1)
|
|
146
|
+
* .filter(isPrime)
|
|
147
|
+
* .take(10)
|
|
148
|
+
* .toArray() // First 10 prime numbers
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* // Combining operations
|
|
152
|
+
* const evens = LazyList.range(0, 100, 2)
|
|
153
|
+
* const odds = LazyList.range(1, 100, 2)
|
|
154
|
+
* const combined = evens.zip(odds)
|
|
155
|
+
* .map(([e, o]) => e + o)
|
|
156
|
+
* .take(5)
|
|
157
|
+
* .toArray() // [1, 5, 9, 13, 17]
|
|
158
|
+
*/
|
|
159
|
+
declare const LazyList: (<A extends Type>(iterable: Iterable<A>) => LazyList<A>) & {
|
|
160
|
+
/**
|
|
161
|
+
* Create an empty LazyList
|
|
162
|
+
* @example
|
|
163
|
+
* const empty = LazyList.empty<number>()
|
|
164
|
+
* empty.toArray() // []
|
|
165
|
+
*/
|
|
166
|
+
empty: <A extends Type>() => LazyList<A>;
|
|
167
|
+
/**
|
|
168
|
+
* Create a LazyList from a single value
|
|
169
|
+
* @example
|
|
170
|
+
* const single = LazyList.of(42)
|
|
171
|
+
* .map(x => x * 2)
|
|
172
|
+
* .toArray() // [84]
|
|
173
|
+
*/
|
|
174
|
+
of: <A extends Type>(value: A) => LazyList<A>;
|
|
175
|
+
/**
|
|
176
|
+
* Create a LazyList from multiple values
|
|
177
|
+
*/
|
|
178
|
+
from: <A extends Type>(...values: A[]) => LazyList<A>;
|
|
179
|
+
/**
|
|
180
|
+
* Create an infinite LazyList by repeatedly applying a function
|
|
181
|
+
* @example
|
|
182
|
+
* // Powers of 2
|
|
183
|
+
* const powers = LazyList.iterate(1, x => x * 2)
|
|
184
|
+
* .take(10)
|
|
185
|
+
* .toArray() // [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* // Fibonacci sequence
|
|
189
|
+
* const fib = LazyList.iterate([0, 1], ([a, b]) => [b, a + b])
|
|
190
|
+
* .map(([a]) => a)
|
|
191
|
+
* .take(8)
|
|
192
|
+
* .toArray() // [0, 1, 1, 2, 3, 5, 8, 13]
|
|
193
|
+
*/
|
|
194
|
+
iterate: <A extends Type>(initial: A, f: (a: A) => A) => LazyList<A>;
|
|
195
|
+
/**
|
|
196
|
+
* Create an infinite LazyList by repeatedly calling a function
|
|
197
|
+
*/
|
|
198
|
+
generate: <A extends Type>(f: () => A) => LazyList<A>;
|
|
199
|
+
/**
|
|
200
|
+
* Create a LazyList of numbers from start to end (exclusive)
|
|
201
|
+
* @example
|
|
202
|
+
* LazyList.range(1, 6).toArray() // [1, 2, 3, 4, 5]
|
|
203
|
+
* LazyList.range(0, 10, 2).toArray() // [0, 2, 4, 6, 8]
|
|
204
|
+
* LazyList.range(10, 0, -1).toArray() // [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
|
|
205
|
+
*
|
|
206
|
+
* @example
|
|
207
|
+
* // Sum of squares from 1 to 100
|
|
208
|
+
* const sum = LazyList.range(1, 101)
|
|
209
|
+
* .map(x => x * x)
|
|
210
|
+
* .reduce((a, b) => a + b, 0) // 338350
|
|
211
|
+
*/
|
|
212
|
+
range: (start: number, end: number, step?: number) => LazyList<number>;
|
|
213
|
+
/**
|
|
214
|
+
* Create a LazyList that repeats a value n times (or infinitely if n is not provided)
|
|
215
|
+
*/
|
|
216
|
+
repeat: <A extends Type>(value: A, n?: number) => LazyList<A>;
|
|
217
|
+
/**
|
|
218
|
+
* Create a LazyList that cycles through an iterable infinitely
|
|
219
|
+
*/
|
|
220
|
+
cycle: <A extends Type>(iterable: Iterable<A>) => LazyList<A>;
|
|
221
|
+
};
|
|
222
|
+
//#endregion
|
|
223
|
+
//#region src/typeclass/ContainerOps.d.ts
|
|
224
|
+
/**
|
|
225
|
+
* Universal operations that work on any container (single-value or collection).
|
|
226
|
+
* These operations make sense for Option, Either, Try, List, Set, etc.
|
|
227
|
+
*
|
|
228
|
+
* @typeParam A - The type of value(s) in the container
|
|
229
|
+
*/
|
|
230
|
+
interface ContainerOps<A extends Type> {
|
|
231
|
+
/**
|
|
232
|
+
* Counts elements that satisfy the predicate.
|
|
233
|
+
* For single-value containers: returns 0 or 1
|
|
234
|
+
* For collections: returns the count of matching elements
|
|
235
|
+
*/
|
|
236
|
+
count(p: (x: A) => boolean): number;
|
|
237
|
+
/**
|
|
238
|
+
* Finds the first element that satisfies the predicate.
|
|
239
|
+
* For single-value containers: returns Some(value) if predicate matches, None otherwise
|
|
240
|
+
* For collections: returns the first matching element wrapped in Option
|
|
241
|
+
*/
|
|
242
|
+
find(p: (a: A) => boolean): Option<A>;
|
|
243
|
+
/**
|
|
244
|
+
* Tests whether any element satisfies the predicate.
|
|
245
|
+
* For single-value containers: tests the single value
|
|
246
|
+
* For collections: returns true if any element matches
|
|
247
|
+
*/
|
|
248
|
+
exists(p: (a: A) => boolean): boolean;
|
|
249
|
+
/**
|
|
250
|
+
* Applies an effect function to each element.
|
|
251
|
+
* For single-value containers: applies to the value if present
|
|
252
|
+
* For collections: applies to each element
|
|
253
|
+
*/
|
|
254
|
+
forEach(f: (a: A) => void): void;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Operations specific to collections (List, Set, etc.).
|
|
258
|
+
* These operations don't make sense for single-value containers.
|
|
259
|
+
*
|
|
260
|
+
* @typeParam A - The element type
|
|
261
|
+
* @typeParam Self - The collection type itself for proper return types
|
|
262
|
+
*/
|
|
263
|
+
interface CollectionOps<A extends Type, Self> {
|
|
264
|
+
/**
|
|
265
|
+
* Drops the first n elements from the collection.
|
|
266
|
+
*/
|
|
267
|
+
drop(n: number): Self;
|
|
268
|
+
/**
|
|
269
|
+
* Drops the last n elements from the collection.
|
|
270
|
+
*/
|
|
271
|
+
dropRight(n: number): Self;
|
|
272
|
+
/**
|
|
273
|
+
* Drops elements from the start while the predicate is true.
|
|
274
|
+
*/
|
|
275
|
+
dropWhile(p: (a: A) => boolean): Self;
|
|
276
|
+
/**
|
|
277
|
+
* Flattens a collection of collections into a single collection.
|
|
278
|
+
*/
|
|
279
|
+
flatten<B>(): Self;
|
|
280
|
+
/**
|
|
281
|
+
* Gets the first element of the collection.
|
|
282
|
+
*/
|
|
283
|
+
get head(): A | undefined;
|
|
284
|
+
/**
|
|
285
|
+
* Gets the first element wrapped in Option.
|
|
286
|
+
*/
|
|
287
|
+
get headOption(): Option<A>;
|
|
288
|
+
/**
|
|
289
|
+
* Converts the collection to an array.
|
|
290
|
+
*/
|
|
291
|
+
toArray<B = A>(): B[];
|
|
292
|
+
}
|
|
293
|
+
//#endregion
|
|
294
|
+
//#region src/typeclass/Functor.d.ts
|
|
295
|
+
/**
|
|
296
|
+
* Functor type class - supports mapping over wrapped values
|
|
297
|
+
*
|
|
298
|
+
* Laws:
|
|
299
|
+
* - Identity: fa.map(x => x) ≡ fa
|
|
300
|
+
* - Composition: fa.map(f).map(g) ≡ fa.map(x => g(f(x)))
|
|
301
|
+
*/
|
|
302
|
+
interface Functor<A extends Type> {
|
|
303
|
+
map<B extends Type>(f: (value: A) => B): Functor<B>;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Applicative functor - supports applying wrapped functions to wrapped values
|
|
307
|
+
*
|
|
308
|
+
* Laws:
|
|
309
|
+
* - Identity: pure(x => x).ap(v) ≡ v
|
|
310
|
+
* - Composition: pure(compose).ap(u).ap(v).ap(w) ≡ u.ap(v.ap(w))
|
|
311
|
+
* - Homomorphism: pure(f).ap(pure(x)) ≡ pure(f(x))
|
|
312
|
+
* - Interchange: u.ap(pure(y)) ≡ pure(f => f(y)).ap(u)
|
|
313
|
+
*/
|
|
314
|
+
interface Applicative<A extends Type> extends Functor<A> {
|
|
315
|
+
ap<B extends Type>(ff: Applicative<(value: A) => B>): Applicative<B>;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Monad type class - supports flat mapping (chaining) operations
|
|
319
|
+
*
|
|
320
|
+
* Laws:
|
|
321
|
+
* - Left identity: pure(a).flatMap(f) ≡ f(a)
|
|
322
|
+
* - Right identity: m.flatMap(pure) ≡ m
|
|
323
|
+
* - Associativity: m.flatMap(f).flatMap(g) ≡ m.flatMap(x => f(x).flatMap(g))
|
|
324
|
+
*/
|
|
325
|
+
interface Monad<A extends Type> extends Applicative<A> {
|
|
326
|
+
flatMap<B extends Type>(f: (value: A) => Monad<B>): Monad<B>;
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Async monad - supports asynchronous monadic operations
|
|
330
|
+
* Extends Monad so it has map, ap, and flatMap in addition to flatMapAsync
|
|
331
|
+
*/
|
|
332
|
+
interface AsyncMonad<A extends Type> extends Monad<A> {
|
|
333
|
+
flatMapAsync<B extends Type>(f: (value: A) => PromiseLike<AsyncMonad<B>>): PromiseLike<AsyncMonad<B>>;
|
|
334
|
+
}
|
|
335
|
+
//#endregion
|
|
336
|
+
//#region src/typeclass/Promisable.d.ts
|
|
337
|
+
/**
|
|
338
|
+
* Promisable trait - supports conversion to Promise
|
|
339
|
+
*
|
|
340
|
+
* Represents containers or values that can be converted to Promise form.
|
|
341
|
+
* This enables integration with async/await patterns and Promise-based APIs
|
|
342
|
+
* while maintaining functional programming principles.
|
|
343
|
+
*
|
|
344
|
+
* @template A - The type of value contained within the Promise
|
|
345
|
+
*
|
|
346
|
+
* @example
|
|
347
|
+
* ```typescript
|
|
348
|
+
* const either: Either<string, number> = Right(42)
|
|
349
|
+
* const promise: Promise<number> = either.toPromise()
|
|
350
|
+
* // Promise resolves with 42
|
|
351
|
+
*
|
|
352
|
+
* const leftEither: Either<string, number> = Left("error")
|
|
353
|
+
* const failedPromise: Promise<number> = leftEither.toPromise()
|
|
354
|
+
* // Promise rejects with "error"
|
|
355
|
+
* ```
|
|
356
|
+
*/
|
|
357
|
+
interface Promisable<A extends Type> {
|
|
358
|
+
/**
|
|
359
|
+
* Converts this container to a Promise
|
|
360
|
+
*
|
|
361
|
+
* The behavior depends on the implementing container:
|
|
362
|
+
* - Success/Right/Some containers resolve with their value
|
|
363
|
+
* - Failure/Left/None containers reject with their error/default error
|
|
364
|
+
*
|
|
365
|
+
* @returns A Promise that resolves or rejects based on the container's state
|
|
366
|
+
*/
|
|
367
|
+
toPromise(): Promise<A>;
|
|
368
|
+
}
|
|
369
|
+
//#endregion
|
|
370
|
+
//#region src/try/Try.d.ts
|
|
371
|
+
/**
|
|
372
|
+
* Possible types of Try instances
|
|
373
|
+
*/
|
|
374
|
+
type TypeNames = "Success" | "Failure";
|
|
375
|
+
interface Try<T$1> extends FunctypeBase<T$1, TypeNames>, Extractable<T$1>, Pipe<T$1>, Promisable<T$1>, Doable<T$1>, Reshapeable<T$1> {
|
|
376
|
+
readonly _tag: TypeNames;
|
|
377
|
+
readonly error: Error | undefined;
|
|
378
|
+
isSuccess(): this is Try<T$1> & {
|
|
379
|
+
readonly _tag: "Success";
|
|
380
|
+
error: undefined;
|
|
381
|
+
};
|
|
382
|
+
isFailure(): this is Try<T$1> & {
|
|
383
|
+
readonly _tag: "Failure";
|
|
384
|
+
error: Error;
|
|
385
|
+
};
|
|
386
|
+
orElse: (defaultValue: T$1) => T$1;
|
|
387
|
+
orThrow: (error?: Error) => T$1;
|
|
388
|
+
or: (alternative: Try<T$1>) => Try<T$1>;
|
|
389
|
+
orNull: () => T$1 | null;
|
|
390
|
+
orUndefined: () => T$1 | undefined;
|
|
391
|
+
toOption: () => Option<T$1>;
|
|
392
|
+
toEither: <E extends Type>(leftValue: E) => Either<E, T$1>;
|
|
393
|
+
toList: () => List<T$1>;
|
|
394
|
+
toTry: () => Try<T$1>;
|
|
395
|
+
map: <U>(f: (value: T$1) => U) => Try<U>;
|
|
396
|
+
ap: <U>(ff: Try<(value: T$1) => U>) => Try<U>;
|
|
397
|
+
flatMap: <U>(f: (value: T$1) => Try<U>) => Try<U>;
|
|
398
|
+
flatMapAsync: <U>(f: (value: T$1) => Promise<Try<U>>) => Promise<Try<U>>;
|
|
399
|
+
/**
|
|
400
|
+
* Pattern matches over the Try, applying onFailure if Failure and onSuccess if Success
|
|
401
|
+
* @param onFailure - Function to apply if the Try is Failure
|
|
402
|
+
* @param onSuccess - Function to apply if the Try is Success
|
|
403
|
+
* @returns The result of applying the appropriate function
|
|
404
|
+
*/
|
|
405
|
+
fold: <U extends Type>(onFailure: (error: Error) => U, onSuccess: (value: T$1) => U) => U;
|
|
406
|
+
toString: () => string;
|
|
407
|
+
/**
|
|
408
|
+
* Pattern matches over the Try, applying a handler function based on the variant
|
|
409
|
+
* @param patterns - Object with handler functions for Success and Failure variants
|
|
410
|
+
* @returns The result of applying the matching handler function
|
|
411
|
+
*/
|
|
412
|
+
match<R$1>(patterns: {
|
|
413
|
+
Success: (value: T$1) => R$1;
|
|
414
|
+
Failure: (error: Error) => R$1;
|
|
415
|
+
}): R$1;
|
|
416
|
+
toValue(): {
|
|
417
|
+
_tag: TypeNames;
|
|
418
|
+
value: T$1 | Error;
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
declare const Try: (<T$1>(f: () => T$1) => Try<T$1>) & {
|
|
422
|
+
/**
|
|
423
|
+
* Type guard to check if a Try is Success
|
|
424
|
+
* @param tryValue - The Try to check
|
|
425
|
+
* @returns True if Try is Success
|
|
426
|
+
*/
|
|
427
|
+
isSuccess: <T$1>(tryValue: Try<T$1>) => tryValue is Try<T$1> & {
|
|
428
|
+
readonly _tag: "Success";
|
|
429
|
+
error: undefined;
|
|
430
|
+
};
|
|
431
|
+
/**
|
|
432
|
+
* Type guard to check if a Try is Failure
|
|
433
|
+
* @param tryValue - The Try to check
|
|
434
|
+
* @returns True if Try is Failure
|
|
435
|
+
*/
|
|
436
|
+
isFailure: <T$1>(tryValue: Try<T$1>) => tryValue is Try<T$1> & {
|
|
437
|
+
readonly _tag: "Failure";
|
|
438
|
+
error: Error;
|
|
439
|
+
};
|
|
440
|
+
/**
|
|
441
|
+
* Creates a Try from JSON string
|
|
442
|
+
* @param json - The JSON string
|
|
443
|
+
* @returns Try instance
|
|
444
|
+
*/
|
|
445
|
+
fromJSON: <T$1>(json: string) => Try<T$1>;
|
|
446
|
+
/**
|
|
447
|
+
* Creates a Try from YAML string
|
|
448
|
+
* @param yaml - The YAML string
|
|
449
|
+
* @returns Try instance
|
|
450
|
+
*/
|
|
451
|
+
fromYAML: <T$1>(yaml: string) => Try<T$1>;
|
|
452
|
+
/**
|
|
453
|
+
* Creates a Try from binary string
|
|
454
|
+
* @param binary - The binary string
|
|
455
|
+
* @returns Try instance
|
|
456
|
+
*/
|
|
457
|
+
fromBinary: <T$1>(binary: string) => Try<T$1>;
|
|
458
|
+
};
|
|
459
|
+
//#endregion
|
|
460
|
+
//#region src/reshapeable/Reshapeable.d.ts
|
|
461
|
+
/**
|
|
462
|
+
* Interface for types that can be reshaped (converted) between different monadic containers.
|
|
463
|
+
* Provides standard conversion methods to transform between Option, Either, List, and Try types.
|
|
464
|
+
*
|
|
465
|
+
* @typeParam T - The type of the value contained in the monad
|
|
466
|
+
*
|
|
467
|
+
* @example
|
|
468
|
+
* // Convert Option to Either
|
|
469
|
+
* const opt = Option(5)
|
|
470
|
+
* const either = opt.toEither("None value") // Right(5)
|
|
471
|
+
*
|
|
472
|
+
* @example
|
|
473
|
+
* // Convert Either to Option
|
|
474
|
+
* const right = Right(10)
|
|
475
|
+
* const option = right.toOption() // Some(10)
|
|
476
|
+
*
|
|
477
|
+
* @example
|
|
478
|
+
* // Convert List to Try
|
|
479
|
+
* const list = List([1, 2, 3])
|
|
480
|
+
* const tryVal = list.toTry() // Success(1) - uses first element
|
|
481
|
+
*
|
|
482
|
+
* @example
|
|
483
|
+
* // Use with Do comprehensions
|
|
484
|
+
* const result = Do(function* () {
|
|
485
|
+
* const x = yield* $(Option(5))
|
|
486
|
+
* const y = yield* $(Right<string, number>(10))
|
|
487
|
+
* return x + y
|
|
488
|
+
* })
|
|
489
|
+
*
|
|
490
|
+
* // Convert to desired type for chaining
|
|
491
|
+
* const asOption = result.toOption()
|
|
492
|
+
* asOption.map(x => x * 2).orElse(0)
|
|
493
|
+
*/
|
|
494
|
+
interface Reshapeable<T$1 extends Type> {
|
|
495
|
+
/**
|
|
496
|
+
* Converts this monad to an Option.
|
|
497
|
+
*
|
|
498
|
+
* Conversion rules:
|
|
499
|
+
* - Option: returns self
|
|
500
|
+
* - Either: Right → Some, Left → None
|
|
501
|
+
* - List: non-empty → Some(head), empty → None
|
|
502
|
+
* - Try: Success → Some, Failure → None
|
|
503
|
+
*
|
|
504
|
+
* @returns An Option containing the value if present, None otherwise
|
|
505
|
+
*/
|
|
506
|
+
toOption(): Option<T$1>;
|
|
507
|
+
/**
|
|
508
|
+
* Converts this monad to an Either.
|
|
509
|
+
*
|
|
510
|
+
* Conversion rules:
|
|
511
|
+
* - Option: Some → Right, None → Left(leftValue)
|
|
512
|
+
* - Either: returns self
|
|
513
|
+
* - List: non-empty → Right(head), empty → Left(leftValue)
|
|
514
|
+
* - Try: Success → Right, Failure → Left(error)
|
|
515
|
+
*
|
|
516
|
+
* @param leftValue - The value to use for the Left case when the source is empty/none/failure
|
|
517
|
+
* @returns An Either with the value as Right or the provided leftValue as Left
|
|
518
|
+
*/
|
|
519
|
+
toEither<E extends Type>(leftValue: E): Either<E, T$1>;
|
|
520
|
+
/**
|
|
521
|
+
* Converts this monad to a List.
|
|
522
|
+
*
|
|
523
|
+
* Conversion rules:
|
|
524
|
+
* - Option: Some → List([value]), None → List([])
|
|
525
|
+
* - Either: Right → List([value]), Left → List([])
|
|
526
|
+
* - List: returns self
|
|
527
|
+
* - Try: Success → List([value]), Failure → List([])
|
|
528
|
+
*
|
|
529
|
+
* @returns A List containing the value(s) if present, empty List otherwise
|
|
530
|
+
*/
|
|
531
|
+
toList(): List<T$1>;
|
|
532
|
+
/**
|
|
533
|
+
* Converts this monad to a Try.
|
|
534
|
+
*
|
|
535
|
+
* Conversion rules:
|
|
536
|
+
* - Option: Some → Success, None → Failure(Error("None"))
|
|
537
|
+
* - Either: Right → Success, Left → Failure(Error(leftValue))
|
|
538
|
+
* - List: non-empty → Success(head), empty → Failure(Error("Empty list"))
|
|
539
|
+
* - Try: returns self
|
|
540
|
+
*
|
|
541
|
+
* @returns A Try containing Success with the value or Failure with an appropriate error
|
|
542
|
+
*/
|
|
543
|
+
toTry(): Try<T$1>;
|
|
544
|
+
}
|
|
545
|
+
//#endregion
|
|
546
|
+
//#region src/branded/ValidatedBrand.d.ts
|
|
547
|
+
type ValidatedBrand<K$1 extends string, T$1> = Brand<K$1, T$1> & {
|
|
548
|
+
readonly __validated: true;
|
|
549
|
+
};
|
|
550
|
+
interface ValidatedBrandCompanion<K$1 extends string, T$1> {
|
|
551
|
+
readonly brand: K$1;
|
|
552
|
+
readonly validate: (value: T$1) => boolean;
|
|
553
|
+
readonly of: (value: T$1) => Option<ValidatedBrand<K$1, T$1>>;
|
|
554
|
+
readonly from: (value: T$1) => Either<string, ValidatedBrand<K$1, T$1>>;
|
|
555
|
+
readonly unsafeOf: (value: T$1) => ValidatedBrand<K$1, T$1>;
|
|
556
|
+
readonly is: (value: unknown) => value is ValidatedBrand<K$1, T$1>;
|
|
557
|
+
readonly unwrap: (branded: Brand<K$1, T$1>) => T$1;
|
|
558
|
+
readonly refine: <K2 extends string>(brand: K2, validate: (value: Brand<K$1, T$1>) => boolean) => ValidatedBrandCompanion<K2, Brand<K$1, T$1>>;
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* Create a validated brand with runtime validation
|
|
562
|
+
* @example
|
|
563
|
+
* const Email = ValidatedBrand("Email", (s: string) => /^[^@]+@[^@]+\.[^@]+$/.test(s))
|
|
564
|
+
* const email = Email.of("user@example.com") // Some(Brand<"Email", string>)
|
|
565
|
+
*
|
|
566
|
+
* @example
|
|
567
|
+
* // With Either for error messages
|
|
568
|
+
* const Port = ValidatedBrand("Port", (n: number) => n >= 1 && n <= 65535)
|
|
569
|
+
* const result = Port.from(8080) // Right(Brand<"Port", number>)
|
|
570
|
+
* const error = Port.from(70000) // Left("Invalid Port: validation failed")
|
|
571
|
+
*
|
|
572
|
+
* @example
|
|
573
|
+
* // Type guard usage
|
|
574
|
+
* const value: unknown = "test@example.com"
|
|
575
|
+
* if (Email.is(value)) {
|
|
576
|
+
* // value is Brand<"Email", string>
|
|
577
|
+
* }
|
|
578
|
+
*
|
|
579
|
+
* @example
|
|
580
|
+
* // Best Practice: Use same brand name for seamless conversion
|
|
581
|
+
* // ValidatedBrand extends Brand, so when using the same brand name,
|
|
582
|
+
* // no casting is needed for conversion
|
|
583
|
+
* const ValidatedUserId = ValidatedBrand("UserId", (s: string) => s.length > 0)
|
|
584
|
+
* type ValidatedUserId = ReturnType<typeof ValidatedUserId.of> extends Option<infer T> ? T : never
|
|
585
|
+
* type UserId = Brand<"UserId", string>
|
|
586
|
+
*
|
|
587
|
+
* const toSimpleUserId = (id: ValidatedUserId): UserId => id // No cast needed!
|
|
588
|
+
*
|
|
589
|
+
* // Avoid different brand names which require casting:
|
|
590
|
+
* // ❌ ValidatedBrand("ValidatedUserId", ...) + Brand<"UserId", string>
|
|
591
|
+
* // ✅ ValidatedBrand("UserId", ...) + Brand<"UserId", string>
|
|
592
|
+
*/
|
|
593
|
+
declare function ValidatedBrand<K$1 extends string, T$1>(brand: K$1, validate: (value: T$1) => boolean): ValidatedBrandCompanion<K$1, T$1>;
|
|
594
|
+
/**
|
|
595
|
+
* Positive number brand (> 0)
|
|
596
|
+
* @example
|
|
597
|
+
* const price = PositiveNumber.of(19.99) // Some(Brand<"PositiveNumber", number>)
|
|
598
|
+
* const invalid = PositiveNumber.of(-5) // None
|
|
599
|
+
* const checked = PositiveNumber.from(0) // Left("Invalid PositiveNumber: validation failed")
|
|
600
|
+
*/
|
|
601
|
+
declare const PositiveNumber: ValidatedBrandCompanion<"PositiveNumber", number>;
|
|
602
|
+
declare const NonNegativeNumber: ValidatedBrandCompanion<"NonNegativeNumber", number>;
|
|
603
|
+
declare const IntegerNumber: ValidatedBrandCompanion<"IntegerNumber", number>;
|
|
604
|
+
declare const PositiveInteger: ValidatedBrandCompanion<"PositiveInteger", Brand<"PositiveNumber", number>>;
|
|
605
|
+
/**
|
|
606
|
+
* Non-empty string brand
|
|
607
|
+
* @example
|
|
608
|
+
* const name = NonEmptyString.of("John") // Some(Brand<"NonEmptyString", string>)
|
|
609
|
+
* const empty = NonEmptyString.of("") // None
|
|
610
|
+
*/
|
|
611
|
+
declare const NonEmptyString: ValidatedBrandCompanion<"NonEmptyString", string>;
|
|
612
|
+
/**
|
|
613
|
+
* Email address brand with basic validation
|
|
614
|
+
* @example
|
|
615
|
+
* const email = EmailAddress.of("user@example.com") // Some(Brand<"EmailAddress", string>)
|
|
616
|
+
* const invalid = EmailAddress.of("not-an-email") // None
|
|
617
|
+
*
|
|
618
|
+
* @example
|
|
619
|
+
* // Using with forms
|
|
620
|
+
* const processEmail = (input: string) => {
|
|
621
|
+
* return EmailAddress.from(input)
|
|
622
|
+
* .map(email => sendWelcomeEmail(email))
|
|
623
|
+
* .orElse("Invalid email address")
|
|
624
|
+
* }
|
|
625
|
+
*/
|
|
626
|
+
declare const EmailAddress: ValidatedBrandCompanion<"EmailAddress", string>;
|
|
627
|
+
declare const UrlString: ValidatedBrandCompanion<"UrlString", string>;
|
|
628
|
+
declare const UUID: ValidatedBrandCompanion<"UUID", string>;
|
|
629
|
+
declare const ISO8601Date: ValidatedBrandCompanion<"ISO8601Date", string>;
|
|
630
|
+
/**
|
|
631
|
+
* Create a number brand with min/max bounds
|
|
632
|
+
* @example
|
|
633
|
+
* const Percentage = BoundedNumber("Percentage", 0, 100)
|
|
634
|
+
* const valid = Percentage.of(75) // Some(Brand<"Percentage", number>)
|
|
635
|
+
* const invalid = Percentage.of(150) // None
|
|
636
|
+
*
|
|
637
|
+
* @example
|
|
638
|
+
* const Port = BoundedNumber("Port", 1, 65535)
|
|
639
|
+
* const httpPort = Port.unsafeOf(80) // Brand<"Port", number>
|
|
640
|
+
* // Port.unsafeOf(70000) // throws Error
|
|
641
|
+
*/
|
|
642
|
+
declare function BoundedNumber(brand: string, min: number, max: number): ValidatedBrandCompanion<string, number>;
|
|
643
|
+
/**
|
|
644
|
+
* Create a string brand with length constraints
|
|
645
|
+
* @example
|
|
646
|
+
* const Username = BoundedString("Username", 3, 20)
|
|
647
|
+
* const valid = Username.of("johndoe") // Some(Brand<"Username", string>)
|
|
648
|
+
* const tooShort = Username.of("jo") // None
|
|
649
|
+
* const tooLong = Username.of("verylongusernamethatexceedslimit") // None
|
|
650
|
+
*/
|
|
651
|
+
declare function BoundedString(brand: string, minLength: number, maxLength: number): ValidatedBrandCompanion<string, string>;
|
|
652
|
+
/**
|
|
653
|
+
* Create a string brand that matches a regex pattern
|
|
654
|
+
* @example
|
|
655
|
+
* const HexColor = PatternString("HexColor", /^#[0-9a-f]{6}$/i)
|
|
656
|
+
* const red = HexColor.of("#ff0000") // Some(Brand<"HexColor", string>)
|
|
657
|
+
* const invalid = HexColor.of("red") // None
|
|
658
|
+
*
|
|
659
|
+
* @example
|
|
660
|
+
* const PhoneNumber = PatternString("PhoneNumber", /^\+?[1-9]\d{1,14}$/)
|
|
661
|
+
* const phone = PhoneNumber.from("+1234567890")
|
|
662
|
+
* .map(p => formatPhoneNumber(p))
|
|
663
|
+
* .orElse("Invalid phone number")
|
|
664
|
+
*/
|
|
665
|
+
declare function PatternString(brand: string, pattern: RegExp): ValidatedBrandCompanion<string, string>;
|
|
666
|
+
//#endregion
|
|
667
|
+
//#region src/companion/Companion.d.ts
|
|
668
|
+
/**
|
|
669
|
+
* Creates a function-object hybrid similar to Scala's companion objects.
|
|
670
|
+
* This utility allows creating TypeScript function objects with attached methods,
|
|
671
|
+
* mimicking Scala's class + companion object pattern without using classes.
|
|
672
|
+
*
|
|
673
|
+
* @param object The main function that will be invoked when the object is called
|
|
674
|
+
* @param companion Additional static methods to attach to the function
|
|
675
|
+
* @returns A function with the attached methods
|
|
676
|
+
*
|
|
677
|
+
* @example
|
|
678
|
+
* const greet = (name: string) => `Hello, ${name}!`;
|
|
679
|
+
* const methods = {
|
|
680
|
+
* formal: (name: string) => `Good day, ${name}.`,
|
|
681
|
+
* casual: (name: string) => `Hey ${name}!`
|
|
682
|
+
* };
|
|
683
|
+
* const Greeter = createCompanionObject(greet, methods);
|
|
684
|
+
*
|
|
685
|
+
* // Usage:
|
|
686
|
+
* Greeter("World"); // Hello, World!
|
|
687
|
+
* Greeter.formal("Sir"); // Good day, Sir.
|
|
688
|
+
* Greeter.casual("Friend"); // Hey Friend!
|
|
689
|
+
*/
|
|
690
|
+
declare function Companion<ObjectF extends object, CompanionF extends object>(object: ObjectF, companion: CompanionF): ObjectF & CompanionF;
|
|
691
|
+
//#endregion
|
|
692
|
+
//#region src/companion/CompanionTypes.d.ts
|
|
693
|
+
/**
|
|
694
|
+
* Helper types for working with the Companion pattern
|
|
695
|
+
* @module CompanionTypes
|
|
696
|
+
*/
|
|
697
|
+
/**
|
|
698
|
+
* Extracts the companion methods type from a Companion object
|
|
699
|
+
* @typeParam T - The Companion type
|
|
700
|
+
* @example
|
|
701
|
+
* ```typescript
|
|
702
|
+
* type OptionCompanionMethods = CompanionMethods<typeof Option>
|
|
703
|
+
* // { from: ..., none: ..., fromJSON: ..., etc. }
|
|
704
|
+
* ```
|
|
705
|
+
*/
|
|
706
|
+
type CompanionMethods<T$1> = T$1 extends ((...args: never[]) => unknown) & infer C ? C : never;
|
|
707
|
+
/**
|
|
708
|
+
* Extracts the instance type from a constructor function
|
|
709
|
+
* @typeParam T - The constructor function type
|
|
710
|
+
* @example
|
|
711
|
+
* ```typescript
|
|
712
|
+
* type OptionInstance = InstanceType<typeof Option>
|
|
713
|
+
* // Option<T>
|
|
714
|
+
* ```
|
|
715
|
+
*/
|
|
716
|
+
type InstanceType<T$1> = T$1 extends ((...args: infer Args) => infer R) ? R extends ((...args: unknown[]) => unknown) ? ReturnType<R> : R : never;
|
|
717
|
+
/**
|
|
718
|
+
* Type guard to check if a value is a Companion object (has both constructor and companion methods)
|
|
719
|
+
* @param value - The value to check
|
|
720
|
+
* @returns True if value is a Companion object
|
|
721
|
+
*/
|
|
722
|
+
declare const isCompanion: (value: unknown) => value is ((...args: never[]) => unknown) & Record<string, unknown>;
|
|
723
|
+
//#endregion
|
|
724
|
+
//#region src/conditional/Cond.d.ts
|
|
725
|
+
/**
|
|
726
|
+
* Conditional expression that enforces exhaustive returns without early returns.
|
|
727
|
+
* Similar to Scala's if/else expressions that always return a value.
|
|
728
|
+
*
|
|
729
|
+
* @example
|
|
730
|
+
* const discount = Cond.of<number>()
|
|
731
|
+
* .when(isPremiumMember, 0.2)
|
|
732
|
+
* .elseWhen(isRegularMember, 0.1)
|
|
733
|
+
* .else(0)
|
|
734
|
+
*
|
|
735
|
+
* @example
|
|
736
|
+
* // Chaining multiple conditions
|
|
737
|
+
* const status = Cond.of<string>()
|
|
738
|
+
* .when(response.status >= 500, "Server Error")
|
|
739
|
+
* .elseWhen(response.status >= 400, "Client Error")
|
|
740
|
+
* .elseWhen(response.status >= 200, "Success")
|
|
741
|
+
* .else("Unknown")
|
|
742
|
+
*/
|
|
743
|
+
type Cond<T$1 extends Type> = {
|
|
744
|
+
/**
|
|
745
|
+
* Add an if condition
|
|
746
|
+
*/
|
|
747
|
+
when: (condition: boolean, value: T$1 | (() => T$1)) => Cond<T$1>;
|
|
748
|
+
/**
|
|
749
|
+
* Add an else-if condition
|
|
750
|
+
*/
|
|
751
|
+
elseWhen: (condition: boolean, value: T$1 | (() => T$1)) => Cond<T$1>;
|
|
752
|
+
/**
|
|
753
|
+
* Terminal else clause - required to get the result
|
|
754
|
+
*/
|
|
755
|
+
else: (value: T$1 | (() => T$1)) => T$1;
|
|
756
|
+
/**
|
|
757
|
+
* Get the result if a condition was met, throws if no condition met
|
|
758
|
+
*/
|
|
759
|
+
orThrow: () => T$1;
|
|
760
|
+
};
|
|
761
|
+
/** @internal */
|
|
762
|
+
type LazyCondChain<T$1> = {
|
|
763
|
+
when: (condition: () => boolean, value: () => T$1) => LazyCondChain<T$1>;
|
|
764
|
+
elseWhen: (condition: () => boolean, value: () => T$1) => LazyCondChain<T$1>;
|
|
765
|
+
else: (value: () => T$1) => T$1;
|
|
766
|
+
};
|
|
767
|
+
/**
|
|
768
|
+
* Conditional expression builder for functional if/else chains
|
|
769
|
+
* @example
|
|
770
|
+
* // Basic usage
|
|
771
|
+
* const size = Cond.of<string>()
|
|
772
|
+
* .when(value > 100, "large")
|
|
773
|
+
* .elseWhen(value > 50, "medium")
|
|
774
|
+
* .else("small")
|
|
775
|
+
*
|
|
776
|
+
* @example
|
|
777
|
+
* // Pattern matching
|
|
778
|
+
* const message = Cond.match(errorCode)({
|
|
779
|
+
* 404: "Not Found",
|
|
780
|
+
* 500: "Server Error",
|
|
781
|
+
* 200: "OK"
|
|
782
|
+
* })
|
|
783
|
+
*
|
|
784
|
+
* @example
|
|
785
|
+
* // Lazy evaluation
|
|
786
|
+
* const result = Cond.lazy<string>()
|
|
787
|
+
* .when(() => checkCondition1(), () => "Result 1")
|
|
788
|
+
* .when(() => checkCondition2(), () => "Result 2")
|
|
789
|
+
* .else(() => "Default")
|
|
790
|
+
*/
|
|
791
|
+
declare const Cond: (<T$1 extends Type>() => Cond<T$1>) & {
|
|
792
|
+
/**
|
|
793
|
+
* Create a conditional expression that must end with else
|
|
794
|
+
* @example
|
|
795
|
+
* const x = 7
|
|
796
|
+
* const result = Cond.of<string>()
|
|
797
|
+
* .when(x > 10, "large")
|
|
798
|
+
* .elseWhen(x > 5, "medium")
|
|
799
|
+
* .else("small")
|
|
800
|
+
* // result = "medium"
|
|
801
|
+
*
|
|
802
|
+
* @example
|
|
803
|
+
* // With lazy evaluation
|
|
804
|
+
* const discount = Cond.of<number>()
|
|
805
|
+
* .when(isPremium, () => calculatePremiumDiscount())
|
|
806
|
+
* .when(isLoyal, () => calculateLoyaltyDiscount())
|
|
807
|
+
* .else(0)
|
|
808
|
+
*/
|
|
809
|
+
of: <T$1 extends Type>() => Cond<T$1>;
|
|
810
|
+
/**
|
|
811
|
+
* Pattern matching helper that ensures exhaustiveness
|
|
812
|
+
* @example
|
|
813
|
+
* type Status = "pending" | "success" | "error"
|
|
814
|
+
* const status: Status = "success"
|
|
815
|
+
* const result = Cond.match(status)({
|
|
816
|
+
* "pending": "Waiting...",
|
|
817
|
+
* "success": "Done!",
|
|
818
|
+
* "error": "Failed!"
|
|
819
|
+
* })
|
|
820
|
+
* // result = "Done!"
|
|
821
|
+
*
|
|
822
|
+
* @example
|
|
823
|
+
* // With function values
|
|
824
|
+
* const action = "compute"
|
|
825
|
+
* const result = Cond.match(action)({
|
|
826
|
+
* "compute": () => expensiveComputation(),
|
|
827
|
+
* "cache": () => getCachedValue(),
|
|
828
|
+
* "skip": () => defaultValue
|
|
829
|
+
* })
|
|
830
|
+
*/
|
|
831
|
+
match: <T$1 extends string | number | symbol>(value: T$1) => <R$1 extends Type>(cases: Record<T$1, R$1 | (() => R$1)>) => R$1;
|
|
832
|
+
/**
|
|
833
|
+
* Create a lazy conditional that defers evaluation
|
|
834
|
+
* @example
|
|
835
|
+
* // Only evaluates conditions and values when needed
|
|
836
|
+
* const getMessage = Cond.lazy<string>()
|
|
837
|
+
* .when(() => isError(), () => computeErrorMessage())
|
|
838
|
+
* .when(() => isWarning(), () => computeWarningMessage())
|
|
839
|
+
* .else(() => "Success")
|
|
840
|
+
*
|
|
841
|
+
* @example
|
|
842
|
+
* // Complex conditional with expensive checks
|
|
843
|
+
* const result = Cond.lazy<Action>()
|
|
844
|
+
* .when(
|
|
845
|
+
* () => user.role === "admin" && checkAdminPermissions(),
|
|
846
|
+
* () => ({ type: "admin", permissions: loadAdminPermissions() })
|
|
847
|
+
* )
|
|
848
|
+
* .when(
|
|
849
|
+
* () => user.role === "user" && user.isActive,
|
|
850
|
+
* () => ({ type: "user", permissions: loadUserPermissions() })
|
|
851
|
+
* )
|
|
852
|
+
* .else(() => ({ type: "guest", permissions: [] }))
|
|
853
|
+
*/
|
|
854
|
+
lazy: <T$1 extends Type>() => LazyCondChain<T$1>;
|
|
855
|
+
};
|
|
856
|
+
//#endregion
|
|
857
|
+
//#region src/conditional/Match.d.ts
|
|
858
|
+
/**
|
|
859
|
+
* Type-level utilities for exhaustiveness checking
|
|
860
|
+
* @internal
|
|
861
|
+
*/
|
|
862
|
+
type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
|
|
863
|
+
/** @internal */
|
|
864
|
+
type IsUnion<T$1> = [T$1] extends [UnionToIntersection<T$1>] ? false : true;
|
|
865
|
+
/** @internal */
|
|
866
|
+
type RequireExhaustive<T$1, Cases> = IsUnion<T$1> extends true ? (keyof Cases extends T$1 ? (T$1 extends keyof Cases ? Cases : never) : never) : Cases;
|
|
867
|
+
/**
|
|
868
|
+
* Pattern types for nested matching
|
|
869
|
+
* @internal
|
|
870
|
+
*/
|
|
871
|
+
type Pattern<T$1> = T$1 | { [K in keyof T$1]?: Pattern<T$1[K]> } | ((value: T$1) => boolean) | {
|
|
872
|
+
_: (value: T$1) => boolean;
|
|
873
|
+
};
|
|
874
|
+
/**
|
|
875
|
+
* Extract result from pattern
|
|
876
|
+
* @internal
|
|
877
|
+
*/
|
|
878
|
+
type PatternResult<T$1, R$1> = R$1 | ((matched: T$1) => R$1);
|
|
879
|
+
/**
|
|
880
|
+
* Pattern matching construct similar to Scala's match expressions.
|
|
881
|
+
* Supports exhaustive matching, nested patterns, and guards.
|
|
882
|
+
*
|
|
883
|
+
* @example
|
|
884
|
+
* // Basic pattern matching
|
|
885
|
+
* const result = Match(value)
|
|
886
|
+
* .case(x => x > 100, "large")
|
|
887
|
+
* .case(x => x > 50, "medium")
|
|
888
|
+
* .default("small")
|
|
889
|
+
*
|
|
890
|
+
* @example
|
|
891
|
+
* // Matching exact values
|
|
892
|
+
* const message = Match(status)
|
|
893
|
+
* .caseValue("pending", "Please wait...")
|
|
894
|
+
* .caseValue("success", "Completed!")
|
|
895
|
+
* .caseValue("error", "Failed")
|
|
896
|
+
* .default("Unknown")
|
|
897
|
+
*
|
|
898
|
+
* @example
|
|
899
|
+
* // Nested pattern matching
|
|
900
|
+
* const user = { name: "John", age: 30, role: "admin" }
|
|
901
|
+
* const msg = Match(user)
|
|
902
|
+
* .case({ role: "admin", age: n => n >= 18 }, "Adult admin")
|
|
903
|
+
* .case({ role: "user" }, u => `User: ${u.name}`)
|
|
904
|
+
* .default("Guest")
|
|
905
|
+
*/
|
|
906
|
+
type Match<T$1 extends Type, R$1 extends Type> = {
|
|
907
|
+
/**
|
|
908
|
+
* Match against a pattern (value, nested object, or predicate)
|
|
909
|
+
*/
|
|
910
|
+
case: (pattern: Pattern<T$1>, result: PatternResult<T$1, R$1>) => Match<T$1, R$1>;
|
|
911
|
+
/**
|
|
912
|
+
* Add a case that matches a specific value (backward compatibility)
|
|
913
|
+
*/
|
|
914
|
+
caseValue: (match: T$1, result: R$1 | (() => R$1)) => Match<T$1, R$1>;
|
|
915
|
+
/**
|
|
916
|
+
* Add a case that matches multiple values (backward compatibility)
|
|
917
|
+
*/
|
|
918
|
+
caseValues: (matches: T$1[], result: R$1 | (() => R$1)) => Match<T$1, R$1>;
|
|
919
|
+
/**
|
|
920
|
+
* Match with a guard function (alias for readability)
|
|
921
|
+
*/
|
|
922
|
+
when: (guard: (value: T$1) => boolean, result: PatternResult<T$1, R$1>) => Match<T$1, R$1>;
|
|
923
|
+
/**
|
|
924
|
+
* Match multiple patterns (OR operation)
|
|
925
|
+
*/
|
|
926
|
+
caseAny: (patterns: Pattern<T$1>[], result: PatternResult<T$1, R$1>) => Match<T$1, R$1>;
|
|
927
|
+
/**
|
|
928
|
+
* Default case - makes match non-exhaustive
|
|
929
|
+
*/
|
|
930
|
+
default: (result: PatternResult<T$1, R$1>) => R$1;
|
|
931
|
+
/**
|
|
932
|
+
* Force exhaustive matching (compile-time check for union types)
|
|
933
|
+
*/
|
|
934
|
+
exhaustive: () => R$1;
|
|
935
|
+
/**
|
|
936
|
+
* Get result if matched, throws if no match
|
|
937
|
+
*/
|
|
938
|
+
orThrow: (errorMessage?: string) => R$1;
|
|
939
|
+
/**
|
|
940
|
+
* Get result wrapped in Option
|
|
941
|
+
*/
|
|
942
|
+
toOption: () => Option<R$1>;
|
|
943
|
+
};
|
|
944
|
+
/**
|
|
945
|
+
* Pattern matching utility for type-safe conditional logic with exhaustiveness checking,
|
|
946
|
+
* nested patterns, and guard support
|
|
947
|
+
*
|
|
948
|
+
* @example
|
|
949
|
+
* // Basic pattern matching
|
|
950
|
+
* const result = Match(value)
|
|
951
|
+
* .case(x => x > 0, "positive")
|
|
952
|
+
* .case(x => x < 0, "negative")
|
|
953
|
+
* .default("zero")
|
|
954
|
+
*
|
|
955
|
+
* @example
|
|
956
|
+
* // Exhaustive matching for union types
|
|
957
|
+
* type Color = "red" | "green" | "blue"
|
|
958
|
+
* const hex = Match.exhaustive<Color, string>({
|
|
959
|
+
* red: "#FF0000",
|
|
960
|
+
* green: "#00FF00",
|
|
961
|
+
* blue: "#0000FF"
|
|
962
|
+
* })(color)
|
|
963
|
+
*
|
|
964
|
+
* @example
|
|
965
|
+
* // Nested pattern matching
|
|
966
|
+
* type User = { name: string; age: number; role: "admin" | "user" }
|
|
967
|
+
* const user: User = { name: "John", age: 30, role: "admin" }
|
|
968
|
+
*
|
|
969
|
+
* const message = Match<User, string>(user)
|
|
970
|
+
* .case({ role: "admin", age: (n) => n >= 18 }, "Adult admin")
|
|
971
|
+
* .case({ role: "user" }, u => `User: ${u.name}`)
|
|
972
|
+
* .default("Unknown")
|
|
973
|
+
*
|
|
974
|
+
* @example
|
|
975
|
+
* // Using exhaustive() method
|
|
976
|
+
* type Status = "idle" | "loading" | "success" | "error"
|
|
977
|
+
* const result = Match<Status, string>("success")
|
|
978
|
+
* .case("idle", "Waiting...")
|
|
979
|
+
* .case("loading", "Loading...")
|
|
980
|
+
* .case("success", "Done!")
|
|
981
|
+
* .case("error", "Failed!")
|
|
982
|
+
* .exhaustive()
|
|
983
|
+
*/
|
|
984
|
+
declare const Match: (<T$1 extends Type, R$1 extends Type>(value: T$1) => Match<T$1, R$1>) & {
|
|
985
|
+
/**
|
|
986
|
+
* Create a type-safe exhaustive match for union types
|
|
987
|
+
* @example
|
|
988
|
+
* type Status = "pending" | "success" | "error"
|
|
989
|
+
* const status: Status = "success"
|
|
990
|
+
* const result = Match.exhaustive<Status, string>({
|
|
991
|
+
* pending: "Waiting...",
|
|
992
|
+
* success: "Done!",
|
|
993
|
+
* error: "Failed!"
|
|
994
|
+
* })(status)
|
|
995
|
+
* // result = "Done!"
|
|
996
|
+
*
|
|
997
|
+
* @example
|
|
998
|
+
* // For function values, wrap in object to prevent execution
|
|
999
|
+
* type Operation = "add" | "subtract" | "multiply"
|
|
1000
|
+
* const ops = Match.exhaustive<Operation, { fn: (a: number, b: number) => number }>({
|
|
1001
|
+
* add: { fn: (a, b) => a + b },
|
|
1002
|
+
* subtract: { fn: (a, b) => a - b },
|
|
1003
|
+
* multiply: { fn: (a, b) => a * b }
|
|
1004
|
+
* })
|
|
1005
|
+
* const compute = ops("multiply").fn
|
|
1006
|
+
* const result = compute(4, 5) // 20
|
|
1007
|
+
*/
|
|
1008
|
+
exhaustive: <T$1 extends string | number | symbol, R$1 extends Type>(cases: RequireExhaustive<T$1, Record<T$1, R$1>>) => (value: T$1) => R$1;
|
|
1009
|
+
/**
|
|
1010
|
+
* Create a partial match that requires a default
|
|
1011
|
+
* @example
|
|
1012
|
+
* const httpCode = 404
|
|
1013
|
+
* const message = Match.partial<number, string>({
|
|
1014
|
+
* 200: "OK",
|
|
1015
|
+
* 201: "Created",
|
|
1016
|
+
* 404: "Not Found",
|
|
1017
|
+
* 500: "Server Error"
|
|
1018
|
+
* }).withDefault("Unknown Status")(httpCode)
|
|
1019
|
+
* // message = "Not Found"
|
|
1020
|
+
*
|
|
1021
|
+
* @example
|
|
1022
|
+
* // With function default
|
|
1023
|
+
* const getMessage = Match.partial<number, string>({
|
|
1024
|
+
* 0: "Zero",
|
|
1025
|
+
* 1: "One",
|
|
1026
|
+
* 2: "Two"
|
|
1027
|
+
* }).withDefault((n) => `Number: ${n}`)
|
|
1028
|
+
* getMessage(5) // "Number: 5"
|
|
1029
|
+
*/
|
|
1030
|
+
partial: <T$1 extends string | number | symbol, R$1 extends Type>(cases: Partial<Record<T$1, R$1 | ((value: T$1) => R$1)>>) => {
|
|
1031
|
+
withDefault: (defaultValue: R$1 | ((value: T$1) => R$1)) => (value: T$1) => R$1;
|
|
1032
|
+
};
|
|
1033
|
+
/**
|
|
1034
|
+
* Pattern match with guards
|
|
1035
|
+
* @example
|
|
1036
|
+
* const score = 85
|
|
1037
|
+
* const grade = Match.withGuards<number, string>([
|
|
1038
|
+
* [n => n >= 90, "A"],
|
|
1039
|
+
* [n => n >= 80, "B"],
|
|
1040
|
+
* [n => n >= 70, "C"],
|
|
1041
|
+
* [n => n >= 60, "D"]
|
|
1042
|
+
* ]).withDefault("F")(score)
|
|
1043
|
+
* // grade = "B"
|
|
1044
|
+
*
|
|
1045
|
+
* @example
|
|
1046
|
+
* // With function results for custom messages
|
|
1047
|
+
* const age = 25
|
|
1048
|
+
* const category = Match.withGuards<number, string>([
|
|
1049
|
+
* [n => n < 13, n => `Child (${n} years)`],
|
|
1050
|
+
* [n => n < 20, n => `Teenager (${n} years)`],
|
|
1051
|
+
* [n => n < 60, n => `Adult (${n} years)`],
|
|
1052
|
+
* [n => n >= 60, n => `Senior (${n} years)`]
|
|
1053
|
+
* ]).withDefault("Unknown")(age)
|
|
1054
|
+
* // category = "Adult (25 years)"
|
|
1055
|
+
*/
|
|
1056
|
+
withGuards: <T$1 extends Type, R$1 extends Type>(guards: Array<[(value: T$1) => boolean, R$1 | ((value: T$1) => R$1)]>) => {
|
|
1057
|
+
withDefault: (defaultValue: R$1 | ((value: T$1) => R$1)) => (value: T$1) => R$1;
|
|
1058
|
+
};
|
|
1059
|
+
/**
|
|
1060
|
+
* Pattern matching for objects with specific structure
|
|
1061
|
+
* @example
|
|
1062
|
+
* type Event =
|
|
1063
|
+
* | { type: "click"; x: number; y: number }
|
|
1064
|
+
* | { type: "keypress"; key: string }
|
|
1065
|
+
* | { type: "hover"; element: string }
|
|
1066
|
+
*
|
|
1067
|
+
* const handler = Match.struct<Event, void>()
|
|
1068
|
+
* .case({ type: "click" }, (e) => console.log(`Click at ${e.x}, ${e.y}`))
|
|
1069
|
+
* .case({ type: "keypress", key: "Enter" }, () => console.log("Enter pressed"))
|
|
1070
|
+
* .case({ type: "hover" }, (e) => console.log(`Hovering over ${e.element}`))
|
|
1071
|
+
* .build()
|
|
1072
|
+
*/
|
|
1073
|
+
struct: <T$1 extends Type, R$1 extends Type>() => {
|
|
1074
|
+
case: (pattern: Pattern<T$1>, handler: (value: T$1) => R$1) => /*elided*/any;
|
|
1075
|
+
build: () => (value: T$1) => R$1;
|
|
1076
|
+
};
|
|
1077
|
+
/**
|
|
1078
|
+
* Create a pattern matcher with guards and nested patterns
|
|
1079
|
+
* @example
|
|
1080
|
+
* type User = {
|
|
1081
|
+
* name: string
|
|
1082
|
+
* age: number
|
|
1083
|
+
* permissions: string[]
|
|
1084
|
+
* }
|
|
1085
|
+
*
|
|
1086
|
+
* const canAccess = Match.builder<User, boolean>()
|
|
1087
|
+
* .when(u => u.permissions.includes("admin"), true)
|
|
1088
|
+
* .case({ age: n => n >= 18, permissions: p => p.length > 0 }, true)
|
|
1089
|
+
* .default(false)
|
|
1090
|
+
* .build()
|
|
1091
|
+
*/
|
|
1092
|
+
builder: <T$1 extends Type, R$1 extends Type>() => {
|
|
1093
|
+
case: (pattern: Pattern<T$1>, result: PatternResult<T$1, R$1>) => /*elided*/any;
|
|
1094
|
+
when: (guard: (value: T$1) => boolean, result: PatternResult<T$1, R$1>) => /*elided*/any;
|
|
1095
|
+
default: (result: PatternResult<T$1, R$1>) => {
|
|
1096
|
+
build: () => (value: T$1) => R$1;
|
|
1097
|
+
};
|
|
1098
|
+
};
|
|
1099
|
+
};
|
|
1100
|
+
//#endregion
|
|
1101
|
+
//#region src/core/base/Base.d.ts
|
|
1102
|
+
/**
|
|
1103
|
+
* Base Object from which most other objects inherit
|
|
1104
|
+
* Now includes automatic Do-notation support via doUnwrap method
|
|
1105
|
+
* @param type - The type name for the object
|
|
1106
|
+
* @param body - The implementation body
|
|
1107
|
+
*/
|
|
1108
|
+
declare function Base<T$1 extends Record<string, unknown>>(type: string, body: T$1): T$1 & {
|
|
1109
|
+
toString(): string;
|
|
1110
|
+
doUnwrap(): DoResult<unknown>;
|
|
1111
|
+
_tag: string;
|
|
1112
|
+
};
|
|
1113
|
+
//#endregion
|
|
1114
|
+
//#region src/core/throwable/Throwable.d.ts
|
|
1115
|
+
/**
|
|
1116
|
+
* The identifier name for Throwable type
|
|
1117
|
+
*/
|
|
1118
|
+
declare const NAME: "Throwable";
|
|
1119
|
+
/**
|
|
1120
|
+
* @internal
|
|
1121
|
+
*/
|
|
1122
|
+
type ThrowableType = Error & Typeable<typeof NAME> & {
|
|
1123
|
+
readonly data?: unknown;
|
|
1124
|
+
readonly cause?: Error;
|
|
1125
|
+
readonly taskInfo?: {
|
|
1126
|
+
name: string;
|
|
1127
|
+
description: string;
|
|
1128
|
+
};
|
|
1129
|
+
};
|
|
1130
|
+
declare class Throwable extends Error implements ThrowableType {
|
|
1131
|
+
readonly _tag: typeof NAME;
|
|
1132
|
+
readonly data?: unknown;
|
|
1133
|
+
readonly cause?: Error;
|
|
1134
|
+
readonly taskInfo?: {
|
|
1135
|
+
name: string;
|
|
1136
|
+
description: string;
|
|
1137
|
+
};
|
|
1138
|
+
protected constructor(message: string, options?: {
|
|
1139
|
+
data?: unknown | undefined;
|
|
1140
|
+
cause?: Error | undefined;
|
|
1141
|
+
stack?: string | undefined;
|
|
1142
|
+
taskInfo?: {
|
|
1143
|
+
name: string;
|
|
1144
|
+
description: string;
|
|
1145
|
+
} | undefined;
|
|
1146
|
+
});
|
|
1147
|
+
static apply(srcError: unknown, data?: unknown, taskInfo?: {
|
|
1148
|
+
name: string;
|
|
1149
|
+
description: string;
|
|
1150
|
+
}): ThrowableType;
|
|
1151
|
+
}
|
|
1152
|
+
//#endregion
|
|
1153
|
+
//#region src/fpromise/FPromise.d.ts
|
|
1154
|
+
/**
|
|
1155
|
+
* Error context information that provides additional metadata about errors.
|
|
1156
|
+
* This context is passed to error mapping functions to provide more information
|
|
1157
|
+
* about the error that occurred.
|
|
1158
|
+
*
|
|
1159
|
+
* @property originalError - The original error that was thrown
|
|
1160
|
+
* @property stack - The stack trace of the error, if available
|
|
1161
|
+
* @property timestamp - The timestamp when the error occurred
|
|
1162
|
+
*/
|
|
1163
|
+
type ErrorContext = {
|
|
1164
|
+
originalError: unknown;
|
|
1165
|
+
stack?: string;
|
|
1166
|
+
timestamp: number;
|
|
1167
|
+
};
|
|
1168
|
+
/**
|
|
1169
|
+
* FPromise is a functional wrapper around JavaScript's Promise with enhanced error handling.
|
|
1170
|
+
* It implements the Functor and AsyncFunctor interfaces, providing map and flatMap operations
|
|
1171
|
+
* for functional composition.
|
|
1172
|
+
*
|
|
1173
|
+
* FPromise adds several features not available in standard Promises:
|
|
1174
|
+
* - Generic error typing for better type safety
|
|
1175
|
+
* - Error recovery mechanisms (recover, recoverWith, recoverWithF)
|
|
1176
|
+
* - Error filtering and categorization
|
|
1177
|
+
* - Side effect methods (tap, tapError)
|
|
1178
|
+
* - Error context preservation
|
|
1179
|
+
* - Conversion to/from Either for more functional error handling
|
|
1180
|
+
*
|
|
1181
|
+
* @template T - The type of the value that the FPromise resolves to
|
|
1182
|
+
* @template E - The type of the error that the FPromise may reject with (defaults to unknown)
|
|
1183
|
+
*/
|
|
1184
|
+
/**
|
|
1185
|
+
* FPromise type that defines the function signature and methods
|
|
1186
|
+
*/
|
|
1187
|
+
type FPromise<T$1 extends Type, E extends Type = unknown> = PromiseLike<T$1> & {
|
|
1188
|
+
readonly _tag: "FPromise";
|
|
1189
|
+
tap: (f: (value: T$1) => void) => FPromise<T$1, E>;
|
|
1190
|
+
mapError: <E2>(f: (error: E, context: ErrorContext) => E2) => FPromise<T$1, E2>;
|
|
1191
|
+
tapError: (f: (error: E) => void) => FPromise<T$1, E>;
|
|
1192
|
+
recover: (fallback: T$1) => FPromise<T$1, never>;
|
|
1193
|
+
recoverWith: (f: (error: E) => T$1) => FPromise<T$1, never>;
|
|
1194
|
+
recoverWithF: <E2>(f: (error: E) => FPromise<T$1, E2>) => FPromise<T$1, E2>;
|
|
1195
|
+
filterError: <E2 extends E>(predicate: (error: E) => boolean, handler: (error: E) => FPromise<T$1, E2>) => FPromise<T$1, E>;
|
|
1196
|
+
logError: (logger: (error: E, context: ErrorContext) => void) => FPromise<T$1, E>;
|
|
1197
|
+
toPromise: () => Promise<T$1>;
|
|
1198
|
+
toEither: () => Promise<Either<E, T$1>>;
|
|
1199
|
+
fold: <R$1 extends Type>(onError: (error: E) => R$1, onSuccess: (value: T$1) => R$1) => FPromise<R$1, never>;
|
|
1200
|
+
map: <U extends Type>(f: (value: T$1) => U) => FPromise<U, E>;
|
|
1201
|
+
flatMap: <U extends Type>(f: (value: T$1) => FPromise<U, E> | PromiseLike<U>) => FPromise<U, E>;
|
|
1202
|
+
flatMapAsync: <U extends Type>(f: (value: T$1) => PromiseLike<U>) => Promise<U>;
|
|
1203
|
+
};
|
|
1204
|
+
/**
|
|
1205
|
+
* Static utility methods for FPromise using the Companion pattern.
|
|
1206
|
+
* These methods provide factory functions and utilities for working with FPromises.
|
|
1207
|
+
*/
|
|
1208
|
+
declare const FPromiseCompanion: {
|
|
1209
|
+
/**
|
|
1210
|
+
* Creates an FPromise that resolves to the provided value.
|
|
1211
|
+
*
|
|
1212
|
+
* @template T - The type of the value
|
|
1213
|
+
* @template E - The type of the error (defaults to never since this FPromise won't reject)
|
|
1214
|
+
* @param value - The value to resolve with
|
|
1215
|
+
* @returns An FPromise that resolves to the value
|
|
1216
|
+
*
|
|
1217
|
+
* @example
|
|
1218
|
+
* const promise = FPromise.resolve(42)
|
|
1219
|
+
* // promise resolves to 42
|
|
1220
|
+
*/
|
|
1221
|
+
resolve: <T$1, E = never>(value: T$1 | PromiseLike<T$1>) => FPromise<T$1, E>;
|
|
1222
|
+
/**
|
|
1223
|
+
* Creates an FPromise that rejects with the provided reason.
|
|
1224
|
+
*
|
|
1225
|
+
* @template T - The type of the value (which will never be produced)
|
|
1226
|
+
* @template E - The type of the error
|
|
1227
|
+
* @param reason - The reason for rejection
|
|
1228
|
+
* @returns An FPromise that rejects with the reason
|
|
1229
|
+
*
|
|
1230
|
+
* @example
|
|
1231
|
+
* const promise = FPromise.reject<number, Error>(new Error("Something went wrong"))
|
|
1232
|
+
* // promise rejects with Error("Something went wrong")
|
|
1233
|
+
*/
|
|
1234
|
+
reject: <T$1, E = unknown>(reason: E) => FPromise<T$1, E>;
|
|
1235
|
+
/**
|
|
1236
|
+
* Creates an FPromise from a regular Promise.
|
|
1237
|
+
*
|
|
1238
|
+
* @template T - The type of the value
|
|
1239
|
+
* @template E - The type of the error
|
|
1240
|
+
* @param promise - The Promise to convert
|
|
1241
|
+
* @returns An FPromise that resolves or rejects with the same value or error
|
|
1242
|
+
*
|
|
1243
|
+
* @example
|
|
1244
|
+
* const promise = FPromise.from(fetch("https://api.example.com/data"))
|
|
1245
|
+
* // promise is an FPromise that resolves or rejects based on the fetch result
|
|
1246
|
+
*/
|
|
1247
|
+
from: <T$1, E = unknown>(promise: Promise<T$1>) => FPromise<T$1, E>;
|
|
1248
|
+
/**
|
|
1249
|
+
* Creates an FPromise from an Either.
|
|
1250
|
+
* If the Either is a Right, the FPromise resolves with the Right value.
|
|
1251
|
+
* If the Either is a Left, the FPromise rejects with the Left value.
|
|
1252
|
+
*
|
|
1253
|
+
* @template L - The type of the Left value (error)
|
|
1254
|
+
* @template R - The type of the Right value (success)
|
|
1255
|
+
* @param either - The Either to convert
|
|
1256
|
+
* @returns An FPromise that resolves or rejects based on the Either
|
|
1257
|
+
*
|
|
1258
|
+
* @example
|
|
1259
|
+
* const either = Right<Error, number>(42)
|
|
1260
|
+
* const promise = FPromise.fromEither(either)
|
|
1261
|
+
* // promise resolves to 42
|
|
1262
|
+
*/
|
|
1263
|
+
fromEither: <L, R$1>(either: Either<L, R$1>) => FPromise<R$1, L>;
|
|
1264
|
+
/**
|
|
1265
|
+
* Runs multiple FPromises in parallel and returns an array of results.
|
|
1266
|
+
* Similar to Promise.all, this will reject if any of the promises reject.
|
|
1267
|
+
*
|
|
1268
|
+
* @template T - The type of the values
|
|
1269
|
+
* @template E - The type of the error
|
|
1270
|
+
* @param promises - An array of FPromises, Promises, or values
|
|
1271
|
+
* @returns An FPromise that resolves to an array of results
|
|
1272
|
+
*
|
|
1273
|
+
* @example
|
|
1274
|
+
* const promises = [FPromise.resolve(1), FPromise.resolve(2), 3]
|
|
1275
|
+
* const result = await FPromise.all(promises).toPromise()
|
|
1276
|
+
* // result is [1, 2, 3]
|
|
1277
|
+
*/
|
|
1278
|
+
all: <T$1, E = unknown>(promises: Array<FPromise<T$1, E> | PromiseLike<T$1> | T$1>) => FPromise<T$1[], E>;
|
|
1279
|
+
/**
|
|
1280
|
+
* Like Promise.allSettled, returns results of all promises whether they succeed or fail.
|
|
1281
|
+
* This will always resolve, never reject.
|
|
1282
|
+
*
|
|
1283
|
+
* @template T - The type of the values
|
|
1284
|
+
* @template E - The type of the errors
|
|
1285
|
+
* @param promises - An array of FPromises or Promises
|
|
1286
|
+
* @returns An FPromise that resolves to an array of Either results
|
|
1287
|
+
*
|
|
1288
|
+
* @example
|
|
1289
|
+
* const promises = [FPromise.resolve(1), FPromise.reject<number, Error>(new Error("Failed"))]
|
|
1290
|
+
* const result = await FPromise.allSettled(promises).toPromise()
|
|
1291
|
+
* // result is [Right(1), Left(Error("Failed"))]
|
|
1292
|
+
*/
|
|
1293
|
+
allSettled: <T$1, E = unknown>(promises: Array<FPromise<T$1, E> | PromiseLike<T$1>>) => FPromise<Array<Either<E, T$1>>, never>;
|
|
1294
|
+
/**
|
|
1295
|
+
* Like Promise.race, returns the first promise to settle (either resolve or reject).
|
|
1296
|
+
*
|
|
1297
|
+
* @template T - The type of the values
|
|
1298
|
+
* @template E - The type of the errors
|
|
1299
|
+
* @param promises - An array of FPromises or Promises
|
|
1300
|
+
* @returns An FPromise that resolves or rejects with the result of the first promise to settle
|
|
1301
|
+
*
|
|
1302
|
+
* @example
|
|
1303
|
+
* const slow = FPromise.resolve(1).tap(() => new Promise(r => setTimeout(r, 100)))
|
|
1304
|
+
* const fast = FPromise.resolve(2).tap(() => new Promise(r => setTimeout(r, 50)))
|
|
1305
|
+
* const result = await FPromise.race([slow, fast]).toPromise()
|
|
1306
|
+
* // result is 2
|
|
1307
|
+
*/
|
|
1308
|
+
race: <T$1, E = unknown>(promises: Array<FPromise<T$1, E> | PromiseLike<T$1>>) => FPromise<T$1, E>;
|
|
1309
|
+
/**
|
|
1310
|
+
* Like Promise.any, returns the first promise to fulfill.
|
|
1311
|
+
* This will only reject if all promises reject.
|
|
1312
|
+
*
|
|
1313
|
+
* @template T - The type of the values
|
|
1314
|
+
* @template E - The type of the errors
|
|
1315
|
+
* @param promises - An array of FPromises or Promises
|
|
1316
|
+
* @returns An FPromise that resolves with the first promise to fulfill or rejects if all promises reject
|
|
1317
|
+
*
|
|
1318
|
+
* @example
|
|
1319
|
+
* const promises = [
|
|
1320
|
+
* FPromise.reject<number, Error>(new Error("First failed")),
|
|
1321
|
+
* FPromise.resolve(2),
|
|
1322
|
+
* FPromise.reject<number, Error>(new Error("Third failed"))
|
|
1323
|
+
* ]
|
|
1324
|
+
* const result = await FPromise.any(promises).toPromise()
|
|
1325
|
+
* // result is 2
|
|
1326
|
+
*/
|
|
1327
|
+
any: <T$1, E = unknown>(promises: Array<FPromise<T$1, E> | PromiseLike<T$1>>) => FPromise<T$1, E>;
|
|
1328
|
+
/**
|
|
1329
|
+
* Retries an operation with exponential backoff.
|
|
1330
|
+
* This is useful for operations that may fail temporarily, such as network requests.
|
|
1331
|
+
*
|
|
1332
|
+
* @template T - The type of the value
|
|
1333
|
+
* @template E - The type of the error
|
|
1334
|
+
* @param operation - A function that returns an FPromise
|
|
1335
|
+
* @param options - Configuration options for the retry
|
|
1336
|
+
* @param options.maxRetries - Maximum number of retry attempts
|
|
1337
|
+
* @param options.baseDelay - Base delay in milliseconds (default: 100)
|
|
1338
|
+
* @param options.shouldRetry - Function that determines whether to retry based on the error (default: always retry)
|
|
1339
|
+
* @returns An FPromise that resolves when the operation succeeds or rejects after all retries fail
|
|
1340
|
+
*
|
|
1341
|
+
* @example
|
|
1342
|
+
* const operation = () => {
|
|
1343
|
+
* if (Math.random() > 0.8) {
|
|
1344
|
+
* return FPromise.resolve("Success!")
|
|
1345
|
+
* }
|
|
1346
|
+
* return FPromise.reject<string, Error>(new Error("Temporary failure"))
|
|
1347
|
+
* }
|
|
1348
|
+
*
|
|
1349
|
+
* const result = await FPromise.retryWithBackoff(operation, {
|
|
1350
|
+
* maxRetries: 3,
|
|
1351
|
+
* baseDelay: 100,
|
|
1352
|
+
* shouldRetry: (error) => error.message === "Temporary failure"
|
|
1353
|
+
* }).toPromise()
|
|
1354
|
+
*/
|
|
1355
|
+
retryWithBackoff: <T$1, E = unknown>(operation: () => FPromise<T$1, E>, options: {
|
|
1356
|
+
maxRetries: number;
|
|
1357
|
+
baseDelay?: number;
|
|
1358
|
+
shouldRetry?: (error: E, attempt: number) => boolean;
|
|
1359
|
+
}) => FPromise<T$1, E>;
|
|
1360
|
+
};
|
|
1361
|
+
/**
|
|
1362
|
+
* Creates an FPromise from an executor function.
|
|
1363
|
+
*
|
|
1364
|
+
* @template T - The type of the value that the FPromise resolves to
|
|
1365
|
+
* @template E - The type of the error that the FPromise may reject with
|
|
1366
|
+
* @param executor - A function that receives resolve and reject functions
|
|
1367
|
+
* @returns An FPromise instance
|
|
1368
|
+
*/
|
|
1369
|
+
declare const FPromise: (<T$1 extends Type, E = unknown>(executor: (resolve: (value: T$1 | PromiseLike<T$1>) => void, reject: (reason?: E) => void) => void) => FPromise<T$1, E>) & {
|
|
1370
|
+
/**
|
|
1371
|
+
* Creates an FPromise that resolves to the provided value.
|
|
1372
|
+
*
|
|
1373
|
+
* @template T - The type of the value
|
|
1374
|
+
* @template E - The type of the error (defaults to never since this FPromise won't reject)
|
|
1375
|
+
* @param value - The value to resolve with
|
|
1376
|
+
* @returns An FPromise that resolves to the value
|
|
1377
|
+
*
|
|
1378
|
+
* @example
|
|
1379
|
+
* const promise = FPromise.resolve(42)
|
|
1380
|
+
* // promise resolves to 42
|
|
1381
|
+
*/
|
|
1382
|
+
resolve: <T$1, E = never>(value: T$1 | PromiseLike<T$1>) => FPromise<T$1, E>;
|
|
1383
|
+
/**
|
|
1384
|
+
* Creates an FPromise that rejects with the provided reason.
|
|
1385
|
+
*
|
|
1386
|
+
* @template T - The type of the value (which will never be produced)
|
|
1387
|
+
* @template E - The type of the error
|
|
1388
|
+
* @param reason - The reason for rejection
|
|
1389
|
+
* @returns An FPromise that rejects with the reason
|
|
1390
|
+
*
|
|
1391
|
+
* @example
|
|
1392
|
+
* const promise = FPromise.reject<number, Error>(new Error("Something went wrong"))
|
|
1393
|
+
* // promise rejects with Error("Something went wrong")
|
|
1394
|
+
*/
|
|
1395
|
+
reject: <T$1, E = unknown>(reason: E) => FPromise<T$1, E>;
|
|
1396
|
+
/**
|
|
1397
|
+
* Creates an FPromise from a regular Promise.
|
|
1398
|
+
*
|
|
1399
|
+
* @template T - The type of the value
|
|
1400
|
+
* @template E - The type of the error
|
|
1401
|
+
* @param promise - The Promise to convert
|
|
1402
|
+
* @returns An FPromise that resolves or rejects with the same value or error
|
|
1403
|
+
*
|
|
1404
|
+
* @example
|
|
1405
|
+
* const promise = FPromise.from(fetch("https://api.example.com/data"))
|
|
1406
|
+
* // promise is an FPromise that resolves or rejects based on the fetch result
|
|
1407
|
+
*/
|
|
1408
|
+
from: <T$1, E = unknown>(promise: Promise<T$1>) => FPromise<T$1, E>;
|
|
1409
|
+
/**
|
|
1410
|
+
* Creates an FPromise from an Either.
|
|
1411
|
+
* If the Either is a Right, the FPromise resolves with the Right value.
|
|
1412
|
+
* If the Either is a Left, the FPromise rejects with the Left value.
|
|
1413
|
+
*
|
|
1414
|
+
* @template L - The type of the Left value (error)
|
|
1415
|
+
* @template R - The type of the Right value (success)
|
|
1416
|
+
* @param either - The Either to convert
|
|
1417
|
+
* @returns An FPromise that resolves or rejects based on the Either
|
|
1418
|
+
*
|
|
1419
|
+
* @example
|
|
1420
|
+
* const either = Right<Error, number>(42)
|
|
1421
|
+
* const promise = FPromise.fromEither(either)
|
|
1422
|
+
* // promise resolves to 42
|
|
1423
|
+
*/
|
|
1424
|
+
fromEither: <L, R$1>(either: Either<L, R$1>) => FPromise<R$1, L>;
|
|
1425
|
+
/**
|
|
1426
|
+
* Runs multiple FPromises in parallel and returns an array of results.
|
|
1427
|
+
* Similar to Promise.all, this will reject if any of the promises reject.
|
|
1428
|
+
*
|
|
1429
|
+
* @template T - The type of the values
|
|
1430
|
+
* @template E - The type of the error
|
|
1431
|
+
* @param promises - An array of FPromises, Promises, or values
|
|
1432
|
+
* @returns An FPromise that resolves to an array of results
|
|
1433
|
+
*
|
|
1434
|
+
* @example
|
|
1435
|
+
* const promises = [FPromise.resolve(1), FPromise.resolve(2), 3]
|
|
1436
|
+
* const result = await FPromise.all(promises).toPromise()
|
|
1437
|
+
* // result is [1, 2, 3]
|
|
1438
|
+
*/
|
|
1439
|
+
all: <T$1, E = unknown>(promises: Array<FPromise<T$1, E> | PromiseLike<T$1> | T$1>) => FPromise<T$1[], E>;
|
|
1440
|
+
/**
|
|
1441
|
+
* Like Promise.allSettled, returns results of all promises whether they succeed or fail.
|
|
1442
|
+
* This will always resolve, never reject.
|
|
1443
|
+
*
|
|
1444
|
+
* @template T - The type of the values
|
|
1445
|
+
* @template E - The type of the errors
|
|
1446
|
+
* @param promises - An array of FPromises or Promises
|
|
1447
|
+
* @returns An FPromise that resolves to an array of Either results
|
|
1448
|
+
*
|
|
1449
|
+
* @example
|
|
1450
|
+
* const promises = [FPromise.resolve(1), FPromise.reject<number, Error>(new Error("Failed"))]
|
|
1451
|
+
* const result = await FPromise.allSettled(promises).toPromise()
|
|
1452
|
+
* // result is [Right(1), Left(Error("Failed"))]
|
|
1453
|
+
*/
|
|
1454
|
+
allSettled: <T$1, E = unknown>(promises: Array<FPromise<T$1, E> | PromiseLike<T$1>>) => FPromise<Array<Either<E, T$1>>, never>;
|
|
1455
|
+
/**
|
|
1456
|
+
* Like Promise.race, returns the first promise to settle (either resolve or reject).
|
|
1457
|
+
*
|
|
1458
|
+
* @template T - The type of the values
|
|
1459
|
+
* @template E - The type of the errors
|
|
1460
|
+
* @param promises - An array of FPromises or Promises
|
|
1461
|
+
* @returns An FPromise that resolves or rejects with the result of the first promise to settle
|
|
1462
|
+
*
|
|
1463
|
+
* @example
|
|
1464
|
+
* const slow = FPromise.resolve(1).tap(() => new Promise(r => setTimeout(r, 100)))
|
|
1465
|
+
* const fast = FPromise.resolve(2).tap(() => new Promise(r => setTimeout(r, 50)))
|
|
1466
|
+
* const result = await FPromise.race([slow, fast]).toPromise()
|
|
1467
|
+
* // result is 2
|
|
1468
|
+
*/
|
|
1469
|
+
race: <T$1, E = unknown>(promises: Array<FPromise<T$1, E> | PromiseLike<T$1>>) => FPromise<T$1, E>;
|
|
1470
|
+
/**
|
|
1471
|
+
* Like Promise.any, returns the first promise to fulfill.
|
|
1472
|
+
* This will only reject if all promises reject.
|
|
1473
|
+
*
|
|
1474
|
+
* @template T - The type of the values
|
|
1475
|
+
* @template E - The type of the errors
|
|
1476
|
+
* @param promises - An array of FPromises or Promises
|
|
1477
|
+
* @returns An FPromise that resolves with the first promise to fulfill or rejects if all promises reject
|
|
1478
|
+
*
|
|
1479
|
+
* @example
|
|
1480
|
+
* const promises = [
|
|
1481
|
+
* FPromise.reject<number, Error>(new Error("First failed")),
|
|
1482
|
+
* FPromise.resolve(2),
|
|
1483
|
+
* FPromise.reject<number, Error>(new Error("Third failed"))
|
|
1484
|
+
* ]
|
|
1485
|
+
* const result = await FPromise.any(promises).toPromise()
|
|
1486
|
+
* // result is 2
|
|
1487
|
+
*/
|
|
1488
|
+
any: <T$1, E = unknown>(promises: Array<FPromise<T$1, E> | PromiseLike<T$1>>) => FPromise<T$1, E>;
|
|
1489
|
+
/**
|
|
1490
|
+
* Retries an operation with exponential backoff.
|
|
1491
|
+
* This is useful for operations that may fail temporarily, such as network requests.
|
|
1492
|
+
*
|
|
1493
|
+
* @template T - The type of the value
|
|
1494
|
+
* @template E - The type of the error
|
|
1495
|
+
* @param operation - A function that returns an FPromise
|
|
1496
|
+
* @param options - Configuration options for the retry
|
|
1497
|
+
* @param options.maxRetries - Maximum number of retry attempts
|
|
1498
|
+
* @param options.baseDelay - Base delay in milliseconds (default: 100)
|
|
1499
|
+
* @param options.shouldRetry - Function that determines whether to retry based on the error (default: always retry)
|
|
1500
|
+
* @returns An FPromise that resolves when the operation succeeds or rejects after all retries fail
|
|
1501
|
+
*
|
|
1502
|
+
* @example
|
|
1503
|
+
* const operation = () => {
|
|
1504
|
+
* if (Math.random() > 0.8) {
|
|
1505
|
+
* return FPromise.resolve("Success!")
|
|
1506
|
+
* }
|
|
1507
|
+
* return FPromise.reject<string, Error>(new Error("Temporary failure"))
|
|
1508
|
+
* }
|
|
1509
|
+
*
|
|
1510
|
+
* const result = await FPromise.retryWithBackoff(operation, {
|
|
1511
|
+
* maxRetries: 3,
|
|
1512
|
+
* baseDelay: 100,
|
|
1513
|
+
* shouldRetry: (error) => error.message === "Temporary failure"
|
|
1514
|
+
* }).toPromise()
|
|
1515
|
+
*/
|
|
1516
|
+
retryWithBackoff: <T$1, E = unknown>(operation: () => FPromise<T$1, E>, options: {
|
|
1517
|
+
maxRetries: number;
|
|
1518
|
+
baseDelay?: number;
|
|
1519
|
+
shouldRetry?: (error: E, attempt: number) => boolean;
|
|
1520
|
+
}) => FPromise<T$1, E>;
|
|
1521
|
+
};
|
|
1522
|
+
//#endregion
|
|
1523
|
+
//#region src/core/task/Task.d.ts
|
|
1524
|
+
/**
|
|
1525
|
+
* Type definition for errors with a _tag property that identifies them as Throwables
|
|
1526
|
+
*/
|
|
1527
|
+
type TaggedThrowable = Error & {
|
|
1528
|
+
_tag: "Throwable";
|
|
1529
|
+
cause?: Error;
|
|
1530
|
+
taskInfo?: {
|
|
1531
|
+
name: string;
|
|
1532
|
+
description: string;
|
|
1533
|
+
};
|
|
1534
|
+
};
|
|
1535
|
+
/**
|
|
1536
|
+
* Type guard to check if an error is a TaggedThrowable
|
|
1537
|
+
*/
|
|
1538
|
+
declare function isTaggedThrowable(error: unknown): error is TaggedThrowable;
|
|
1539
|
+
interface TaskParams {
|
|
1540
|
+
readonly name?: string;
|
|
1541
|
+
readonly description?: string;
|
|
1542
|
+
}
|
|
1543
|
+
interface TaskMetadata {
|
|
1544
|
+
readonly name: string;
|
|
1545
|
+
readonly description: string;
|
|
1546
|
+
}
|
|
1547
|
+
interface TaskOutcome<T$1> extends FunctypeBase<T$1, "Ok" | "Err">, Extractable<T$1>, AsyncMonad<T$1>, Promisable<T$1>, Doable<T$1> {
|
|
1548
|
+
readonly _tag: "Ok" | "Err";
|
|
1549
|
+
readonly _meta: TaskMetadata;
|
|
1550
|
+
readonly value?: T$1;
|
|
1551
|
+
readonly error?: Throwable;
|
|
1552
|
+
readonly map: <U>(f: (value: T$1) => U) => TaskOutcome<U>;
|
|
1553
|
+
readonly flatMap: <U>(f: (value: T$1) => TaskOutcome<U> | Either<Throwable, U>) => TaskOutcome<U>;
|
|
1554
|
+
readonly ap: <U>(ff: TaskOutcome<(value: T$1) => U>) => TaskOutcome<U>;
|
|
1555
|
+
readonly mapAsync: <U>(f: (value: T$1) => Promise<U>) => Promise<TaskOutcome<U>>;
|
|
1556
|
+
readonly flatMapAsync: <U>(f: (value: T$1) => Promise<TaskOutcome<U>>) => Promise<TaskOutcome<U>>;
|
|
1557
|
+
readonly mapError: (f: (error: Throwable) => Throwable) => TaskOutcome<T$1>;
|
|
1558
|
+
readonly recover: (value: T$1) => Ok<T$1>;
|
|
1559
|
+
readonly recoverWith: (f: (error: Throwable) => T$1) => Ok<T$1>;
|
|
1560
|
+
readonly isSuccess: () => this is Ok<T$1>;
|
|
1561
|
+
readonly isFailure: () => this is Err<T$1>;
|
|
1562
|
+
readonly isOk: () => this is Ok<T$1>;
|
|
1563
|
+
readonly isErr: () => this is Err<T$1>;
|
|
1564
|
+
readonly toEither: () => Either<Throwable, T$1>;
|
|
1565
|
+
readonly toTry: () => Try<T$1>;
|
|
1566
|
+
readonly toOption: () => Option<T$1>;
|
|
1567
|
+
readonly toList: () => List<T$1>;
|
|
1568
|
+
readonly fold: <U>(onErr: (error: Throwable) => U, onOk: (value: T$1) => U) => U;
|
|
1569
|
+
readonly match: <U>(patterns: {
|
|
1570
|
+
Ok: (value: T$1) => U;
|
|
1571
|
+
Err: (error: Throwable) => U;
|
|
1572
|
+
}) => U;
|
|
1573
|
+
}
|
|
1574
|
+
interface Ok<T$1> extends TaskOutcome<T$1> {
|
|
1575
|
+
readonly _tag: "Ok";
|
|
1576
|
+
readonly value: T$1;
|
|
1577
|
+
readonly error: undefined;
|
|
1578
|
+
}
|
|
1579
|
+
interface Err<T$1> extends TaskOutcome<T$1> {
|
|
1580
|
+
readonly _tag: "Err";
|
|
1581
|
+
readonly value: undefined;
|
|
1582
|
+
readonly error: Throwable;
|
|
1583
|
+
}
|
|
1584
|
+
type TaskSuccess<T$1> = Ok<T$1>;
|
|
1585
|
+
type TaskFailure<T$1> = Err<T$1>;
|
|
1586
|
+
/**
|
|
1587
|
+
* Err constructor - Creates a failed TaskOutcome
|
|
1588
|
+
* @param error - The error object
|
|
1589
|
+
* @param data - Additional data related to the error
|
|
1590
|
+
* @param params - Task parameters
|
|
1591
|
+
*/
|
|
1592
|
+
declare const Err: <T$1>(error: unknown, data?: unknown, params?: TaskParams) => Err<T$1>;
|
|
1593
|
+
/**
|
|
1594
|
+
* Ok constructor - Creates a successful TaskOutcome
|
|
1595
|
+
* @param data - The successful value
|
|
1596
|
+
* @param params - Task parameters
|
|
1597
|
+
*/
|
|
1598
|
+
declare const Ok: <T$1>(data: T$1, params?: TaskParams) => Ok<T$1>;
|
|
1599
|
+
type TaskResult<T$1> = Promise<TaskOutcome<T$1>>;
|
|
1600
|
+
/**
|
|
1601
|
+
* The CancellationToken is a control structure that allows long-running tasks to be cancelled
|
|
1602
|
+
* Cancellation is cooperative, meaning the task must check the token and respond to cancellation requests
|
|
1603
|
+
*/
|
|
1604
|
+
type CancellationToken = {
|
|
1605
|
+
/** Whether the token has been cancelled */
|
|
1606
|
+
readonly isCancelled: boolean;
|
|
1607
|
+
/** Signal that can be used with fetch and other abortable APIs */
|
|
1608
|
+
readonly signal: AbortSignal;
|
|
1609
|
+
/** Register a callback to be called when cancellation occurs */
|
|
1610
|
+
onCancel(callback: () => void): void;
|
|
1611
|
+
};
|
|
1612
|
+
/**
|
|
1613
|
+
* Create a cancellation token and controller
|
|
1614
|
+
* The controller can be used to cancel operations that use the token
|
|
1615
|
+
*/
|
|
1616
|
+
type CancellationTokenSource = {
|
|
1617
|
+
/** The token to be passed to cancellable operations */
|
|
1618
|
+
readonly token: CancellationToken;
|
|
1619
|
+
/** Cancel all operations using this token */
|
|
1620
|
+
cancel(): void;
|
|
1621
|
+
};
|
|
1622
|
+
/**
|
|
1623
|
+
* Create a cancellation token source
|
|
1624
|
+
* @returns A CancellationTokenSource that can be used to create and control cancellation tokens
|
|
1625
|
+
*/
|
|
1626
|
+
declare const createCancellationTokenSource: () => CancellationTokenSource;
|
|
1627
|
+
type Sync<T$1> = TaskOutcome<T$1>;
|
|
1628
|
+
type Async<T$1> = TaskResult<T$1>;
|
|
1629
|
+
declare const Task: (<T$1 = unknown>(params?: TaskParams) => {
|
|
1630
|
+
_type: string;
|
|
1631
|
+
/**
|
|
1632
|
+
* Run an async operation with explicit try/catch/finally semantics
|
|
1633
|
+
* Returns a raw Promise that can interact with traditional Promise-based code
|
|
1634
|
+
*
|
|
1635
|
+
* @param t - The main operation function that returns a value or Promise
|
|
1636
|
+
* @param e - Optional error handler function
|
|
1637
|
+
* @param f - Optional finally handler function
|
|
1638
|
+
* @param cancellationToken - Optional token for cancellation support
|
|
1639
|
+
*/
|
|
1640
|
+
Async: <U = T$1>(t: () => U | Promise<U> | TaskOutcome<U> | Promise<TaskOutcome<U>>, e?: (error: unknown) => unknown | TaskOutcome<U>, f?: () => Promise<void> | void, cancellationToken?: CancellationToken) => FPromise<TaskOutcome<U>>;
|
|
1641
|
+
/**
|
|
1642
|
+
* Run a synchronous operation with explicit try/catch/finally semantics
|
|
1643
|
+
* Returns a TaskOutcome for functional error handling
|
|
1644
|
+
*
|
|
1645
|
+
* @param t - The main operation function that returns a value
|
|
1646
|
+
* @param e - Optional error handler function
|
|
1647
|
+
* @param f - Optional finally handler function
|
|
1648
|
+
*/
|
|
1649
|
+
Sync: <U = T$1>(t: () => U, e?: (error: unknown) => unknown, f?: () => void) => TaskOutcome<U>;
|
|
1650
|
+
/**
|
|
1651
|
+
* Run an async operation with progress tracking capabilities
|
|
1652
|
+
* Returns a Promise and provides progress updates via callback
|
|
1653
|
+
*
|
|
1654
|
+
* @param t - The main operation that receives a progress updater function
|
|
1655
|
+
* @param onProgress - Callback that receives progress updates (0-100)
|
|
1656
|
+
* @param e - Optional error handler function
|
|
1657
|
+
* @param f - Optional finally handler function
|
|
1658
|
+
* @param cancellationToken - Optional token for cancellation support
|
|
1659
|
+
*/
|
|
1660
|
+
AsyncWithProgress: <U = T$1>(t: (updateProgress: (percent: number) => void) => U | Promise<U> | TaskOutcome<U> | Promise<TaskOutcome<U>>, onProgress: (percent: number) => void, e?: (error: unknown) => unknown | TaskOutcome<U>, f?: () => Promise<void> | void, cancellationToken?: CancellationToken) => FPromise<TaskOutcome<U>>;
|
|
1661
|
+
toString(): string;
|
|
1662
|
+
doUnwrap(): DoResult<unknown>;
|
|
1663
|
+
_tag: string;
|
|
1664
|
+
}) & {
|
|
1665
|
+
/**
|
|
1666
|
+
* Create a successful Task result
|
|
1667
|
+
*/
|
|
1668
|
+
success: <T$1>(data: T$1, params?: TaskParams) => Ok<T$1>;
|
|
1669
|
+
/**
|
|
1670
|
+
* Create a failed Task result
|
|
1671
|
+
*/
|
|
1672
|
+
fail: <T$1>(error: unknown, data?: unknown, params?: TaskParams) => Err<T$1>;
|
|
1673
|
+
/**
|
|
1674
|
+
* Create a successful Task result (alias for success)
|
|
1675
|
+
* Preferred for new code
|
|
1676
|
+
*/
|
|
1677
|
+
ok: <T$1>(data: T$1, params?: TaskParams) => Ok<T$1>;
|
|
1678
|
+
/**
|
|
1679
|
+
* Create a failed Task result (alias for fail)
|
|
1680
|
+
* Preferred for new code
|
|
1681
|
+
*/
|
|
1682
|
+
err: <T$1>(error: unknown, data?: unknown, params?: TaskParams) => Err<T$1>;
|
|
1683
|
+
/**
|
|
1684
|
+
* Create TaskOutcome from Either
|
|
1685
|
+
* @param either - Either to convert
|
|
1686
|
+
* @param params - Task parameters
|
|
1687
|
+
*/
|
|
1688
|
+
fromEither: <T$1>(either: Either<Throwable, T$1>, params?: TaskParams) => TaskOutcome<T$1>;
|
|
1689
|
+
/**
|
|
1690
|
+
* Create TaskOutcome from Try
|
|
1691
|
+
* @param tryValue - Try to convert
|
|
1692
|
+
* @param params - Task parameters
|
|
1693
|
+
*/
|
|
1694
|
+
fromTry: <T$1>(tryValue: Try<T$1>, params?: TaskParams) => TaskOutcome<T$1>;
|
|
1695
|
+
/**
|
|
1696
|
+
* Extract the error chain from a Throwable error
|
|
1697
|
+
* Returns an array of errors from outermost to innermost
|
|
1698
|
+
*
|
|
1699
|
+
* @param error - The error to extract the chain from
|
|
1700
|
+
* @returns An array of errors in the chain, from outermost to innermost
|
|
1701
|
+
*/
|
|
1702
|
+
getErrorChain: (error: Error | undefined) => Error[];
|
|
1703
|
+
/**
|
|
1704
|
+
* Format the error chain as a string with the option to include task details
|
|
1705
|
+
*
|
|
1706
|
+
* @param error - The error to format
|
|
1707
|
+
* @param options - Formatting options
|
|
1708
|
+
* @returns A formatted string representation of the error chain
|
|
1709
|
+
*/
|
|
1710
|
+
formatErrorChain: (error: Error | undefined, options?: {
|
|
1711
|
+
includeTasks?: boolean;
|
|
1712
|
+
separator?: string;
|
|
1713
|
+
includeStackTrace?: boolean;
|
|
1714
|
+
}) => string;
|
|
1715
|
+
/**
|
|
1716
|
+
* Convert a Promise-returning function to a Task-compatible function
|
|
1717
|
+
*/
|
|
1718
|
+
fromPromise: <U, Args extends unknown[]>(promiseFn: (...args: Args) => Promise<U>, params?: TaskParams) => ((...args: Args) => FPromise<TaskOutcome<U>>);
|
|
1719
|
+
/**
|
|
1720
|
+
* Convert a Task result to a Promise
|
|
1721
|
+
*/
|
|
1722
|
+
toPromise: <U>(taskOutcome: TaskOutcome<U>) => Promise<U>;
|
|
1723
|
+
/**
|
|
1724
|
+
* Race multiple tasks and return the result of the first one to complete
|
|
1725
|
+
* Optionally specify a timeout after which the race will fail
|
|
1726
|
+
*
|
|
1727
|
+
* @param tasks - Array of tasks to race (as FPromises)
|
|
1728
|
+
* @param timeoutMs - Optional timeout in milliseconds
|
|
1729
|
+
* @param params - Task parameters for the race operation
|
|
1730
|
+
* @returns A promise that resolves with the first task to complete or rejects if all tasks fail
|
|
1731
|
+
*/
|
|
1732
|
+
race: <T$1>(tasks: Array<FPromise<T$1> | FPromise<TaskOutcome<T$1>>>, timeoutMs?: number, params?: TaskParams) => FPromise<TaskOutcome<T$1>>;
|
|
1733
|
+
/**
|
|
1734
|
+
* Convert a Node.js style callback function to a Task-compatible function
|
|
1735
|
+
* Node.js callbacks typically have the signature (error, result) => void
|
|
1736
|
+
*
|
|
1737
|
+
* @param nodeFn - Function that accepts a Node.js style callback
|
|
1738
|
+
* @param params - Task parameters
|
|
1739
|
+
* @returns A function that returns an FPromise
|
|
1740
|
+
*/
|
|
1741
|
+
fromNodeCallback: <T$1, Args extends unknown[]>(nodeFn: (...args: [...Args, (error: unknown, result: T$1) => void]) => void, params?: TaskParams) => ((...args: Args) => FPromise<TaskOutcome<T$1>>);
|
|
1742
|
+
/**
|
|
1743
|
+
* Create a cancellation token source
|
|
1744
|
+
* @returns A cancellation token source that can be used to control task cancellation
|
|
1745
|
+
*/
|
|
1746
|
+
createCancellationTokenSource: () => CancellationTokenSource;
|
|
1747
|
+
/**
|
|
1748
|
+
* Create a task that can be cancelled
|
|
1749
|
+
*
|
|
1750
|
+
* @param task - The task function to make cancellable
|
|
1751
|
+
* @param params - Task parameters
|
|
1752
|
+
* @returns An object with the task and a function to cancel it
|
|
1753
|
+
*/
|
|
1754
|
+
cancellable: <T$1>(task: (token: CancellationToken) => Promise<T$1> | Promise<TaskOutcome<T$1>>, params?: TaskParams) => {
|
|
1755
|
+
task: FPromise<TaskOutcome<T$1>>;
|
|
1756
|
+
cancel: () => void;
|
|
1757
|
+
};
|
|
1758
|
+
/**
|
|
1759
|
+
* Creates a task with progress tracking
|
|
1760
|
+
*
|
|
1761
|
+
* @param task - The task function that accepts a progress updater
|
|
1762
|
+
* @param onProgress - Callback function that receives progress updates
|
|
1763
|
+
* @param params - Task parameters
|
|
1764
|
+
* @returns An object with the task, cancel function, and current progress
|
|
1765
|
+
*/
|
|
1766
|
+
withProgress: <T$1>(task: (updateProgress: (percent: number) => void, token: CancellationToken) => Promise<T$1> | Promise<TaskOutcome<T$1>>, onProgress?: (percent: number) => void, params?: TaskParams) => {
|
|
1767
|
+
task: FPromise<TaskOutcome<T$1>>;
|
|
1768
|
+
cancel: () => void;
|
|
1769
|
+
currentProgress: () => number;
|
|
1770
|
+
};
|
|
1771
|
+
};
|
|
1772
|
+
type Task = ReturnType<typeof Task>;
|
|
1773
|
+
//#endregion
|
|
1774
|
+
//#region src/error/ErrorFormatter.d.ts
|
|
1775
|
+
/**
|
|
1776
|
+
* Type definition for task information that may be attached to errors
|
|
1777
|
+
*/
|
|
1778
|
+
type TaskErrorInfo = {
|
|
1779
|
+
name?: string;
|
|
1780
|
+
description?: string;
|
|
1781
|
+
[key: string]: unknown;
|
|
1782
|
+
};
|
|
1783
|
+
/**
|
|
1784
|
+
* Type definition for an error with potential task information
|
|
1785
|
+
*/
|
|
1786
|
+
type ErrorWithTaskInfo = Error & {
|
|
1787
|
+
taskInfo?: TaskErrorInfo;
|
|
1788
|
+
data?: unknown;
|
|
1789
|
+
};
|
|
1790
|
+
/**
|
|
1791
|
+
* Type definition for a structured error chain element
|
|
1792
|
+
*/
|
|
1793
|
+
type ErrorChainElement = {
|
|
1794
|
+
message?: string;
|
|
1795
|
+
name?: string;
|
|
1796
|
+
taskInfo?: TaskErrorInfo;
|
|
1797
|
+
stack?: string;
|
|
1798
|
+
[key: string]: unknown;
|
|
1799
|
+
};
|
|
1800
|
+
/**
|
|
1801
|
+
* Options for formatting error chains
|
|
1802
|
+
*/
|
|
1803
|
+
type ErrorFormatterOptions = {
|
|
1804
|
+
/** Include task names in the formatted output */
|
|
1805
|
+
includeTasks?: boolean;
|
|
1806
|
+
/** Include stack traces in the formatted output */
|
|
1807
|
+
includeStackTrace?: boolean;
|
|
1808
|
+
/** Separator between error lines (default: newline) */
|
|
1809
|
+
separator?: string;
|
|
1810
|
+
/** Include detailed error data in the output */
|
|
1811
|
+
includeData?: boolean;
|
|
1812
|
+
/** Maximum number of stack frames to include if stack trace is enabled */
|
|
1813
|
+
maxStackFrames?: number;
|
|
1814
|
+
/** Title to display at the start of the formatted error */
|
|
1815
|
+
title?: string;
|
|
1816
|
+
/** Format the output with colors for console display */
|
|
1817
|
+
colors?: boolean;
|
|
1818
|
+
};
|
|
1819
|
+
/**
|
|
1820
|
+
* Safely stringify data including BigInt values and circular references
|
|
1821
|
+
*/
|
|
1822
|
+
declare function safeStringify(obj: unknown): string;
|
|
1823
|
+
/**
|
|
1824
|
+
* Format a stack trace string for better readability
|
|
1825
|
+
*/
|
|
1826
|
+
declare function formatStackTrace(stack: string | undefined): string;
|
|
1827
|
+
/**
|
|
1828
|
+
* Create a formatted string representation of an error for better logging and display
|
|
1829
|
+
*
|
|
1830
|
+
* @example
|
|
1831
|
+
* ```typescript
|
|
1832
|
+
* const error = new Error("Something went wrong");
|
|
1833
|
+
* console.error(formatError(error, { colors: true, includeData: true }));
|
|
1834
|
+
* ```
|
|
1835
|
+
*/
|
|
1836
|
+
declare function formatError(error: unknown, options?: ErrorFormatterOptions): string;
|
|
1837
|
+
/**
|
|
1838
|
+
* Create a serializer function for Pino or other JSON loggers
|
|
1839
|
+
* to better represent errors with their full context
|
|
1840
|
+
*/
|
|
1841
|
+
declare function createErrorSerializer(): (err: unknown) => unknown;
|
|
1842
|
+
//#endregion
|
|
1843
|
+
//#region src/error/ParseError.d.ts
|
|
1844
|
+
declare const ParseError: (message?: string) => Error & {
|
|
1845
|
+
name: "ParseError";
|
|
1846
|
+
};
|
|
1847
|
+
type ParseError = Error & {
|
|
1848
|
+
name: "ParseError";
|
|
1849
|
+
};
|
|
1850
|
+
//#endregion
|
|
1851
|
+
//#region src/error/typed/TypedError.d.ts
|
|
1852
|
+
/**
|
|
1853
|
+
* Type-safe error codes using template literal types
|
|
1854
|
+
*/
|
|
1855
|
+
type ErrorCode = "VALIDATION_FAILED" | "NETWORK_ERROR" | "AUTH_REQUIRED" | "NOT_FOUND" | "PERMISSION_DENIED" | "RATE_LIMITED" | "INTERNAL_ERROR" | "BAD_REQUEST" | "CONFLICT" | "TIMEOUT";
|
|
1856
|
+
/**
|
|
1857
|
+
* Template literal type for error messages based on error code
|
|
1858
|
+
*/
|
|
1859
|
+
type ErrorMessage<T$1 extends ErrorCode> = T$1 extends "VALIDATION_FAILED" ? `Validation failed: ${string}` : T$1 extends "NETWORK_ERROR" ? `Network error: ${string}` : T$1 extends "AUTH_REQUIRED" ? `Authentication required: ${string}` : T$1 extends "NOT_FOUND" ? `Not found: ${string}` : T$1 extends "PERMISSION_DENIED" ? `Permission denied: ${string}` : T$1 extends "RATE_LIMITED" ? `Rate limit exceeded: ${string}` : T$1 extends "INTERNAL_ERROR" ? `Internal server error: ${string}` : T$1 extends "BAD_REQUEST" ? `Bad request: ${string}` : T$1 extends "CONFLICT" ? `Conflict: ${string}` : T$1 extends "TIMEOUT" ? `Request timeout: ${string}` : never;
|
|
1860
|
+
/**
|
|
1861
|
+
* HTTP status codes mapped to error codes
|
|
1862
|
+
*/
|
|
1863
|
+
type ErrorStatus<T$1 extends ErrorCode> = T$1 extends "VALIDATION_FAILED" | "BAD_REQUEST" ? 400 : T$1 extends "AUTH_REQUIRED" ? 401 : T$1 extends "PERMISSION_DENIED" ? 403 : T$1 extends "NOT_FOUND" ? 404 : T$1 extends "CONFLICT" ? 409 : T$1 extends "RATE_LIMITED" ? 429 : T$1 extends "TIMEOUT" ? 408 : T$1 extends "INTERNAL_ERROR" ? 500 : T$1 extends "NETWORK_ERROR" ? 503 : 500;
|
|
1864
|
+
/**
|
|
1865
|
+
* Context type for each error code
|
|
1866
|
+
*/
|
|
1867
|
+
type TypedErrorContext<T$1 extends ErrorCode> = T$1 extends "VALIDATION_FAILED" ? {
|
|
1868
|
+
field: string;
|
|
1869
|
+
value: unknown;
|
|
1870
|
+
rule: string;
|
|
1871
|
+
} : T$1 extends "NETWORK_ERROR" ? {
|
|
1872
|
+
url: string;
|
|
1873
|
+
method: string;
|
|
1874
|
+
statusCode?: number;
|
|
1875
|
+
} : T$1 extends "AUTH_REQUIRED" ? {
|
|
1876
|
+
resource: string;
|
|
1877
|
+
requiredRole?: string;
|
|
1878
|
+
} : T$1 extends "NOT_FOUND" ? {
|
|
1879
|
+
resource: string;
|
|
1880
|
+
id: string | number;
|
|
1881
|
+
} : T$1 extends "PERMISSION_DENIED" ? {
|
|
1882
|
+
action: string;
|
|
1883
|
+
resource: string;
|
|
1884
|
+
userId?: string;
|
|
1885
|
+
} : T$1 extends "RATE_LIMITED" ? {
|
|
1886
|
+
limit: number;
|
|
1887
|
+
window: string;
|
|
1888
|
+
retryAfter?: number;
|
|
1889
|
+
} : T$1 extends "INTERNAL_ERROR" ? {
|
|
1890
|
+
errorId: string;
|
|
1891
|
+
timestamp: string;
|
|
1892
|
+
} : T$1 extends "BAD_REQUEST" ? {
|
|
1893
|
+
reason: string;
|
|
1894
|
+
expected?: string;
|
|
1895
|
+
} : T$1 extends "CONFLICT" ? {
|
|
1896
|
+
resource: string;
|
|
1897
|
+
conflictingValue: string;
|
|
1898
|
+
} : T$1 extends "TIMEOUT" ? {
|
|
1899
|
+
duration: number;
|
|
1900
|
+
operation: string;
|
|
1901
|
+
} : Record<string, unknown>;
|
|
1902
|
+
/**
|
|
1903
|
+
* Type-safe error class with template literal types
|
|
1904
|
+
*/
|
|
1905
|
+
interface TypedError<T$1 extends ErrorCode> extends Throwable {
|
|
1906
|
+
readonly code: T$1;
|
|
1907
|
+
readonly message: ErrorMessage<T$1>;
|
|
1908
|
+
readonly status: ErrorStatus<T$1>;
|
|
1909
|
+
readonly context: TypedErrorContext<T$1>;
|
|
1910
|
+
readonly timestamp: string;
|
|
1911
|
+
readonly traceId?: string;
|
|
1912
|
+
}
|
|
1913
|
+
declare const TypedError: (<T$1 extends ErrorCode>(code: T$1, message: ErrorMessage<T$1>, context: TypedErrorContext<T$1>, options?: {
|
|
1914
|
+
cause?: unknown;
|
|
1915
|
+
traceId?: string;
|
|
1916
|
+
}) => TypedError<T$1>) & {
|
|
1917
|
+
/**
|
|
1918
|
+
* Create a validation error
|
|
1919
|
+
* @example
|
|
1920
|
+
* const error = TypedError.validation("email", "test@", "must be valid email")
|
|
1921
|
+
* // Type: TypedError<"VALIDATION_FAILED">
|
|
1922
|
+
* // Message must match: "Validation failed: ..."
|
|
1923
|
+
*/
|
|
1924
|
+
validation: (field: string, value: unknown, rule: string) => TypedError<"VALIDATION_FAILED">;
|
|
1925
|
+
/**
|
|
1926
|
+
* Create a network error
|
|
1927
|
+
* @example
|
|
1928
|
+
* const error = TypedError.network("https://api.example.com", "POST", 500)
|
|
1929
|
+
* // Type: TypedError<"NETWORK_ERROR">
|
|
1930
|
+
*/
|
|
1931
|
+
network: (url: string, method: string, statusCode?: number) => TypedError<"NETWORK_ERROR">;
|
|
1932
|
+
/**
|
|
1933
|
+
* Create an authentication error
|
|
1934
|
+
* @example
|
|
1935
|
+
* const error = TypedError.auth("/api/admin", "admin")
|
|
1936
|
+
* // Type: TypedError<"AUTH_REQUIRED">
|
|
1937
|
+
*/
|
|
1938
|
+
auth: (resource: string, requiredRole?: string) => TypedError<"AUTH_REQUIRED">;
|
|
1939
|
+
/**
|
|
1940
|
+
* Create a not found error
|
|
1941
|
+
* @example
|
|
1942
|
+
* const error = TypedError.notFound("user", "123")
|
|
1943
|
+
* // Type: TypedError<"NOT_FOUND">
|
|
1944
|
+
*/
|
|
1945
|
+
notFound: (resource: string, id: string | number) => TypedError<"NOT_FOUND">;
|
|
1946
|
+
/**
|
|
1947
|
+
* Create a permission denied error
|
|
1948
|
+
* @example
|
|
1949
|
+
* const error = TypedError.permission("delete", "post", "user123")
|
|
1950
|
+
* // Type: TypedError<"PERMISSION_DENIED">
|
|
1951
|
+
*/
|
|
1952
|
+
permission: (action: string, resource: string, userId?: string) => TypedError<"PERMISSION_DENIED">;
|
|
1953
|
+
/**
|
|
1954
|
+
* Create a rate limit error
|
|
1955
|
+
* @example
|
|
1956
|
+
* const error = TypedError.rateLimit(100, "1h", 3600)
|
|
1957
|
+
* // Type: TypedError<"RATE_LIMITED">
|
|
1958
|
+
*/
|
|
1959
|
+
rateLimit: (limit: number, window: string, retryAfter?: number) => TypedError<"RATE_LIMITED">;
|
|
1960
|
+
/**
|
|
1961
|
+
* Create an internal error
|
|
1962
|
+
* @example
|
|
1963
|
+
* const error = TypedError.internal("ERR-500-ABC123")
|
|
1964
|
+
* // Type: TypedError<"INTERNAL_ERROR">
|
|
1965
|
+
*/
|
|
1966
|
+
internal: (errorId: string) => TypedError<"INTERNAL_ERROR">;
|
|
1967
|
+
/**
|
|
1968
|
+
* Create a bad request error
|
|
1969
|
+
* @example
|
|
1970
|
+
* const error = TypedError.badRequest("Invalid JSON", "valid JSON object")
|
|
1971
|
+
* // Type: TypedError<"BAD_REQUEST">
|
|
1972
|
+
*/
|
|
1973
|
+
badRequest: (reason: string, expected?: string) => TypedError<"BAD_REQUEST">;
|
|
1974
|
+
/**
|
|
1975
|
+
* Create a conflict error
|
|
1976
|
+
* @example
|
|
1977
|
+
* const error = TypedError.conflict("email", "user@example.com")
|
|
1978
|
+
* // Type: TypedError<"CONFLICT">
|
|
1979
|
+
*/
|
|
1980
|
+
conflict: (resource: string, conflictingValue: string) => TypedError<"CONFLICT">;
|
|
1981
|
+
/**
|
|
1982
|
+
* Create a timeout error
|
|
1983
|
+
* @example
|
|
1984
|
+
* const error = TypedError.timeout(30000, "database query")
|
|
1985
|
+
* // Type: TypedError<"TIMEOUT">
|
|
1986
|
+
*/
|
|
1987
|
+
timeout: (duration: number, operation: string) => TypedError<"TIMEOUT">;
|
|
1988
|
+
/**
|
|
1989
|
+
* Check if a value is a TypedError
|
|
1990
|
+
*/
|
|
1991
|
+
isTypedError: (value: unknown) => value is TypedError<ErrorCode>;
|
|
1992
|
+
/**
|
|
1993
|
+
* Check if a TypedError has a specific code
|
|
1994
|
+
*/
|
|
1995
|
+
hasCode: <T$1 extends ErrorCode>(error: TypedError<ErrorCode>, code: T$1) => error is TypedError<T$1>;
|
|
1996
|
+
};
|
|
1997
|
+
//#endregion
|
|
1998
|
+
//#region src/error/typed/Validation.d.ts
|
|
1999
|
+
/**
|
|
2000
|
+
* Validation rule types using template literal types
|
|
2001
|
+
*/
|
|
2002
|
+
type ValidationRule = `min:${number}` | `max:${number}` | `minLength:${number}` | `maxLength:${number}` | `pattern:${string}` | `email` | `url` | `uuid` | `required` | `numeric` | `alpha` | `alphanumeric` | `date` | `future` | `past` | `in:${string}` | `notIn:${string}`;
|
|
2003
|
+
/**
|
|
2004
|
+
* Validator function type
|
|
2005
|
+
*/
|
|
2006
|
+
type Validator<T$1> = (value: unknown) => Either<TypedError<"VALIDATION_FAILED">, T$1>;
|
|
2007
|
+
/**
|
|
2008
|
+
* Field validation result
|
|
2009
|
+
*/
|
|
2010
|
+
type FieldValidation<T$1> = {
|
|
2011
|
+
field: string;
|
|
2012
|
+
value: unknown;
|
|
2013
|
+
result: Either<TypedError<"VALIDATION_FAILED">, T$1>;
|
|
2014
|
+
};
|
|
2015
|
+
/**
|
|
2016
|
+
* Form validation result
|
|
2017
|
+
*/
|
|
2018
|
+
type FormValidation<T$1 extends Record<string, Type>> = Either<List<TypedError<"VALIDATION_FAILED">>, T$1>;
|
|
2019
|
+
declare const Validation: (<T$1 extends Type>(rule: ValidationRule) => Validator<T$1>) & {
|
|
2020
|
+
/**
|
|
2021
|
+
* Common pre-built validators
|
|
2022
|
+
*/
|
|
2023
|
+
validators: {
|
|
2024
|
+
email: Validator<string>;
|
|
2025
|
+
url: Validator<string>;
|
|
2026
|
+
uuid: Validator<string>;
|
|
2027
|
+
required: Validator<string>;
|
|
2028
|
+
numeric: Validator<number>;
|
|
2029
|
+
positiveNumber: Validator<number>;
|
|
2030
|
+
nonEmptyString: Validator<string>;
|
|
2031
|
+
};
|
|
2032
|
+
/**
|
|
2033
|
+
* Create a validator from a rule string
|
|
2034
|
+
* @example
|
|
2035
|
+
* const validator = Validation.rule<number>("min:18")
|
|
2036
|
+
* const result = validator(25) // Right(25)
|
|
2037
|
+
* const error = validator(15) // Left(TypedError)
|
|
2038
|
+
*/
|
|
2039
|
+
rule: <T$1 extends Type>(rule: ValidationRule) => Validator<T$1>;
|
|
2040
|
+
/**
|
|
2041
|
+
* Combine multiple validators
|
|
2042
|
+
* @example
|
|
2043
|
+
* const validator = Validation.combine(
|
|
2044
|
+
* Validation.rule<string>("required"),
|
|
2045
|
+
* Validation.rule<string>("email"),
|
|
2046
|
+
* Validation.rule<string>("maxLength:100")
|
|
2047
|
+
* )
|
|
2048
|
+
*/
|
|
2049
|
+
combine: <T$1 extends Type>(...validators: Validator<T$1>[]) => Validator<T$1>;
|
|
2050
|
+
/**
|
|
2051
|
+
* Create a custom validator
|
|
2052
|
+
* @example
|
|
2053
|
+
* const isEven = Validation.custom<number>(
|
|
2054
|
+
* (value) => typeof value === "number" && value % 2 === 0,
|
|
2055
|
+
* "must be an even number"
|
|
2056
|
+
* )
|
|
2057
|
+
*/
|
|
2058
|
+
custom: <T$1 extends Type>(predicate: (value: unknown) => boolean, errorMessage: string) => Validator<T$1>;
|
|
2059
|
+
/**
|
|
2060
|
+
* Validate a form with multiple fields
|
|
2061
|
+
* @example
|
|
2062
|
+
* const schema = {
|
|
2063
|
+
* name: Validation.rule<string>("required"),
|
|
2064
|
+
* email: Validation.rule<string>("email"),
|
|
2065
|
+
* age: Validation.rule<number>("min:18")
|
|
2066
|
+
* }
|
|
2067
|
+
* const result = Validation.form(schema, { name: "John", email: "john@example.com", age: 25 })
|
|
2068
|
+
*/
|
|
2069
|
+
form: <T$1 extends Record<string, Type>>(schema: { [K in keyof T$1]: Validator<T$1[K]> }, data: Record<string, unknown>) => FormValidation<T$1>;
|
|
2070
|
+
};
|
|
2071
|
+
//#endregion
|
|
2072
|
+
//#region src/foldable/index.d.ts
|
|
2073
|
+
/**
|
|
2074
|
+
* Utility functions for working with Foldable data structures
|
|
2075
|
+
*/
|
|
2076
|
+
declare const FoldableUtils: {
|
|
2077
|
+
/**
|
|
2078
|
+
* Converts a Foldable to an Option
|
|
2079
|
+
*
|
|
2080
|
+
* @param foldable - The foldable structure to convert
|
|
2081
|
+
* @returns An Option containing the value, or None if empty
|
|
2082
|
+
*/
|
|
2083
|
+
toOption: <A extends Type>(foldable: Foldable<A>) => Option<A>;
|
|
2084
|
+
/**
|
|
2085
|
+
* Converts a Foldable to a List
|
|
2086
|
+
*
|
|
2087
|
+
* @param foldable - The foldable structure to convert
|
|
2088
|
+
* @returns A List containing the value(s), or empty List if empty
|
|
2089
|
+
*/
|
|
2090
|
+
toList: <A extends Type>(foldable: Foldable<A>) => List<A>;
|
|
2091
|
+
/**
|
|
2092
|
+
* Converts a Foldable to an Either
|
|
2093
|
+
*
|
|
2094
|
+
* @param foldable - The foldable structure to convert
|
|
2095
|
+
* @param left - The value to use for Left if empty
|
|
2096
|
+
* @returns Either.Right with the value if non-empty, or Either.Left with left if empty
|
|
2097
|
+
*/
|
|
2098
|
+
toEither: <A extends Type, E>(foldable: Foldable<A>, left: E) => Either<E, A>;
|
|
2099
|
+
/**
|
|
2100
|
+
* Checks if the Foldable is empty
|
|
2101
|
+
*
|
|
2102
|
+
* @param foldable - The foldable structure to check
|
|
2103
|
+
* @returns true if empty, false otherwise
|
|
2104
|
+
*/
|
|
2105
|
+
isEmpty: <A extends Type>(foldable: Foldable<A>) => boolean;
|
|
2106
|
+
/**
|
|
2107
|
+
* Calculates the size of the Foldable
|
|
2108
|
+
*
|
|
2109
|
+
* @param foldable - The foldable structure to measure
|
|
2110
|
+
* @returns The size (number of elements)
|
|
2111
|
+
*/
|
|
2112
|
+
size: <A extends Type>(foldable: Foldable<A>) => number;
|
|
2113
|
+
};
|
|
2114
|
+
//#endregion
|
|
2115
|
+
//#region src/hkt/index.d.ts
|
|
2116
|
+
/**
|
|
2117
|
+
* Type function for representing higher-kinded types
|
|
2118
|
+
*/
|
|
2119
|
+
type Kind<F, A> = F extends ((arg: infer T) => infer R) ? R : never;
|
|
2120
|
+
/**
|
|
2121
|
+
* Type constructors for common Functype data types
|
|
2122
|
+
*/
|
|
2123
|
+
type OptionKind = <A>(a: A) => Option<A>;
|
|
2124
|
+
type ListKind = <A>(a: A) => List<A>;
|
|
2125
|
+
type EitherKind<E> = <A>(a: A) => Either<E, A>;
|
|
2126
|
+
type TryKind = <A>(a: A) => Try<A>;
|
|
2127
|
+
/**
|
|
2128
|
+
* Generic container types for type-safe operations
|
|
2129
|
+
* @internal
|
|
2130
|
+
*/
|
|
2131
|
+
type Mappable<T$1> = {
|
|
2132
|
+
map<U>(f: (value: T$1) => U): unknown;
|
|
2133
|
+
};
|
|
2134
|
+
/**
|
|
2135
|
+
* @internal
|
|
2136
|
+
*/
|
|
2137
|
+
type Flattenable = {
|
|
2138
|
+
flatten(): unknown;
|
|
2139
|
+
};
|
|
2140
|
+
/**
|
|
2141
|
+
* @internal
|
|
2142
|
+
*/
|
|
2143
|
+
type FlatMappable<T$1> = {
|
|
2144
|
+
flatMap<U>(f: (value: T$1) => unknown): unknown;
|
|
2145
|
+
};
|
|
2146
|
+
/**
|
|
2147
|
+
* Universal type that includes all potential return types from the HKT functions
|
|
2148
|
+
* Used to avoid 'any' usage which the linter prohibits
|
|
2149
|
+
*/
|
|
2150
|
+
type UniversalContainer = Option<unknown> | List<unknown> | Either<unknown, unknown> | Try<unknown>;
|
|
2151
|
+
/**
|
|
2152
|
+
* HKT provides utilities for working with higher-kinded types
|
|
2153
|
+
* This allows writing generic code that works across different
|
|
2154
|
+
* container types like Option, List, Either, etc.
|
|
2155
|
+
*/
|
|
2156
|
+
declare const HKT: {
|
|
2157
|
+
(): {
|
|
2158
|
+
_tag: string;
|
|
2159
|
+
map: <F, A, B>(fa: unknown, f: (a: A) => B) => unknown;
|
|
2160
|
+
flatten: <F, A>(ffa: unknown) => unknown;
|
|
2161
|
+
flatMap: <F, A, B>(fa: unknown, f: (a: A) => unknown) => unknown;
|
|
2162
|
+
ap: <F, A, B>(ff: unknown, fa: unknown) => unknown;
|
|
2163
|
+
sequence: <F, G, A>(fga: unknown) => unknown;
|
|
2164
|
+
traverse: <F, G, A, B>(fa: unknown, f: (a: A) => unknown) => unknown;
|
|
2165
|
+
_type: string;
|
|
2166
|
+
};
|
|
2167
|
+
map<F = unknown, A = unknown, B = unknown>(fa: unknown, f: (a: A) => B): unknown;
|
|
2168
|
+
flatten<F = unknown, A = unknown>(ffa: unknown): unknown;
|
|
2169
|
+
flatMap<F = unknown, A = unknown, B = unknown>(fa: unknown, f: (a: A) => unknown): unknown;
|
|
2170
|
+
ap<F = unknown, A = unknown, B = unknown>(ff: unknown, fa: unknown): unknown;
|
|
2171
|
+
sequence<F = unknown, G = unknown, A = unknown>(fga: unknown): unknown;
|
|
2172
|
+
traverse<F = unknown, G = unknown, A = unknown, B = unknown>(fa: unknown, f: (a: A) => unknown): unknown;
|
|
2173
|
+
isOption: <T$1 extends Type>(value: unknown) => value is Option<T$1> & Mappable<T$1> & FlatMappable<T$1>;
|
|
2174
|
+
isList: <T$1 extends Type>(value: unknown) => value is List<T$1> & Mappable<T$1> & Flattenable & FlatMappable<T$1>;
|
|
2175
|
+
isEither: <E extends Type, A extends Type>(value: unknown) => value is Either<E, A> & Mappable<A> & FlatMappable<A>;
|
|
2176
|
+
isTry: <T$1 extends Type>(value: unknown) => value is Try<T$1> & Mappable<T$1> & FlatMappable<T$1>;
|
|
2177
|
+
};
|
|
2178
|
+
//#endregion
|
|
2179
|
+
//#region src/identity/Identity.d.ts
|
|
2180
|
+
type Identity<T$1> = {
|
|
2181
|
+
id: T$1;
|
|
2182
|
+
isSame?: (other: Identity<T$1>) => boolean;
|
|
2183
|
+
};
|
|
2184
|
+
declare const Identity: (<T$1>(value: T$1) => Identity<T$1>) & {
|
|
2185
|
+
/**
|
|
2186
|
+
* Creates an Identity. Alias for Identity constructor.
|
|
2187
|
+
* @param value - The value to wrap
|
|
2188
|
+
* @returns Identity instance
|
|
2189
|
+
*/
|
|
2190
|
+
of: <T$1>(value: T$1) => Identity<T$1>;
|
|
2191
|
+
/**
|
|
2192
|
+
* Creates an Identity. Same as of.
|
|
2193
|
+
* @param value - The value to wrap
|
|
2194
|
+
* @returns Identity instance
|
|
2195
|
+
*/
|
|
2196
|
+
pure: <T$1>(value: T$1) => Identity<T$1>;
|
|
2197
|
+
};
|
|
2198
|
+
//#endregion
|
|
2199
|
+
//#region src/lazy/Lazy.d.ts
|
|
2200
|
+
/**
|
|
2201
|
+
* Lazy type module
|
|
2202
|
+
* @module Lazy
|
|
2203
|
+
* @category Core
|
|
2204
|
+
*/
|
|
2205
|
+
/**
|
|
2206
|
+
* The Lazy type represents a computation that is deferred until needed.
|
|
2207
|
+
* It provides memoization and safe evaluation with integration to Option, Either, and Try.
|
|
2208
|
+
* @typeParam T - The type of the value to be computed
|
|
2209
|
+
*/
|
|
2210
|
+
interface Lazy<T$1 extends Type> extends FunctypeBase<T$1, "Lazy">, Extractable<T$1>, Pipe<T$1> {
|
|
2211
|
+
/** Tag identifying this as a Lazy type */
|
|
2212
|
+
readonly _tag: "Lazy";
|
|
2213
|
+
/** Whether the computation has been evaluated */
|
|
2214
|
+
readonly isEvaluated: boolean;
|
|
2215
|
+
/**
|
|
2216
|
+
* Returns the computed value or a default value if computation fails
|
|
2217
|
+
* @param defaultValue - The value to return if computation fails
|
|
2218
|
+
* @returns The computed value or defaultValue
|
|
2219
|
+
*/
|
|
2220
|
+
orElse(defaultValue: T$1): T$1;
|
|
2221
|
+
/**
|
|
2222
|
+
* Returns the computed value or null if computation fails
|
|
2223
|
+
* @returns The computed value or null
|
|
2224
|
+
*/
|
|
2225
|
+
orNull(): T$1 | null;
|
|
2226
|
+
/**
|
|
2227
|
+
* Returns the computed value or throws an error if computation fails
|
|
2228
|
+
* @param error - Optional custom error to throw. If not provided, throws the computation error or a default error
|
|
2229
|
+
* @returns The computed value
|
|
2230
|
+
* @throws The specified error, computation error, or a default error
|
|
2231
|
+
*/
|
|
2232
|
+
orThrow(error?: Error): T$1;
|
|
2233
|
+
/**
|
|
2234
|
+
* Returns this Lazy if computation succeeds, otherwise returns the alternative Lazy
|
|
2235
|
+
* @param alternative - The alternative Lazy to use if computation fails
|
|
2236
|
+
* @returns This Lazy or the alternative
|
|
2237
|
+
*/
|
|
2238
|
+
or(alternative: Lazy<T$1>): Lazy<T$1>;
|
|
2239
|
+
/**
|
|
2240
|
+
* Maps the value inside the Lazy using the provided function
|
|
2241
|
+
* @param f - The mapping function
|
|
2242
|
+
* @returns A new Lazy containing the mapped value
|
|
2243
|
+
*/
|
|
2244
|
+
map<U extends Type>(f: (value: T$1) => U): Lazy<U>;
|
|
2245
|
+
/**
|
|
2246
|
+
* Applies a wrapped function to a wrapped value (Applicative pattern)
|
|
2247
|
+
* @param ff - A Lazy containing a function from T to U
|
|
2248
|
+
* @returns A new Lazy containing the result
|
|
2249
|
+
*/
|
|
2250
|
+
ap<U extends Type>(ff: Lazy<(value: T$1) => U>): Lazy<U>;
|
|
2251
|
+
/**
|
|
2252
|
+
* Maps the value inside the Lazy using an async function
|
|
2253
|
+
* @param f - The async mapping function
|
|
2254
|
+
* @returns A Promise of a new Lazy containing the mapped value
|
|
2255
|
+
*/
|
|
2256
|
+
mapAsync<U extends Type>(f: (value: T$1) => Promise<U>): Promise<Lazy<U>>;
|
|
2257
|
+
/**
|
|
2258
|
+
* Maps the value using a function that returns a Lazy
|
|
2259
|
+
* @param f - The mapping function returning a Lazy
|
|
2260
|
+
* @returns A new Lazy containing the flattened result
|
|
2261
|
+
*/
|
|
2262
|
+
flatMap<U extends Type>(f: (value: T$1) => Lazy<U>): Lazy<U>;
|
|
2263
|
+
/**
|
|
2264
|
+
* Maps the value using an async function that returns a Lazy
|
|
2265
|
+
* @param f - The async mapping function returning a Lazy
|
|
2266
|
+
* @returns A Promise of a new Lazy containing the flattened result
|
|
2267
|
+
*/
|
|
2268
|
+
flatMapAsync<U extends Type>(f: (value: T$1) => Promise<Lazy<U>>): Promise<Lazy<U>>;
|
|
2269
|
+
/**
|
|
2270
|
+
* Returns a Lazy that filters the value based on a predicate
|
|
2271
|
+
* @param predicate - The predicate function
|
|
2272
|
+
* @returns A Lazy containing an Option of the value
|
|
2273
|
+
*/
|
|
2274
|
+
filter(predicate: (value: T$1) => boolean): Lazy<Option<T$1>>;
|
|
2275
|
+
/**
|
|
2276
|
+
* Recovers from a failed computation by providing an alternative value
|
|
2277
|
+
* @param f - Function that takes the error and returns a recovery value
|
|
2278
|
+
* @returns A new Lazy that will use the recovery function if computation fails
|
|
2279
|
+
*/
|
|
2280
|
+
recover(f: (error: unknown) => T$1): Lazy<T$1>;
|
|
2281
|
+
/**
|
|
2282
|
+
* Recovers from a failed computation by providing an alternative Lazy
|
|
2283
|
+
* @param f - Function that takes the error and returns a recovery Lazy
|
|
2284
|
+
* @returns A new Lazy that will use the recovery Lazy if computation fails
|
|
2285
|
+
*/
|
|
2286
|
+
recoverWith(f: (error: unknown) => Lazy<T$1>): Lazy<T$1>;
|
|
2287
|
+
/**
|
|
2288
|
+
* Evaluates the computation and returns it as an Option
|
|
2289
|
+
* @returns Some containing the value if successful, None if computation fails
|
|
2290
|
+
*/
|
|
2291
|
+
toOption(): Option<T$1>;
|
|
2292
|
+
/**
|
|
2293
|
+
* Evaluates the computation and returns it as an Either
|
|
2294
|
+
* @returns Right containing the value if successful, Left containing the error if computation fails
|
|
2295
|
+
*/
|
|
2296
|
+
toEither(): Either<unknown, T$1>;
|
|
2297
|
+
/**
|
|
2298
|
+
* Evaluates the computation and returns it as an Either with a mapped error
|
|
2299
|
+
* @param mapError - Function to map the error
|
|
2300
|
+
* @returns Right containing the value if successful, Left containing the mapped error if computation fails
|
|
2301
|
+
*/
|
|
2302
|
+
toEitherWith<E>(mapError: (error: unknown) => E): Either<E, T$1>;
|
|
2303
|
+
/**
|
|
2304
|
+
* Evaluates the computation and returns it as a Try
|
|
2305
|
+
* @returns Try containing the result of the computation
|
|
2306
|
+
*/
|
|
2307
|
+
toTry(): Try<T$1>;
|
|
2308
|
+
/**
|
|
2309
|
+
* Applies an effect function to the value if computation succeeds
|
|
2310
|
+
* @param f - The effect function
|
|
2311
|
+
* @returns This Lazy for chaining
|
|
2312
|
+
*/
|
|
2313
|
+
tap(f: (value: T$1) => void): Lazy<T$1>;
|
|
2314
|
+
/**
|
|
2315
|
+
* Applies an effect function to the error if computation fails
|
|
2316
|
+
* @param f - The effect function for errors
|
|
2317
|
+
* @returns This Lazy for chaining
|
|
2318
|
+
*/
|
|
2319
|
+
tapError(f: (error: unknown) => void): Lazy<T$1>;
|
|
2320
|
+
/**
|
|
2321
|
+
* Pattern matching on the Lazy value
|
|
2322
|
+
* @param f - Function to apply to the computed value
|
|
2323
|
+
* @returns The result of applying f to the computed value
|
|
2324
|
+
*/
|
|
2325
|
+
fold<U>(f: (value: T$1) => U): U;
|
|
2326
|
+
/**
|
|
2327
|
+
* Pattern matching with success and failure handlers
|
|
2328
|
+
* @param onFailure - Function to handle computation failure
|
|
2329
|
+
* @param onSuccess - Function to handle successful computation
|
|
2330
|
+
* @returns The result of the appropriate handler
|
|
2331
|
+
*/
|
|
2332
|
+
foldWith<U>(onFailure: (error: unknown) => U, onSuccess: (value: T$1) => U): U;
|
|
2333
|
+
/**
|
|
2334
|
+
* Left fold operation
|
|
2335
|
+
* @param z - Initial value
|
|
2336
|
+
* @returns Function that takes an operator and applies it
|
|
2337
|
+
*/
|
|
2338
|
+
foldLeft: <B>(z: B) => (op: (b: B, a: T$1) => B) => B;
|
|
2339
|
+
/**
|
|
2340
|
+
* Right fold operation
|
|
2341
|
+
* @param z - Initial value
|
|
2342
|
+
* @returns Function that takes an operator and applies it
|
|
2343
|
+
*/
|
|
2344
|
+
foldRight: <B>(z: B) => (op: (a: T$1, b: B) => B) => B;
|
|
2345
|
+
/**
|
|
2346
|
+
* Pattern matching for the Lazy type
|
|
2347
|
+
* @param patterns - Object with handler for Lazy pattern
|
|
2348
|
+
* @returns The result of the matched handler
|
|
2349
|
+
*/
|
|
2350
|
+
match<R$1>(patterns: {
|
|
2351
|
+
Lazy: (value: T$1) => R$1;
|
|
2352
|
+
}): R$1;
|
|
2353
|
+
/**
|
|
2354
|
+
* Creates a string representation of the Lazy
|
|
2355
|
+
* @returns String representation showing evaluation status
|
|
2356
|
+
*/
|
|
2357
|
+
toString(): string;
|
|
2358
|
+
/**
|
|
2359
|
+
* Converts the Lazy to a value object
|
|
2360
|
+
* @returns Object representation of the Lazy with evaluation state
|
|
2361
|
+
*/
|
|
2362
|
+
toValue(): {
|
|
2363
|
+
_tag: "Lazy";
|
|
2364
|
+
evaluated: boolean;
|
|
2365
|
+
value?: T$1;
|
|
2366
|
+
};
|
|
2367
|
+
}
|
|
2368
|
+
/**
|
|
2369
|
+
* Creates a Lazy computation that defers evaluation until needed.
|
|
2370
|
+
* Results are memoized after first evaluation.
|
|
2371
|
+
*
|
|
2372
|
+
* @example
|
|
2373
|
+
* // Basic lazy evaluation
|
|
2374
|
+
* const expensive = Lazy(() => {
|
|
2375
|
+
* console.log("Computing...")
|
|
2376
|
+
* return 42
|
|
2377
|
+
* })
|
|
2378
|
+
* // Nothing printed yet
|
|
2379
|
+
* const result = expensive.orThrow() // Prints "Computing..." and returns 42
|
|
2380
|
+
* const cached = expensive.orThrow() // Returns 42 without printing
|
|
2381
|
+
*
|
|
2382
|
+
* @example
|
|
2383
|
+
* // Error handling
|
|
2384
|
+
* const risky = Lazy(() => {
|
|
2385
|
+
* if (Math.random() > 0.5) throw new Error("Failed")
|
|
2386
|
+
* return "Success"
|
|
2387
|
+
* })
|
|
2388
|
+
* const safe = risky.orElse("Default") // Returns "Success" or "Default"
|
|
2389
|
+
* const option = risky.toOption() // Some("Success") or None
|
|
2390
|
+
* const either = risky.toEither() // Right("Success") or Left(Error)
|
|
2391
|
+
*
|
|
2392
|
+
* @example
|
|
2393
|
+
* // Chaining computations
|
|
2394
|
+
* const result = Lazy(() => 10)
|
|
2395
|
+
* .map(x => x * 2)
|
|
2396
|
+
* .flatMap(x => Lazy(() => x + 5))
|
|
2397
|
+
* .recover(err => 0)
|
|
2398
|
+
* .orThrow() // 25
|
|
2399
|
+
*
|
|
2400
|
+
* @example
|
|
2401
|
+
* // Integration with functype
|
|
2402
|
+
* const userOption = Option({ id: 1, name: "Alice" })
|
|
2403
|
+
* const userName = Lazy.fromOption(userOption, () => ({ id: 0, name: "Anonymous" }))
|
|
2404
|
+
* .map(user => user.name)
|
|
2405
|
+
* .orThrow() // "Alice"
|
|
2406
|
+
*/
|
|
2407
|
+
declare const Lazy: (<T$1 extends Type>(thunk: () => T$1) => Lazy<T$1>) & {
|
|
2408
|
+
/**
|
|
2409
|
+
* Creates a Lazy from a thunk (deferred computation)
|
|
2410
|
+
* @param thunk - Function that computes the value
|
|
2411
|
+
* @returns A new Lazy instance
|
|
2412
|
+
*/
|
|
2413
|
+
of: <T$1 extends Type>(thunk: () => T$1) => Lazy<T$1>;
|
|
2414
|
+
/**
|
|
2415
|
+
* Creates a Lazy from an immediate value
|
|
2416
|
+
* @param value - The value to wrap
|
|
2417
|
+
* @returns A new Lazy instance that returns the value
|
|
2418
|
+
*/
|
|
2419
|
+
fromValue: <T$1 extends Type>(value: T$1) => Lazy<T$1>;
|
|
2420
|
+
/**
|
|
2421
|
+
* Creates a Lazy from an Option
|
|
2422
|
+
* @param option - The Option to convert
|
|
2423
|
+
* @param defaultThunk - Thunk to compute default value if Option is None
|
|
2424
|
+
* @returns A new Lazy instance
|
|
2425
|
+
*/
|
|
2426
|
+
fromOption: <T$1 extends Type>(option: Option<T$1>, defaultThunk: () => T$1) => Lazy<T$1>;
|
|
2427
|
+
/**
|
|
2428
|
+
* Creates a Lazy from a Try
|
|
2429
|
+
* @param tryValue - The Try to convert
|
|
2430
|
+
* @returns A new Lazy instance
|
|
2431
|
+
*/
|
|
2432
|
+
fromTry: <T$1 extends Type>(tryValue: Try<T$1>) => Lazy<T$1>;
|
|
2433
|
+
/**
|
|
2434
|
+
* Creates a Lazy from an Either
|
|
2435
|
+
* @param either - The Either to convert
|
|
2436
|
+
* @returns A new Lazy instance
|
|
2437
|
+
*/
|
|
2438
|
+
fromEither: <E, T$1 extends Type>(either: Either<E, T$1>) => Lazy<T$1>;
|
|
2439
|
+
/**
|
|
2440
|
+
* Creates a Lazy that will throw an error since promises need to be awaited first
|
|
2441
|
+
* @param promise - The Promise to convert
|
|
2442
|
+
* @returns A new Lazy instance that throws an error
|
|
2443
|
+
*/
|
|
2444
|
+
fromPromise: <T$1 extends Type>(_promise: Promise<T$1>) => Lazy<T$1>;
|
|
2445
|
+
/**
|
|
2446
|
+
* Creates a failed Lazy that will throw when evaluated
|
|
2447
|
+
* @param error - The error to throw
|
|
2448
|
+
* @returns A new Lazy instance that throws the error
|
|
2449
|
+
*/
|
|
2450
|
+
fail: <T$1 extends Type>(error: unknown) => Lazy<T$1>;
|
|
2451
|
+
};
|
|
2452
|
+
//#endregion
|
|
2453
|
+
//#region src/traversable/Traversable.d.ts
|
|
2454
|
+
/**
|
|
2455
|
+
* Traversable typeclass for data structures that can be traversed through
|
|
2456
|
+
*/
|
|
2457
|
+
interface Traversable<A extends Type> extends AsyncMonad<A> {
|
|
2458
|
+
get size(): number;
|
|
2459
|
+
get isEmpty(): boolean;
|
|
2460
|
+
contains(value: A): boolean;
|
|
2461
|
+
reduce(f: (b: A, a: A) => A): A;
|
|
2462
|
+
reduceRight(f: (b: A, a: A) => A): A;
|
|
2463
|
+
}
|
|
2464
|
+
//#endregion
|
|
2465
|
+
//#region src/map/Map.d.ts
|
|
2466
|
+
/**
|
|
2467
|
+
* A traversable interface for map that excludes map and flatMap operations
|
|
2468
|
+
*/
|
|
2469
|
+
type SafeTraversable<K$1, V> = Omit<Traversable<Tuple<[K$1, V]>>, "map" | "flatMap" | "flatMapAsync" | "ap">;
|
|
2470
|
+
interface Map$1<K$1, V> extends SafeTraversable<K$1, V>, Collection<Tuple<[K$1, V]>>, Typeable<"Map">, Serializable<[K$1, V][]>, Pipe<[K$1, V][]>, Foldable<Tuple<[K$1, V]>>, Iterable<[K$1, V]> {
|
|
2471
|
+
readonly _tag: "Map";
|
|
2472
|
+
add(item: Tuple<[K$1, V]>): Map$1<K$1, V>;
|
|
2473
|
+
remove(value: K$1): Map$1<K$1, V>;
|
|
2474
|
+
map<U>(f: (value: V) => U): Map$1<K$1, U>;
|
|
2475
|
+
ap<U>(ff: Map$1<K$1, (value: V) => U>): Map$1<K$1, U>;
|
|
2476
|
+
flatMap<K2, V2>(f: (entry: Tuple<[K$1, V]>) => Iterable<[K2, V2]>): Map$1<K2, V2>;
|
|
2477
|
+
flatMapAsync<U>(f: (value: V) => PromiseLike<Map$1<K$1, U>>): PromiseLike<Map$1<K$1, U>>;
|
|
2478
|
+
get(key: K$1): Option<V>;
|
|
2479
|
+
getOrElse(key: K$1, defaultValue: V): V;
|
|
2480
|
+
orElse(key: K$1, alternative: Option<V>): Option<V>;
|
|
2481
|
+
fold<U extends Type>(onEmpty: () => U, onValue: (value: Tuple<[K$1, V]>) => U): U;
|
|
2482
|
+
foldLeft<B>(z: B): (op: (b: B, a: Tuple<[K$1, V]>) => B) => B;
|
|
2483
|
+
foldRight<B>(z: B): (op: (a: Tuple<[K$1, V]>, b: B) => B) => B;
|
|
2484
|
+
/**
|
|
2485
|
+
* Pattern matches over the Map, applying a handler function based on whether it's empty
|
|
2486
|
+
* @param patterns - Object with handler functions for Empty and NonEmpty variants
|
|
2487
|
+
* @returns The result of applying the matching handler function
|
|
2488
|
+
*/
|
|
2489
|
+
match<R$1>(patterns: {
|
|
2490
|
+
Empty: () => R$1;
|
|
2491
|
+
NonEmpty: (entries: Array<Tuple<[K$1, V]>>) => R$1;
|
|
2492
|
+
}): R$1;
|
|
2493
|
+
toValue(): {
|
|
2494
|
+
_tag: "Map";
|
|
2495
|
+
value: [K$1, V][];
|
|
2496
|
+
};
|
|
2497
|
+
}
|
|
2498
|
+
declare const Map$1: (<K$1, V>(entries?: readonly (readonly [K$1, V])[] | IterableIterator<[K$1, V]> | null) => Map$1<K$1, V>) & {
|
|
2499
|
+
/**
|
|
2500
|
+
* Creates a Map from JSON string
|
|
2501
|
+
* @param json - The JSON string
|
|
2502
|
+
* @returns Map instance
|
|
2503
|
+
*/
|
|
2504
|
+
fromJSON: <K$1, V>(json: string) => Map$1<K$1, V>;
|
|
2505
|
+
/**
|
|
2506
|
+
* Creates a Map from YAML string
|
|
2507
|
+
* @param yaml - The YAML string
|
|
2508
|
+
* @returns Map instance
|
|
2509
|
+
*/
|
|
2510
|
+
fromYAML: <K$1, V>(yaml: string) => Map$1<K$1, V>;
|
|
2511
|
+
/**
|
|
2512
|
+
* Creates a Map from binary string
|
|
2513
|
+
* @param binary - The binary string
|
|
2514
|
+
* @returns Map instance
|
|
2515
|
+
*/
|
|
2516
|
+
fromBinary: <K$1, V>(binary: string) => Map$1<K$1, V>;
|
|
2517
|
+
};
|
|
2518
|
+
//#endregion
|
|
2519
|
+
//#region src/map/shim.d.ts
|
|
2520
|
+
/**
|
|
2521
|
+
* Type alias for the native JavaScript Map
|
|
2522
|
+
* @interface
|
|
2523
|
+
* @module Map
|
|
2524
|
+
* @category Collections
|
|
2525
|
+
*/
|
|
2526
|
+
type ESMapType<K$1, V> = Map<K$1, V>;
|
|
2527
|
+
/**
|
|
2528
|
+
* Reference to the native JavaScript Map
|
|
2529
|
+
* @module Map
|
|
2530
|
+
* @category Collections
|
|
2531
|
+
*/
|
|
2532
|
+
declare const ESMap: MapConstructor;
|
|
2533
|
+
//#endregion
|
|
2534
|
+
//#region src/matchable/Matchable.d.ts
|
|
2535
|
+
/**
|
|
2536
|
+
* Pattern matching interface for functional data types.
|
|
2537
|
+
*
|
|
2538
|
+
* @typeParam A - The type of elements in the data structure
|
|
2539
|
+
* @typeParam Tags - The type of tags used for pattern matching
|
|
2540
|
+
*/
|
|
2541
|
+
interface Matchable<A, Tags extends string = string> {
|
|
2542
|
+
/**
|
|
2543
|
+
* Pattern matches against this data structure, applying handlers for each variant based on tag.
|
|
2544
|
+
* Similar to fold but with stronger type safety for tag-based variants.
|
|
2545
|
+
*
|
|
2546
|
+
* The return type is inferred from the pattern handlers when not explicitly specified.
|
|
2547
|
+
*
|
|
2548
|
+
* @param patterns - An object containing handler functions for each variant
|
|
2549
|
+
* @returns The result of applying the matching handler function
|
|
2550
|
+
*/
|
|
2551
|
+
match<R$1>(patterns: Record<Tags, (value: A) => R$1>): R$1;
|
|
2552
|
+
}
|
|
2553
|
+
/**
|
|
2554
|
+
* Utility functions for working with Matchable data structures
|
|
2555
|
+
*/
|
|
2556
|
+
declare const MatchableUtils: {
|
|
2557
|
+
/**
|
|
2558
|
+
* Helper function to create a default case for pattern matching
|
|
2559
|
+
*
|
|
2560
|
+
* @param handler - The default handler function to apply
|
|
2561
|
+
* @returns A function that always applies the default handler
|
|
2562
|
+
*/
|
|
2563
|
+
default: <A, R$1>(handler: (value: A) => R$1) => (value: A) => R$1;
|
|
2564
|
+
/**
|
|
2565
|
+
* Helper function to create a match pattern that guards based on a predicate
|
|
2566
|
+
*
|
|
2567
|
+
* @param predicate - The predicate function for guarding
|
|
2568
|
+
* @param handler - The handler function to apply if the predicate passes
|
|
2569
|
+
* @returns A function that applies the handler only if the predicate passes
|
|
2570
|
+
*/
|
|
2571
|
+
when: <A, R$1>(predicate: (value: A) => boolean, handler: (value: A) => R$1) => (value: A) => R$1 | undefined;
|
|
2572
|
+
};
|
|
2573
|
+
//#endregion
|
|
2574
|
+
//#region src/ref/Ref.d.ts
|
|
2575
|
+
/**
|
|
2576
|
+
* A mutable reference container that holds a value of type A.
|
|
2577
|
+
* This provides controlled mutability in a functional context.
|
|
2578
|
+
*
|
|
2579
|
+
* @example
|
|
2580
|
+
* const counter = Ref(0)
|
|
2581
|
+
* counter.get() // 0
|
|
2582
|
+
* counter.set(5)
|
|
2583
|
+
* counter.get() // 5
|
|
2584
|
+
* counter.update(n => n + 1)
|
|
2585
|
+
* counter.get() // 6
|
|
2586
|
+
*/
|
|
2587
|
+
interface Ref<A> {
|
|
2588
|
+
/**
|
|
2589
|
+
* Get the current value
|
|
2590
|
+
*/
|
|
2591
|
+
get(): A;
|
|
2592
|
+
/**
|
|
2593
|
+
* Set a new value
|
|
2594
|
+
*/
|
|
2595
|
+
set(value: A): void;
|
|
2596
|
+
/**
|
|
2597
|
+
* Update the value using a function
|
|
2598
|
+
*/
|
|
2599
|
+
update(f: (current: A) => A): void;
|
|
2600
|
+
/**
|
|
2601
|
+
* Update and return the old value
|
|
2602
|
+
*/
|
|
2603
|
+
getAndSet(value: A): A;
|
|
2604
|
+
/**
|
|
2605
|
+
* Update and return the new value
|
|
2606
|
+
*/
|
|
2607
|
+
updateAndGet(f: (current: A) => A): A;
|
|
2608
|
+
/**
|
|
2609
|
+
* Update and return the old value
|
|
2610
|
+
*/
|
|
2611
|
+
getAndUpdate(f: (current: A) => A): A;
|
|
2612
|
+
/**
|
|
2613
|
+
* Compare and swap - only updates if current value equals expected
|
|
2614
|
+
*/
|
|
2615
|
+
compareAndSet(expected: A, newValue: A): boolean;
|
|
2616
|
+
/**
|
|
2617
|
+
* Modify the value and return a result
|
|
2618
|
+
*/
|
|
2619
|
+
modify<B>(f: (current: A) => [A, B]): B;
|
|
2620
|
+
}
|
|
2621
|
+
declare const Ref: (<A extends Type>(initial: A) => Ref<A>) & {
|
|
2622
|
+
/**
|
|
2623
|
+
* Creates a Ref. Alias for Ref constructor.
|
|
2624
|
+
* @param initial - The initial value
|
|
2625
|
+
* @returns Ref instance
|
|
2626
|
+
*/
|
|
2627
|
+
of: <A extends Type>(initial: A) => Ref<A>;
|
|
2628
|
+
};
|
|
2629
|
+
//#endregion
|
|
2630
|
+
//#region src/serialization/SerializationCompanion.d.ts
|
|
2631
|
+
/**
|
|
2632
|
+
* Serialization result containing methods for different formats
|
|
2633
|
+
*/
|
|
2634
|
+
interface SerializationResult {
|
|
2635
|
+
/** Serializes to JSON string */
|
|
2636
|
+
toJSON: () => string;
|
|
2637
|
+
/** Serializes to YAML string */
|
|
2638
|
+
toYAML: () => string;
|
|
2639
|
+
/** Serializes to base64-encoded binary string */
|
|
2640
|
+
toBinary: () => string;
|
|
2641
|
+
}
|
|
2642
|
+
/**
|
|
2643
|
+
* Creates a serializer for a simple tagged value
|
|
2644
|
+
* @param tag - The type tag (e.g., "Some", "List", "Success")
|
|
2645
|
+
* @param value - The value to serialize
|
|
2646
|
+
* @returns Serialization methods
|
|
2647
|
+
*/
|
|
2648
|
+
declare const createSerializer: (tag: string, value: unknown) => SerializationResult;
|
|
2649
|
+
/**
|
|
2650
|
+
* Creates a serializer for complex objects with custom serialization logic
|
|
2651
|
+
* @param data - The data object to serialize (should include _tag)
|
|
2652
|
+
* @returns Serialization methods
|
|
2653
|
+
*/
|
|
2654
|
+
declare const createCustomSerializer: (data: Record<string, unknown>) => SerializationResult;
|
|
2655
|
+
/**
|
|
2656
|
+
* Generic deserializer from JSON
|
|
2657
|
+
* @param json - The JSON string to parse
|
|
2658
|
+
* @param reconstructor - Function to reconstruct the type from parsed data
|
|
2659
|
+
* @returns Reconstructed instance
|
|
2660
|
+
*/
|
|
2661
|
+
declare const fromJSON: <T$1>(json: string, reconstructor: (parsed: {
|
|
2662
|
+
_tag: string;
|
|
2663
|
+
[key: string]: unknown;
|
|
2664
|
+
}) => T$1) => T$1;
|
|
2665
|
+
/**
|
|
2666
|
+
* Generic deserializer from YAML (simple format)
|
|
2667
|
+
* @param yaml - The YAML string to parse
|
|
2668
|
+
* @param reconstructor - Function to reconstruct the type from parsed data
|
|
2669
|
+
* @returns Reconstructed instance
|
|
2670
|
+
*/
|
|
2671
|
+
declare const fromYAML: <T$1>(yaml: string, reconstructor: (parsed: {
|
|
2672
|
+
_tag: string;
|
|
2673
|
+
[key: string]: unknown;
|
|
2674
|
+
}) => T$1) => T$1;
|
|
2675
|
+
/**
|
|
2676
|
+
* Generic deserializer from binary (base64-encoded JSON)
|
|
2677
|
+
* @param binary - The base64-encoded binary string
|
|
2678
|
+
* @param reconstructor - Function to reconstruct the type from parsed data
|
|
2679
|
+
* @returns Reconstructed instance
|
|
2680
|
+
*/
|
|
2681
|
+
declare const fromBinary: <T$1>(binary: string, reconstructor: (parsed: {
|
|
2682
|
+
_tag: string;
|
|
2683
|
+
[key: string]: unknown;
|
|
2684
|
+
}) => T$1) => T$1;
|
|
2685
|
+
/**
|
|
2686
|
+
* Creates companion serialization methods for a type
|
|
2687
|
+
* @param reconstructor - Function to reconstruct the type from parsed data
|
|
2688
|
+
* @returns Companion methods object with fromJSON, fromYAML, and fromBinary
|
|
2689
|
+
*/
|
|
2690
|
+
declare const createSerializationCompanion: <T$1>(reconstructor: (parsed: {
|
|
2691
|
+
_tag: string;
|
|
2692
|
+
[key: string]: unknown;
|
|
2693
|
+
}) => T$1) => {
|
|
2694
|
+
fromJSON: (json: string) => T$1;
|
|
2695
|
+
fromYAML: (yaml: string) => T$1;
|
|
2696
|
+
fromBinary: (binary: string) => T$1;
|
|
2697
|
+
};
|
|
2698
|
+
//#endregion
|
|
2699
|
+
//#region src/set/Set.d.ts
|
|
2700
|
+
interface Set<A> extends FunctypeCollection<A, "Set">, Collection<A> {
|
|
2701
|
+
add: (value: A) => Set<A>;
|
|
2702
|
+
remove: (value: A) => Set<A>;
|
|
2703
|
+
contains: (value: A) => boolean;
|
|
2704
|
+
has: (value: A) => boolean;
|
|
2705
|
+
map: <B>(f: (a: A) => B) => Set<B>;
|
|
2706
|
+
flatMap: <B>(f: (a: A) => Iterable<B>) => Set<B>;
|
|
2707
|
+
filter: (p: (a: A) => boolean) => Set<A>;
|
|
2708
|
+
filterNot: (p: (a: A) => boolean) => Set<A>;
|
|
2709
|
+
fold: <U extends Type>(onEmpty: () => U, onValue: (value: A) => U) => U;
|
|
2710
|
+
toList: () => List<A>;
|
|
2711
|
+
toSet: () => Set<A>;
|
|
2712
|
+
toArray: <B = A>() => B[];
|
|
2713
|
+
toString: () => string;
|
|
2714
|
+
}
|
|
2715
|
+
declare const Set: (<A>(iterable?: Iterable<A>) => Set<A>) & {
|
|
2716
|
+
/**
|
|
2717
|
+
* Creates a Set from JSON string
|
|
2718
|
+
* @param json - The JSON string
|
|
2719
|
+
* @returns Set instance
|
|
2720
|
+
*/
|
|
2721
|
+
fromJSON: <A>(json: string) => Set<A>;
|
|
2722
|
+
/**
|
|
2723
|
+
* Creates a Set from YAML string
|
|
2724
|
+
* @param yaml - The YAML string
|
|
2725
|
+
* @returns Set instance
|
|
2726
|
+
*/
|
|
2727
|
+
fromYAML: <A>(yaml: string) => Set<A>;
|
|
2728
|
+
/**
|
|
2729
|
+
* Creates a Set from binary string
|
|
2730
|
+
* @param binary - The binary string
|
|
2731
|
+
* @returns Set instance
|
|
2732
|
+
*/
|
|
2733
|
+
fromBinary: <A>(binary: string) => Set<A>;
|
|
2734
|
+
};
|
|
2735
|
+
//#endregion
|
|
2736
|
+
//#region src/valuable/Valuable.d.ts
|
|
2737
|
+
/**
|
|
2738
|
+
* Parameters for creating a Valuable instance
|
|
2739
|
+
*/
|
|
2740
|
+
type ValuableParams<Tag extends string, T$1, V> = {
|
|
2741
|
+
_tag: Tag;
|
|
2742
|
+
impl: T$1;
|
|
2743
|
+
value: V;
|
|
2744
|
+
};
|
|
2745
|
+
/**
|
|
2746
|
+
* Represents a type that can extract its inner value. Creates a Valuable wrapper that adds value extraction capabilities.
|
|
2747
|
+
* @param params - Configuration parameters
|
|
2748
|
+
* @module Valuable
|
|
2749
|
+
* @category Utilities
|
|
2750
|
+
*/
|
|
2751
|
+
declare function Valuable<Tag extends string, V, T$1 = object>(params: ValuableParams<Tag, T$1, V>): T$1 & {
|
|
2752
|
+
toValue: () => {
|
|
2753
|
+
_tag: Tag;
|
|
2754
|
+
value: V;
|
|
2755
|
+
};
|
|
2756
|
+
_tag: Tag;
|
|
2757
|
+
};
|
|
2758
|
+
type Valuable<Tag extends string, V, T$1 = object> = Typeable<Tag, T$1> & {
|
|
2759
|
+
toValue: () => {
|
|
2760
|
+
_tag: Tag;
|
|
2761
|
+
value: V;
|
|
2762
|
+
};
|
|
2763
|
+
};
|
|
2764
|
+
//#endregion
|
|
2765
|
+
//#region src/stack/Stack.d.ts
|
|
2766
|
+
/**
|
|
2767
|
+
* Stack data structure - Last In, First Out (LIFO)
|
|
2768
|
+
* Implements the Traversable interface for working with ordered collections
|
|
2769
|
+
*/
|
|
2770
|
+
type Stack<A extends Type> = {
|
|
2771
|
+
/**
|
|
2772
|
+
* Push a value onto the top of the stack
|
|
2773
|
+
* @param value - The value to push
|
|
2774
|
+
* @returns A new Stack with the value added
|
|
2775
|
+
*/
|
|
2776
|
+
push(value: A): Stack<A>;
|
|
2777
|
+
/**
|
|
2778
|
+
* Remove and return the top value from the stack
|
|
2779
|
+
* @returns A tuple containing the new Stack and the value
|
|
2780
|
+
*/
|
|
2781
|
+
pop(): [Stack<A>, Option<A>];
|
|
2782
|
+
/**
|
|
2783
|
+
* Return the top value without removing it
|
|
2784
|
+
* @returns The top value wrapped in an Option
|
|
2785
|
+
*/
|
|
2786
|
+
peek(): Option<A>;
|
|
2787
|
+
/**
|
|
2788
|
+
* Transforms each element in the stack using the provided function
|
|
2789
|
+
* @param f - The mapping function
|
|
2790
|
+
* @returns A new Stack with transformed elements
|
|
2791
|
+
*/
|
|
2792
|
+
map<B extends Type>(f: (a: A) => B): Stack<B>;
|
|
2793
|
+
/**
|
|
2794
|
+
* Maps each element to a Stack and flattens the result
|
|
2795
|
+
* @param f - The mapping function returning a Stack
|
|
2796
|
+
* @returns A new flattened Stack
|
|
2797
|
+
*/
|
|
2798
|
+
flatMap<B extends Type>(f: (a: A) => Stack<B>): Stack<B>;
|
|
2799
|
+
/**
|
|
2800
|
+
* Applies a Stack of functions to this Stack
|
|
2801
|
+
* @param ff - Stack of functions to apply
|
|
2802
|
+
* @returns A new Stack with applied functions
|
|
2803
|
+
*/
|
|
2804
|
+
ap<B extends Type>(ff: Stack<(value: A) => B>): Stack<B>;
|
|
2805
|
+
/**
|
|
2806
|
+
* Maps each element to an async Stack and flattens the result
|
|
2807
|
+
* @param f - The async mapping function returning a Stack
|
|
2808
|
+
* @returns A promise of the new flattened Stack
|
|
2809
|
+
*/
|
|
2810
|
+
flatMapAsync<B extends Type>(f: (value: A) => PromiseLike<Stack<B>>): PromiseLike<Stack<B>>;
|
|
2811
|
+
/**
|
|
2812
|
+
* Convert the stack to a List
|
|
2813
|
+
* @returns A List containing all elements
|
|
2814
|
+
*/
|
|
2815
|
+
toList(): List<A>;
|
|
2816
|
+
/**
|
|
2817
|
+
* Convert the stack to an array
|
|
2818
|
+
* @returns An array of all elements
|
|
2819
|
+
*/
|
|
2820
|
+
toArray(): A[];
|
|
2821
|
+
/**
|
|
2822
|
+
* Returns a string representation of the stack
|
|
2823
|
+
* @returns A string representation
|
|
2824
|
+
*/
|
|
2825
|
+
toString(): string;
|
|
2826
|
+
/**
|
|
2827
|
+
* Pattern matches over the Stack, applying a handler function based on whether it's empty
|
|
2828
|
+
* @param patterns - Object with handler functions for Empty and NonEmpty variants
|
|
2829
|
+
* @returns The result of applying the matching handler function
|
|
2830
|
+
*/
|
|
2831
|
+
match<R$1>(patterns: {
|
|
2832
|
+
Empty: () => R$1;
|
|
2833
|
+
NonEmpty: (values: A[]) => R$1;
|
|
2834
|
+
}): R$1;
|
|
2835
|
+
} & Traversable<A> & Valuable<"Stack", A[]> & Serializable<A> & Pipe<A[]> & Foldable<A> & Matchable<A[], "Empty" | "NonEmpty">;
|
|
2836
|
+
declare const Stack: (<A extends Type>(values?: A[]) => Stack<A>) & {
|
|
2837
|
+
/**
|
|
2838
|
+
* Creates an empty stack
|
|
2839
|
+
* @returns An empty Stack instance
|
|
2840
|
+
*/
|
|
2841
|
+
empty: <A extends Type>() => Stack<A>;
|
|
2842
|
+
/**
|
|
2843
|
+
* Creates a Stack from a single value
|
|
2844
|
+
* @param value - The value to create a stack with
|
|
2845
|
+
* @returns A Stack with a single value
|
|
2846
|
+
*/
|
|
2847
|
+
of: <A extends Type>(value: A) => Stack<A>;
|
|
2848
|
+
/**
|
|
2849
|
+
* Creates a Stack from JSON string
|
|
2850
|
+
* @param json - The JSON string
|
|
2851
|
+
* @returns Stack instance
|
|
2852
|
+
*/
|
|
2853
|
+
fromJSON: <A>(json: string) => Stack<A>;
|
|
2854
|
+
/**
|
|
2855
|
+
* Creates a Stack from YAML string
|
|
2856
|
+
* @param yaml - The YAML string
|
|
2857
|
+
* @returns Stack instance
|
|
2858
|
+
*/
|
|
2859
|
+
fromYAML: <A>(yaml: string) => Stack<A>;
|
|
2860
|
+
/**
|
|
2861
|
+
* Creates a Stack from binary string
|
|
2862
|
+
* @param binary - The binary string
|
|
2863
|
+
* @returns Stack instance
|
|
2864
|
+
*/
|
|
2865
|
+
fromBinary: <A>(binary: string) => Stack<A>;
|
|
2866
|
+
};
|
|
2867
|
+
//#endregion
|
|
2868
|
+
//#region src/option/Option.d.ts
|
|
2869
|
+
/**
|
|
2870
|
+
* Option type module
|
|
2871
|
+
* @module Option
|
|
2872
|
+
* @category Core
|
|
2873
|
+
*/
|
|
2874
|
+
/**
|
|
2875
|
+
* The Option type represents a value that may or may not exist.
|
|
2876
|
+
* It's used to handle potentially null or undefined values in a type-safe way.
|
|
2877
|
+
* @typeParam T - The type of the value contained in the Option
|
|
2878
|
+
*/
|
|
2879
|
+
interface Option<T$1 extends Type> extends Functype<T$1, "Some" | "None">, Promisable<T$1>, Doable<T$1>, Reshapeable<T$1> {
|
|
2880
|
+
/** The contained value (undefined for None) */
|
|
2881
|
+
readonly value: T$1 | undefined;
|
|
2882
|
+
/** Whether this Option contains no value */
|
|
2883
|
+
isEmpty: boolean;
|
|
2884
|
+
/**
|
|
2885
|
+
* Returns true if this Option is a Some (contains a value)
|
|
2886
|
+
* @returns true if this Option contains a value, false otherwise
|
|
2887
|
+
*/
|
|
2888
|
+
isSome(): this is Option<T$1> & {
|
|
2889
|
+
value: T$1;
|
|
2890
|
+
isEmpty: false;
|
|
2891
|
+
};
|
|
2892
|
+
/**
|
|
2893
|
+
* Returns true if this Option is a None (contains no value)
|
|
2894
|
+
* @returns true if this Option is empty, false otherwise
|
|
2895
|
+
*/
|
|
2896
|
+
isNone(): this is Option<T$1> & {
|
|
2897
|
+
value: undefined;
|
|
2898
|
+
isEmpty: true;
|
|
2899
|
+
};
|
|
2900
|
+
/**
|
|
2901
|
+
* Returns the contained value or a default value if None
|
|
2902
|
+
* @param defaultValue - The value to return if this Option is None
|
|
2903
|
+
* @returns The contained value or defaultValue
|
|
2904
|
+
*/
|
|
2905
|
+
orElse(defaultValue: T$1): T$1;
|
|
2906
|
+
/**
|
|
2907
|
+
* Returns the contained value or throws an error if None
|
|
2908
|
+
* @param error - Optional custom error to throw. If not provided, throws a default error
|
|
2909
|
+
* @returns The contained value
|
|
2910
|
+
* @throws The specified error or a default error if the Option is None
|
|
2911
|
+
*/
|
|
2912
|
+
orThrow(error?: Error): T$1;
|
|
2913
|
+
/**
|
|
2914
|
+
* Returns this Option if it contains a value, otherwise returns the alternative container
|
|
2915
|
+
* @param alternative - The alternative Option to return if this is None
|
|
2916
|
+
* @returns This Option or the alternative
|
|
2917
|
+
*/
|
|
2918
|
+
or(alternative: Option<T$1>): Option<T$1>;
|
|
2919
|
+
/**
|
|
2920
|
+
* Returns the contained value or null if None
|
|
2921
|
+
* @returns The contained value or null
|
|
2922
|
+
*/
|
|
2923
|
+
orNull(): T$1 | null;
|
|
2924
|
+
/**
|
|
2925
|
+
* Returns the contained value or undefined if None
|
|
2926
|
+
* @returns The contained value or undefined
|
|
2927
|
+
*/
|
|
2928
|
+
orUndefined(): T$1 | undefined;
|
|
2929
|
+
/**
|
|
2930
|
+
* Maps the value inside the Option using the provided function
|
|
2931
|
+
* @param f - The mapping function
|
|
2932
|
+
* @returns A new Option containing the mapped value, or None if this Option is None
|
|
2933
|
+
*/
|
|
2934
|
+
map<U extends Type>(f: (value: T$1) => U): Option<U>;
|
|
2935
|
+
/**
|
|
2936
|
+
* Applies a wrapped function to a wrapped value (Applicative pattern)
|
|
2937
|
+
* @param ff - An Option containing a function from T to U
|
|
2938
|
+
* @returns A new Option containing the result of applying the function
|
|
2939
|
+
*/
|
|
2940
|
+
ap<U extends Type>(ff: Option<(value: T$1) => U>): Option<U>;
|
|
2941
|
+
/**
|
|
2942
|
+
* Returns this Option if it contains a value that satisfies the predicate, otherwise returns None
|
|
2943
|
+
* @param predicate - The predicate function to test the value
|
|
2944
|
+
* @returns This Option or None
|
|
2945
|
+
*/
|
|
2946
|
+
filter(predicate: (value: T$1) => boolean): Option<T$1>;
|
|
2947
|
+
/**
|
|
2948
|
+
* Maps the value using a function that returns an Option
|
|
2949
|
+
* @param f - The mapping function returning an Option
|
|
2950
|
+
* @returns The result of applying f to the contained value, or None if this Option is None
|
|
2951
|
+
*/
|
|
2952
|
+
flatMap<U extends Type>(f: (value: T$1) => Option<U>): Option<U>;
|
|
2953
|
+
/**
|
|
2954
|
+
* Maps the value using an async function that returns an Option
|
|
2955
|
+
* @param f - The async mapping function returning an Option
|
|
2956
|
+
* @returns Promise of the result of applying f to the contained value, or None if this Option is None
|
|
2957
|
+
*/
|
|
2958
|
+
flatMapAsync<U extends Type>(f: (value: T$1) => Promise<Option<U>>): Promise<Option<U>>;
|
|
2959
|
+
/**
|
|
2960
|
+
* Applies a binary operator to a start value and the contained value
|
|
2961
|
+
* @param f - The binary operator
|
|
2962
|
+
* @returns The result of the reduction
|
|
2963
|
+
*/
|
|
2964
|
+
reduce<U>(f: (acc: U, value: T$1) => U): U;
|
|
2965
|
+
/**
|
|
2966
|
+
* Applies a binary operator to the contained value and a start value
|
|
2967
|
+
* @param f - The binary operator
|
|
2968
|
+
* @returns The result of the reduction
|
|
2969
|
+
*/
|
|
2970
|
+
reduceRight<U>(f: (acc: U, value: T$1) => U): U;
|
|
2971
|
+
/**
|
|
2972
|
+
* Pattern matches over the Option, applying onNone if None and onSome if Some
|
|
2973
|
+
* @param onNone - Function to apply if the Option is None
|
|
2974
|
+
* @param onSome - Function to apply if the Option has a value
|
|
2975
|
+
* @returns The result of applying the appropriate function
|
|
2976
|
+
*/
|
|
2977
|
+
fold<U>(onNone: () => U, onSome: (value: T$1) => U): U;
|
|
2978
|
+
/**
|
|
2979
|
+
* Left-associative fold using the provided zero value and operation
|
|
2980
|
+
* @param z - Zero/identity value
|
|
2981
|
+
* @returns A function that takes an operation to apply
|
|
2982
|
+
*/
|
|
2983
|
+
foldLeft<B>(z: B): (op: (b: B, a: T$1) => B) => B;
|
|
2984
|
+
/**
|
|
2985
|
+
* Right-associative fold using the provided zero value and operation
|
|
2986
|
+
* @param z - Zero/identity value
|
|
2987
|
+
* @returns A function that takes an operation to apply
|
|
2988
|
+
*/
|
|
2989
|
+
foldRight<B>(z: B): (op: (a: T$1, b: B) => B) => B;
|
|
2990
|
+
/**
|
|
2991
|
+
* Converts this Option to a List
|
|
2992
|
+
* @returns A List containing the value if Some, or empty List if None
|
|
2993
|
+
*/
|
|
2994
|
+
toList(): List<T$1>;
|
|
2995
|
+
/**
|
|
2996
|
+
* Checks if this Option contains the specified value
|
|
2997
|
+
* @param value - The value to check for
|
|
2998
|
+
* @returns true if this Option contains the value, false otherwise
|
|
2999
|
+
*/
|
|
3000
|
+
contains(value: T$1): boolean;
|
|
3001
|
+
/** The number of elements in this Option (0 or 1) */
|
|
3002
|
+
size: number;
|
|
3003
|
+
/**
|
|
3004
|
+
* Converts this Option to an Either
|
|
3005
|
+
* @param left - The value to use for Left if this Option is None
|
|
3006
|
+
* @returns Either.Right with the contained value if Some, or Either.Left with left if None
|
|
3007
|
+
*/
|
|
3008
|
+
toEither<E>(left: E): Either<E, T$1>;
|
|
3009
|
+
/**
|
|
3010
|
+
* Returns a string representation of this Option
|
|
3011
|
+
* @returns A string representation
|
|
3012
|
+
*/
|
|
3013
|
+
toString(): string;
|
|
3014
|
+
/**
|
|
3015
|
+
* Returns a simple object representation of this Option
|
|
3016
|
+
* @returns An object with _tag and value properties
|
|
3017
|
+
*/
|
|
3018
|
+
toValue(): {
|
|
3019
|
+
_tag: "Some" | "None";
|
|
3020
|
+
value: T$1;
|
|
3021
|
+
};
|
|
3022
|
+
/**
|
|
3023
|
+
* Pattern matches over the Option, applying a handler function based on the variant
|
|
3024
|
+
* @param patterns - Object with handler functions for Some and None variants
|
|
3025
|
+
* @returns The result of applying the matching handler function
|
|
3026
|
+
*/
|
|
3027
|
+
match<R$1>(patterns: {
|
|
3028
|
+
Some: (value: T$1) => R$1;
|
|
3029
|
+
None: () => R$1;
|
|
3030
|
+
}): R$1;
|
|
3031
|
+
}
|
|
3032
|
+
/**
|
|
3033
|
+
* Creates a Some variant of Option containing a value.
|
|
3034
|
+
* @param value - The value to wrap in Some
|
|
3035
|
+
* @returns A new Some instance containing the value
|
|
3036
|
+
* @typeParam T - The type of the value
|
|
3037
|
+
*/
|
|
3038
|
+
declare const Some: <T$1 extends Type>(value: T$1) => Option<T$1>;
|
|
3039
|
+
/**
|
|
3040
|
+
* Creates a None variant of Option representing absence of a value.
|
|
3041
|
+
* @returns A new None instance
|
|
3042
|
+
* @typeParam T - The type that would be contained if this was a Some
|
|
3043
|
+
*/
|
|
3044
|
+
declare const None: <T$1 extends Type>() => Option<T$1>;
|
|
3045
|
+
/**
|
|
3046
|
+
* Safely wraps a value that might be null or undefined in an Option.
|
|
3047
|
+
* Creates Some if the value is defined, None otherwise.
|
|
3048
|
+
* @param value - The value to wrap (might be null/undefined)
|
|
3049
|
+
* @returns Some(value) if value is defined, None otherwise
|
|
3050
|
+
* @typeParam T - The type of the value
|
|
3051
|
+
*/
|
|
3052
|
+
declare const OptionConstructor: <T$1 extends Type>(value: T$1 | null | undefined) => Option<T$1>;
|
|
3053
|
+
declare const Option: (<T$1 extends Type>(value: T$1 | null | undefined) => Option<T$1>) & {
|
|
3054
|
+
/**
|
|
3055
|
+
* Creates an Option from any value. Alias for Option function.
|
|
3056
|
+
* @param value - The value to wrap
|
|
3057
|
+
* @returns Some(value) if value is defined, None otherwise
|
|
3058
|
+
* @typeParam T - The type of the value
|
|
3059
|
+
*/
|
|
3060
|
+
from: <T$1>(value: T$1) => Option<T$1>;
|
|
3061
|
+
/**
|
|
3062
|
+
* Returns a None instance. Alias for None function.
|
|
3063
|
+
* @returns A None instance
|
|
3064
|
+
* @typeParam T - The type that would be contained if this was a Some
|
|
3065
|
+
*/
|
|
3066
|
+
none: <T$1>() => Option<T$1>;
|
|
3067
|
+
/**
|
|
3068
|
+
* Type guard to check if an Option is Some
|
|
3069
|
+
* @param option - The Option to check
|
|
3070
|
+
* @returns True if Option is Some
|
|
3071
|
+
*/
|
|
3072
|
+
isSome: <T$1>(option: Option<T$1>) => option is Option<T$1> & {
|
|
3073
|
+
value: T$1;
|
|
3074
|
+
isEmpty: false;
|
|
3075
|
+
};
|
|
3076
|
+
/**
|
|
3077
|
+
* Type guard to check if an Option is None
|
|
3078
|
+
* @param option - The Option to check
|
|
3079
|
+
* @returns True if Option is None
|
|
3080
|
+
*/
|
|
3081
|
+
isNone: <T$1>(option: Option<T$1>) => option is Option<T$1> & {
|
|
3082
|
+
value: undefined;
|
|
3083
|
+
isEmpty: true;
|
|
3084
|
+
};
|
|
3085
|
+
/**
|
|
3086
|
+
* Creates an Option from JSON string
|
|
3087
|
+
* @param json - The JSON string
|
|
3088
|
+
* @returns Option instance
|
|
3089
|
+
*/
|
|
3090
|
+
fromJSON: <T$1>(json: string) => Option<T$1>;
|
|
3091
|
+
/**
|
|
3092
|
+
* Creates an Option from YAML string
|
|
3093
|
+
* @param yaml - The YAML string
|
|
3094
|
+
* @returns Option instance
|
|
3095
|
+
*/
|
|
3096
|
+
fromYAML: <T$1>(yaml: string) => Option<T$1>;
|
|
3097
|
+
/**
|
|
3098
|
+
* Creates an Option from binary string
|
|
3099
|
+
* @param binary - The binary string
|
|
3100
|
+
* @returns Option instance
|
|
3101
|
+
*/
|
|
3102
|
+
fromBinary: <T$1>(binary: string) => Option<T$1>;
|
|
3103
|
+
};
|
|
3104
|
+
//#endregion
|
|
3105
|
+
//#region src/list/List.d.ts
|
|
3106
|
+
interface List<A> extends FunctypeCollection<A, "List">, Doable<A>, Reshapeable<A> {
|
|
3107
|
+
readonly length: number;
|
|
3108
|
+
readonly [Symbol.iterator]: () => Iterator<A>;
|
|
3109
|
+
map: <B>(f: (a: A) => B) => List<B>;
|
|
3110
|
+
ap: <B>(ff: List<(value: A) => B>) => List<B>;
|
|
3111
|
+
flatMap: <B>(f: (a: A) => Iterable<B>) => List<B>;
|
|
3112
|
+
flatMapAsync: <B>(f: (a: A) => PromiseLike<Iterable<B>>) => PromiseLike<List<B>>;
|
|
3113
|
+
filter<S extends A>(predicate: (a: A) => a is S): List<S>;
|
|
3114
|
+
filter(predicate: (a: A) => unknown): List<A>;
|
|
3115
|
+
filterNot: (p: (a: A) => boolean) => List<A>;
|
|
3116
|
+
/** @internal */
|
|
3117
|
+
filterType: <T$1 extends Typeable<string, unknown>>(tag: string) => List<T$1 & A>;
|
|
3118
|
+
remove: (value: A) => List<A>;
|
|
3119
|
+
removeAt: (index: number) => List<A>;
|
|
3120
|
+
add: (item: A) => List<A>;
|
|
3121
|
+
get: (index: number) => Option<A>;
|
|
3122
|
+
concat: (other: List<A>) => List<A>;
|
|
3123
|
+
/**
|
|
3124
|
+
* Pattern matches over the List, applying a handler function based on whether it's empty
|
|
3125
|
+
* @param patterns - Object with handler functions for Empty and NonEmpty variants
|
|
3126
|
+
* @returns The result of applying the matching handler function
|
|
3127
|
+
*/
|
|
3128
|
+
match<R$1>(patterns: {
|
|
3129
|
+
Empty: () => R$1;
|
|
3130
|
+
NonEmpty: (values: A[]) => R$1;
|
|
3131
|
+
}): R$1;
|
|
3132
|
+
}
|
|
3133
|
+
declare const List: (<A>(values?: Iterable<A>) => List<A>) & {
|
|
3134
|
+
/**
|
|
3135
|
+
* Creates a List from JSON string
|
|
3136
|
+
* @param json - The JSON string
|
|
3137
|
+
* @returns List instance
|
|
3138
|
+
*/
|
|
3139
|
+
fromJSON: <A>(json: string) => List<A>;
|
|
3140
|
+
/**
|
|
3141
|
+
* Creates a List from YAML string
|
|
3142
|
+
* @param yaml - The YAML string
|
|
3143
|
+
* @returns List instance
|
|
3144
|
+
*/
|
|
3145
|
+
fromYAML: <A>(yaml: string) => List<A>;
|
|
3146
|
+
/**
|
|
3147
|
+
* Creates a List from binary string
|
|
3148
|
+
* @param binary - The binary string
|
|
3149
|
+
* @returns List instance
|
|
3150
|
+
*/
|
|
3151
|
+
fromBinary: <A>(binary: string) => List<A>;
|
|
3152
|
+
};
|
|
3153
|
+
//#endregion
|
|
3154
|
+
//#region src/collections/index.d.ts
|
|
3155
|
+
/**
|
|
3156
|
+
* Represents a collection with conversion capabilities
|
|
3157
|
+
* @interface
|
|
3158
|
+
* @module Collections
|
|
3159
|
+
* @category Core
|
|
3160
|
+
*/
|
|
3161
|
+
interface Collection<A> {
|
|
3162
|
+
toList(): List<A>;
|
|
3163
|
+
toSet(): Set<A>;
|
|
3164
|
+
toString(): string;
|
|
3165
|
+
}
|
|
3166
|
+
//#endregion
|
|
3167
|
+
//#region src/functype/Functype.d.ts
|
|
3168
|
+
/**
|
|
3169
|
+
* Base interface for all functype data structures.
|
|
3170
|
+
* This provides a standard contract with core functional programming traits.
|
|
3171
|
+
*
|
|
3172
|
+
* @typeParam A - The type of value contained in the functor
|
|
3173
|
+
* @typeParam Tag - The type tag for pattern matching (e.g., "Some" | "None" for Option)
|
|
3174
|
+
*
|
|
3175
|
+
* @example
|
|
3176
|
+
* ```typescript
|
|
3177
|
+
* // Implementing FunctypeBase for a custom data structure
|
|
3178
|
+
* class MyContainer<T> implements FunctypeBase<T, "Empty" | "Full"> {
|
|
3179
|
+
* // Implementation of all required methods...
|
|
3180
|
+
* }
|
|
3181
|
+
* ```
|
|
3182
|
+
*/
|
|
3183
|
+
interface FunctypeBase<A, Tag extends string = string> extends AsyncMonad<A>, Traversable<A>, Serializable<A>, Foldable<A>, Typeable<Tag>, ContainerOps<A> {
|
|
3184
|
+
readonly _tag: Tag;
|
|
3185
|
+
}
|
|
3186
|
+
/**
|
|
3187
|
+
* Interface for single-value containers like Option, Either, Try.
|
|
3188
|
+
* Extends FunctypeBase with extraction methods and Pipe.
|
|
3189
|
+
*
|
|
3190
|
+
* @typeParam A - The type of value contained
|
|
3191
|
+
* @typeParam Tag - The type tag for pattern matching
|
|
3192
|
+
*/
|
|
3193
|
+
interface Functype<A, Tag extends string = string> extends FunctypeBase<A, Tag>, Extractable<A>, Pipe<A>, Matchable<A, Tag> {
|
|
3194
|
+
toValue(): {
|
|
3195
|
+
_tag: Tag;
|
|
3196
|
+
value: A;
|
|
3197
|
+
};
|
|
3198
|
+
}
|
|
3199
|
+
/**
|
|
3200
|
+
* A version of Functype for collection types that need iteration support.
|
|
3201
|
+
* Extends FunctypeBase with Iterable protocol but without Extractable.
|
|
3202
|
+
*
|
|
3203
|
+
* @typeParam A - The element type of the collection
|
|
3204
|
+
* @typeParam Tag - The type tag for pattern matching
|
|
3205
|
+
*/
|
|
3206
|
+
interface FunctypeCollection<A, Tag extends string = string> extends Omit<FunctypeBase<A, Tag>, "flatMapAsync" | "flatMap">, Iterable<A>, Pipe<A[]>, Collection<A>, CollectionOps<A, FunctypeCollection<A, Tag>> {
|
|
3207
|
+
toValue(): {
|
|
3208
|
+
_tag: Tag;
|
|
3209
|
+
value: A[];
|
|
3210
|
+
};
|
|
3211
|
+
flatMap<B extends Type>(f: (value: A) => Iterable<B>): FunctypeCollection<B, Tag>;
|
|
3212
|
+
flatMapAsync<B extends Type>(f: (value: A) => PromiseLike<Iterable<B>>): PromiseLike<FunctypeCollection<B, Tag>>;
|
|
3213
|
+
}
|
|
3214
|
+
//#endregion
|
|
3215
|
+
//#region src/either/Either.d.ts
|
|
3216
|
+
/**
|
|
3217
|
+
* Either type module
|
|
3218
|
+
* @module Either
|
|
3219
|
+
* @category Core
|
|
3220
|
+
*/
|
|
3221
|
+
interface Either<L extends Type, R$1 extends Type> extends FunctypeBase<R$1, "Left" | "Right">, Promisable<R$1>, Doable<R$1>, Reshapeable<R$1>, Extractable<R$1> {
|
|
3222
|
+
readonly _tag: "Left" | "Right";
|
|
3223
|
+
value: L | R$1;
|
|
3224
|
+
isLeft(): this is Either<L, R$1> & {
|
|
3225
|
+
readonly _tag: "Left";
|
|
3226
|
+
value: L;
|
|
3227
|
+
};
|
|
3228
|
+
isRight(): this is Either<L, R$1> & {
|
|
3229
|
+
readonly _tag: "Right";
|
|
3230
|
+
value: R$1;
|
|
3231
|
+
};
|
|
3232
|
+
orElse: (defaultValue: R$1) => R$1;
|
|
3233
|
+
orThrow: (error?: Error) => R$1;
|
|
3234
|
+
or(alternative: Either<L, R$1>): Either<L, R$1>;
|
|
3235
|
+
orNull: () => R$1 | null;
|
|
3236
|
+
orUndefined: () => R$1 | undefined;
|
|
3237
|
+
readonly map: <U extends Type>(f: (value: R$1) => U) => Either<L, U>;
|
|
3238
|
+
ap: <U extends Type>(ff: Either<L, (value: R$1) => U>) => Either<L, U>;
|
|
3239
|
+
merge: <L1 extends Type, R1 extends Type>(other: Either<L1, R1>) => Either<L | L1, [R$1, R1]>;
|
|
3240
|
+
mapAsync: <U extends Type>(f: (value: R$1) => Promise<U>) => Promise<Either<L, U>>;
|
|
3241
|
+
flatMap: <U extends Type>(f: (value: R$1) => Either<L, U>) => Either<L, U>;
|
|
3242
|
+
flatMapAsync: <U extends Type>(f: (value: R$1) => Promise<Either<L, U>>) => Promise<Either<L, U>>;
|
|
3243
|
+
toOption: () => Option<R$1>;
|
|
3244
|
+
toList: () => List<R$1>;
|
|
3245
|
+
toString: () => string;
|
|
3246
|
+
[Symbol.iterator]: () => Iterator<R$1>;
|
|
3247
|
+
yield: () => Generator<R$1, void, unknown>;
|
|
3248
|
+
traverse: <U extends Type>(f: (value: R$1) => Either<L, U>) => Either<L, U[]>;
|
|
3249
|
+
lazyMap: <U extends Type>(f: (value: R$1) => U) => Generator<Either<L, U>, void, unknown>;
|
|
3250
|
+
tap: (f: (value: R$1) => void) => Either<L, R$1>;
|
|
3251
|
+
tapLeft: (f: (value: L) => void) => Either<L, R$1>;
|
|
3252
|
+
mapLeft: <L2 extends Type>(f: (value: L) => L2) => Either<L2, R$1>;
|
|
3253
|
+
bimap: <L2 extends Type, R2 extends Type>(fl: (value: L) => L2, fr: (value: R$1) => R2) => Either<L2, R2>;
|
|
3254
|
+
fold: <T$1 extends Type>(onLeft: (value: L) => T$1, onRight: (value: R$1) => T$1) => T$1;
|
|
3255
|
+
swap: () => Either<R$1, L>;
|
|
3256
|
+
/**
|
|
3257
|
+
* Pipes the value through the provided function based on whether this is a Left or Right
|
|
3258
|
+
* @param onLeft - The function to apply if this is a Left
|
|
3259
|
+
* @param onRight - The function to apply if this is a Right
|
|
3260
|
+
* @returns The result of applying the appropriate function
|
|
3261
|
+
*/
|
|
3262
|
+
pipeEither<U extends Type>(onLeft: (value: L) => U, onRight: (value: R$1) => U): U;
|
|
3263
|
+
/**
|
|
3264
|
+
* Pipes the Either value through the provided function
|
|
3265
|
+
* @param f - The function to apply to the value (Left or Right)
|
|
3266
|
+
* @returns The result of applying the function to the value
|
|
3267
|
+
*/
|
|
3268
|
+
pipe<U extends Type>(f: (value: L | R$1) => U): U;
|
|
3269
|
+
/**
|
|
3270
|
+
* Pattern matches over the Either, applying a handler function based on the variant
|
|
3271
|
+
* @param patterns - Object with handler functions for Left and Right variants
|
|
3272
|
+
* @returns The result of applying the matching handler function
|
|
3273
|
+
*/
|
|
3274
|
+
match<T$1>(patterns: {
|
|
3275
|
+
Left: (value: L) => T$1;
|
|
3276
|
+
Right: (value: R$1) => T$1;
|
|
3277
|
+
}): T$1;
|
|
3278
|
+
/**
|
|
3279
|
+
* Returns the value and tag for inspection
|
|
3280
|
+
*/
|
|
3281
|
+
toValue(): {
|
|
3282
|
+
_tag: "Left" | "Right";
|
|
3283
|
+
value: L | R$1;
|
|
3284
|
+
};
|
|
3285
|
+
/**
|
|
3286
|
+
* Custom JSON serialization that excludes getter properties
|
|
3287
|
+
*/
|
|
3288
|
+
toJSON(): {
|
|
3289
|
+
_tag: "Left" | "Right";
|
|
3290
|
+
value: L | R$1;
|
|
3291
|
+
};
|
|
3292
|
+
}
|
|
3293
|
+
type TestEither<L extends Type, R$1 extends Type> = Either<L, R$1> & AsyncMonad<R$1>;
|
|
3294
|
+
declare const Right: <L extends Type, R$1 extends Type>(value: R$1) => Either<L, R$1>;
|
|
3295
|
+
declare const Left: <L extends Type, R$1 extends Type>(value: L) => Either<L, R$1>;
|
|
3296
|
+
declare const isRight: <L extends Type, R$1 extends Type>(either: Either<L, R$1>) => either is Either<L, R$1> & {
|
|
3297
|
+
value: R$1;
|
|
3298
|
+
};
|
|
3299
|
+
declare const isLeft: <L extends Type, R$1 extends Type>(either: Either<L, R$1>) => either is Either<L, R$1> & {
|
|
3300
|
+
value: L;
|
|
3301
|
+
};
|
|
3302
|
+
declare const tryCatch: <L extends Type, R$1 extends Type>(f: () => R$1, onError: (error: unknown) => L) => Either<L, R$1>;
|
|
3303
|
+
declare const TypeCheckRight: <L extends Type, R$1 extends Type>(value: R$1) => TestEither<L, R$1>;
|
|
3304
|
+
declare const TypeCheckLeft: <L extends Type, R$1 extends Type>(value: L) => TestEither<L, R$1>;
|
|
3305
|
+
declare const tryCatchAsync: <L extends Type, R$1 extends Type>(f: () => Promise<R$1>, onError: (error: unknown) => L) => Promise<Either<L, R$1>>;
|
|
3306
|
+
declare const Either: (<L extends Type, R$1 extends Type>(value: R$1 | L, isRight: boolean) => Either<L, R$1>) & {
|
|
3307
|
+
/**
|
|
3308
|
+
* Creates a Left instance
|
|
3309
|
+
* @param value - The left value
|
|
3310
|
+
* @returns Left Either
|
|
3311
|
+
*/
|
|
3312
|
+
left: <L extends Type, R$1 extends Type>(value: L) => Either<L, R$1>;
|
|
3313
|
+
/**
|
|
3314
|
+
* Creates a Right instance
|
|
3315
|
+
* @param value - The right value
|
|
3316
|
+
* @returns Right Either
|
|
3317
|
+
*/
|
|
3318
|
+
right: <L extends Type, R$1 extends Type>(value: R$1) => Either<L, R$1>;
|
|
3319
|
+
/**
|
|
3320
|
+
* Type guard to check if an Either is Right
|
|
3321
|
+
* @param either - The Either to check
|
|
3322
|
+
* @returns True if Either is Right
|
|
3323
|
+
*/
|
|
3324
|
+
isRight: <L extends Type, R$1 extends Type>(either: Either<L, R$1>) => either is Either<L, R$1> & {
|
|
3325
|
+
value: R$1;
|
|
3326
|
+
};
|
|
3327
|
+
/**
|
|
3328
|
+
* Type guard to check if an Either is Left
|
|
3329
|
+
* @param either - The Either to check
|
|
3330
|
+
* @returns True if Either is Left
|
|
3331
|
+
*/
|
|
3332
|
+
isLeft: <L extends Type, R$1 extends Type>(either: Either<L, R$1>) => either is Either<L, R$1> & {
|
|
3333
|
+
value: L;
|
|
3334
|
+
};
|
|
3335
|
+
/**
|
|
3336
|
+
* Combines an array of Eithers into a single Either containing an array
|
|
3337
|
+
* @param eithers - Array of Either values
|
|
3338
|
+
* @returns Either with array of values or first Left encountered
|
|
3339
|
+
*/
|
|
3340
|
+
sequence: <L extends Type, R$1 extends Type>(eithers: Either<L, R$1>[]) => Either<L, R$1[]>;
|
|
3341
|
+
/**
|
|
3342
|
+
* Maps an array through a function that returns Either, then sequences the results
|
|
3343
|
+
* @param arr - Array of values
|
|
3344
|
+
* @param f - Function that returns Either
|
|
3345
|
+
* @returns Either with array of results or first Left encountered
|
|
3346
|
+
*/
|
|
3347
|
+
traverse: <L extends Type, R$1 extends Type, U extends Type>(arr: R$1[], f: (value: R$1) => Either<L, U>) => Either<L, U[]>;
|
|
3348
|
+
/**
|
|
3349
|
+
* Creates an Either from a nullable value
|
|
3350
|
+
* @param value - The value that might be null or undefined
|
|
3351
|
+
* @param leftValue - The value to use for Left if value is null/undefined
|
|
3352
|
+
* @returns Right if value is not null/undefined, Left otherwise
|
|
3353
|
+
*/
|
|
3354
|
+
fromNullable: <L extends Type, R$1 extends Type>(value: R$1 | null | undefined, leftValue: L) => Either<L, R$1>;
|
|
3355
|
+
/**
|
|
3356
|
+
* Creates an Either based on a predicate
|
|
3357
|
+
* @param value - The value to test
|
|
3358
|
+
* @param predicate - The predicate function
|
|
3359
|
+
* @param leftValue - The value to use for Left if predicate fails
|
|
3360
|
+
* @returns Right if predicate passes, Left otherwise
|
|
3361
|
+
*/
|
|
3362
|
+
fromPredicate: <L extends Type, R$1 extends Type>(value: R$1, predicate: (value: R$1) => boolean, leftValue: L) => Either<L, R$1>;
|
|
3363
|
+
/**
|
|
3364
|
+
* Applicative apply - applies a wrapped function to a wrapped value
|
|
3365
|
+
* @param eitherF - Either containing a function
|
|
3366
|
+
* @param eitherV - Either containing a value
|
|
3367
|
+
* @returns Either with function applied to value
|
|
3368
|
+
*/
|
|
3369
|
+
ap: <L extends Type, R$1 extends Type, U extends Type>(eitherF: Either<L, (value: R$1) => U>, eitherV: Either<L, R$1>) => Either<L, U>;
|
|
3370
|
+
/**
|
|
3371
|
+
* Creates an Either from a Promise
|
|
3372
|
+
* @param promise - The Promise to convert
|
|
3373
|
+
* @param onRejected - Function to convert rejection reason to Left value
|
|
3374
|
+
* @returns Promise that resolves to Either
|
|
3375
|
+
*/
|
|
3376
|
+
fromPromise: <L, R$1>(promise: Promise<R$1>, onRejected: (reason: unknown) => L) => Promise<Either<L, R$1>>;
|
|
3377
|
+
/**
|
|
3378
|
+
* Creates an Either from JSON string
|
|
3379
|
+
* @param json - The JSON string
|
|
3380
|
+
* @returns Either instance
|
|
3381
|
+
*/
|
|
3382
|
+
fromJSON: <L extends Type, R$1 extends Type>(json: string) => Either<L, R$1>;
|
|
3383
|
+
/**
|
|
3384
|
+
* Creates an Either from YAML string
|
|
3385
|
+
* @param yaml - The YAML string
|
|
3386
|
+
* @returns Either instance
|
|
3387
|
+
*/
|
|
3388
|
+
fromYAML: <L extends Type, R$1 extends Type>(yaml: string) => Either<L, R$1>;
|
|
3389
|
+
/**
|
|
3390
|
+
* Creates an Either from binary string
|
|
3391
|
+
* @param binary - The binary string
|
|
3392
|
+
* @returns Either instance
|
|
3393
|
+
*/
|
|
3394
|
+
fromBinary: <L extends Type, R$1 extends Type>(binary: string) => Either<L, R$1>;
|
|
3395
|
+
};
|
|
3396
|
+
//#endregion
|
|
3397
|
+
//#region src/do/index.d.ts
|
|
3398
|
+
type OptionLike = {
|
|
3399
|
+
_tag: "Some" | "None";
|
|
3400
|
+
isSome(): boolean;
|
|
3401
|
+
get(): unknown;
|
|
3402
|
+
};
|
|
3403
|
+
type EitherLike = {
|
|
3404
|
+
_tag: "Left" | "Right";
|
|
3405
|
+
isLeft(): boolean;
|
|
3406
|
+
isRight(): boolean;
|
|
3407
|
+
value: unknown;
|
|
3408
|
+
};
|
|
3409
|
+
type ListLike = {
|
|
3410
|
+
_tag: "List";
|
|
3411
|
+
toArray(): unknown[];
|
|
3412
|
+
};
|
|
3413
|
+
type TryLike = {
|
|
3414
|
+
_tag: "Success" | "Failure";
|
|
3415
|
+
isSuccess(): boolean;
|
|
3416
|
+
get(): unknown;
|
|
3417
|
+
};
|
|
3418
|
+
/**
|
|
3419
|
+
* Executes a generator-based monadic comprehension
|
|
3420
|
+
* Returns the same monad type as the first yielded monad (Scala semantics)
|
|
3421
|
+
*
|
|
3422
|
+
* - Option comprehensions return Option (None on short-circuit)
|
|
3423
|
+
* - Either comprehensions return Either (Left with error on short-circuit)
|
|
3424
|
+
* - List comprehensions return List (empty or cartesian product)
|
|
3425
|
+
* - Try comprehensions return Try (Failure with error on short-circuit)
|
|
3426
|
+
*
|
|
3427
|
+
* Type Inference Notes:
|
|
3428
|
+
* - TypeScript infers the correct return type for homogeneous comprehensions
|
|
3429
|
+
* - For mixed monad types, TypeScript returns a union type
|
|
3430
|
+
* - Use DoTyped<T> or type assertions for mixed scenarios
|
|
3431
|
+
*
|
|
3432
|
+
* @example
|
|
3433
|
+
* ```typescript
|
|
3434
|
+
* // Option comprehension returns Option:
|
|
3435
|
+
* const result = Do(function* () {
|
|
3436
|
+
* const x = yield* $(Option(5));
|
|
3437
|
+
* const y = yield* $(Option(10));
|
|
3438
|
+
* return x + y;
|
|
3439
|
+
* });
|
|
3440
|
+
* // result: Option(15)
|
|
3441
|
+
*
|
|
3442
|
+
* // Either comprehension returns Either:
|
|
3443
|
+
* const result = Do(function* () {
|
|
3444
|
+
* const x = yield* $(Right(5));
|
|
3445
|
+
* const y = yield* $(Left("error"));
|
|
3446
|
+
* return x + y;
|
|
3447
|
+
* });
|
|
3448
|
+
* // result: Left("error") - error is preserved
|
|
3449
|
+
*
|
|
3450
|
+
* // List comprehension returns List with cartesian product:
|
|
3451
|
+
* const result = Do(function* () {
|
|
3452
|
+
* const x = yield* $(List([1, 2]));
|
|
3453
|
+
* const y = yield* $(List([3, 4]));
|
|
3454
|
+
* return x + y;
|
|
3455
|
+
* });
|
|
3456
|
+
* // result: List([4, 5, 5, 6])
|
|
3457
|
+
*
|
|
3458
|
+
* // Mixed types - use type assertion or DoTyped:
|
|
3459
|
+
* const result = Do(function* () {
|
|
3460
|
+
* const x = yield* $(Option(5));
|
|
3461
|
+
* const y = yield* $(Right<string, number>(10));
|
|
3462
|
+
* return x + y;
|
|
3463
|
+
* }) as Option<number>;
|
|
3464
|
+
* // result: Option(15)
|
|
3465
|
+
* ```
|
|
3466
|
+
*
|
|
3467
|
+
* @param gen - Generator function that yields monads and returns a result
|
|
3468
|
+
* @returns The same monad type as the first yield
|
|
3469
|
+
*/
|
|
3470
|
+
declare function Do<T$1>(gen: () => Generator<OptionLike, T$1, unknown>): Option<T$1>;
|
|
3471
|
+
declare function Do<L, R$1>(gen: () => Generator<EitherLike, R$1, unknown>): Either<L, R$1>;
|
|
3472
|
+
declare function Do<T$1>(gen: () => Generator<ListLike, T$1, unknown>): List<T$1>;
|
|
3473
|
+
declare function Do<T$1>(gen: () => Generator<TryLike, T$1, unknown>): Try<T$1>;
|
|
3474
|
+
declare function Do<T$1>(gen: () => Generator<OptionLike | EitherLike | ListLike | TryLike, T$1, unknown>): Reshapeable<T$1>;
|
|
3475
|
+
declare function Do<T$1>(gen: () => Generator<unknown, T$1, unknown>): unknown;
|
|
3476
|
+
/**
|
|
3477
|
+
* Executes an async generator-based monadic comprehension
|
|
3478
|
+
* Returns the same monad type as the first yielded monad
|
|
3479
|
+
*
|
|
3480
|
+
* @example
|
|
3481
|
+
* ```typescript
|
|
3482
|
+
* const result = await DoAsync(async function* () {
|
|
3483
|
+
* const user = yield* $(await fetchUser(id)); // Promise<Option<User>> → User
|
|
3484
|
+
* const profile = yield* $(await getProfile(user)); // Promise<Either<Error, Profile>> → Profile
|
|
3485
|
+
* return { user, profile };
|
|
3486
|
+
* });
|
|
3487
|
+
* // result type matches first yield
|
|
3488
|
+
* ```
|
|
3489
|
+
*
|
|
3490
|
+
* @param gen - Async generator function that yields monads/promises and returns a result
|
|
3491
|
+
* @returns Promise of the same monad type as first yield
|
|
3492
|
+
*/
|
|
3493
|
+
declare function DoAsync<T$1>(gen: () => AsyncGenerator<OptionLike, T$1, unknown>): Promise<Option<T$1>>;
|
|
3494
|
+
declare function DoAsync<L, R$1>(gen: () => AsyncGenerator<EitherLike, R$1, unknown>): Promise<Either<L, R$1>>;
|
|
3495
|
+
declare function DoAsync<T$1>(gen: () => AsyncGenerator<ListLike, T$1, unknown>): Promise<List<T$1>>;
|
|
3496
|
+
declare function DoAsync<T$1>(gen: () => AsyncGenerator<TryLike, T$1, unknown>): Promise<Try<T$1>>;
|
|
3497
|
+
declare function DoAsync<T$1>(gen: () => AsyncGenerator<OptionLike | EitherLike | ListLike | TryLike, T$1, unknown>): Promise<Reshapeable<T$1>>;
|
|
3498
|
+
declare function DoAsync<T$1>(gen: () => AsyncGenerator<unknown, T$1, unknown>): Promise<unknown>;
|
|
3499
|
+
/**
|
|
3500
|
+
* Helper function to check if a value implements the Doable interface
|
|
3501
|
+
* @param value - Value to check
|
|
3502
|
+
* @returns True if the value implements Doable
|
|
3503
|
+
*/
|
|
3504
|
+
declare function isDoCapable<T$1>(value: unknown): value is Doable<T$1>;
|
|
3505
|
+
/**
|
|
3506
|
+
* Manually unwrap a monad using the Doable interface
|
|
3507
|
+
* Useful for testing or when you need to unwrap outside of a Do-comprehension
|
|
3508
|
+
*
|
|
3509
|
+
* @param monad - Monad to unwrap
|
|
3510
|
+
* @returns The unwrapped value
|
|
3511
|
+
* @throws Error if the monad cannot be unwrapped
|
|
3512
|
+
*/
|
|
3513
|
+
declare function unwrap<T$1>(monad: Doable<T$1>): T$1;
|
|
3514
|
+
/**
|
|
3515
|
+
* Type helper for Do-notation generators.
|
|
3516
|
+
* Provides better type hints in IDEs.
|
|
3517
|
+
*
|
|
3518
|
+
* @example
|
|
3519
|
+
* ```typescript
|
|
3520
|
+
* const result = Do(function* (): DoGenerator<number> {
|
|
3521
|
+
* const x = yield* $(List([1, 2])) // x is still unknown but return type is clear
|
|
3522
|
+
* const y = yield* $(List([3, 4]))
|
|
3523
|
+
* return x + y
|
|
3524
|
+
* })
|
|
3525
|
+
* ```
|
|
3526
|
+
*/
|
|
3527
|
+
type DoGenerator<T$1, TYield = unknown> = Generator<TYield, T$1, unknown>;
|
|
3528
|
+
/**
|
|
3529
|
+
* Extracts values from monads in Do-notation with type inference.
|
|
3530
|
+
* The '$' symbol is the universal extraction operator in functional programming.
|
|
3531
|
+
*
|
|
3532
|
+
* @example
|
|
3533
|
+
* ```typescript
|
|
3534
|
+
* const result = Do(function* () {
|
|
3535
|
+
* const x = yield* $(Option(5)) // x: number
|
|
3536
|
+
* const y = yield* $(List([1, 2, 3])) // y: number (for cartesian product)
|
|
3537
|
+
* const name = yield* $(Right("Alice")) // name: string
|
|
3538
|
+
* return `${name}: ${x + y}`
|
|
3539
|
+
* })
|
|
3540
|
+
* ```
|
|
3541
|
+
*
|
|
3542
|
+
* @param monad - Any monad that can be unwrapped (Option, Either, List, Try, etc.)
|
|
3543
|
+
* @returns A generator that yields the monad and returns its extracted value
|
|
3544
|
+
*/
|
|
3545
|
+
declare function $<T$1>(monad: Option<T$1>): Generator<Option<T$1>, T$1, T$1>;
|
|
3546
|
+
declare function $<L, R$1>(monad: Either<L, R$1>): Generator<Either<L, R$1>, R$1, R$1>;
|
|
3547
|
+
declare function $<T$1>(monad: List<T$1>): Generator<List<T$1>, T$1, T$1>;
|
|
3548
|
+
declare function $<T$1>(monad: Try<T$1>): Generator<Try<T$1>, T$1, T$1>;
|
|
3549
|
+
declare function $<T$1>(monad: Doable<T$1>): Generator<Doable<T$1>, T$1, T$1>;
|
|
3550
|
+
declare function $<M>(monad: M): Generator<M, InferYieldType<M>, InferYieldType<M>>;
|
|
3551
|
+
type InferYieldType<M> = M extends {
|
|
3552
|
+
isSome(): boolean;
|
|
3553
|
+
get(): infer T;
|
|
3554
|
+
} ? T : M extends {
|
|
3555
|
+
isRight(): boolean;
|
|
3556
|
+
value: infer R;
|
|
3557
|
+
} ? R : M extends {
|
|
3558
|
+
toArray(): (infer T)[];
|
|
3559
|
+
} ? T : M extends {
|
|
3560
|
+
isSuccess(): boolean;
|
|
3561
|
+
get(): infer T;
|
|
3562
|
+
} ? T : M extends Doable<infer T> ? T : unknown;
|
|
3563
|
+
declare const NoneError: (message?: string) => Error;
|
|
3564
|
+
interface LeftErrorType<L> extends Error {
|
|
3565
|
+
value: L;
|
|
3566
|
+
}
|
|
3567
|
+
declare const LeftError: <L>(value: L, message?: string) => LeftErrorType<L>;
|
|
3568
|
+
declare const EmptyListError: (message?: string) => Error;
|
|
3569
|
+
interface FailureErrorType extends Error {
|
|
3570
|
+
cause: Error;
|
|
3571
|
+
}
|
|
3572
|
+
declare const FailureError: (cause: Error, message?: string) => FailureErrorType;
|
|
3573
|
+
//#endregion
|
|
3574
|
+
export { EitherKind as $, isCompanion as $t, OptionConstructor as A, Ok as At, fromBinary as B, createCancellationTokenSource as Bt, Functype as C, ContainerOps as Cn, formatError as Ct, List as D, DoResult as Dn, CancellationToken as Dt, Collection as E, isExtractable as En, Async as Et, Set as F, TaskMetadata as Ft, MatchableUtils as G, NAME as Gt, fromYAML as H, ErrorContext as Ht, SerializationResult as I, TaskOutcome as It, Map$1 as J, Base as Jt, ESMap as K, Throwable as Kt, createCustomSerializer as L, TaskParams as Lt, Stack as M, TaggedThrowable as Mt, Valuable as N, Task as Nt, None as O, Doable as On, CancellationTokenSource as Ot, ValuableParams as P, TaskFailure as Pt, Identity as Q, InstanceType as Qt, createSerializationCompanion as R, TaskResult as Rt, tryCatchAsync as S, CollectionOps as Sn, createErrorSerializer as St, FunctypeCollection as T, Extractable as Tn, safeStringify as Tt, Ref as U, FPromise as Ut, fromJSON as V, isTaggedThrowable as Vt, Matchable as W, FPromiseCompanion as Wt, Traversable as X, Cond as Xt, SafeTraversable as Y, Match as Yt, Lazy as Z, CompanionMethods as Zt, TypeCheckLeft as _, Promisable as _n, ParseError as _t, EmptyListError as a, IntegerNumber as an, UniversalContainer as at, isRight as b, Functor as bn, ErrorWithTaskInfo as bt, LeftError as c, PatternString as cn, FormValidation as ct, isDoCapable as d, UUID as dn, Validator as dt, Companion as en, HKT as et, unwrap as f, UrlString as fn, ErrorCode as ft, TestEither as g, TypeNames as gn, TypedErrorContext as gt, Right as h, Try as hn, TypedError as ht, DoGenerator as i, ISO8601Date as in, TryKind as it, Some as j, Sync as jt, Option as k, Err as kt, LeftErrorType as l, PositiveInteger as ln, Validation as lt, Left as m, ValidatedBrandCompanion as mn, ErrorStatus as mt, Do as n, BoundedString as nn, ListKind as nt, FailureError as o, NonEmptyString as on, FoldableUtils as ot, Either as p, ValidatedBrand as pn, ErrorMessage as pt, ESMapType as q, ThrowableType as qt, DoAsync as r, EmailAddress as rn, OptionKind as rt, FailureErrorType as s, NonNegativeNumber as sn, FieldValidation as st, $ as t, BoundedNumber as tn, Kind as tt, NoneError as u, PositiveNumber as un, ValidationRule as ut, TypeCheckRight as v, Applicative as vn, ErrorChainElement as vt, FunctypeBase as w, LazyList as wn, formatStackTrace as wt, tryCatch as x, Monad as xn, TaskErrorInfo as xt, isLeft as y, AsyncMonad as yn, ErrorFormatterOptions as yt, createSerializer as z, TaskSuccess as zt };
|
|
3575
|
+
//# sourceMappingURL=index-Bnjlo4cT.d.ts.map
|