@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/match.d.cts CHANGED
@@ -1,15 +1,474 @@
1
- import { Result } from "./result.cjs";
2
- import { Option } from "./option.cjs";
3
-
4
- //#region src/match.d.ts
5
- declare const matchRes: <T, E, U>(r: Result<T, E>, branches: {
1
+ import { Option as Option$1 } from "./option.cjs";
2
+ import { ExperimentalOption } from "./option-experimental.cjs";
3
+ import { Result as Result$1 } from "./result.cjs";
4
+ import { ExperimentalResult } from "./result-experimental.cjs";
5
+ type Result<T, E> = ExperimentalResult<T, E>;
6
+ type Option<T> = ExperimentalOption<T>;
7
+ type LegacyResult<T, E> = Result$1<T, E> | ExperimentalResult<T, E>;
8
+ type LegacyOption<T> = Option$1<T> | ExperimentalOption<T>;
9
+ type MatchableValue = {
10
+ readonly _tag: string;
11
+ };
12
+ /**
13
+ * Extract the `_tag` values from a discriminated union.
14
+ */
15
+ type GetTags<T> = T extends {
16
+ readonly _tag: infer Tag extends string;
17
+ } ? Tag : never;
18
+ /**
19
+ * Extract the variant of T that has the given tag.
20
+ */
21
+ type ExtractByTag<T, Tag> = T extends {
22
+ readonly _tag: Tag;
23
+ } ? T : never;
24
+ type IsExactlyUnknown<T> = unknown extends T ? [T] extends [unknown] ? true : false : false;
25
+ type IsPromiseLike<T> = T extends PromiseLike<unknown> ? true : false;
26
+ /**
27
+ * Extract the inner type from a discriminated union based on tag.
28
+ *
29
+ * For Option/Result-like variants, extracts via `unwrap`/`unwrapErr`.
30
+ * For other discriminated unions, falls back to the narrowed variant itself.
31
+ */
32
+ type GetInnerType<T, Tag extends string> = Tag extends "Some" ? T extends Option<infer V> ? V : T extends Option$1<infer V> ? V : unknown : Tag extends "Ok" ? T extends Result<infer V, unknown> ? V : T extends Result$1<infer V, unknown> ? V : unknown : Tag extends "Err" ? T extends Result<unknown, infer E> ? E : T extends Result$1<unknown, infer E> ? E : unknown : ExtractByTag<T, Tag> extends {
33
+ unwrap(): infer V;
34
+ } ? V : unknown;
35
+ type MatchInput<T, Tag extends string> = Tag extends "Some" ? T extends Option<infer V> ? V : T extends Option$1<infer V> ? V : ExtractByTag<T, Tag> : Tag extends "Ok" ? T extends Result<infer V, unknown> ? V : T extends Result$1<infer V, unknown> ? V : ExtractByTag<T, Tag> : Tag extends "Err" ? T extends Result<unknown, infer E> ? E : T extends Result$1<unknown, infer E> ? E : ExtractByTag<T, Tag> : ExtractByTag<T, Tag>;
36
+ type HasPromiseLikePayloadForTag<T, Tag extends string> = Tag extends GetTags<T> ? IsExactlyUnknown<GetInnerType<T, Tag>> extends true ? false : IsPromiseLike<GetInnerType<T, Tag>> : false;
37
+ type HasPromiseLikePayload<T> = true extends HasPromiseLikePayloadForTag<T, "Some"> | HasPromiseLikePayloadForTag<T, "Ok"> | HasPromiseLikePayloadForTag<T, "Err"> ? true : false;
38
+ type SyncMatchable<T extends MatchableValue> = HasPromiseLikePayload<T> extends true ? never : T;
39
+ declare function predicateAnd<T>(...predicates: Array<(value: T) => boolean>): (value: T) => boolean;
40
+ declare function predicateOr<T>(...predicates: Array<(value: T) => boolean>): (value: T) => boolean;
41
+ /**
42
+ * Represents a compile-time exhaustiveness error.
43
+ * When pattern matching is incomplete, this type surfaces the missing cases.
44
+ */
45
+ type ExhaustiveError<Missing extends string> = {
46
+ readonly _tag: "ExhaustiveError";
47
+ readonly error: "Pattern matching is not exhaustive";
48
+ readonly missingCases: Missing;
49
+ };
50
+ /** Base pattern interface */
51
+ interface BasePattern<Tag extends string> {
52
+ readonly _patternTag: Tag;
53
+ }
54
+ /** Pattern for matching Option.Some with optional predicate guard */
55
+ interface SomePattern<T = unknown> extends BasePattern<"Some"> {
56
+ readonly predicate?: (val: T) => boolean;
57
+ readonly _hasPredicate: boolean;
58
+ }
59
+ /** Pattern for matching Option.None */
60
+ interface NonePattern extends BasePattern<"None"> {
61
+ readonly _hasPredicate: false;
62
+ }
63
+ /** Pattern for matching Result.Ok with optional predicate guard */
64
+ interface OkPattern<T = unknown> extends BasePattern<"Ok"> {
65
+ readonly predicate?: (val: T) => boolean;
66
+ readonly _hasPredicate: boolean;
67
+ }
68
+ /** Pattern for matching Result.Err with optional predicate guard */
69
+ interface ErrPattern<E = unknown> extends BasePattern<"Err"> {
70
+ readonly predicate?: (err: E) => boolean;
71
+ readonly _hasPredicate: boolean;
72
+ }
73
+ /** Wildcard pattern that matches anything */
74
+ interface WildcardPattern extends BasePattern<"_"> {}
75
+ /** Union of all pattern types */
76
+ type Pattern<T = unknown, E = unknown> = SomePattern<T> | NonePattern | OkPattern<T> | ErrPattern<E> | WildcardPattern;
77
+ /** Pattern for Some without predicate - marks tag as fully consumed */
78
+ interface SomePatternNoPredicate<_T = unknown> extends BasePattern<"Some"> {
79
+ readonly predicate: undefined;
80
+ readonly _hasPredicate: false;
81
+ }
82
+ /** Pattern for Some with predicate - does NOT consume tag */
83
+ interface SomePatternWithPredicate<T = unknown> extends BasePattern<"Some"> {
84
+ readonly predicate: (val: T) => boolean;
85
+ readonly _hasPredicate: true;
86
+ }
87
+ /** Pattern for Ok without predicate - marks tag as fully consumed */
88
+ interface OkPatternNoPredicate<_T = unknown> extends BasePattern<"Ok"> {
89
+ readonly predicate: undefined;
90
+ readonly _hasPredicate: false;
91
+ }
92
+ /** Pattern for Ok with predicate - does NOT consume tag */
93
+ interface OkPatternWithPredicate<T = unknown> extends BasePattern<"Ok"> {
94
+ readonly predicate: (val: T) => boolean;
95
+ readonly _hasPredicate: true;
96
+ }
97
+ /** Pattern for Err without predicate - marks tag as fully consumed */
98
+ interface ErrPatternNoPredicate<_E = unknown> extends BasePattern<"Err"> {
99
+ readonly predicate: undefined;
100
+ readonly _hasPredicate: false;
101
+ }
102
+ /** Pattern for Err with predicate - does NOT consume tag */
103
+ interface ErrPatternWithPredicate<E = unknown> extends BasePattern<"Err"> {
104
+ readonly predicate: (err: E) => boolean;
105
+ readonly _hasPredicate: true;
106
+ }
107
+ /**
108
+ * Pattern constructors for type-safe pattern matching.
109
+ *
110
+ * @example
111
+ * ```ts
112
+ * import { match, P } from "@carbonteq/fp";
113
+ *
114
+ * // Basic patterns
115
+ * match(option)
116
+ * .with(P.Some(), (v) => v * 2)
117
+ * .with(P.None(), () => 0)
118
+ * .exhaustive();
119
+ *
120
+ * // With predicate guards
121
+ * match(option)
122
+ * .with(P.Some((x) => x > 10), (v) => "big")
123
+ * .with(P.Some(), (v) => "small")
124
+ * .with(P.None(), () => "none")
125
+ * .exhaustive();
126
+ * ```
127
+ */
128
+ export declare const P: {
129
+ /**
130
+ * Predicate helper that checks if a value is in the `Some` state.
131
+ *
132
+ * @example
133
+ * ```ts
134
+ * match(option)
135
+ * .when(P.IsSome, () => "has value")
136
+ * .otherwise(() => "none");
137
+ * ```
138
+ */
139
+ readonly IsSome: (value: MatchableValue) => boolean;
140
+ /**
141
+ * Predicate helper that checks if a value is in the `None` state.
142
+ */
143
+ readonly IsNone: (value: MatchableValue) => boolean;
144
+ /**
145
+ * Predicate helper that checks if a value is in the `Ok` state.
146
+ */
147
+ readonly IsOk: (value: MatchableValue) => boolean;
148
+ /**
149
+ * Predicate helper that checks if a value is in the `Err` state.
150
+ */
151
+ readonly IsErr: (value: MatchableValue) => boolean;
152
+ /**
153
+ * Predicate helper for exact equality checks.
154
+ * Uses `Object.is` semantics.
155
+ *
156
+ * @example
157
+ * ```ts
158
+ * match(result)
159
+ * .with(P.Ok(P.eq(42)), () => "exactly 42")
160
+ * .with(P.Ok(), () => "other ok")
161
+ * .with(P.Err(), () => "err")
162
+ * .exhaustive();
163
+ * ```
164
+ */
165
+ readonly eq: <T>(expected: T) => (actual: T) => boolean;
166
+ /**
167
+ * Predicate helper for membership checks against a list of values.
168
+ * Uses `Object.is` semantics.
169
+ *
170
+ * @example
171
+ * ```ts
172
+ * match(result)
173
+ * .with(P.Err(P.oneOf("timeout", "offline")), () => "retry")
174
+ * .with(P.Err(), () => "fail")
175
+ * .with(P.Ok(), () => "ok")
176
+ * .exhaustive();
177
+ * ```
178
+ */
179
+ readonly oneOf: <T>(...expected: readonly T[]) => (actual: T) => boolean;
180
+ /**
181
+ * Predicate helper that negates another predicate.
182
+ *
183
+ * @example
184
+ * ```ts
185
+ * match(result)
186
+ * .with(P.Ok(P.not(P.eq(42))), () => "not 42")
187
+ * .with(P.Ok(), () => "exactly 42")
188
+ * .with(P.Err(), () => "err")
189
+ * .exhaustive();
190
+ * ```
191
+ */
192
+ readonly not: <T>(predicate: (value: T) => boolean) => (value: T) => boolean;
193
+ /**
194
+ * Predicate helper that requires all predicates to match.
195
+ */
196
+ readonly and: typeof predicateAnd;
197
+ /**
198
+ * Alias of `P.and`.
199
+ */
200
+ readonly all: typeof predicateAnd;
201
+ /**
202
+ * Predicate helper that requires at least one predicate to match.
203
+ */
204
+ readonly or: typeof predicateOr;
205
+ /**
206
+ * Alias of `P.or`.
207
+ */
208
+ readonly any: typeof predicateOr;
209
+ /**
210
+ * Pattern that matches `Option.Some`.
211
+ * Without predicate: consumes the tag for exhaustiveness.
212
+ * With predicate: does NOT consume the tag (allows fallthrough).
213
+ */
214
+ readonly Some: {
215
+ <T = unknown>(): SomePatternNoPredicate<T>;
216
+ <T>(predicate: (val: T) => boolean): SomePatternWithPredicate<T>;
217
+ };
218
+ /**
219
+ * Pattern that matches `Option.None`.
220
+ */
221
+ readonly None: () => NonePattern;
222
+ /**
223
+ * Pattern that matches `Result.Ok`.
224
+ * Without predicate: consumes the tag for exhaustiveness.
225
+ * With predicate: does NOT consume the tag (allows fallthrough).
226
+ */
227
+ readonly Ok: {
228
+ <T = unknown>(): OkPatternNoPredicate<T>;
229
+ <T>(predicate: (val: T) => boolean): OkPatternWithPredicate<T>;
230
+ };
231
+ /**
232
+ * Pattern that matches `Result.Err`.
233
+ * Without predicate: consumes the tag for exhaustiveness.
234
+ * With predicate: does NOT consume the tag (allows fallthrough).
235
+ */
236
+ readonly Err: {
237
+ <E = unknown>(): ErrPatternNoPredicate<E>;
238
+ <E>(predicate: (err: E) => boolean): ErrPatternWithPredicate<E>;
239
+ };
240
+ /**
241
+ * Wildcard pattern that matches anything.
242
+ * Use with `otherwise()` for catch-all handling.
243
+ */
244
+ readonly _: WildcardPattern;
245
+ };
246
+ /**
247
+ * Handler function type for pattern matching.
248
+ */
249
+ type MatchHandler<T, R> = (value: T) => R;
250
+ /**
251
+ * Builder interface for fluent pattern matching.
252
+ *
253
+ * @template T - The type being matched
254
+ * @template Matched - Union of tags that have been matched
255
+ * @template Returns - Union of return types from handlers
256
+ */
257
+ interface MatchBuilder<T, Matched extends string, Returns> {
258
+ /**
259
+ * Add a pattern match case using a string tag.
260
+ *
261
+ * @example
262
+ * ```ts
263
+ * match(option)
264
+ * .with("Some", (v) => v * 2)
265
+ * .with("None", () => 0)
266
+ * .exhaustive();
267
+ * ```
268
+ */
269
+ with<Tag extends Exclude<GetTags<T>, Matched>, R>(tag: Tag, handler: MatchHandler<MatchInput<T, Tag>, R>): MatchBuilder<T, Matched | Tag, Returns | R>;
270
+ /**
271
+ * Add a pattern match case using a Pattern object without predicate.
272
+ * This variant fully consumes the tag for exhaustiveness checking.
273
+ *
274
+ * @example
275
+ * ```ts
276
+ * match(option)
277
+ * .with(P.Some(), (v) => v * 2)
278
+ * .with(P.None(), () => "none")
279
+ * .exhaustive();
280
+ * ```
281
+ */
282
+ with<Tag extends Exclude<GetTags<T>, Matched>, R>(pattern: BasePattern<Tag> & {
283
+ readonly _hasPredicate: false;
284
+ }, handler: MatchHandler<MatchInput<T, Tag>, R>): MatchBuilder<T, Matched | Tag, Returns | R>;
285
+ /**
286
+ * Add a pattern match case using a Pattern object with predicate guard.
287
+ * This variant does NOT consume the tag (allows multiple guarded patterns).
288
+ * The predicate and handler receive the same payload or narrowed tagged object.
289
+ *
290
+ * @example
291
+ * ```ts
292
+ * match(option)
293
+ * .with(P.Some((x) => x > 10), () => "big")
294
+ * .with(P.Some(), () => "small") // Still allowed
295
+ * .with(P.None(), () => "none")
296
+ * .exhaustive();
297
+ * ```
298
+ */
299
+ with<Tag extends GetTags<T>, R>(pattern: BasePattern<Tag> & {
300
+ readonly _hasPredicate: true;
301
+ readonly predicate: (value: MatchInput<T, NoInfer<Tag>>) => boolean;
302
+ }, handler: MatchHandler<MatchInput<T, Tag>, R>): MatchBuilder<T, Matched, Returns | R>;
303
+ /**
304
+ * Add a wildcard pattern case that matches any remaining value.
305
+ *
306
+ * This consumes all tags for exhaustiveness checking.
307
+ */
308
+ with<R>(pattern: WildcardPattern, handler: MatchHandler<T, R>): MatchBuilder<T, GetTags<T>, Returns | R>;
309
+ /**
310
+ * Add a predicate-based match case.
311
+ *
312
+ * NOTE: `when()` predicates are evaluated before tag/pattern `.with(...)`
313
+ * cases, regardless of declaration order.
314
+ *
315
+ * @example
316
+ * ```ts
317
+ * match(option)
318
+ * .when((v) => v.isSome() && v.unwrap() > 10, () => "big some")
319
+ * .otherwise(() => "other");
320
+ * ```
321
+ */
322
+ when<R>(predicate: (value: T) => boolean, handler: MatchHandler<T, R>): MatchBuilder<T, Matched, Returns | R>;
323
+ /**
324
+ * Finalize the match, requiring all cases to be handled.
325
+ * Returns a compile-time error type if cases are missing.
326
+ *
327
+ * @example
328
+ * ```ts
329
+ * // Compiles: all cases handled
330
+ * match(option)
331
+ * .with("Some", (v) => v)
332
+ * .with("None", () => 0)
333
+ * .exhaustive();
334
+ *
335
+ * // Type error: missing "None" case
336
+ * match(option)
337
+ * .with("Some", (v) => v)
338
+ * .exhaustive();
339
+ * ```
340
+ */
341
+ exhaustive(): [Exclude<GetTags<T>, Matched>] extends [never] ? Returns : ExhaustiveError<Exclude<GetTags<T>, Matched>>;
342
+ /**
343
+ * Finalize the match with a fallback for unhandled cases.
344
+ *
345
+ * @example
346
+ * ```ts
347
+ * match(option)
348
+ * .with("Some", (v) => v * 2)
349
+ * .otherwise(() => 0); // Handles None
350
+ * ```
351
+ */
352
+ otherwise<R>(fallback: MatchHandler<T, R>): Returns | R;
353
+ }
354
+ /**
355
+ * Error thrown when pattern matching fails at runtime.
356
+ * This should rarely occur if compile-time exhaustiveness is properly enforced.
357
+ */
358
+ export declare class UnmatchedCaseError extends Error {
359
+ readonly name = "UnmatchedCaseError";
360
+ constructor(tag: string);
361
+ }
362
+ /**
363
+ * Create a fluent pattern matcher for discriminated unions.
364
+ *
365
+ * Supports `Option`, `Result`, and any type with a `_tag` discriminant.
366
+ *
367
+ * `match()` is synchronous. It rejects Promise-like inner payloads
368
+ * (for example `Result<Promise<T>, E>` and `Option<Promise<T>>`).
369
+ * Settle first (for stable types) via `toPromise()` and then match
370
+ * the settled value.
371
+ *
372
+ * @template T - The discriminated union type to match
373
+ * @param value - The value to pattern match on
374
+ * @returns A `MatchBuilder` for fluent pattern matching
375
+ *
376
+ * @example
377
+ * ```ts
378
+ * import { match, P, Option, Result } from "@carbonteq/fp";
379
+ *
380
+ * // Option matching
381
+ * const doubled = match(Option.Some(21))
382
+ * .with("Some", (v) => v * 2)
383
+ * .with("None", () => 0)
384
+ * .exhaustive();
385
+ * // 42
386
+ *
387
+ * // Result matching
388
+ * const message = match(Result.Ok("success"))
389
+ * .with("Ok", (v) => `Got: ${v}`)
390
+ * .with("Err", (e) => `Error: ${e}`)
391
+ * .exhaustive();
392
+ * // "Got: success"
393
+ *
394
+ * // With predicate guards
395
+ * const category = match(Option.Some(75))
396
+ * .with(P.Some((x) => x >= 90), () => "A")
397
+ * .with(P.Some((x) => x >= 80), () => "B")
398
+ * .with(P.Some((x) => x >= 70), () => "C")
399
+ * .with(P.Some(), () => "F")
400
+ * .with(P.None(), () => "N/A")
401
+ * .exhaustive();
402
+ * // "C"
403
+ *
404
+ * // With otherwise fallback
405
+ * const safe = match(option)
406
+ * .with("Some", (v) => v)
407
+ * .otherwise(() => defaultValue);
408
+ *
409
+ * // Generic discriminated unions
410
+ * type Shape =
411
+ * | { _tag: "circle"; radius: number }
412
+ * | { _tag: "rect"; width: number; height: number };
413
+ *
414
+ * const area = match(shape)
415
+ * .with("circle", (s) => Math.PI * s.radius ** 2)
416
+ * .with("rect", (s) => s.width * s.height)
417
+ * .exhaustive();
418
+ * ```
419
+ */
420
+ export declare function match<T extends MatchableValue>(value: SyncMatchable<T>): MatchBuilder<T, never, never>;
421
+ /**
422
+ * Pattern match on a Result using an object with Ok and Err handlers.
423
+ *
424
+ * @deprecated Use `result.match()` instance method or `match(result)` builder instead.
425
+ *
426
+ * @example
427
+ * ```ts
428
+ * // Deprecated:
429
+ * matchRes(result, { Ok: ..., Err: ... });
430
+ *
431
+ * // Preferred:
432
+ * result.match({ Ok: ..., Err: ... });
433
+ * // or
434
+ * match(result).with("Ok", ...).with("Err", ...).exhaustive();
435
+ * ```
436
+ */
437
+ export declare const matchRes: <T, E, U>(r: LegacyResult<T, E>, branches: {
6
438
  Ok: (val: T) => U;
7
439
  Err: (err: E) => U;
8
440
  }) => U;
9
- declare const matchOpt: <T, U>(o: Option<T>, branches: {
441
+ /**
442
+ * Pattern match on an Option using an object with Some and None handlers.
443
+ *
444
+ * @deprecated Use `option.match()` instance method or `match(option)` builder instead.
445
+ *
446
+ * @example
447
+ * ```ts
448
+ * // Deprecated:
449
+ * matchOpt(option, { Some: ..., None: ... });
450
+ *
451
+ * // Preferred:
452
+ * option.match({ Some: ..., None: ... });
453
+ * // or
454
+ * match(option).with("Some", ...).with("None", ...).exhaustive();
455
+ * ```
456
+ */
457
+ export declare const matchOpt: <T, U>(o: LegacyOption<T>, branches: {
10
458
  Some: (val: T) => U;
11
459
  None: () => U;
12
460
  }) => U;
13
- //#endregion
14
- export { matchOpt, matchRes };
461
+ /**
462
+ * Pattern match on a Result (alias for matchRes).
463
+ *
464
+ * @deprecated Use `result.match()` instance method or `match(result)` builder instead.
465
+ */
466
+ export declare const matchResult: typeof matchRes;
467
+ /**
468
+ * Pattern match on an Option (alias for matchOpt).
469
+ *
470
+ * @deprecated Use `option.match()` instance method or `match(option)` builder instead.
471
+ */
472
+ export declare const matchOption: typeof matchOpt;
473
+ export type { ErrPattern, ErrPatternNoPredicate, ErrPatternWithPredicate, ExhaustiveError, MatchBuilder, NonePattern, OkPattern, OkPatternNoPredicate, OkPatternWithPredicate, Pattern, SomePattern, SomePatternNoPredicate, SomePatternWithPredicate, WildcardPattern };
15
474
  //# sourceMappingURL=match.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"match.d.cts","names":[],"sources":["../src/match.ts"],"sourcesContent":[],"mappings":";;;;cAGa,uBACR,OAAO,GAAG;YACS,MAAM;EAFjB,GAAA,EAAA,CAAA,GAAA,EAE+B,CAG3C,EAAA,GAHiD,CAGjD;CAJW,EAAA,GAET,CAFS;AAAG,cAMF,QANE,EAAA,CAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,EAOV,MAPU,CAOH,CAPG,CAAA,EAAA,QAAA,EAAA;EAAV,IAAA,EAAA,CAAA,GAAA,EAQqB,CARrB,EAAA,GAQ2B,CAR3B;EACmB,IAAA,EAAA,GAAA,GAOuB,CAPvB;CAAM,EAAA,GAQ3B,CAR2B"}
1
+ {"version":3,"file":"match.d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