functype 0.8.61 → 0.8.62

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.
Files changed (81) hide show
  1. package/dist/Map-BNxKlujn.mjs +46 -0
  2. package/dist/Map-BNxKlujn.mjs.map +1 -0
  3. package/dist/branded/Brand.d.ts +46 -0
  4. package/dist/branded/index.d.ts +1 -48
  5. package/dist/branded/index.mjs +23 -2
  6. package/dist/branded/index.mjs.map +1 -1
  7. package/dist/collections/index.d.ts +8 -0
  8. package/dist/companion/Companion.d.ts +23 -0
  9. package/dist/companion/index.d.ts +1 -0
  10. package/dist/core/base/Base.d.ts +10 -0
  11. package/dist/core/base/index.d.ts +1 -0
  12. package/dist/core/index.d.ts +4 -0
  13. package/dist/core/info/Info.d.ts +26 -0
  14. package/dist/core/info/index.d.ts +1 -0
  15. package/dist/core/task/Task.d.ts +57 -0
  16. package/dist/core/task/index.d.ts +1 -0
  17. package/dist/core/throwable/Throwable.d.ts +18 -0
  18. package/dist/core/throwable/index.d.ts +1 -0
  19. package/dist/either/Either.d.ts +57 -0
  20. package/dist/either/index.d.ts +1 -3
  21. package/dist/either/index.mjs +13 -2
  22. package/dist/either/index.mjs.map +1 -1
  23. package/dist/error/ParseError.d.ts +6 -0
  24. package/dist/error/index.d.ts +1 -0
  25. package/dist/fpromise/FPromise.d.ts +369 -0
  26. package/dist/fpromise/index.d.ts +2 -3
  27. package/dist/fpromise/index.mjs +6 -2
  28. package/dist/fpromise/index.mjs.map +1 -1
  29. package/dist/fpromise/retry.d.ts +106 -0
  30. package/dist/functor/index.d.ts +18 -0
  31. package/dist/hkt/index.d.ts +49 -0
  32. package/dist/identity/Identity.d.ts +5 -0
  33. package/dist/identity/index.d.ts +1 -0
  34. package/dist/index-CgMYHgdG.mjs +1286 -0
  35. package/dist/index-CgMYHgdG.mjs.map +1 -0
  36. package/dist/index.d.ts +28 -3
  37. package/dist/index.mjs +174 -2
  38. package/dist/index.mjs.map +1 -1
  39. package/dist/iterable/index.d.ts +25 -0
  40. package/dist/list/List.d.ts +45 -0
  41. package/dist/list/index.d.ts +1 -3
  42. package/dist/list/index.mjs +5 -2
  43. package/dist/list/index.mjs.map +1 -1
  44. package/dist/map/Map.d.ts +19 -0
  45. package/dist/map/index.d.ts +2 -3
  46. package/dist/map/index.mjs +7 -2
  47. package/dist/map/index.mjs.map +1 -1
  48. package/dist/map/shim.d.ts +2 -0
  49. package/dist/option/Option.d.ts +164 -0
  50. package/dist/option/index.d.ts +1 -870
  51. package/dist/option/index.mjs +7 -2
  52. package/dist/option/index.mjs.map +1 -1
  53. package/dist/serializable/Serializable.d.ts +15 -0
  54. package/dist/serializable/index.d.ts +1 -0
  55. package/dist/set/Set.d.ts +16 -0
  56. package/dist/set/index.d.ts +2 -3
  57. package/dist/set/index.mjs +5 -2
  58. package/dist/set/index.mjs.map +1 -1
  59. package/dist/set/shim.d.ts +2 -0
  60. package/dist/try/Try.d.ts +20 -0
  61. package/dist/try/index.d.ts +1 -3
  62. package/dist/try/index.mjs +44 -2
  63. package/dist/try/index.mjs.map +1 -1
  64. package/dist/tuple/Tuple.d.ts +11 -0
  65. package/dist/tuple/index.d.ts +1 -1
  66. package/dist/tuple/index.mjs +27 -2
  67. package/dist/tuple/index.mjs.map +1 -1
  68. package/dist/typeable/Typeable.d.ts +10 -0
  69. package/dist/typeable/index.d.ts +1 -0
  70. package/dist/util/index.d.ts +3 -0
  71. package/dist/util/isIterable.d.ts +1 -0
  72. package/dist/valuable/Valuable.d.ts +13 -0
  73. package/dist/valuable/index.d.ts +1 -0
  74. package/package.json +7 -6
  75. package/dist/Tuple-GXgoHfiN.d.ts +0 -54
  76. package/dist/chunk-A3EHNBZM.mjs +0 -2
  77. package/dist/chunk-A3EHNBZM.mjs.map +0 -1
  78. package/dist/chunk-OQWAJKZJ.mjs +0 -2
  79. package/dist/chunk-OQWAJKZJ.mjs.map +0 -1
  80. package/dist/chunk-TQJDL6YW.mjs +0 -2
  81. package/dist/chunk-TQJDL6YW.mjs.map +0 -1
