@carbonteq/fp 0.8.2 → 0.10.0

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 (195) hide show
  1. package/README.md +718 -95
  2. package/dist/errors.cjs +20 -0
  3. package/dist/errors.cjs.map +1 -0
  4. package/dist/errors.d.cts +10 -0
  5. package/dist/errors.d.cts.map +1 -0
  6. package/dist/errors.d.mts +10 -0
  7. package/dist/errors.d.mts.map +1 -0
  8. package/dist/errors.mjs +17 -0
  9. package/dist/errors.mjs.map +1 -0
  10. package/dist/flow-experimental.cjs +286 -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 +284 -0
  17. package/dist/flow-experimental.mjs.map +1 -0
  18. package/dist/flow.cjs +296 -0
  19. package/dist/flow.cjs.map +1 -0
  20. package/dist/flow.d.cts +79 -0
  21. package/dist/flow.d.cts.map +1 -0
  22. package/dist/flow.d.mts +79 -0
  23. package/dist/flow.d.mts.map +1 -0
  24. package/dist/flow.mjs +294 -0
  25. package/dist/flow.mjs.map +1 -0
  26. package/dist/index.cjs +28 -7
  27. package/dist/index.d.cts +11 -5
  28. package/dist/index.d.mts +11 -0
  29. package/dist/index.mjs +12 -0
  30. package/dist/match.cjs +294 -3
  31. package/dist/match.cjs.map +1 -0
  32. package/dist/match.d.cts +481 -5
  33. package/dist/match.d.cts.map +1 -0
  34. package/dist/match.d.mts +481 -0
  35. package/dist/match.d.mts.map +1 -0
  36. package/dist/match.mjs +287 -0
  37. package/dist/match.mjs.map +1 -0
  38. package/dist/option-experimental.cjs +1503 -0
  39. package/dist/option-experimental.cjs.map +1 -0
  40. package/dist/option-experimental.d.cts +1383 -0
  41. package/dist/option-experimental.d.cts.map +1 -0
  42. package/dist/option-experimental.d.mts +1383 -0
  43. package/dist/option-experimental.d.mts.map +1 -0
  44. package/dist/option-experimental.mjs +1501 -0
  45. package/dist/option-experimental.mjs.map +1 -0
  46. package/dist/option.cjs +806 -4
  47. package/dist/option.cjs.map +1 -0
  48. package/dist/option.d.cts +580 -3
  49. package/dist/option.d.cts.map +1 -0
  50. package/dist/option.d.mts +580 -0
  51. package/dist/option.d.mts.map +1 -0
  52. package/dist/option.mjs +805 -0
  53. package/dist/option.mjs.map +1 -0
  54. package/dist/result-experimental.cjs +1266 -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 +1265 -0
  61. package/dist/result-experimental.mjs.map +1 -0
  62. package/dist/result.cjs +1077 -5
  63. package/dist/result.cjs.map +1 -0
  64. package/dist/result.d.cts +833 -3
  65. package/dist/result.d.cts.map +1 -0
  66. package/dist/result.d.mts +833 -0
  67. package/dist/result.d.mts.map +1 -0
  68. package/dist/result.mjs +1077 -0
  69. package/dist/result.mjs.map +1 -0
  70. package/dist/tuple.cjs +3 -0
  71. package/dist/tuple.cjs.map +1 -1
  72. package/dist/tuple.d.cts +5 -2
  73. package/dist/tuple.d.cts.map +1 -1
  74. package/dist/tuple.d.mts +15 -0
  75. package/dist/tuple.d.mts.map +1 -0
  76. package/dist/{tuple.js → tuple.mjs} +3 -1
  77. package/dist/tuple.mjs.map +1 -0
  78. package/dist/unit.cjs +8 -2
  79. package/dist/unit.cjs.map +1 -0
  80. package/dist/unit.d.cts +8 -2
  81. package/dist/unit.d.cts.map +1 -0
  82. package/dist/unit.d.mts +8 -0
  83. package/dist/unit.d.mts.map +1 -0
  84. package/dist/unit.mjs +7 -0
  85. package/dist/unit.mjs.map +1 -0
  86. package/dist/utils.cjs +24 -0
  87. package/dist/utils.cjs.map +1 -0
  88. package/dist/utils.d.cts +14 -0
  89. package/dist/utils.d.cts.map +1 -0
  90. package/dist/utils.d.mts +14 -0
  91. package/dist/utils.d.mts.map +1 -0
  92. package/dist/utils.mjs +20 -0
  93. package/dist/utils.mjs.map +1 -0
  94. package/examples/README.md +46 -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 +237 -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 +241 -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 +147 -0
  108. package/examples/match/index.ts +19 -0
  109. package/examples/match-experimental/01-experimental-match.ts +150 -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 +120 -48
  150. package/dist/chunk-CUT6urMc.cjs +0 -30
  151. package/dist/index.d.ts +0 -5
  152. package/dist/index.js +0 -6
  153. package/dist/match-BQP0RNSk.js +0 -13
  154. package/dist/match-BQP0RNSk.js.map +0 -1
  155. package/dist/match-COIWq-e8.d.cts +0 -18
  156. package/dist/match-COIWq-e8.d.cts.map +0 -1
  157. package/dist/match-DXKW7ym5.d.ts +0 -18
  158. package/dist/match-DXKW7ym5.d.ts.map +0 -1
  159. package/dist/match-DyAuOeqx.cjs +0 -25
  160. package/dist/match-DyAuOeqx.cjs.map +0 -1
  161. package/dist/match.d.ts +0 -5
  162. package/dist/match.js +0 -3
  163. package/dist/option-8_eiz02z.d.cts +0 -45
  164. package/dist/option-8_eiz02z.d.cts.map +0 -1
  165. package/dist/option-Be_DXVE6.js +0 -181
  166. package/dist/option-Be_DXVE6.js.map +0 -1
  167. package/dist/option-CoelyMEg.d.ts +0 -45
  168. package/dist/option-CoelyMEg.d.ts.map +0 -1
  169. package/dist/option-DHCdfOco.cjs +0 -193
  170. package/dist/option-DHCdfOco.cjs.map +0 -1
  171. package/dist/option.d.ts +0 -3
  172. package/dist/option.js +0 -4
  173. package/dist/result-CdGWA5IL.js +0 -318
  174. package/dist/result-CdGWA5IL.js.map +0 -1
  175. package/dist/result-DsVjSFnO.d.ts +0 -132
  176. package/dist/result-DsVjSFnO.d.ts.map +0 -1
  177. package/dist/result-Dy_C5dC6.d.cts +0 -132
  178. package/dist/result-Dy_C5dC6.d.cts.map +0 -1
  179. package/dist/result-nuhyCRyu.cjs +0 -336
  180. package/dist/result-nuhyCRyu.cjs.map +0 -1
  181. package/dist/result.d.ts +0 -3
  182. package/dist/result.js +0 -4
  183. package/dist/tuple.d.ts +0 -12
  184. package/dist/tuple.d.ts.map +0 -1
  185. package/dist/tuple.js.map +0 -1
  186. package/dist/unit-CnjFzJN1.cjs +0 -12
  187. package/dist/unit-CnjFzJN1.cjs.map +0 -1
  188. package/dist/unit-CoBLIJFc.d.ts +0 -9
  189. package/dist/unit-CoBLIJFc.d.ts.map +0 -1
  190. package/dist/unit-D0y_qAUW.d.cts +0 -9
  191. package/dist/unit-D0y_qAUW.d.cts.map +0 -1
  192. package/dist/unit-udy5j53P.js +0 -6
  193. package/dist/unit-udy5j53P.js.map +0 -1
  194. package/dist/unit.d.ts +0 -2
  195. package/dist/unit.js +0 -3
