@carbonteq/fp 0.9.1 → 0.11.1

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 (149) hide show
  1. package/README.md +326 -97
  2. package/dist/errors.cjs +2 -3
  3. package/dist/errors.cjs.map +1 -1
  4. package/dist/errors.d.cts +2 -5
  5. package/dist/errors.d.cts.map +1 -1
  6. package/dist/errors.d.mts +2 -5
  7. package/dist/errors.d.mts.map +1 -1
  8. package/dist/errors.mjs +1 -2
  9. package/dist/errors.mjs.map +1 -1
  10. package/dist/flow-experimental.cjs +332 -0
  11. package/dist/flow-experimental.cjs.map +1 -0
  12. package/dist/flow-experimental.d.cts +69 -0
  13. package/dist/flow-experimental.d.cts.map +1 -0
  14. package/dist/flow-experimental.d.mts +69 -0
  15. package/dist/flow-experimental.d.mts.map +1 -0
  16. package/dist/flow-experimental.mjs +330 -0
  17. package/dist/flow-experimental.mjs.map +1 -0
  18. package/dist/flow.cjs +340 -0
  19. package/dist/flow.cjs.map +1 -0
  20. package/dist/flow.d.cts +77 -0
  21. package/dist/flow.d.cts.map +1 -0
  22. package/dist/flow.d.mts +77 -0
  23. package/dist/flow.d.mts.map +1 -0
  24. package/dist/flow.mjs +338 -0
  25. package/dist/flow.mjs.map +1 -0
  26. package/dist/index.cjs +29 -14
  27. package/dist/index.d.cts +10 -5
  28. package/dist/index.d.mts +10 -5
  29. package/dist/index.mjs +10 -5
  30. package/dist/match.cjs +376 -3
  31. package/dist/match.cjs.map +1 -1
  32. package/dist/match.d.cts +467 -8
  33. package/dist/match.d.cts.map +1 -1
  34. package/dist/match.d.mts +467 -8
  35. package/dist/match.d.mts.map +1 -1
  36. package/dist/match.mjs +371 -3
  37. package/dist/match.mjs.map +1 -1
  38. package/dist/option-experimental.cjs +1547 -0
  39. package/dist/option-experimental.cjs.map +1 -0
  40. package/dist/option-experimental.d.cts +1380 -0
  41. package/dist/option-experimental.d.cts.map +1 -0
  42. package/dist/option-experimental.d.mts +1380 -0
  43. package/dist/option-experimental.d.mts.map +1 -0
  44. package/dist/option-experimental.mjs +1545 -0
  45. package/dist/option-experimental.mjs.map +1 -0
  46. package/dist/option.cjs +650 -71
  47. package/dist/option.cjs.map +1 -1
  48. package/dist/option.d.cts +528 -39
  49. package/dist/option.d.cts.map +1 -1
  50. package/dist/option.d.mts +528 -39
  51. package/dist/option.d.mts.map +1 -1
  52. package/dist/option.mjs +648 -70
  53. package/dist/option.mjs.map +1 -1
  54. package/dist/result-experimental.cjs +1346 -0
  55. package/dist/result-experimental.cjs.map +1 -0
  56. package/dist/result-experimental.d.cts +1127 -0
  57. package/dist/result-experimental.d.cts.map +1 -0
  58. package/dist/result-experimental.d.mts +1127 -0
  59. package/dist/result-experimental.d.mts.map +1 -0
  60. package/dist/result-experimental.mjs +1345 -0
  61. package/dist/result-experimental.mjs.map +1 -0
  62. package/dist/result.cjs +746 -81
  63. package/dist/result.cjs.map +1 -1
  64. package/dist/result.d.cts +799 -104
  65. package/dist/result.d.cts.map +1 -1
  66. package/dist/result.d.mts +799 -104
  67. package/dist/result.d.mts.map +1 -1
  68. package/dist/result.mjs +742 -73
  69. package/dist/result.mjs.map +1 -1
  70. package/dist/tuple.cjs +5 -4
  71. package/dist/tuple.cjs.map +1 -1
  72. package/dist/tuple.d.cts +9 -7
  73. package/dist/tuple.d.cts.map +1 -1
  74. package/dist/tuple.d.mts +9 -7
  75. package/dist/tuple.d.mts.map +1 -1
  76. package/dist/tuple.mjs +4 -3
  77. package/dist/tuple.mjs.map +1 -1
  78. package/dist/unit.cjs +3 -3
  79. package/dist/unit.cjs.map +1 -1
  80. package/dist/unit.d.cts +4 -5
  81. package/dist/unit.d.cts.map +1 -1
  82. package/dist/unit.d.mts +4 -5
  83. package/dist/unit.d.mts.map +1 -1
  84. package/dist/unit.mjs +2 -2
  85. package/dist/unit.mjs.map +1 -1
  86. package/dist/utils.cjs +50 -3
  87. package/dist/utils.cjs.map +1 -1
  88. package/dist/utils.d.cts +14 -4
  89. package/dist/utils.d.cts.map +1 -1
  90. package/dist/utils.d.mts +14 -4
  91. package/dist/utils.d.mts.map +1 -1
  92. package/dist/utils.mjs +46 -3
  93. package/dist/utils.mjs.map +1 -1
  94. package/examples/README.md +60 -0
  95. package/examples/flow/01-basics.ts +74 -0
  96. package/examples/flow/02-async.ts +73 -0
  97. package/examples/flow/03-recovery.ts +60 -0
  98. package/examples/flow/04-complex-backend.ts +240 -0
  99. package/examples/flow/05-complex-backend-asyncGen.ts +224 -0
  100. package/examples/flow/index.ts +13 -0
  101. package/examples/flow-experimental/01-basics.ts +78 -0
  102. package/examples/flow-experimental/02-async.ts +77 -0
  103. package/examples/flow-experimental/03-recovery.ts +93 -0
  104. package/examples/flow-experimental/04-complex-backend.ts +244 -0
  105. package/examples/flow-experimental/05-complex-backend-asyncGen.ts +229 -0
  106. package/examples/flow-experimental/index.ts +14 -0
  107. package/examples/match/01-stable-match.ts +154 -0
  108. package/examples/match/index.ts +19 -0
  109. package/examples/match-experimental/01-experimental-match.ts +157 -0
  110. package/examples/match-experimental/index.ts +18 -0
  111. package/examples/option/01-basics.ts +16 -0
  112. package/examples/option/02-map-flatMap.ts +36 -0
  113. package/examples/option/03-zip-flatZip.ts +23 -0
  114. package/examples/option/04-constructors.ts +21 -0
  115. package/examples/option/05-transform.ts +15 -0
  116. package/examples/option/06-aggregation.ts +14 -0
  117. package/examples/option/index.ts +49 -0
  118. package/examples/option-experimental/01-map.ts +109 -0
  119. package/examples/option-experimental/02-flatMap.ts +234 -0
  120. package/examples/option-experimental/03-zip.ts +197 -0
  121. package/examples/option-experimental/04-flatZip.ts +251 -0
  122. package/examples/option-experimental/05-gen.ts +360 -0
  123. package/examples/option-experimental/06-genAdapter.ts +463 -0
  124. package/examples/option-experimental/07-asyncGen.ts +353 -0
  125. package/examples/option-experimental/08-asyncGenAdapter.ts +514 -0
  126. package/examples/option-experimental/09-workflow-simple-sync.ts +321 -0
  127. package/examples/option-experimental/10-workflow-async.ts +378 -0
  128. package/examples/option-experimental/index.ts +341 -0
  129. package/examples/option-experimental/shared-domain.ts +325 -0
  130. package/examples/result/01-basics.ts +16 -0
  131. package/examples/result/02-map-flatMap.ts +34 -0
  132. package/examples/result/03-zip-flatZip.ts +23 -0
  133. package/examples/result/04-validation.ts +44 -0
  134. package/examples/result/05-constructors.ts +30 -0
  135. package/examples/result/06-utilities.ts +36 -0
  136. package/examples/result/index.ts +53 -0
  137. package/examples/result-experimental/01-map.ts +60 -0
  138. package/examples/result-experimental/02-flatMap.ts +169 -0
  139. package/examples/result-experimental/03-zip.ts +145 -0
  140. package/examples/result-experimental/04-flatZip.ts +252 -0
  141. package/examples/result-experimental/05-gen.ts +293 -0
  142. package/examples/result-experimental/06-genAdapter.ts +396 -0
  143. package/examples/result-experimental/07-asyncGen.ts +379 -0
  144. package/examples/result-experimental/08-asyncGenAdapter.ts +553 -0
  145. package/examples/result-experimental/09-workflow-simple-sync.ts +304 -0
  146. package/examples/result-experimental/10-workflow-async.ts +356 -0
  147. package/examples/result-experimental/index.ts +312 -0
  148. package/examples/result-experimental/shared-domain.ts +263 -0
  149. package/package.json +54 -31