@@ -1,870 +1 @@
1
- import { T as Typeable, a as Type, F as Functor, V as Valuable, A as AsyncFunctor, E as ExtractTag, b as Tuple } from '../Tuple-GXgoHfiN.js';
2
- import '../branded/index.js';
3
-
4
- /**
5
- * Creates a function-object hybrid similar to Scala's companion objects.
6
- * This utility allows creating TypeScript function objects with attached methods,
7
- * mimicking Scala's class + companion object pattern without using classes.
8
- *
9
- * @param object The main function that will be invoked when the object is called
10
- * @param companion Additional static methods to attach to the function
11
- * @returns A function with the attached methods
12
- *
13
- * @example
14
- * const greet = (name: string) => `Hello, ${name}!`;
15
- * const methods = {
16
- * formal: (name: string) => `Good day, ${name}.`,
17
- * casual: (name: string) => `Hey ${name}!`
18
- * };
19
- * const Greeter = createCompanionObject(greet, methods);
20
- *
21
- * // Usage:
22
- * Greeter("World"); // Hello, World!
23
- * Greeter.formal("Sir"); // Good day, Sir.
24
- * Greeter.casual("Friend"); // Hey Friend!
25
- */
26
- declare function Companion<ObjectF extends object, CompanionF extends object>(object: ObjectF, companion: CompanionF): ObjectF & CompanionF;
27
-
28
- /**
29
- * Base Object from which most other objects inherit
30
- * @param type
31
- * @param body
32
- * @constructor
33
- */
34
- declare function Base<T>(type: string, body: T): T & {
35
- toString(): string;
36
- _tag: string;
37
- };
38
-
39
- declare const NAME: "Throwable";
40
- type ThrowableType = Error & Typeable<typeof NAME> & {
41
- readonly data?: unknown;
42
- readonly cause?: Error;
43
- };
44
- declare class Throwable extends Error implements ThrowableType {
45
- readonly _tag: typeof NAME;
46
- readonly data?: unknown;
47
- readonly cause?: Error;
48
- protected constructor(message: string, options?: {
49
- data?: unknown | undefined;
50
- cause?: Error | undefined;
51
- stack?: string | undefined;
52
- });
53
- static apply(srcError: unknown, data?: unknown): ThrowableType;
54
- }
55
-
56
- /**
57
- * Creates a Some variant of Option containing a value.
58
- * @param value - The value to wrap in Some
59
- * @returns A new Some instance containing the value
60
- * @typeParam T - The type of the value
61
- */
62
- declare const Some: <T extends Type>(value: T) => Option<T>;
63
- /**
64
- * Creates a None variant of Option representing absence of a value.
65
- * @returns A new None instance
66
- * @typeParam T - The type that would be contained if this was a Some
67
- */
68
- declare const None: <T extends Type>() => Option<T>;
69
- /**
70
- * Option type module
71
- * @module Option
72
- * @category Core
73
- */
74
- /**
75
- * The Option type represents a value that may or may not exist.
76
- * It's used to handle potentially null or undefined values in a type-safe way.
77
- * @typeParam T - The type of the value contained in the Option
78
- */
79
- type Option<T extends Type> = {
80
- /** Tag identifying if this is a Some or None variant */
81
- readonly _tag: "Some" | "None";
82
- /** The contained value (undefined for None) */
83
- readonly value: T | undefined;
84
- /** Whether this Option contains no value */
85
- isEmpty: boolean;
86
- /**
87
- * Extracts the value if present
88
- * @throws Error if the Option is None
89
- * @returns The contained value
90
- */
91
- get(): T;
92
- /**
93
- * Returns the contained value or a default value if None
94
- * @param defaultValue - The value to return if this Option is None
95
- * @returns The contained value or defaultValue
96
- */
97
- getOrElse(defaultValue: T): T;
98
- /**
99
- * Returns the contained value or throws a specified error if None
100
- * @param error - The error to throw if this Option is None
101
- * @returns The contained value
102
- * @throws The specified error if the Option is None
103
- */
104
- getOrThrow(error: Error): T;
105
- /**
106
- * Returns this Option if it contains a value, otherwise returns the alternative
107
- * @param alternative - The alternative Option to return if this is None
108
- * @returns This Option or the alternative
109
- */
110
- orElse(alternative: Option<T>): Option<T>;
111
- /**
112
- * Returns the contained value or null if None
113
- * @returns The contained value or null
114
- */
115
- orNull(): T | null;
116
- /**
117
- * Maps the value inside the Option using the provided function
118
- * @param f - The mapping function
119
- * @returns A new Option containing the mapped value, or None if this Option is None
120
- */
121
- map<U extends Type>(f: (value: T) => U): Option<U>;
122
- /**
123
- * Returns this Option if it contains a value that satisfies the predicate, otherwise returns None
124
- * @param predicate - The predicate function to test the value
125
- * @returns This Option or None
126
- */
127
- filter(predicate: (value: T) => boolean): Option<T>;
128
- /**
129
- * Maps the value using a function that returns an Option
130
- * @param f - The mapping function returning an Option
131
- * @returns The result of applying f to the contained value, or None if this Option is None
132
- */
133
- flatMap<U extends Type>(f: (value: T) => Option<U>): Option<U>;
134
- /**
135
- * Maps the value using an async function that returns an Option
136
- * @param f - The async mapping function returning an Option
137
- * @returns Promise of the result of applying f to the contained value, or None if this Option is None
138
- */
139
- flatMapAsync<U extends Type>(f: (value: T) => Promise<Option<U>>): Promise<Option<U>>;
140
- /**
141
- * Applies a binary operator to a start value and the contained value
142
- * @param f - The binary operator
143
- * @returns The result of the reduction
144
- */
145
- reduce<U>(f: (acc: U, value: T) => U): U;
146
- /**
147
- * Applies a binary operator to the contained value and a start value
148
- * @param f - The binary operator
149
- * @returns The result of the reduction
150
- */
151
- reduceRight<U>(f: (acc: U, value: T) => U): U;
152
- /**
153
- * Pattern matches over the Option, applying onNone if None and onSome if Some
154
- * @param onNone - Function to apply if the Option is None
155
- * @param onSome - Function to apply if the Option has a value
156
- * @returns The result of applying the appropriate function
157
- */
158
- fold<U>(onNone: () => U, onSome: (value: T) => U): U;
159
- /**
160
- * Left-associative fold using the provided zero value and operation
161
- * @param z - Zero/identity value
162
- * @returns A function that takes an operation to apply
163
- */
164
- foldLeft<B>(z: B): (op: (b: B, a: T) => B) => B;
165
- /**
166
- * Right-associative fold using the provided zero value and operation
167
- * @param z - Zero/identity value
168
- * @returns A function that takes an operation to apply
169
- */
170
- foldRight<B>(z: B): (op: (a: T, b: B) => B) => B;
171
- /**
172
- * Converts this Option to a List
173
- * @returns A List containing the value if Some, or empty List if None
174
- */
175
- toList(): List<T>;
176
- /**
177
- * Checks if this Option contains the specified value
178
- * @param value - The value to check for
179
- * @returns true if this Option contains the value, false otherwise
180
- */
181
- contains(value: T): boolean;
182
- /** The number of elements in this Option (0 or 1) */
183
- size: number;
184
- /**
185
- * Converts this Option to an Either
186
- * @param left - The value to use for Left if this Option is None
187
- * @returns Either.Right with the contained value if Some, or Either.Left with left if None
188
- */
189
- toEither<E>(left: E): Either<E, T>;
190
- /**
191
- * Returns a string representation of this Option
192
- * @returns A string representation
193
- */
194
- toString(): string;
195
- /**
196
- * Returns a simple object representation of this Option
197
- * @returns An object with _tag and value properties
198
- */
199
- toValue(): {
200
- _tag: "Some" | "None";
201
- value: T;
202
- };
203
- } & (Traversable<T> & Functor<T> & Typeable<"Some" | "None"> & Valuable<"Some" | "None", T> & AsyncFunctor<T>);
204
- /**
205
- * Safely wraps a value that might be null or undefined in an Option.
206
- * Creates Some if the value is defined, None otherwise.
207
- * @param value - The value to wrap (might be null/undefined)
208
- * @returns Some(value) if value is defined, None otherwise
209
- * @typeParam T - The type of the value
210
- */
211
- declare const Option: {
212
- <T extends Type>(value: T | null | undefined): Option<T>;
213
- from<T>(value: T): Option<T>;
214
- none<T>(): Option<T>;
215
- };
216
-
217
- type IterableType<A extends Type> = {
218
- count(p: (x: A) => boolean): number;
219
- find(p: (a: A) => boolean): Option<A>;
220
- forEach(f: (a: A) => void): void;
221
- drop(n: number): IterableType<A>;
222
- dropRight(n: number): IterableType<A>;
223
- dropWhile(p: (a: A) => boolean): IterableType<A>;
224
- exists(p: (a: A) => boolean): boolean;
225
- filter(p: (a: A) => boolean): IterableType<A>;
226
- filterNot(p: (a: A) => boolean): IterableType<A>;
227
- flatten<B>(): IterableType<B>;
228
- reduce(f: (b: A, a: A) => A): A;
229
- reduceRight(f: (b: A, a: A) => A): A;
230
- foldLeft<B>(z: B): (op: (b: B, a: A) => B) => B;
231
- foldRight<B>(z: B): (op: (a: A, b: B) => B) => B;
232
- get head(): A | undefined;
233
- get headOption(): Option<A>;
234
- get isEmpty(): boolean;
235
- map<B extends Type>(f: (a: A) => B): IterableType<B>;
236
- flatMap<B extends Type>(f: (a: A) => IterableType<B>): IterableType<B>;
237
- get size(): number;
238
- toArray<B = A>(): readonly B[];
239
- } & Iterable<A> & Functor<A> & AsyncFunctor<A>;
240
-
241
- type Converters<A> = {
242
- toList(): List<A>;
243
- toSet(): Set<A>;
244
- toString(): string;
245
- };
246
- type Collection<A> = Converters<A>;
247
-
248
- type Set<A> = {
249
- add: (value: A) => Set<A>;
250
- remove: (value: A) => Set<A>;
251
- contains: (value: A) => boolean;
252
- has: (value: A) => boolean;
253
- map: <B>(f: (a: A) => B) => Set<B>;
254
- flatMap: <B>(f: (a: A) => IterableType<B>) => Set<B>;
255
- toList: () => List<A>;
256
- toSet: () => Set<A>;
257
- toString: () => string;
258
- } & IterableType<A> & Collection<A> & Typeable<"Set">;
259
- declare const Set: <A>(iterable?: Iterable<A> | IterableType<A>) => Set<A>;
260
-
261
- type List<A> = {
262
- readonly length: number;
263
- readonly [Symbol.iterator]: () => Iterator<A>;
264
- map: <B>(f: (a: A) => B) => List<B>;
265
- flatMap: <B>(f: (a: A) => IterableType<B>) => List<B>;
266
- flatMapAsync: <B>(f: (a: A) => PromiseLike<IterableType<B>>) => PromiseLike<List<B>>;
267
- forEach: (f: (a: A) => void) => void;
268
- count: (p: (x: A) => boolean) => number;
269
- exists: (p: (a: A) => boolean) => boolean;
270
- filter<S extends A>(predicate: (a: A) => a is S): List<S>;
271
- filter(predicate: (a: A) => unknown): List<A>;
272
- filterNot: (p: (a: A) => boolean) => List<A>;
273
- filterType: <T extends Typeable<string, unknown>>(tag: string) => List<T & A>;
274
- find: <T extends A = A>(predicate: (a: A) => boolean, tag?: ExtractTag<T>) => Option<T>;
275
- readonly head: A | undefined;
276
- readonly headOption: Option<A>;
277
- readonly isEmpty: boolean;
278
- toArray: <B = A>() => B[];
279
- reduce: (f: (prev: A, curr: A) => A) => A;
280
- reduceRight: (f: (prev: A, curr: A) => A) => A;
281
- foldLeft: <B>(z: B) => (op: (b: B, a: A) => B) => B;
282
- foldRight: <B>(z: B) => (op: (a: A, b: B) => B) => B;
283
- remove: (value: A) => List<A>;
284
- removeAt: (index: number) => List<A>;
285
- add: (item: A) => List<A>;
286
- get: (index: number) => Option<A>;
287
- concat: (other: List<A>) => List<A>;
288
- toList: () => List<A>;
289
- toSet: () => Set<A>;
290
- toString: () => string;
291
- toValue: () => {
292
- _tag: "List";
293
- value: A[];
294
- };
295
- drop: (n: number) => List<A>;
296
- dropRight: (n: number) => List<A>;
297
- dropWhile: (p: (a: A) => boolean) => List<A>;
298
- flatten: <B>() => List<B>;
299
- } & IterableType<A> & AsyncFunctor<A> & Typeable<"List">;
300
- declare const List: <A>(values?: Iterable<A>) => List<A>;
301
-
302
- type TestEither<L extends Type, R extends Type> = Either<L, R> & Functor<R> & AsyncFunctor<R>;
303
- declare const Right: <L extends Type, R extends Type>(value: R) => Either<L, R>;
304
- declare const Left: <L extends Type, R extends Type>(value: L) => Either<L, R>;
305
- declare const isRight: <L extends Type, R extends Type>(either: Either<L, R>) => either is Either<L, R> & {
306
- value: R;
307
- };
308
- declare const isLeft: <L extends Type, R extends Type>(either: Either<L, R>) => either is Either<L, R> & {
309
- value: L;
310
- };
311
- declare const tryCatch: <L extends Type, R extends Type>(f: () => R, onError: (error: unknown) => L) => Either<L, R>;
312
- declare const TypeCheckRight: <L extends Type, R extends Type>(value: R) => TestEither<L, R>;
313
- declare const TypeCheckLeft: <L extends Type, R extends Type>(value: L) => TestEither<L, R>;
314
- declare const tryCatchAsync: <L extends Type, R extends Type>(f: () => Promise<R>, onError: (error: unknown) => L) => Promise<Either<L, R>>;
315
- /**
316
- * Either type module
317
- * @module Either
318
- * @category Core
319
- */
320
- type Either<L extends Type, R extends Type> = {
321
- readonly _tag: "Left" | "Right";
322
- value: L | R;
323
- isLeft: () => boolean;
324
- isRight: () => boolean;
325
- getOrElse: (defaultValue: R) => R;
326
- getOrThrow: () => R;
327
- map: <U extends Type>(f: (value: R) => U) => Either<L, U>;
328
- merge: <L1 extends Type, R1 extends Type>(other: Either<L1, R1>) => Either<L | L1, [R, R1]>;
329
- mapAsync: <U extends Type>(f: (value: R) => Promise<U>) => Promise<Either<L, U>>;
330
- flatMap: <U extends Type>(f: (value: R) => Either<L, U>) => Either<L, U>;
331
- flatMapAsync: <U extends Type>(f: (value: R) => Promise<Either<L, U>>) => Promise<Either<L, U>>;
332
- toOption: () => Option<R>;
333
- toList: () => List<R>;
334
- toString: () => string;
335
- [Symbol.iterator]: () => Iterator<R>;
336
- yield: () => Generator<R, void, unknown>;
337
- traverse: <U extends Type>(f: (value: R) => Either<L, U>) => Either<L, U[]>;
338
- lazyMap: <U extends Type>(f: (value: R) => U) => Generator<Either<L, U>, void, unknown>;
339
- tap: (f: (value: R) => void) => Either<L, R>;
340
- tapLeft: (f: (value: L) => void) => Either<L, R>;
341
- mapLeft: <L2 extends Type>(f: (value: L) => L2) => Either<L2, R>;
342
- bimap: <L2 extends Type, R2 extends Type>(fl: (value: L) => L2, fr: (value: R) => R2) => Either<L2, R2>;
343
- fold: <T extends Type>(onLeft: (value: L) => T, onRight: (value: R) => T) => T;
344
- swap: () => Either<R, L>;
345
- } & Typeable<"Left" | "Right"> & Valuable<"Left" | "Right", L | R> & PromiseLike<R> & AsyncFunctor<R>;
346
- declare const Either: {
347
- sequence: <L extends Type, R extends Type>(eithers: Either<L, R>[]) => Either<L, R[]>;
348
- traverse: <L extends Type, R extends Type, U extends Type>(arr: R[], f: (value: R) => Either<L, U>) => Either<L, U[]>;
349
- fromNullable: <L extends Type, R extends Type>(value: R | null | undefined, leftValue: L) => Either<L, R>;
350
- fromPredicate: <L extends Type, R extends Type>(value: R, predicate: (value: R) => boolean, leftValue: L) => Either<L, R>;
351
- ap: <L extends Type, R extends Type, U extends Type>(eitherF: Either<L, (value: R) => U>, eitherV: Either<L, R>) => Either<L, U>;
352
- fromPromise: <L, R>(promise: Promise<R>, onRejected: (reason: unknown) => L) => Promise<Either<L, R>>;
353
- };
354
-
355
- /**
356
- * Error context information that provides additional metadata about errors.
357
- * This context is passed to error mapping functions to provide more information
358
- * about the error that occurred.
359
- *
360
- * @property originalError - The original error that was thrown
361
- * @property stack - The stack trace of the error, if available
362
- * @property timestamp - The timestamp when the error occurred
363
- */
364
- type ErrorContext = {
365
- originalError: unknown;
366
- stack?: string;
367
- timestamp: number;
368
- };
369
- /**
370
- * Static utility methods for FPromise using the Companion pattern.
371
- * These methods provide factory functions and utilities for working with FPromises.
372
- */
373
- declare const FPromiseCompanion: {
374
- /**
375
- * Creates an FPromise that resolves to the provided value.
376
- *
377
- * @template T - The type of the value
378
- * @template E - The type of the error (defaults to never since this FPromise won't reject)
379
- * @param value - The value to resolve with
380
- * @returns An FPromise that resolves to the value
381
- *
382
- * @example
383
- * const promise = FPromise.resolve(42)
384
- * // promise resolves to 42
385
- */
386
- resolve: <T, E = never>(value: T | PromiseLike<T>) => FPromise<T, E>;
387
- /**
388
- * Creates an FPromise that rejects with the provided reason.
389
- *
390
- * @template T - The type of the value (which will never be produced)
391
- * @template E - The type of the error
392
- * @param reason - The reason for rejection
393
- * @returns An FPromise that rejects with the reason
394
- *
395
- * @example
396
- * const promise = FPromise.reject<number, Error>(new Error("Something went wrong"))
397
- * // promise rejects with Error("Something went wrong")
398
- */
399
- reject: <T, E = unknown>(reason: E) => FPromise<T, E>;
400
- /**
401
- * Creates an FPromise from a regular Promise.
402
- *
403
- * @template T - The type of the value
404
- * @template E - The type of the error
405
- * @param promise - The Promise to convert
406
- * @returns An FPromise that resolves or rejects with the same value or error
407
- *
408
- * @example
409
- * const promise = FPromise.from(fetch("https://api.example.com/data"))
410
- * // promise is an FPromise that resolves or rejects based on the fetch result
411
- */
412
- from: <T, E = unknown>(promise: Promise<T>) => FPromise<T, E>;
413
- /**
414
- * Creates an FPromise from an Either.
415
- * If the Either is a Right, the FPromise resolves with the Right value.
416
- * If the Either is a Left, the FPromise rejects with the Left value.
417
- *
418
- * @template L - The type of the Left value (error)
419
- * @template R - The type of the Right value (success)
420
- * @param either - The Either to convert
421
- * @returns An FPromise that resolves or rejects based on the Either
422
- *
423
- * @example
424
- * const either = Right<Error, number>(42)
425
- * const promise = FPromise.fromEither(either)
426
- * // promise resolves to 42
427
- */
428
- fromEither: <L, R>(either: Either<L, R>) => FPromise<R, L>;
429
- /**
430
- * Runs multiple FPromises in parallel and returns an array of results.
431
- * Similar to Promise.all, this will reject if any of the promises reject.
432
- *
433
- * @template T - The type of the values
434
- * @template E - The type of the error
435
- * @param promises - An array of FPromises, Promises, or values
436
- * @returns An FPromise that resolves to an array of results
437
- *
438
- * @example
439
- * const promises = [FPromise.resolve(1), FPromise.resolve(2), 3]
440
- * const result = await FPromise.all(promises).toPromise()
441
- * // result is [1, 2, 3]
442
- */
443
- all: <T, E = unknown>(promises: Array<FPromise<T, E> | PromiseLike<T> | T>) => FPromise<T[], E>;
444
- /**
445
- * Like Promise.allSettled, returns results of all promises whether they succeed or fail.
446
- * This will always resolve, never reject.
447
- *
448
- * @template T - The type of the values
449
- * @template E - The type of the errors
450
- * @param promises - An array of FPromises or Promises
451
- * @returns An FPromise that resolves to an array of Either results
452
- *
453
- * @example
454
- * const promises = [FPromise.resolve(1), FPromise.reject<number, Error>(new Error("Failed"))]
455
- * const result = await FPromise.allSettled(promises).toPromise()
456
- * // result is [Right(1), Left(Error("Failed"))]
457
- */
458
- allSettled: <T, E = unknown>(promises: Array<FPromise<T, E> | PromiseLike<T>>) => FPromise<Array<Either<E, T>>, never>;
459
- /**
460
- * Like Promise.race, returns the first promise to settle (either resolve or reject).
461
- *
462
- * @template T - The type of the values
463
- * @template E - The type of the errors
464
- * @param promises - An array of FPromises or Promises
465
- * @returns An FPromise that resolves or rejects with the result of the first promise to settle
466
- *
467
- * @example
468
- * const slow = FPromise.resolve(1).tap(() => new Promise(r => setTimeout(r, 100)))
469
- * const fast = FPromise.resolve(2).tap(() => new Promise(r => setTimeout(r, 50)))
470
- * const result = await FPromise.race([slow, fast]).toPromise()
471
- * // result is 2
472
- */
473
- race: <T, E = unknown>(promises: Array<FPromise<T, E> | PromiseLike<T>>) => FPromise<T, E>;
474
- /**
475
- * Like Promise.any, returns the first promise to fulfill.
476
- * This will only reject if all promises reject.
477
- *
478
- * @template T - The type of the values
479
- * @template E - The type of the errors
480
- * @param promises - An array of FPromises or Promises
481
- * @returns An FPromise that resolves with the first promise to fulfill or rejects if all promises reject
482
- *
483
- * @example
484
- * const promises = [
485
- * FPromise.reject<number, Error>(new Error("First failed")),
486
- * FPromise.resolve(2),
487
- * FPromise.reject<number, Error>(new Error("Third failed"))
488
- * ]
489
- * const result = await FPromise.any(promises).toPromise()
490
- * // result is 2
491
- */
492
- any: <T, E = unknown>(promises: Array<FPromise<T, E> | PromiseLike<T>>) => FPromise<T, E>;
493
- /**
494
- * Retries an operation with exponential backoff.
495
- * This is useful for operations that may fail temporarily, such as network requests.
496
- *
497
- * @template T - The type of the value
498
- * @template E - The type of the error
499
- * @param operation - A function that returns an FPromise
500
- * @param options - Configuration options for the retry
501
- * @param options.maxRetries - Maximum number of retry attempts
502
- * @param options.baseDelay - Base delay in milliseconds (default: 100)
503
- * @param options.shouldRetry - Function that determines whether to retry based on the error (default: always retry)
504
- * @returns An FPromise that resolves when the operation succeeds or rejects after all retries fail
505
- *
506
- * @example
507
- * const operation = () => {
508
- * if (Math.random() > 0.8) {
509
- * return FPromise.resolve("Success!")
510
- * }
511
- * return FPromise.reject<string, Error>(new Error("Temporary failure"))
512
- * }
513
- *
514
- * const result = await FPromise.retryWithBackoff(operation, {
515
- * maxRetries: 3,
516
- * baseDelay: 100,
517
- * shouldRetry: (error) => error.message === "Temporary failure"
518
- * }).toPromise()
519
- */
520
- retryWithBackoff: <T, E = unknown>(operation: () => FPromise<T, E>, options: {
521
- maxRetries: number;
522
- baseDelay?: number;
523
- shouldRetry?: (error: E, attempt: number) => boolean;
524
- }) => FPromise<T, E>;
525
- };
526
- /**
527
- * FPromise is a functional wrapper around JavaScript's Promise with enhanced error handling.
528
- * It implements the Functor and AsyncFunctor interfaces, providing map and flatMap operations
529
- * for functional composition.
530
- *
531
- * FPromise adds several features not available in standard Promises:
532
- * - Generic error typing for better type safety
533
- * - Error recovery mechanisms (recover, recoverWith, recoverWithF)
534
- * - Error filtering and categorization
535
- * - Side effect methods (tap, tapError)
536
- * - Error context preservation
537
- * - Conversion to/from Either for more functional error handling
538
- *
539
- * @template T - The type of the value that the FPromise resolves to
540
- * @template E - The type of the error that the FPromise may reject with (defaults to unknown)
541
- */
542
- /**
543
- * FPromise type that defines the function signature and methods
544
- */
545
- interface FPromise<T extends Type, E extends Type = unknown> extends PromiseLike<T> {
546
- readonly _tag: "FPromise";
547
- tap: (f: (value: T) => void) => FPromise<T, E>;
548
- mapError: <E2>(f: (error: E, context: ErrorContext) => E2) => FPromise<T, E2>;
549
- tapError: (f: (error: E) => void) => FPromise<T, E>;
550
- recover: (fallback: T) => FPromise<T, never>;
551
- recoverWith: (f: (error: E) => T) => FPromise<T, never>;
552
- recoverWithF: <E2>(f: (error: E) => FPromise<T, E2>) => FPromise<T, E2>;
553
- filterError: <E2 extends E>(predicate: (error: E) => boolean, handler: (error: E) => FPromise<T, E2>) => FPromise<T, E>;
554
- logError: (logger: (error: E, context: ErrorContext) => void) => FPromise<T, E>;
555
- toPromise: () => Promise<T>;
556
- toEither: () => Promise<T>;
557
- map: <U extends Type>(f: (value: T) => U) => FPromise<U, E>;
558
- flatMap: <U extends Type>(f: (value: T) => FPromise<U, E> | PromiseLike<U>) => FPromise<U, E>;
559
- flatMapAsync: <U extends Type>(f: (value: T) => PromiseLike<U>) => Promise<U>;
560
- }
561
- /**
562
- * Creates an FPromise from an executor function.
563
- *
564
- * @template T - The type of the value that the FPromise resolves to
565
- * @template E - The type of the error that the FPromise may reject with
566
- * @param executor - A function that receives resolve and reject functions
567
- * @returns An FPromise instance
568
- */
569
- declare const FPromise: (<T extends Type, E = unknown>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: E) => void) => void) => FPromise<T, E>) & {
570
- /**
571
- * Creates an FPromise that resolves to the provided value.
572
- *
573
- * @template T - The type of the value
574
- * @template E - The type of the error (defaults to never since this FPromise won't reject)
575
- * @param value - The value to resolve with
576
- * @returns An FPromise that resolves to the value
577
- *
578
- * @example
579
- * const promise = FPromise.resolve(42)
580
- * // promise resolves to 42
581
- */
582
- resolve: <T, E = never>(value: T | PromiseLike<T>) => FPromise<T, E>;
583
- /**
584
- * Creates an FPromise that rejects with the provided reason.
585
- *
586
- * @template T - The type of the value (which will never be produced)
587
- * @template E - The type of the error
588
- * @param reason - The reason for rejection
589
- * @returns An FPromise that rejects with the reason
590
- *
591
- * @example
592
- * const promise = FPromise.reject<number, Error>(new Error("Something went wrong"))
593
- * // promise rejects with Error("Something went wrong")
594
- */
595
- reject: <T, E = unknown>(reason: E) => FPromise<T, E>;
596
- /**
597
- * Creates an FPromise from a regular Promise.
598
- *
599
- * @template T - The type of the value
600
- * @template E - The type of the error
601
- * @param promise - The Promise to convert
602
- * @returns An FPromise that resolves or rejects with the same value or error
603
- *
604
- * @example
605
- * const promise = FPromise.from(fetch("https://api.example.com/data"))
606
- * // promise is an FPromise that resolves or rejects based on the fetch result
607
- */
608
- from: <T, E = unknown>(promise: Promise<T>) => FPromise<T, E>;
609
- /**
610
- * Creates an FPromise from an Either.
611
- * If the Either is a Right, the FPromise resolves with the Right value.
612
- * If the Either is a Left, the FPromise rejects with the Left value.
613
- *
614
- * @template L - The type of the Left value (error)
615
- * @template R - The type of the Right value (success)
616
- * @param either - The Either to convert
617
- * @returns An FPromise that resolves or rejects based on the Either
618
- *
619
- * @example
620
- * const either = Right<Error, number>(42)
621
- * const promise = FPromise.fromEither(either)
622
- * // promise resolves to 42
623
- */
624
- fromEither: <L, R>(either: Either<L, R>) => FPromise<R, L>;
625
- /**
626
- * Runs multiple FPromises in parallel and returns an array of results.
627
- * Similar to Promise.all, this will reject if any of the promises reject.
628
- *
629
- * @template T - The type of the values
630
- * @template E - The type of the error
631
- * @param promises - An array of FPromises, Promises, or values
632
- * @returns An FPromise that resolves to an array of results
633
- *
634
- * @example
635
- * const promises = [FPromise.resolve(1), FPromise.resolve(2), 3]
636
- * const result = await FPromise.all(promises).toPromise()
637
- * // result is [1, 2, 3]
638
- */
639
- all: <T, E = unknown>(promises: Array<FPromise<T, E> | PromiseLike<T> | T>) => FPromise<T[], E>;
640
- /**
641
- * Like Promise.allSettled, returns results of all promises whether they succeed or fail.
642
- * This will always resolve, never reject.
643
- *
644
- * @template T - The type of the values
645
- * @template E - The type of the errors
646
- * @param promises - An array of FPromises or Promises
647
- * @returns An FPromise that resolves to an array of Either results
648
- *
649
- * @example
650
- * const promises = [FPromise.resolve(1), FPromise.reject<number, Error>(new Error("Failed"))]
651
- * const result = await FPromise.allSettled(promises).toPromise()
652
- * // result is [Right(1), Left(Error("Failed"))]
653
- */
654
- allSettled: <T, E = unknown>(promises: Array<FPromise<T, E> | PromiseLike<T>>) => FPromise<Array<Either<E, T>>, never>;
655
- /**
656
- * Like Promise.race, returns the first promise to settle (either resolve or reject).
657
- *
658
- * @template T - The type of the values
659
- * @template E - The type of the errors
660
- * @param promises - An array of FPromises or Promises
661
- * @returns An FPromise that resolves or rejects with the result of the first promise to settle
662
- *
663
- * @example
664
- * const slow = FPromise.resolve(1).tap(() => new Promise(r => setTimeout(r, 100)))
665
- * const fast = FPromise.resolve(2).tap(() => new Promise(r => setTimeout(r, 50)))
666
- * const result = await FPromise.race([slow, fast]).toPromise()
667
- * // result is 2
668
- */
669
- race: <T, E = unknown>(promises: Array<FPromise<T, E> | PromiseLike<T>>) => FPromise<T, E>;
670
- /**
671
- * Like Promise.any, returns the first promise to fulfill.
672
- * This will only reject if all promises reject.
673
- *
674
- * @template T - The type of the values
675
- * @template E - The type of the errors
676
- * @param promises - An array of FPromises or Promises
677
- * @returns An FPromise that resolves with the first promise to fulfill or rejects if all promises reject
678
- *
679
- * @example
680
- * const promises = [
681
- * FPromise.reject<number, Error>(new Error("First failed")),
682
- * FPromise.resolve(2),
683
- * FPromise.reject<number, Error>(new Error("Third failed"))
684
- * ]
685
- * const result = await FPromise.any(promises).toPromise()
686
- * // result is 2
687
- */
688
- any: <T, E = unknown>(promises: Array<FPromise<T, E> | PromiseLike<T>>) => FPromise<T, E>;
689
- /**
690
- * Retries an operation with exponential backoff.
691
- * This is useful for operations that may fail temporarily, such as network requests.
692
- *
693
- * @template T - The type of the value
694
- * @template E - The type of the error
695
- * @param operation - A function that returns an FPromise
696
- * @param options - Configuration options for the retry
697
- * @param options.maxRetries - Maximum number of retry attempts
698
- * @param options.baseDelay - Base delay in milliseconds (default: 100)
699
- * @param options.shouldRetry - Function that determines whether to retry based on the error (default: always retry)
700
- * @returns An FPromise that resolves when the operation succeeds or rejects after all retries fail
701
- *
702
- * @example
703
- * const operation = () => {
704
- * if (Math.random() > 0.8) {
705
- * return FPromise.resolve("Success!")
706
- * }
707
- * return FPromise.reject<string, Error>(new Error("Temporary failure"))
708
- * }
709
- *
710
- * const result = await FPromise.retryWithBackoff(operation, {
711
- * maxRetries: 3,
712
- * baseDelay: 100,
713
- * shouldRetry: (error) => error.message === "Temporary failure"
714
- * }).toPromise()
715
- */
716
- retryWithBackoff: <T, E = unknown>(operation: () => FPromise<T, E>, options: {
717
- maxRetries: number;
718
- baseDelay?: number;
719
- shouldRetry?: (error: E, attempt: number) => boolean;
720
- }) => FPromise<T, E>;
721
- };
722
-
723
- type TaskParams = {
724
- name?: string;
725
- description?: string;
726
- };
727
- type TaskInfo = {
728
- _task: TaskParams;
729
- };
730
- type TaskException<T> = Either<Throwable, T> & TaskInfo;
731
- /**
732
- * TaskException factory function
733
- * @param error
734
- * @param _task
735
- * @param data
736
- * @constructor
737
- */
738
- declare const TaskException: <T>(error: unknown, data?: unknown, _task?: TaskParams) => TaskException<T>;
739
- type TaskResult<T> = Either<Throwable, T> & TaskInfo;
740
- declare const TaskResult: <T>(data: T, _task?: TaskParams) => TaskResult<T>;
741
- type Sync<T> = Either<Throwable, T>;
742
- type Async<T> = FPromise<Sync<T>>;
743
- /**
744
- * Task adapter for bridging promise-based code with functional error handling patterns
745
- */
746
- declare const Task: <T = unknown>(params?: TaskParams) => {
747
- _type: string;
748
- /**
749
- * Run an async operation with explicit try/catch/finally semantics
750
- * Returns a raw Promise that can interact with traditional Promise-based code
751
- */
752
- Async: <U = T>(t: () => U | Promise<U>, e?: (error: unknown) => unknown, f?: () => Promise<void> | void) => FPromise<U>;
753
- /**
754
- * Run a synchronous operation with explicit try/catch/finally semantics
755
- * Returns an Either for functional error handling
756
- */
757
- Sync: <U = T>(t: () => U, e?: (error: unknown) => unknown, f?: () => void) => Sync<U>;
758
- /**
759
- * Create a successful Task result
760
- */
761
- success: (data: T) => TaskResult<T>;
762
- /**
763
- * Create a failed Task result
764
- */
765
- fail: (error: unknown) => TaskException<T>;
766
- /**
767
- * Convert a Promise-returning function to a Task-compatible function
768
- */
769
- fromPromise: <U, Args extends unknown[]>(promiseFn: (...args: Args) => Promise<U>) => ((...args: Args) => FPromise<U>);
770
- /**
771
- * Convert a Task result to a Promise
772
- */
773
- toPromise: <U>(taskResult: TaskResult<U> | TaskException<U>) => Promise<U>;
774
- toString(): string;
775
- _tag: string;
776
- };
777
-
778
- type TypeNames = "Success" | "Failure";
779
- type Try<T> = {
780
- readonly _tag: TypeNames;
781
- readonly error: Error | undefined;
782
- isSuccess: () => boolean;
783
- isFailure: () => boolean;
784
- get: () => T;
785
- getOrElse: (defaultValue: T) => T;
786
- orElse: (alternative: Try<T>) => Try<T>;
787
- orThrow: (error: Error) => T;
788
- toEither: () => Either<Error, T>;
789
- map: <U>(f: (value: T) => U) => Try<U>;
790
- flatMap: <U>(f: (value: T) => Try<U>) => Try<U>;
791
- toString: () => string;
792
- } & Typeable<TypeNames> & Valuable<TypeNames, T | Error>;
793
- declare const Try: <T>(f: () => T) => Try<T>;
794
-
795
- /**
796
- * Type function for representing higher-kinded types
797
- */
798
- type Kind<F, A> = F extends (arg: infer T) => infer R ? R : never;
799
- /**
800
- * Type constructors for common Functype data types
801
- */
802
- type OptionKind = <A>(a: A) => Option<A>;
803
- type ListKind = <A>(a: A) => List<A>;
804
- type EitherKind<E> = <A>(a: A) => Either<E, A>;
805
- type TryKind = <A>(a: A) => Try<A>;
806
- /**
807
- * Universal type that includes all potential return types from the HKT functions
808
- * Used to avoid 'any' usage which the linter prohibits
809
- */
810
- type UniversalContainer = Option<unknown> | List<unknown> | Either<unknown, unknown> | Try<unknown>;
811
- /**
812
- * HKT provides utilities for working with higher-kinded types
813
- * This allows writing generic code that works across different
814
- * container types like Option, List, Either, etc.
815
- */
816
- declare const HKT: {
817
- (): {
818
- _type: string;
819
- map: <F, A, B>(fa: unknown, f: (a: A) => B) => unknown;
820
- flatten: <F, A>(ffa: unknown) => unknown;
821
- flatMap: <F, A, B>(fa: unknown, f: (a: A) => unknown) => unknown;
822
- ap: <F, A, B>(ff: unknown, fa: unknown) => unknown;
823
- sequence: <F, G, A>(fga: unknown) => unknown;
824
- traverse: <F, G, A, B>(fa: unknown, f: (a: A) => unknown) => unknown;
825
- toString(): string;
826
- _tag: string;
827
- };
828
- map<F = unknown, A = unknown, B = unknown>(fa: unknown, f: (a: A) => B): unknown;
829
- flatten<F = unknown, A = unknown>(ffa: unknown): unknown;
830
- flatMap<F = unknown, A = unknown, B = unknown>(fa: unknown, f: (a: A) => unknown): unknown;
831
- ap<F = unknown, A = unknown, B = unknown>(ff: unknown, fa: unknown): unknown;
832
- sequence<F = unknown, G = unknown, A = unknown>(fga: unknown): unknown;
833
- traverse<F = unknown, G = unknown, A = unknown, B = unknown>(fa: unknown, f: (a: A) => unknown): unknown;
834
- isOption: <T extends Type>(value: unknown) => value is Option<T>;
835
- isList: <T extends Type>(value: unknown) => value is List<T>;
836
- isEither: <E extends Type, A extends Type>(value: unknown) => value is Either<E, A>;
837
- isTry: <T extends Type>(value: unknown) => value is Try<T>;
838
- };
839
-
840
- type Identity<T> = {
841
- id: T;
842
- isSame?: (other: Identity<T>) => boolean;
843
- };
844
- declare function Identity<T>(value: T): Identity<T>;
845
-
846
- type IESMap<K, V> = Map<K, V>;
847
-
848
- type SafeTraversable<K, V> = Omit<Traversable<Tuple<[K, V]>>, "map" | "flatMap">;
849
- type Map$1<K, V> = {
850
- add(item: Tuple<[K, V]>): Map$1<K, V>;
851
- remove(value: K): Map$1<K, V>;
852
- map<U>(f: (value: V) => U): Map$1<K, U>;
853
- flatMap<K2, V2>(f: (entry: Tuple<[K, V]>) => IterableType<[K2, V2]>): Map$1<K2, V2>;
854
- get(key: K): Option<V>;
855
- getOrElse(key: K, defaultValue: V): V;
856
- orElse(key: K, alternative: Option<V>): Option<V>;
857
- } & SafeTraversable<K, V> & Collection<Tuple<[K, V]>> & Typeable<"Map"> & Valuable<"Map", IESMap<K, V>>;
858
- declare const Map$1: <K, V>(entries?: readonly (readonly [K, V])[] | IterableIterator<[K, V]> | null) => Map$1<K, V>;
859
-
860
- type Traversable<A extends Type> = Functor<A> & {
861
- get size(): number;
862
- get isEmpty(): boolean;
863
- contains(value: A): boolean;
864
- reduce(f: (b: A, a: A) => A): A;
865
- reduceRight(f: (b: A, a: A) => A): A;
866
- foldLeft<B>(z: B): (op: (b: B, a: A) => B) => B;
867
- foldRight<B>(z: B): (op: (a: A, b: B) => B) => B;
868
- };
869
-
870
- export { type Async as A, Base as B, Companion as C, Either as E, FPromise as F, HKT as H, Identity as I, type Kind as K, List as L, Map$1 as M, None, type OptionKind as O, Option, Right as R, Set as S, Some, type TestEither as T, type UniversalContainer as U, Left as a, isLeft as b, TypeCheckRight as c, TypeCheckLeft as d, tryCatchAsync as e, type ErrorContext as f, FPromiseCompanion as g, Try as h, isRight as i, type Traversable as j, type TaskParams as k, type TaskInfo as l, TaskException as m, TaskResult as n, type Sync as o, Task as p, type ThrowableType as q, Throwable as r, type ListKind as s, tryCatch as t, type EitherKind as u, type TryKind as v, type IterableType as w };
1
+ export * from './Option';