functype 0.8.67 → 0.8.69

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