@@ -0,0 +1,481 @@
1
+ import { ExperimentalResult } from "./result-experimental.mjs";
2
+ import { ExperimentalOption } from "./option-experimental.mjs";
3
+ import { Result as Result$1 } from "./result.mjs";
4
+ import { Option as Option$1 } from "./option.mjs";
5
+
6
+ //#region src/match.d.ts
7
+ type Result<T, E> = ExperimentalResult<T, E>;
8
+ type Option<T> = ExperimentalOption<T>;
9
+ type LegacyResult<T, E> = Result$1<T, E> | ExperimentalResult<T, E>;
10
+ type LegacyOption<T> = Option$1<T> | ExperimentalOption<T>;
11
+ type MatchableValue = {
12
+ readonly _tag: string;
13
+ };
14
+ /**
15
+ * Extract the `_tag` values from a discriminated union.
16
+ */
17
+ type GetTags<T> = T extends {
18
+ readonly _tag: infer Tag extends string;
19
+ } ? Tag : never;
20
+ /**
21
+ * Extract the variant of T that has the given tag.
22
+ */
23
+ type ExtractByTag<T, Tag> = T extends {
24
+ readonly _tag: Tag;
25
+ } ? T : never;
26
+ type IsExactlyUnknown<T> = unknown extends T ? [T] extends [unknown] ? true : false : false;
27
+ type IsPromiseLike<T> = T extends PromiseLike<unknown> ? true : false;
28
+ /**
29
+ * Extract the inner type from a discriminated union based on tag.
30
+ *
31
+ * For Option/Result-like variants, extracts via `unwrap`/`unwrapErr`.
32
+ * For other discriminated unions, falls back to the narrowed variant itself.
33
+ */
34
+ 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 {
35
+ unwrap(): infer V;
36
+ } ? V : unknown;
37
+ type MatchInput<T, Tag extends string> = Tag extends "Some" | "Ok" | "Err" ? IsExactlyUnknown<GetInnerType<T, Tag>> extends true ? ExtractByTag<T, Tag> : GetInnerType<T, Tag> : ExtractByTag<T, Tag>;
38
+ type HasPromiseLikePayloadForTag<T, Tag extends string> = Tag extends GetTags<T> ? IsExactlyUnknown<GetInnerType<T, Tag>> extends true ? false : IsPromiseLike<GetInnerType<T, Tag>> : false;
39
+ type HasPromiseLikePayload<T> = true extends HasPromiseLikePayloadForTag<T, "Some"> | HasPromiseLikePayloadForTag<T, "Ok"> | HasPromiseLikePayloadForTag<T, "Err"> ? true : false;
40
+ type SyncMatchable<T extends MatchableValue> = HasPromiseLikePayload<T> extends true ? never : T;
41
+ declare function predicateAnd<T>(...predicates: Array<(value: T) => boolean>): (value: T) => boolean;
42
+ declare function predicateOr<T>(...predicates: Array<(value: T) => boolean>): (value: T) => boolean;
43
+ /**
44
+ * Represents a compile-time exhaustiveness error.
45
+ * When pattern matching is incomplete, this type surfaces the missing cases.
46
+ */
47
+ type ExhaustiveError<Missing extends string> = {
48
+ readonly _tag: "ExhaustiveError";
49
+ readonly error: "Pattern matching is not exhaustive";
50
+ readonly missingCases: Missing;
51
+ };
52
+ /** Base pattern interface */
53
+ interface BasePattern<Tag extends string> {
54
+ readonly _patternTag: Tag;
55
+ }
56
+ /** Pattern for matching Option.Some with optional predicate guard */
57
+ interface SomePattern<T = unknown> extends BasePattern<"Some"> {
58
+ readonly predicate?: (val: T) => boolean;
59
+ readonly _hasPredicate: boolean;
60
+ }
61
+ /** Pattern for matching Option.None */
62
+ interface NonePattern extends BasePattern<"None"> {
63
+ readonly _hasPredicate: false;
64
+ }
65
+ /** Pattern for matching Result.Ok with optional predicate guard */
66
+ interface OkPattern<T = unknown> extends BasePattern<"Ok"> {
67
+ readonly predicate?: (val: T) => boolean;
68
+ readonly _hasPredicate: boolean;
69
+ }
70
+ /** Pattern for matching Result.Err with optional predicate guard */
71
+ interface ErrPattern<E = unknown> extends BasePattern<"Err"> {
72
+ readonly predicate?: (err: E) => boolean;
73
+ readonly _hasPredicate: boolean;
74
+ }
75
+ /** Wildcard pattern that matches anything */
76
+ interface WildcardPattern extends BasePattern<"_"> {}
77
+ /** Union of all pattern types */
78
+ type Pattern<T = unknown, E = unknown> = SomePattern<T> | NonePattern | OkPattern<T> | ErrPattern<E> | WildcardPattern;
79
+ /** Pattern for Some without predicate - marks tag as fully consumed */
80
+ interface SomePatternNoPredicate<_T = unknown> extends BasePattern<"Some"> {
81
+ readonly predicate: undefined;
82
+ readonly _hasPredicate: false;
83
+ }
84
+ /** Pattern for Some with predicate - does NOT consume tag */
85
+ interface SomePatternWithPredicate<T = unknown> extends BasePattern<"Some"> {
86
+ readonly predicate: (val: T) => boolean;
87
+ readonly _hasPredicate: true;
88
+ }
89
+ /** Pattern for Ok without predicate - marks tag as fully consumed */
90
+ interface OkPatternNoPredicate<_T = unknown> extends BasePattern<"Ok"> {
91
+ readonly predicate: undefined;
92
+ readonly _hasPredicate: false;
93
+ }
94
+ /** Pattern for Ok with predicate - does NOT consume tag */
95
+ interface OkPatternWithPredicate<T = unknown> extends BasePattern<"Ok"> {
96
+ readonly predicate: (val: T) => boolean;
97
+ readonly _hasPredicate: true;
98
+ }
99
+ /** Pattern for Err without predicate - marks tag as fully consumed */
100
+ interface ErrPatternNoPredicate<_E = unknown> extends BasePattern<"Err"> {
101
+ readonly predicate: undefined;
102
+ readonly _hasPredicate: false;
103
+ }
104
+ /** Pattern for Err with predicate - does NOT consume tag */
105
+ interface ErrPatternWithPredicate<E = unknown> extends BasePattern<"Err"> {
106
+ readonly predicate: (err: E) => boolean;
107
+ readonly _hasPredicate: true;
108
+ }
109
+ /**
110
+ * Pattern constructors for type-safe pattern matching.
111
+ *
112
+ * @example
113
+ * ```ts
114
+ * import { match, P } from "@carbonteq/fp";
115
+ *
116
+ * // Basic patterns
117
+ * match(option)
118
+ * .with(P.Some(), (v) => v * 2)
119
+ * .with(P.None(), () => 0)
120
+ * .exhaustive();
121
+ *
122
+ * // With predicate guards
123
+ * match(option)
124
+ * .with(P.Some((x) => x > 10), (v) => "big")
125
+ * .with(P.Some(), (v) => "small")
126
+ * .with(P.None(), () => "none")
127
+ * .exhaustive();
128
+ * ```
129
+ */
130
+ declare const P: {
131
+ /**
132
+ * Predicate helper that checks if a value is in the `Some` state.
133
+ *
134
+ * @example
135
+ * ```ts
136
+ * match(option)
137
+ * .when(P.IsSome, () => "has value")
138
+ * .otherwise(() => "none");
139
+ * ```
140
+ */
141
+ readonly IsSome: (value: MatchableValue) => boolean;
142
+ /**
143
+ * Predicate helper that checks if a value is in the `None` state.
144
+ */
145
+ readonly IsNone: (value: MatchableValue) => boolean;
146
+ /**
147
+ * Predicate helper that checks if a value is in the `Ok` state.
148
+ */
149
+ readonly IsOk: (value: MatchableValue) => boolean;
150
+ /**
151
+ * Predicate helper that checks if a value is in the `Err` state.
152
+ */
153
+ readonly IsErr: (value: MatchableValue) => boolean;
154
+ /**
155
+ * Predicate helper for exact equality checks.
156
+ * Uses `Object.is` semantics.
157
+ *
158
+ * @example
159
+ * ```ts
160
+ * match(result)
161
+ * .with(P.Ok(P.eq(42)), () => "exactly 42")
162
+ * .with(P.Ok(), () => "other ok")
163
+ * .with(P.Err(), () => "err")
164
+ * .exhaustive();
165
+ * ```
166
+ */
167
+ readonly eq: <T>(expected: T) => (actual: T) => boolean;
168
+ /**
169
+ * Predicate helper for membership checks against a list of values.
170
+ * Uses `Object.is` semantics.
171
+ *
172
+ * @example
173
+ * ```ts
174
+ * match(result)
175
+ * .with(P.Err(P.oneOf("timeout", "offline")), () => "retry")
176
+ * .with(P.Err(), () => "fail")
177
+ * .with(P.Ok(), () => "ok")
178
+ * .exhaustive();
179
+ * ```
180
+ */
181
+ readonly oneOf: <T>(...expected: readonly T[]) => (actual: T) => boolean;
182
+ /**
183
+ * Predicate helper that negates another predicate.
184
+ *
185
+ * @example
186
+ * ```ts
187
+ * match(result)
188
+ * .with(P.Ok(P.not(P.eq(42))), () => "not 42")
189
+ * .with(P.Ok(), () => "exactly 42")
190
+ * .with(P.Err(), () => "err")
191
+ * .exhaustive();
192
+ * ```
193
+ */
194
+ readonly not: <T>(predicate: (value: T) => boolean) => (value: T) => boolean;
195
+ /**
196
+ * Predicate helper that requires all predicates to match.
197
+ */
198
+ readonly and: typeof predicateAnd;
199
+ /**
200
+ * Alias of `P.and`.
201
+ */
202
+ readonly all: typeof predicateAnd;
203
+ /**
204
+ * Predicate helper that requires at least one predicate to match.
205
+ */
206
+ readonly or: typeof predicateOr;
207
+ /**
208
+ * Alias of `P.or`.
209
+ */
210
+ readonly any: typeof predicateOr;
211
+ /**
212
+ * Pattern that matches `Option.Some`.
213
+ * Without predicate: consumes the tag for exhaustiveness.
214
+ * With predicate: does NOT consume the tag (allows fallthrough).
215
+ */
216
+ readonly Some: {
217
+ <T = unknown>(): SomePatternNoPredicate<T>;
218
+ <T>(predicate: (val: T) => boolean): SomePatternWithPredicate<T>;
219
+ };
220
+ /**
221
+ * Pattern that matches `Option.None`.
222
+ */
223
+ readonly None: () => NonePattern;
224
+ /**
225
+ * Pattern that matches `Result.Ok`.
226
+ * Without predicate: consumes the tag for exhaustiveness.
227
+ * With predicate: does NOT consume the tag (allows fallthrough).
228
+ */
229
+ readonly Ok: {
230
+ <T = unknown>(): OkPatternNoPredicate<T>;
231
+ <T>(predicate: (val: T) => boolean): OkPatternWithPredicate<T>;
232
+ };
233
+ /**
234
+ * Pattern that matches `Result.Err`.
235
+ * Without predicate: consumes the tag for exhaustiveness.
236
+ * With predicate: does NOT consume the tag (allows fallthrough).
237
+ */
238
+ readonly Err: {
239
+ <E = unknown>(): ErrPatternNoPredicate<E>;
240
+ <E>(predicate: (err: E) => boolean): ErrPatternWithPredicate<E>;
241
+ };
242
+ /**
243
+ * Wildcard pattern that matches anything.
244
+ * Use with `otherwise()` for catch-all handling.
245
+ */
246
+ readonly _: WildcardPattern;
247
+ };
248
+ /**
249
+ * Handler function type for pattern matching.
250
+ */
251
+ type MatchHandler<T, R> = (value: T) => R;
252
+ /**
253
+ * Builder interface for fluent pattern matching.
254
+ *
255
+ * @template T - The type being matched
256
+ * @template Matched - Union of tags that have been matched
257
+ * @template Returns - Union of return types from handlers
258
+ */
259
+ interface MatchBuilder<T, Matched extends string, Returns> {
260
+ /**
261
+ * Add a pattern match case using a string tag.
262
+ *
263
+ * @example
264
+ * ```ts
265
+ * match(option)
266
+ * .with("Some", (v) => v * 2)
267
+ * .with("None", () => 0)
268
+ * .exhaustive();
269
+ * ```
270
+ */
271
+ with<Tag extends Exclude<GetTags<T>, Matched>, R>(tag: Tag, handler: MatchHandler<MatchInput<T, Tag>, R>): MatchBuilder<T, Matched | Tag, Returns | R>;
272
+ /**
273
+ * Add a pattern match case using a Pattern object without predicate.
274
+ * This variant fully consumes the tag for exhaustiveness checking.
275
+ *
276
+ * @example
277
+ * ```ts
278
+ * match(option)
279
+ * .with(P.Some(), (v) => v * 2)
280
+ * .with(P.None(), () => "none")
281
+ * .exhaustive();
282
+ * ```
283
+ */
284
+ with<Tag extends Exclude<GetTags<T>, Matched>, R>(pattern: BasePattern<Tag> & {
285
+ readonly _hasPredicate: false;
286
+ }, handler: MatchHandler<MatchInput<T, Tag>, R>): MatchBuilder<T, Matched | Tag, Returns | R>;
287
+ /**
288
+ * Add a pattern match case using a Pattern object with predicate guard.
289
+ * This variant does NOT consume the tag (allows multiple guarded patterns).
290
+ *
291
+ * @example
292
+ * ```ts
293
+ * match(option)
294
+ * .with(P.Some((x) => x > 10), () => "big")
295
+ * .with(P.Some(), () => "small") // Still allowed
296
+ * .with(P.None(), () => "none")
297
+ * .exhaustive();
298
+ * ```
299
+ */
300
+ with<Tag extends GetTags<T>, R>(pattern: BasePattern<Tag> & {
301
+ readonly _hasPredicate: true;
302
+ }, handler: MatchHandler<GetInnerType<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
+ 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
+ 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
+ declare const matchRes: <T, E, U>(r: LegacyResult<T, E>, branches: {
438
+ Ok: (val: T) => U;
439
+ Err: (err: E) => U;
440
+ }) => U;
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
+ declare const matchOpt: <T, U>(o: LegacyOption<T>, branches: {
458
+ Some: (val: T) => U;
459
+ None: () => U;
460
+ }) => U;
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
+ declare const matchResult: <T, E, U>(r: LegacyResult<T, E>, branches: {
467
+ Ok: (val: T) => U;
468
+ Err: (err: E) => U;
469
+ }) => U;
470
+ /**
471
+ * Pattern match on an Option (alias for matchOpt).
472
+ *
473
+ * @deprecated Use `option.match()` instance method or `match(option)` builder instead.
474
+ */
475
+ declare const matchOption: <T, U>(o: LegacyOption<T>, branches: {
476
+ Some: (val: T) => U;
477
+ None: () => U;
478
+ }) => U;
479
+ //#endregion
480
+ export { type ErrPattern, type ErrPatternNoPredicate, type ErrPatternWithPredicate, type ExhaustiveError, type MatchBuilder, type NonePattern, type OkPattern, type OkPatternNoPredicate, type OkPatternWithPredicate, P, type Pattern, type SomePattern, type SomePatternNoPredicate, type SomePatternWithPredicate, UnmatchedCaseError, type WildcardPattern, match, matchOpt, matchOption, matchRes, matchResult };
481
+ //# sourceMappingURL=match.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"match.d.mts","names":[],"sources":["../src/match.ts"],"mappings":";;;;;;KAKK,MAAA,SAAe,kBAAA,CAAmB,CAAA,EAAG,CAAA;AAAA,KACrC,MAAA,MAAY,kBAAA,CAAmB,CAAA;AAAA,KAE/B,YAAA,SAAqB,QAAA,CAAU,CAAA,EAAG,CAAA,IAAK,kBAAA,CAAmB,CAAA,EAAG,CAAA;AAAA,KAC7D,YAAA,MAAkB,QAAA,CAAU,CAAA,IAAK,kBAAA,CAAmB,CAAA;AAAA,KAEpD,cAAA;EAAA,SAA4B,IAAA;AAAA;;;;KAS5B,OAAA,MAAa,CAAA;EAAA,SAAqB,IAAA;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