@@ -4,6 +4,64 @@ import { type PatternResult } from "../result";
44import { type ComplexMatchedValue , type ComplexUnMatchedValue } from "../types" ;
55import { type MatchBuilder } from "./builder" ;
66export * from "./builder" ;
7+ /**
8+ * Creates a match builder or applies a single pattern.
9+ *
10+ * **Supported call styles:**
11+ * - Classic: `match(input)` → returns a match builder
12+ * - Classic: `match(input, pattern, handler)` → returns a result or the input
13+ * - Curried: `match(pattern, handler)` → returns a function waiting for the input
14+ *
15+ * If the input matches the pattern, the handler runs and the result is wrapped for chaining.
16+ * The input is not mutated.
17+ *
18+ * ```ts
19+ * const input = <{
20+ * type: "text";
21+ * value: string;
22+ * } | {
23+ * type: "count";
24+ * value: number;
25+ * }>{
26+ * type: "text",
27+ * value: "hello",
28+ * };
29+ *
30+ * P.match(input)
31+ * .with(
32+ * { type: "text" },
33+ * (item) => item.value.toUpperCase(),
34+ * )
35+ * .with(
36+ * { type: "count" },
37+ * (item) => `${item.value}:${item.type}`,
38+ * )
39+ * .otherwise(() => "unknown"); // "HELLO"
40+ *
41+ * pipe(
42+ * input,
43+ * P.match(
44+ * { type: "count" },
45+ * (item) => item.value * 2,
46+ * ),
47+ * P.otherwise(() => 0),
48+ * ); // 4
49+ *
50+ * P.match(input)
51+ * .when(
52+ * (item) => typeof item === "number",
53+ * () => "ok",
54+ * )
55+ * .otherwise(() => "no"); // "ok"
56+ * ```
57+ *
58+ * @see [`P.when`](https://utils.duplojs.dev/en/v1/api/pattern/when) For predicate branches
59+ * @see [`P.otherwise`](https://utils.duplojs.dev/en/v1/api/pattern/otherwise) For fallbacks
60+ * @see https://utils.duplojs.dev/en/v1/api/pattern/match
61+ *
62+ * @namespace P
63+ *
64+ */
765export declare function match < GenericInput extends unknown > ( input : GenericInput ) : MatchBuilder < IsEqual < GenericInput , unknown > extends true ? AnyValue : GenericInput > ;
866export declare function match < GenericInput extends unknown , GenericInputValue extends Exclude < IsEqual < GenericInput , unknown > extends true ? AnyValue : GenericInput , PatternResult > , GenericInputPatternResult extends Extract < GenericInput , PatternResult > , const GenericPattern extends Pattern < GenericInputValue > , GenericPatternValue extends PatternValue < GenericPattern > , GenericOutput extends AnyValue | EscapeVoid , GenericMatchedValue extends Extract < ComplexMatchedValue < GenericInputValue , GenericPatternValue > , any > > ( pattern : FixDeepFunctionInfer < Pattern < GenericInputValue > , GenericPattern > , theFunction : ( value : Extract < ComplexMatchedValue < GenericInputValue , PatternValue < GenericPattern > > , any > ) => GenericOutput ) : ( input : GenericInput | GenericInputValue | GenericInputPatternResult ) => BreakGenericLink < ( ( IsEqual < GenericMatchedValue , never > extends true ? never : PatternResult < GenericOutput > ) | GenericInputPatternResult | Extract < ComplexUnMatchedValue < GenericInputValue , GenericPatternValue > , any > ) > ;
967export declare function match < GenericInput extends unknown , GenericInputValue extends Exclude < IsEqual < GenericInput , unknown > extends true ? AnyValue : GenericInput , PatternResult > , GenericInputPatternResult extends Extract < GenericInput , PatternResult > , const GenericPattern extends Pattern < GenericInputValue > , GenericPatternValue extends PatternValue < GenericPattern > , GenericOutput extends AnyValue | EscapeVoid , GenericMatchedValue extends Extract < ComplexMatchedValue < GenericInputValue , GenericPatternValue > , any > > ( input : GenericInput | GenericInputValue | GenericInputPatternResult , pattern : FixDeepFunctionInfer < Pattern < GenericInputValue > , GenericPattern > , theFunction : ( value : Extract < ComplexMatchedValue < GenericInputValue , PatternValue < GenericPattern > > , any > ) => GenericOutput ) : BreakGenericLink < ( IsEqual < GenericMatchedValue , never > extends true ? never : PatternResult < GenericOutput > ) | GenericInputPatternResult | Extract < ComplexUnMatchedValue < GenericInputValue , GenericPatternValue > , any > > ;
0 commit comments