From 7286851015affcfec4c031ee6e6f7c75ec2b07c4 Mon Sep 17 00:00:00 2001 From: ecyrbe Date: Tue, 1 Aug 2023 15:42:46 +0200 Subject: [PATCH] feat(match): allow to customize predicate --- src/internals/match/Match.ts | 2 +- src/internals/match/impl/match.ts | 24 ++++++++++++++---------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/internals/match/Match.ts b/src/internals/match/Match.ts index 3769ea6..de313cb 100644 --- a/src/internals/match/Match.ts +++ b/src/internals/match/Match.ts @@ -22,7 +22,7 @@ import * as Impl from "./impl/match"; */ export type Match< valueOrWithClauses = unset, - withClauses extends Impl.With[] | unset | _ = unset + withClauses extends Impl.With[] | unset | _ = unset > = PartialApply< MatchFn, withClauses extends unset diff --git a/src/internals/match/impl/match.ts b/src/internals/match/impl/match.ts index 8f34b71..6ffbb59 100644 --- a/src/internals/match/impl/match.ts +++ b/src/internals/match/impl/match.ts @@ -18,10 +18,6 @@ type ReplaceArgsWithConstraint = pattern extends arg< ? { [key in keyof pattern]: ReplaceArgsWithConstraint } : pattern; -type DoesMatch = exact extends true ? - Equal : - value extends ReplaceArgsWithConstraint ? true : false; - type ExtractArgObject = pattern extends arg ? { [K in N]: value } : pattern extends [] @@ -61,16 +57,24 @@ type ExtractArgs = WithDefaultArgs< [value] >; -export type Match[]> = patterns extends [ - With, - ...infer restPatterns extends With[] +interface PatternMatchFn extends Fn { + return: this["arg0"] extends ReplaceArgsWithConstraint ? true : false; +} + +interface ExactMatchFn extends Fn { + return: Equal; +} + +export type Match[]> = patterns extends [ + With, + ...infer restPatterns extends With[] ] - ? DoesMatch extends true + ? Call extends true ? handler extends Fn ? Call, ExtractArgs>> : handler : Match : never; -export type With = { exact: exact; pattern: pattern; handler: handler }; -export type WithExact = With; +export type With = { predicate: predicate; pattern: pattern; handler: handler }; +export type WithExact = With;