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,3 +1,373 @@
1
- export { f as ErrorContext, F as FPromise, g as FPromiseCompanion } from '../option/index.js';
2
- import '../Tuple-DfdXAbL_.js';
3
- import '../branded/index.js';
1
+ import { E as Either } from '../Either-BfXNbTHo.js';
2
+ import { a as Type } from '../Valuable-CtuVEKTZ.js';
3
+
4
+ /**
5
+ * Error context information that provides additional metadata about errors.
6
+ * This context is passed to error mapping functions to provide more information
7
+ * about the error that occurred.
8
+ *
9
+ * @property originalError - The original error that was thrown
10
+ * @property stack - The stack trace of the error, if available
11
+ * @property timestamp - The timestamp when the error occurred
12
+ */
13
+ type ErrorContext = {
14
+ originalError: unknown;
15
+ stack?: string;
16
+ timestamp: number;
17
+ };
18
+ /**
19
+ * Static utility methods for FPromise using the Companion pattern.
20
+ * These methods provide factory functions and utilities for working with FPromises.
21
+ */
22
+ declare const FPromiseCompanion: {
23
+ /**
24
+ * Creates an FPromise that resolves to the provided value.
25
+ *
26
+ * @template T - The type of the value
27
+ * @template E - The type of the error (defaults to never since this FPromise won't reject)
28
+ * @param value - The value to resolve with
29
+ * @returns An FPromise that resolves to the value
30
+ *
31
+ * @example
32
+ * const promise = FPromise.resolve(42)
33
+ * // promise resolves to 42
34
+ */
35
+ resolve: <T, E = never>(value: T | PromiseLike<T>) => FPromise<T, E>;
36
+ /**
37
+ * Creates an FPromise that rejects with the provided reason.
38
+ *
39
+ * @template T - The type of the value (which will never be produced)
40
+ * @template E - The type of the error
41
+ * @param reason - The reason for rejection
42
+ * @returns An FPromise that rejects with the reason
43
+ *
44
+ * @example
45
+ * const promise = FPromise.reject<number, Error>(new Error("Something went wrong"))
46
+ * // promise rejects with Error("Something went wrong")
47
+ */
48
+ reject: <T, E = unknown>(reason: E) => FPromise<T, E>;
49
+ /**
50
+ * Creates an FPromise from a regular Promise.
51
+ *
52
+ * @template T - The type of the value
53
+ * @template E - The type of the error
54
+ * @param promise - The Promise to convert
55
+ * @returns An FPromise that resolves or rejects with the same value or error
56
+ *
57
+ * @example
58
+ * const promise = FPromise.from(fetch("https://api.example.com/data"))
59
+ * // promise is an FPromise that resolves or rejects based on the fetch result
60
+ */
61
+ from: <T, E = unknown>(promise: Promise<T>) => FPromise<T, E>;
62
+ /**
63
+ * Creates an FPromise from an Either.
64
+ * If the Either is a Right, the FPromise resolves with the Right value.
65
+ * If the Either is a Left, the FPromise rejects with the Left value.
66
+ *
67
+ * @template L - The type of the Left value (error)
68
+ * @template R - The type of the Right value (success)
69
+ * @param either - The Either to convert
70
+ * @returns An FPromise that resolves or rejects based on the Either
71
+ *
72
+ * @example
73
+ * const either = Right<Error, number>(42)
74
+ * const promise = FPromise.fromEither(either)
75
+ * // promise resolves to 42
76
+ */
77
+ fromEither: <L, R>(either: Either<L, R>) => FPromise<R, L>;
78
+ /**
79
+ * Runs multiple FPromises in parallel and returns an array of results.
80
+ * Similar to Promise.all, this will reject if any of the promises reject.
81
+ *
82
+ * @template T - The type of the values
83
+ * @template E - The type of the error
84
+ * @param promises - An array of FPromises, Promises, or values
85
+ * @returns An FPromise that resolves to an array of results
86
+ *
87
+ * @example
88
+ * const promises = [FPromise.resolve(1), FPromise.resolve(2), 3]
89
+ * const result = await FPromise.all(promises).toPromise()
90
+ * // result is [1, 2, 3]
91
+ */
92
+ all: <T, E = unknown>(promises: Array<FPromise<T, E> | PromiseLike<T> | T>) => FPromise<T[], E>;
93
+ /**
94
+ * Like Promise.allSettled, returns results of all promises whether they succeed or fail.
95
+ * This will always resolve, never reject.
96
+ *
97
+ * @template T - The type of the values
98
+ * @template E - The type of the errors
99
+ * @param promises - An array of FPromises or Promises
100
+ * @returns An FPromise that resolves to an array of Either results
101
+ *
102
+ * @example
103
+ * const promises = [FPromise.resolve(1), FPromise.reject<number, Error>(new Error("Failed"))]
104
+ * const result = await FPromise.allSettled(promises).toPromise()
105
+ * // result is [Right(1), Left(Error("Failed"))]
106
+ */
107
+ allSettled: <T, E = unknown>(promises: Array<FPromise<T, E> | PromiseLike<T>>) => FPromise<Array<Either<E, T>>, never>;
108
+ /**
109
+ * Like Promise.race, returns the first promise to settle (either resolve or reject).
110
+ *
111
+ * @template T - The type of the values
112
+ * @template E - The type of the errors
113
+ * @param promises - An array of FPromises or Promises
114
+ * @returns An FPromise that resolves or rejects with the result of the first promise to settle
115
+ *
116
+ * @example
117
+ * const slow = FPromise.resolve(1).tap(() => new Promise(r => setTimeout(r, 100)))
118
+ * const fast = FPromise.resolve(2).tap(() => new Promise(r => setTimeout(r, 50)))
119
+ * const result = await FPromise.race([slow, fast]).toPromise()
120
+ * // result is 2
121
+ */
122
+ race: <T, E = unknown>(promises: Array<FPromise<T, E> | PromiseLike<T>>) => FPromise<T, E>;
123
+ /**
124
+ * Like Promise.any, returns the first promise to fulfill.
125
+ * This will only reject if all promises reject.
126
+ *
127
+ * @template T - The type of the values
128
+ * @template E - The type of the errors
129
+ * @param promises - An array of FPromises or Promises
130
+ * @returns An FPromise that resolves with the first promise to fulfill or rejects if all promises reject
131
+ *
132
+ * @example
133
+ * const promises = [
134
+ * FPromise.reject<number, Error>(new Error("First failed")),
135
+ * FPromise.resolve(2),
136
+ * FPromise.reject<number, Error>(new Error("Third failed"))
137
+ * ]
138
+ * const result = await FPromise.any(promises).toPromise()
139
+ * // result is 2
140
+ */
141
+ any: <T, E = unknown>(promises: Array<FPromise<T, E> | PromiseLike<T>>) => FPromise<T, E>;
142
+ /**
143
+ * Retries an operation with exponential backoff.
144
+ * This is useful for operations that may fail temporarily, such as network requests.
145
+ *
146
+ * @template T - The type of the value
147
+ * @template E - The type of the error
148
+ * @param operation - A function that returns an FPromise
149
+ * @param options - Configuration options for the retry
150
+ * @param options.maxRetries - Maximum number of retry attempts
151
+ * @param options.baseDelay - Base delay in milliseconds (default: 100)
152
+ * @param options.shouldRetry - Function that determines whether to retry based on the error (default: always retry)
153
+ * @returns An FPromise that resolves when the operation succeeds or rejects after all retries fail
154
+ *
155
+ * @example
156
+ * const operation = () => {
157
+ * if (Math.random() > 0.8) {
158
+ * return FPromise.resolve("Success!")
159
+ * }
160
+ * return FPromise.reject<string, Error>(new Error("Temporary failure"))
161
+ * }
162
+ *
163
+ * const result = await FPromise.retryWithBackoff(operation, {
164
+ * maxRetries: 3,
165
+ * baseDelay: 100,
166
+ * shouldRetry: (error) => error.message === "Temporary failure"
167
+ * }).toPromise()
168
+ */
169
+ retryWithBackoff: <T, E = unknown>(operation: () => FPromise<T, E>, options: {
170
+ maxRetries: number;
171
+ baseDelay?: number;
172
+ shouldRetry?: (error: E, attempt: number) => boolean;
173
+ }) => FPromise<T, E>;
174
+ };
175
+ /**
176
+ * FPromise is a functional wrapper around JavaScript's Promise with enhanced error handling.
177
+ * It implements the Functor and AsyncFunctor interfaces, providing map and flatMap operations
178
+ * for functional composition.
179
+ *
180
+ * FPromise adds several features not available in standard Promises:
181
+ * - Generic error typing for better type safety
182
+ * - Error recovery mechanisms (recover, recoverWith, recoverWithF)
183
+ * - Error filtering and categorization
184
+ * - Side effect methods (tap, tapError)
185
+ * - Error context preservation
186
+ * - Conversion to/from Either for more functional error handling
187
+ *
188
+ * @template T - The type of the value that the FPromise resolves to
189
+ * @template E - The type of the error that the FPromise may reject with (defaults to unknown)
190
+ */
191
+ /**
192
+ * FPromise type that defines the function signature and methods
193
+ */
194
+ type FPromise<T extends Type, E extends Type = unknown> = PromiseLike<T> & {
195
+ readonly _tag: "FPromise";
196
+ tap: (f: (value: T) => void) => FPromise<T, E>;
197
+ mapError: <E2>(f: (error: E, context: ErrorContext) => E2) => FPromise<T, E2>;
198
+ tapError: (f: (error: E) => void) => FPromise<T, E>;
199
+ recover: (fallback: T) => FPromise<T, never>;
200
+ recoverWith: (f: (error: E) => T) => FPromise<T, never>;
201
+ recoverWithF: <E2>(f: (error: E) => FPromise<T, E2>) => FPromise<T, E2>;
202
+ filterError: <E2 extends E>(predicate: (error: E) => boolean, handler: (error: E) => FPromise<T, E2>) => FPromise<T, E>;
203
+ logError: (logger: (error: E, context: ErrorContext) => void) => FPromise<T, E>;
204
+ toPromise: () => Promise<T>;
205
+ toEither: () => Promise<T>;
206
+ fold: <R extends Type>(onError: (error: E) => R, onSuccess: (value: T) => R) => FPromise<R, never>;
207
+ map: <U extends Type>(f: (value: T) => U) => FPromise<U, E>;
208
+ flatMap: <U extends Type>(f: (value: T) => FPromise<U, E> | PromiseLike<U>) => FPromise<U, E>;
209
+ flatMapAsync: <U extends Type>(f: (value: T) => PromiseLike<U>) => Promise<U>;
210
+ };
211
+ /**
212
+ * Creates an FPromise from an executor function.
213
+ *
214
+ * @template T - The type of the value that the FPromise resolves to
215
+ * @template E - The type of the error that the FPromise may reject with
216
+ * @param executor - A function that receives resolve and reject functions
217
+ * @returns An FPromise instance
218
+ */
219
+ declare const FPromise: (<T extends Type, E = unknown>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: E) => void) => void) => FPromise<T, E>) & {
220
+ /**
221
+ * Creates an FPromise that resolves to the provided value.
222
+ *
223
+ * @template T - The type of the value
224
+ * @template E - The type of the error (defaults to never since this FPromise won't reject)
225
+ * @param value - The value to resolve with
226
+ * @returns An FPromise that resolves to the value
227
+ *
228
+ * @example
229
+ * const promise = FPromise.resolve(42)
230
+ * // promise resolves to 42
231
+ */
232
+ resolve: <T, E = never>(value: T | PromiseLike<T>) => FPromise<T, E>;
233
+ /**
234
+ * Creates an FPromise that rejects with the provided reason.
235
+ *
236
+ * @template T - The type of the value (which will never be produced)
237
+ * @template E - The type of the error
238
+ * @param reason - The reason for rejection
239
+ * @returns An FPromise that rejects with the reason
240
+ *
241
+ * @example
242
+ * const promise = FPromise.reject<number, Error>(new Error("Something went wrong"))
243
+ * // promise rejects with Error("Something went wrong")
244
+ */
245
+ reject: <T, E = unknown>(reason: E) => FPromise<T, E>;
246
+ /**
247
+ * Creates an FPromise from a regular Promise.
248
+ *
249
+ * @template T - The type of the value
250
+ * @template E - The type of the error
251
+ * @param promise - The Promise to convert
252
+ * @returns An FPromise that resolves or rejects with the same value or error
253
+ *
254
+ * @example
255
+ * const promise = FPromise.from(fetch("https://api.example.com/data"))
256
+ * // promise is an FPromise that resolves or rejects based on the fetch result
257
+ */
258
+ from: <T, E = unknown>(promise: Promise<T>) => FPromise<T, E>;
259
+ /**
260
+ * Creates an FPromise from an Either.
261
+ * If the Either is a Right, the FPromise resolves with the Right value.
262
+ * If the Either is a Left, the FPromise rejects with the Left value.
263
+ *
264
+ * @template L - The type of the Left value (error)
265
+ * @template R - The type of the Right value (success)
266
+ * @param either - The Either to convert
267
+ * @returns An FPromise that resolves or rejects based on the Either
268
+ *
269
+ * @example
270
+ * const either = Right<Error, number>(42)
271
+ * const promise = FPromise.fromEither(either)
272
+ * // promise resolves to 42
273
+ */
274
+ fromEither: <L, R>(either: Either<L, R>) => FPromise<R, L>;
275
+ /**
276
+ * Runs multiple FPromises in parallel and returns an array of results.
277
+ * Similar to Promise.all, this will reject if any of the promises reject.
278
+ *
279
+ * @template T - The type of the values
280
+ * @template E - The type of the error
281
+ * @param promises - An array of FPromises, Promises, or values
282
+ * @returns An FPromise that resolves to an array of results
283
+ *
284
+ * @example
285
+ * const promises = [FPromise.resolve(1), FPromise.resolve(2), 3]
286
+ * const result = await FPromise.all(promises).toPromise()
287
+ * // result is [1, 2, 3]
288
+ */
289
+ all: <T, E = unknown>(promises: Array<FPromise<T, E> | PromiseLike<T> | T>) => FPromise<T[], E>;
290
+ /**
291
+ * Like Promise.allSettled, returns results of all promises whether they succeed or fail.
292
+ * This will always resolve, never reject.
293
+ *
294
+ * @template T - The type of the values
295
+ * @template E - The type of the errors
296
+ * @param promises - An array of FPromises or Promises
297
+ * @returns An FPromise that resolves to an array of Either results
298
+ *
299
+ * @example
300
+ * const promises = [FPromise.resolve(1), FPromise.reject<number, Error>(new Error("Failed"))]
301
+ * const result = await FPromise.allSettled(promises).toPromise()
302
+ * // result is [Right(1), Left(Error("Failed"))]
303
+ */
304
+ allSettled: <T, E = unknown>(promises: Array<FPromise<T, E> | PromiseLike<T>>) => FPromise<Array<Either<E, T>>, never>;
305
+ /**
306
+ * Like Promise.race, returns the first promise to settle (either resolve or reject).
307
+ *
308
+ * @template T - The type of the values
309
+ * @template E - The type of the errors
310
+ * @param promises - An array of FPromises or Promises
311
+ * @returns An FPromise that resolves or rejects with the result of the first promise to settle
312
+ *
313
+ * @example
314
+ * const slow = FPromise.resolve(1).tap(() => new Promise(r => setTimeout(r, 100)))
315
+ * const fast = FPromise.resolve(2).tap(() => new Promise(r => setTimeout(r, 50)))
316
+ * const result = await FPromise.race([slow, fast]).toPromise()
317
+ * // result is 2
318
+ */
319
+ race: <T, E = unknown>(promises: Array<FPromise<T, E> | PromiseLike<T>>) => FPromise<T, E>;
320
+ /**
321
+ * Like Promise.any, returns the first promise to fulfill.
322
+ * This will only reject if all promises reject.
323
+ *
324
+ * @template T - The type of the values
325
+ * @template E - The type of the errors
326
+ * @param promises - An array of FPromises or Promises
327
+ * @returns An FPromise that resolves with the first promise to fulfill or rejects if all promises reject
328
+ *
329
+ * @example
330
+ * const promises = [
331
+ * FPromise.reject<number, Error>(new Error("First failed")),
332
+ * FPromise.resolve(2),
333
+ * FPromise.reject<number, Error>(new Error("Third failed"))
334
+ * ]
335
+ * const result = await FPromise.any(promises).toPromise()
336
+ * // result is 2
337
+ */
338
+ any: <T, E = unknown>(promises: Array<FPromise<T, E> | PromiseLike<T>>) => FPromise<T, E>;
339
+ /**
340
+ * Retries an operation with exponential backoff.
341
+ * This is useful for operations that may fail temporarily, such as network requests.
342
+ *
343
+ * @template T - The type of the value
344
+ * @template E - The type of the error
345
+ * @param operation - A function that returns an FPromise
346
+ * @param options - Configuration options for the retry
347
+ * @param options.maxRetries - Maximum number of retry attempts
348
+ * @param options.baseDelay - Base delay in milliseconds (default: 100)
349
+ * @param options.shouldRetry - Function that determines whether to retry based on the error (default: always retry)
350
+ * @returns An FPromise that resolves when the operation succeeds or rejects after all retries fail
351
+ *
352
+ * @example
353
+ * const operation = () => {
354
+ * if (Math.random() > 0.8) {
355
+ * return FPromise.resolve("Success!")
356
+ * }
357
+ * return FPromise.reject<string, Error>(new Error("Temporary failure"))
358
+ * }
359
+ *
360
+ * const result = await FPromise.retryWithBackoff(operation, {
361
+ * maxRetries: 3,
362
+ * baseDelay: 100,
363
+ * shouldRetry: (error) => error.message === "Temporary failure"
364
+ * }).toPromise()
365
+ */
366
+ retryWithBackoff: <T, E = unknown>(operation: () => FPromise<T, E>, options: {
367
+ maxRetries: number;
368
+ baseDelay?: number;
369
+ shouldRetry?: (error: E, attempt: number) => boolean;
370
+ }) => FPromise<T, E>;
371
+ };
372
+
373
+ export { type ErrorContext, FPromise, FPromiseCompanion };
@@ -1,2 +1,2 @@
1
- export{t as FPromise,s as FPromiseCompanion}from'../chunk-NTL4HYMA.mjs';import'../chunk-PXFJPCM7.mjs';import'../chunk-TQJDL6YW.mjs';//# sourceMappingURL=index.mjs.map
1
+ export{u as FPromise,t as FPromiseCompanion}from'../chunk-5DWCHDSA.mjs';import'../chunk-7PQA3W7W.mjs';import'../chunk-TQJDL6YW.mjs';//# sourceMappingURL=index.mjs.map
2
2
  //# sourceMappingURL=index.mjs.map