package/dist/option.d.cts CHANGED
@@ -1,7 +1,5 @@
1
- import { UNIT } from "./unit.cjs";
2
1
  import { Result } from "./result.cjs";
3
-
4
- //#region src/option.d.ts
2
+ import { UNIT } from "./unit.cjs";
5
3
  type Mapper<T, U> = (val: T) => U;
6
4
  type AsyncMapper<T, U> = (val: T) => Promise<U>;
7
5
  type FlatMapper<T, U> = (val: T) => Option<U>;
@@ -10,90 +8,581 @@ type Predicate<T> = (val: T) => boolean;
10
8
  type FilterPredicate<T> = (val: Awaited<T>) => boolean;
11
9
  type AsyncPredicate<T> = (val: Awaited<T>) => Promise<boolean>;
12
10
  type Falsy = false | 0 | "" | null | undefined;
13
- declare class UnwrappedNone extends Error {
11
+ export declare class UnwrappedNone extends Error {
14
12
  readonly name = "UnwrapError";
15
13
  constructor();
16
14
  }
17
- type UnitOption = Option<UNIT>;
18
- type UnwrapOption<T> = T extends Option<infer R> ? R : never;
19
- type CombinedOptions<T extends Option<unknown>[]> = { [K in keyof T]: UnwrapOption<T[K]> };
15
+ /** Option alias for presence-without-payload values. */
16
+ export type UnitOption = Option<UNIT>;
17
+ /** Extracts the contained value type from an Option type. */
18
+ export type UnwrapOption<T> = T extends Option<infer R> ? R : never;
19
+ type CombinedOptions<T extends Option<unknown>[]> = { [K in keyof T]: UnwrapOption<T[K]>; };
20
20
  interface MatchCases<T, U> {
21
21
  Some: (val: T) => U;
22
22
  None: () => U;
23
23
  }
24
- declare class Option<T> {
24
+ /**
25
+ * Wrapper that makes Option yieldable with proper type tracking.
26
+ *
27
+ * @internal
28
+ */
29
+ declare class OptionYieldWrap<T> {
30
+ readonly option: Option<T>;
31
+ constructor(option: Option<T>);
32
+ [Symbol.iterator](): Generator<OptionYieldWrap<T>, T, unknown>;
33
+ }
34
+ /**
35
+ * Wrapper that makes Option yieldable in async generators with proper type tracking.
36
+ *
37
+ * @internal
38
+ */
39
+ declare class AsyncOptionYieldWrap<T> {
40
+ readonly option: Promise<Option<T>>;
41
+ constructor(option: Option<T> | Promise<Option<T>>);
42
+ [Symbol.asyncIterator](): AsyncGenerator<AsyncOptionYieldWrap<T>, Awaited<T>, unknown>;
43
+ }
44
+ /**
45
+ * Represents an optional value: `Some<T>` or `None`.
46
+ * Provides composable, null-safe transformation and chaining methods.
47
+ */
48
+ export declare class Option<T> {
25
49
  #private;
26
50
  /** Discriminant tag for type-level identification */
27
51
  readonly _tag: "Some" | "None";
52
+ /**
53
+ * Creates a new Option instance with internal context.
54
+ *
55
+ * @param val - Stored value or sentinel
56
+ * @param ctx - Internal async tracking context
57
+ * @param tag - Discriminant tag for Some/None
58
+ *
59
+ * @internal
60
+ */
28
61
  private constructor();
29
62
  /** Singleton None instance */
30
63
  static readonly None: Option<never>;
31
- /** Create a Some containing the given value */
64
+ /**
65
+ * Creates a Some variant containing the provided value.
66
+ *
67
+ * @template Inner - Type of the wrapped value
68
+ * @param val - Value to wrap in Some
69
+ * @returns Option in the Some state
70
+ */
32
71
  static Some<Inner>(this: void, val: Inner): Option<Inner>;
33
- /** Create Option from nullable value - returns None for null/undefined */
72
+ /**
73
+ * Creates an Option from a nullable value, returning None for null/undefined.
74
+ *
75
+ * @template T - Input type that may include null/undefined
76
+ * @param val - Value to convert
77
+ * @returns Some for non-nullish values, otherwise None
78
+ */
34
79
  static fromNullable<T>(val: T): Option<NonNullable<T>>;
35
- /** Create Option from potentially falsy value - returns None for falsy */
80
+ /**
81
+ * Creates an Option from a potentially falsy value, returning None for falsy.
82
+ *
83
+ * Treats false, 0, "", null, and undefined as absence.
84
+ *
85
+ * @template T - Input type
86
+ * @param val - Value to convert
87
+ * @returns Some for truthy values, otherwise None
88
+ */
36
89
  static fromFalsy<T>(val: T | Falsy): Option<T>;
37
- /** Create Option based on predicate result */
90
+ /**
91
+ * Creates an Option based on a predicate.
92
+ *
93
+ * @template T - Input type
94
+ * @param val - Value to test
95
+ * @param pred - Predicate to decide Some vs None
96
+ * @returns Some when predicate passes, otherwise None
97
+ */
38
98
  static fromPredicate<T>(val: T, pred: Predicate<T>): Option<T>;
39
- /** Wrap Promise<Option<T>> as Option<Promise<T>> */
99
+ /**
100
+ * Wraps a Promise<Option<T>> as Option<Promise<T>>.
101
+ *
102
+ * NOTE: the returned value starts as `Some(Promise<T>)`; final `None` is known
103
+ * only after settlement. Use `toPromise()` (or await `unwrap`/`safeUnwrap`) to
104
+ * observe settled absence.
105
+ *
106
+ * @template U - Inner value type
107
+ * @param o - Promise resolving to an Option
108
+ * @returns Option containing a Promise of the inner value
109
+ */
40
110
  static fromPromise<U>(o: Promise<Option<U>>): Option<Promise<U>>;
41
- /** Combine multiple Options - returns Some array if all are Some, else None */
111
+ /**
112
+ * Combines multiple Options into a single Option of an array.
113
+ *
114
+ * Returns Some of all values if all inputs are Some; otherwise None.
115
+ *
116
+ * @template T - Variadic tuple of Option types
117
+ * @param options - Options to combine
118
+ * @returns Some of tuple values, or None if any input is None
119
+ */
42
120
  static all<T extends Option<unknown>[]>(...options: T): Option<CombinedOptions<T>>;
43
- /** Returns first Some, or None if all are None */
121
+ /**
122
+ * Returns the first Some in the list, or None if all are None.
123
+ *
124
+ * Awaits candidates in input order, without waiting for the suffix after success.
125
+ *
126
+ * @template T - Value type
127
+ * @param options - Options to search
128
+ * @returns The first Some found, otherwise None
129
+ */
44
130
  static any<T>(...options: Option<T>[]): Option<T>;
45
- /** Type guard for Some state */
131
+ /**
132
+ * Type guard that narrows to Some.
133
+ *
134
+ * @returns true if this Option is Some
135
+ */
46
136
  isSome(): this is Option<T> & {
47
137
  readonly _tag: "Some";
48
138
  };
49
- /** Type guard for None state */
50
- isNone(): this is Option<never> & {
139
+ /**
140
+ * Type guard that narrows to None.
141
+ *
142
+ * Includes async None tracking while preserving the stored payload type.
143
+ *
144
+ * @returns true if this Option is None
145
+ */
146
+ isNone(): this is Option<T> & {
51
147
  readonly _tag: "None";
52
148
  };
53
- /** Type guard for Unit value */
149
+ /**
150
+ * Type guard for the UNIT sentinel value.
151
+ *
152
+ * @returns true if the contained value is UNIT
153
+ */
54
154
  isUnit(): this is Option<UNIT>;
55
- /** Returns value or throws UnwrapError */
56
- unwrap(): T;
57
- /** Returns value or the provided default */
58
- unwrapOr(defaultValue: T): T;
59
- /** Returns value or calls factory to get default */
60
- unwrapOrElse(fn: () => T): T;
61
- /** Returns value or null for None */
62
- safeUnwrap(): T | null;
63
- /** Pattern match on Option state */
155
+ /**
156
+ * Returns the contained value or throws if None.
157
+ *
158
+ * @throws UnwrappedNone when called on None
159
+ * @returns The contained value
160
+ */
161
+ unwrap(this: Option<never>): never;
162
+ unwrap(this: Option<Promise<T>>): Promise<T>;
163
+ unwrap(this: Option<T>): T;
164
+ /**
165
+ * Returns the contained value or the provided default.
166
+ * Promise-backed options wait for settlement before choosing the default.
167
+ * @param defaultValue - Value to use when None
168
+ * @returns The contained value or default
169
+ */
170
+ unwrapOr<U>(this: Option<never>, defaultValue: U): U;
171
+ unwrapOr<Curr>(this: Option<Promise<Curr>>, defaultValue: Promise<NoInfer<Awaited<Curr>>>): Promise<Awaited<Curr>>;
172
+ unwrapOr<Curr>(this: Option<Promise<Curr>>, defaultValue: NoInfer<Awaited<Curr>> | Promise<NoInfer<Awaited<Curr>>>): Awaited<Curr> | Promise<Awaited<Curr>>;
173
+ unwrapOr(this: Option<T>, defaultValue: T): T;
174
+ /**
175
+ * Returns the contained value or computes a default lazily.
176
+ * Promise-backed options invoke the factory only after settled absence.
177
+ * @param fn - Default factory invoked only when None
178
+ * @returns The contained value or computed default
179
+ */
180
+ unwrapOrElse<U>(this: Option<never>, fn: () => U): U;
181
+ unwrapOrElse<Curr>(this: Option<Promise<Curr>>, fn: () => Promise<NoInfer<Awaited<Curr>>>): Promise<Awaited<Curr>>;
182
+ unwrapOrElse<Curr>(this: Option<Promise<Curr>>, fn: () => NoInfer<Awaited<Curr>> | Promise<NoInfer<Awaited<Curr>>>): Awaited<Curr> | Promise<Awaited<Curr>>;
183
+ unwrapOrElse(this: Option<T>, fn: () => T): T;
184
+ /**
185
+ * Returns the contained value or null for None.
186
+ *
187
+ * @returns The contained value, or null when None
188
+ */
189
+ safeUnwrap(this: Option<never>): null;
190
+ safeUnwrap(this: Option<Promise<T>>): Promise<T | null>;
191
+ safeUnwrap(this: Option<T>): T | null;
192
+ /**
193
+ * Exhaustive pattern match on Option state.
194
+ *
195
+ * @template U - Result type
196
+ * @param cases - Handlers for Some and None
197
+ * @returns Result of the matching handler
198
+ */
64
199
  match<U>(cases: MatchCases<T, U>): U;
200
+ /**
201
+ * Transforms the contained value and wraps it in a new Option.
202
+ *
203
+ * If None, returns None without calling the mapper. Supports async values
204
+ * and async mappers; returns Option<Promise<U>> when async is involved.
205
+ *
206
+ * @template U - Result value type
207
+ * @param mapper - Function to transform the contained value
208
+ * @returns Option of the mapped value (sync or async)
209
+ *
210
+ * @see flatMap
211
+ */
65
212
  map<U, Curr = Awaited<T>>(this: Option<Promise<Curr>>, mapper: AsyncMapper<Curr, U>): Option<Promise<U>>;
66
213
  map<U, Curr = Awaited<T>>(this: Option<Promise<Curr>>, mapper: Mapper<Curr, U>): Option<Promise<U>>;
67
214
  map<U>(this: Option<T>, mapper: AsyncMapper<T, U>): Option<Promise<U>>;
68
215
  map<U>(this: Option<T>, mapper: Mapper<T, U>): Option<U>;
216
+ /**
217
+ * Safely maps a promised value while tracking None in async context.
218
+ *
219
+ * @template Curr - Input value type
220
+ * @template U - Output value type
221
+ * @param p - Promise to map over
222
+ * @param mapper - Transform function
223
+ * @param ctx - Async context for None tracking
224
+ * @returns Promise of the transformed value or sentinel
225
+ */
69
226
  private static safeMap;
227
+ /**
228
+ * Maps the value or returns a default (unwrapped).
229
+ *
230
+ * If None, returns the default without calling fn. Supports async values
231
+ * and async mappers; returns Promise<U> when async is involved.
232
+ *
233
+ * @template U - Result type
234
+ * @param defaultVal - Value to return if None
235
+ * @param fn - Mapper applied when Some
236
+ * @returns Mapped value or default (sync or async)
237
+ */
70
238
  mapOr<U, Curr = Awaited<T>>(this: Option<Promise<Curr>>, defaultVal: U, fn: Mapper<Curr, U>): Promise<U>;
71
239
  mapOr<U, Curr = Awaited<T>>(this: Option<Promise<Curr>>, defaultVal: U, fn: AsyncMapper<Curr, U>): Promise<U>;
240
+ mapOr<U>(this: Option<T>, defaultVal: U, fn: AsyncMapper<T, U>): Promise<U>;
72
241
  mapOr<U>(this: Option<T>, defaultVal: U, fn: Mapper<T, U>): U;
73
- flatMap<U, Curr = Awaited<T>>(this: Option<Promise<Curr>>, mapper: AsyncFlatMapper<Curr, U>): Option<Promise<U>>;
74
- flatMap<U, Curr = Awaited<T>>(this: Option<Promise<Curr>>, mapper: FlatMapper<Curr, U>): Option<Promise<U>>;
242
+ /**
243
+ * Chains operations that return Option, flattening nested Options.
244
+ *
245
+ * If None, returns None without calling the mapper. Supports async values
246
+ * and async mappers; returns Option<Promise<U>> when async is involved.
247
+ * Mapper throws propagate synchronously for sync inputs and reject the
248
+ * returned promise chain for promise inputs; they do not become None.
249
+ *
250
+ * @template U - Result value type
251
+ * @param mapper - Function returning an Option
252
+ * @returns Flattened Option result (sync or async)
253
+ *
254
+ * @see map
255
+ */
256
+ flatMap<U, Curr = Awaited<T>>(this: Option<Promise<Curr>>, mapper: AsyncFlatMapper<Curr, U>): Option<Promise<Awaited<U>>>;
257
+ flatMap<U, Curr = Awaited<T>>(this: Option<Promise<Curr>>, mapper: FlatMapper<Curr, U>): Option<Promise<Awaited<U>>>;
75
258
  flatMap<U>(this: Option<T>, mapper: AsyncFlatMapper<T, U>): Option<Promise<U>>;
76
259
  flatMap<U>(this: Option<T>, mapper: FlatMapper<T, U>): Option<U>;
260
+ /**
261
+ * Filters the contained value by a predicate.
262
+ *
263
+ * If Some, returns Some when predicate passes, otherwise None. Supports
264
+ * async values and async predicates; returns Option<Promise<T>> when async.
265
+ *
266
+ * @param pred - Predicate function
267
+ * @returns Option filtered by the predicate (sync or async)
268
+ */
77
269
  filter(pred: FilterPredicate<T>): Option<T>;
78
270
  filter(pred: AsyncPredicate<T>): Option<Promise<T>>;
271
+ /**
272
+ * Pairs the original value with a derived value in a tuple.
273
+ *
274
+ * If None, returns None. Supports async values and async derivation; returns
275
+ * Option<Promise<[Curr, Awaited<U>]>> when async is involved.
276
+ *
277
+ * @template U - Derived value type
278
+ * @param fn - Function to derive a value from the contained value
279
+ * @returns Option of tuple with original and derived values
280
+ */
79
281
  zip<U, Curr>(this: Option<Promise<Curr>>, fn: (val: Curr) => U): Option<Promise<[Curr, Awaited<U>]>>;
80
282
  zip<U, Curr>(this: Option<Curr>, fn: (val: Curr) => Promise<U>): Option<Promise<[Curr, Awaited<U>]>>;
81
283
  zip<U, Curr>(this: Option<Curr>, fn: (val: Curr) => U): Option<[Curr, U]>;
284
+ /**
285
+ * Pairs the original value with a derived Option value, flattening the result.
286
+ *
287
+ * If None, returns None. Supports async values and async Option derivation;
288
+ * returns Option<Promise<[Curr, U]>> when async is involved.
289
+ *
290
+ * @template U - Derived Option value type
291
+ * @param fn - Function returning an Option
292
+ * @returns Option of tuple with original and derived values
293
+ */
82
294
  flatZip<U, Curr>(this: Option<Promise<Curr>>, fn: (val: Curr) => Promise<Option<U>>): Option<Promise<[Curr, U]>>;
83
295
  flatZip<U, Curr>(this: Option<Promise<Curr>>, fn: (val: Curr) => Option<U>): Option<Promise<[Curr, U]>>;
84
- flatZip<U, Curr>(this: Option<Curr>, fn: (val: Curr) => Promise<Option<U>>): Option<Promise<[T, U]>>;
85
- flatZip<U, Curr>(this: Option<Curr>, fn: (val: Curr) => Option<U>): Option<[T, U]>;
86
- /** Execute side effect for Some, return self */
296
+ flatZip<U, Curr>(this: Option<Curr>, fn: (val: Curr) => Promise<Option<U>>): Option<Promise<[Curr, U]>>;
297
+ flatZip<U, Curr>(this: Option<Curr>, fn: (val: Curr) => Option<U>): Option<[Curr, U]>;
298
+ /**
299
+ * Executes a side effect for Some values and returns self.
300
+ *
301
+ * For `Option<Promise<T>>`, `tap` receives the wrapped promise object,
302
+ * not the settled inner value.
303
+ *
304
+ * @param fn - Side effect function
305
+ * @returns The same Option (chainable)
306
+ */
87
307
  tap(fn: (val: T) => void): Option<T>;
88
- /** Convert Option to Result */
308
+ /**
309
+ * Converts this Option to a Result, using the provided error for None.
310
+ * Promise-backed Options preserve settled absence and propagate rejections.
311
+ *
312
+ * @template E - Error type
313
+ * @param error - Error value for None
314
+ * @returns Ok with value when Some, otherwise Err with error
315
+ */
89
316
  toResult<E>(error: E): Result<T, E>;
90
- /** Resolve inner Promise and maintain Option structure */
317
+ /**
318
+ * Resolves the inner promise (if any) while preserving Option structure.
319
+ *
320
+ * @template Curr - Awaited value type
321
+ * @returns Promise of Option with awaited value, or None
322
+ */
91
323
  toPromise<Curr = Awaited<T>>(): Promise<Option<Curr>>;
92
- /** Map over array elements inside Option */
324
+ /**
325
+ * Maps over array elements inside an Option<Array<T>>.
326
+ *
327
+ * @template Inner - Array element type
328
+ * @template Out - Mapped element type
329
+ * @param mapper - Function to map each element
330
+ * @returns Option containing the mapped array
331
+ * @throws TypeError if called on a non-array value
332
+ */
93
333
  innerMap<Inner, Out>(this: Option<Array<Inner>>, mapper: (val: Inner) => Out): Option<NoInfer<Out>[]>;
94
- /** String representation */
334
+ /**
335
+ * Returns a string representation of the Option.
336
+ *
337
+ * @returns "Option::None" or "Option::Some(value)"
338
+ */
95
339
  toString(): string;
340
+ /**
341
+ * Makes Option iterable for use with generator-based syntax.
342
+ *
343
+ * Yields self and returns the unwrapped value when resumed.
344
+ * Used internally by {@link Option.gen} for `yield*` syntax.
345
+ *
346
+ * @example
347
+ * ```ts
348
+ * const result = Option.gen(function* () {
349
+ * const value = yield* Option.Some(42);
350
+ * return value * 2;
351
+ * });
352
+ * // Some(84)
353
+ * ```
354
+ *
355
+ * @internal
356
+ */
357
+ [Symbol.iterator](): Generator<Option<T>, T, unknown>;
358
+ /**
359
+ * Makes Option iterable for use with async generator-based syntax.
360
+ *
361
+ * Yields self and returns the unwrapped value when resumed.
362
+ * Used internally by {@link Option.asyncGen} for `yield*` syntax.
363
+ *
364
+ * @example
365
+ * ```ts
366
+ * const result = await Option.asyncGen(async function* () {
367
+ * const value = yield* await Promise.resolve(Option.Some(42));
368
+ * return value * 2;
369
+ * });
370
+ * // Some(84)
371
+ * ```
372
+ *
373
+ * @internal
374
+ */
375
+ [Symbol.asyncIterator](): AsyncGenerator<Option<T>, Awaited<T>, unknown>;
376
+ /**
377
+ * Generator-based syntax for chaining Option operations (simplified, no adapter).
378
+ *
379
+ * Provides imperative-style code while maintaining functional Option handling.
380
+ * Use `yield*` to unwrap Options or short-circuit on `None`.
381
+ *
382
+ * Short-circuits on first `None`, returning singleton `None`. Uses iteration
383
+ * instead of recursion to avoid stack overflow on deep chains.
384
+ *
385
+ * @template T - The return type of the generator
386
+ * @param genFn - A generator function that yields Options
387
+ * @returns `Some<T>` with the generator's return value, or `None`
388
+ *
389
+ * @example
390
+ * ```ts
391
+ * // Simple sync chain
392
+ * const result = Option.gen(function* () {
393
+ * const a = yield* Option.Some(1);
394
+ * const b = yield* Option.Some(2);
395
+ * return a + b;
396
+ * });
397
+ * // Some(3)
398
+ *
399
+ * // None short-circuit
400
+ * const shortCircuit = Option.gen(function* () {
401
+ * const a = yield* Option.Some(1);
402
+ * const b = yield* Option.None; // Short-circuits here
403
+ * const c = yield* Option.Some(3); // Never executes
404
+ * return a + b + c;
405
+ * });
406
+ * // None
407
+ *
408
+ * // Chaining optional operations
409
+ * const email = Option.gen(function* () {
410
+ * const user = yield* findUser(userId);
411
+ * const profile = yield* Option.fromNullable(user.profile);
412
+ * const settings = yield* Option.fromNullable(profile.settings);
413
+ * return settings.email;
414
+ * });
415
+ *
416
+ * // Nested optional access
417
+ * const city = Option.gen(function* () {
418
+ * const user = yield* Option.fromNullable(getUser());
419
+ * const address = yield* Option.fromNullable(user?.address);
420
+ * const city = yield* Option.fromNullable(address?.city);
421
+ * return city;
422
+ * });
423
+ * ```
424
+ *
425
+ * @see {@link genAdapter} for better type inference in complex chains
426
+ * @see {@link asyncGen} for async operations
427
+ */
428
+ static gen<T>(this: void, genFn: () => Generator<Option<unknown>, T, unknown>): Option<T>;
429
+ /**
430
+ * Generator-based syntax for chaining Option operations with an adapter function.
431
+ *
432
+ * Provides better type inference and IDE support compared to {@link gen}.
433
+ * The `$` adapter wraps Options and enables clearer type tracking.
434
+ *
435
+ * Short-circuits on first `None`, returning singleton `None`. Uses iteration
436
+ * instead of recursion to avoid stack overflow on deep chains.
437
+ *
438
+ * @template Eff - The effect type (inferred from usage)
439
+ * @template T - The return type of the generator
440
+ * @param genFn - A generator function receiving an adapter `$`
441
+ * @returns `Some<T>` with the generator's return value, or `None`
442
+ *
443
+ * @example
444
+ * ```ts
445
+ * // Better type inference
446
+ * const result = Option.genAdapter(function* ($) {
447
+ * const a = yield* $(Option.Some(1));
448
+ * const b = yield* $(Option.Some(2));
449
+ * return a + b;
450
+ * });
451
+ * // Some(3)
452
+ *
453
+ * // Deep nested access with better type safety
454
+ * const email = Option.genAdapter(function* ($) {
455
+ * const user = yield* $(Option.fromNullable(apiResponse?.user));
456
+ * const profile = yield* $(Option.fromNullable(user?.profile));
457
+ * const contact = yield* $(Option.fromNullable(profile?.contact));
458
+ * return contact?.email;
459
+ * });
460
+ *
461
+ * // Complex validation chain
462
+ * const validConfig = Option.genAdapter(function* ($) {
463
+ * const raw = yield* $(loadConfig());
464
+ * const parsed = yield* $(parseConfig(raw));
465
+ * const validated = yield* $(validateConfig(parsed));
466
+ * return validated;
467
+ * });
468
+ * ```
469
+ *
470
+ * @see {@link gen} for simpler chains without adapter
471
+ * @see {@link asyncGenAdapter} for async operations
472
+ */
473
+ static genAdapter<Eff extends OptionYieldWrap<any>, T>(this: void, genFn: (adapter: <A>(option: Option<A>) => OptionYieldWrap<A>) => Generator<Eff, T, any>): Option<T>;
474
+ /**
475
+ * Async generator-based syntax for chaining Option operations (simplified, no adapter).
476
+ *
477
+ * Use `yield*` with `Option<T>` values directly. For `Promise<Option<T>>`,
478
+ * await first then `yield*`.
479
+ *
480
+ * Short-circuits on first `None`, returning singleton `None`. Uses async iteration
481
+ * instead of recursion to avoid stack overflow on deep chains.
482
+ *
483
+ * @template T - The return type of the generator
484
+ * @param genFn - An async generator function that yields Options
485
+ * @returns `Promise<Some<T>>` with the generator's return value, or `Promise<None>`
486
+ *
487
+ * @example
488
+ * ```ts
489
+ * // Simple async chain
490
+ * const result = await Option.asyncGen(async function* () {
491
+ * const a = yield* Option.Some(1);
492
+ * const b = yield* await asyncOperation(a); // await Promise<Option> first
493
+ * const c = yield* Option.Some(3);
494
+ * return a + b + c;
495
+ * });
496
+ * // Some(result)
497
+ *
498
+ * // None short-circuit in async
499
+ * const shortCircuit = await Option.asyncGen(async function* () {
500
+ * const data = yield* await fetchOptionalData();
501
+ * const parsed = yield* parse(data); // Short-circuits on None
502
+ * const validated = yield* validate(parsed); // Never executes
503
+ * return validated;
504
+ * });
505
+ * // None
506
+ *
507
+ * // Mixed sync/async workflow
508
+ * const result = await Option.asyncGen(async function* () {
509
+ * const id = yield* Option.Some(parseInt(input)); // sync
510
+ * const user = yield* await fetchUser(id); // async
511
+ * const profile = yield* Option.fromNullable(user?.profile); // sync
512
+ * const enriched = yield* await enrichProfile(profile); // async
513
+ * return enriched;
514
+ * });
515
+ *
516
+ * // Complex pipeline with multiple optional steps
517
+ * async function processLead(email: string): Promise<Option<Lead>> {
518
+ * return await Option.asyncGen(async function* () {
519
+ * const normalized = yield* Option.Some(normalizeEmail(email));
520
+ * const existing = yield* await findExistingLead(normalized);
521
+ * const enriched = yield* await enrichLeadData(existing);
522
+ * const validated = yield* validateLead(enriched);
523
+ * return validated;
524
+ * });
525
+ * }
526
+ * ```
527
+ *
528
+ * Settles promise-backed Options before checking for None; raw rejections propagate.
529
+ * @see {@link asyncGenAdapter} for better type inference and cleaner syntax
530
+ * @see {@link gen} for synchronous operations
531
+ */
532
+ static asyncGen<T>(this: void, genFn: () => AsyncGenerator<Option<unknown>, T, unknown>): Promise<Option<T>>;
533
+ /**
534
+ * Async generator-based syntax for chaining Option operations with an adapter function.
535
+ *
536
+ * The `$` adapter handles both sync `Option<T>` and async `Promise<Option<T>>`,
537
+ * automatically awaiting promises. This provides cleaner syntax for mixed sync/async workflows.
538
+ * Settles promise-backed Options before checking for None; raw rejections propagate.
539
+ *
540
+ * Short-circuits on first `None`, returning singleton `None`. Uses async iteration
541
+ * instead of recursion to avoid stack overflow on deep chains.
542
+ *
543
+ * @template Eff - The effect type (inferred from usage)
544
+ * @template T - The return type of the generator
545
+ * @param genFn - An async generator function receiving an adapter `$`
546
+ * @returns `Promise<Some<T>>` with the generator's return value, or `Promise<None>`
547
+ *
548
+ * @example
549
+ * ```ts
550
+ * // No need to manually await - adapter handles it
551
+ * const result = await Option.asyncGenAdapter(async function* ($) {
552
+ * const a = yield* $(Option.Some(1)); // sync Option
553
+ * const b = yield* $(asyncOperation(a)); // Promise<Option> - auto-awaited
554
+ * const c = yield* $(Option.Some(3));
555
+ * return a + b + c;
556
+ * });
557
+ *
558
+ * // Complex workflow with database and API calls
559
+ * const userData = await Option.asyncGenAdapter(async function* ($) {
560
+ * const session = yield* $(getSession()); // Promise<Option<Session>>
561
+ * const userId = yield* $(Option.fromNullable(session?.userId));
562
+ * const profile = yield* $(await fetchProfile(userId)); // Promise<Option<Profile>>
563
+ * const preferences = yield* $(Option.fromNullable(profile?.preferences));
564
+ * const enriched = yield* $(await enrichPreferences(preferences));
565
+ * return enriched;
566
+ * });
567
+ *
568
+ * // Real-world: Optional data enrichment
569
+ * async function getProductDetails(productId: string): Promise<Option<ProductDetails>> {
570
+ * return await Option.asyncGenAdapter(async function* ($) {
571
+ * const product = yield* $(await fetchProduct(productId));
572
+ * const pricing = yield* $(await fetchPricing(product.sku));
573
+ * const inventory = yield* $(await checkInventory(product.id));
574
+ * const reviews = yield* $(await fetchReviews(product.id));
575
+ * const related = yield* $(await fetchRelatedProducts(product.category));
576
+ *
577
+ * return { product, pricing, inventory, reviews, related };
578
+ * });
579
+ * }
580
+ * ```
581
+ *
582
+ * @see {@link asyncGen} when you want explicit `await` for async operations
583
+ * @see {@link genAdapter} for synchronous operations
584
+ */
585
+ static asyncGenAdapter<Eff extends AsyncOptionYieldWrap<any>, T>(this: void, genFn: (adapter: <A>(option: Option<A> | Promise<Option<A>>) => AsyncOptionYieldWrap<A>) => AsyncGenerator<Eff, T, any>): Promise<Option<T>>;
96
586
  }
97
- //#endregion
98
- export { Option, UnitOption, UnwrapOption, UnwrappedNone };
587
+ export {};
99
588
  //# sourceMappingURL=option.d.cts.map