import type { Condition } from '../../mod.js'; import { Matcher } from '../../mod.js'; import { Def } from '../Constant/mod.js'; /** ## match : 模式匹配 @example Usage : 普通调用(匹配参数为一个以上时) ```ts // 1. 简单的模式匹配 const name='jiojio' const age=match(name, ['jiojio',18], [(name)=>name==='dio',19], [Def,20]) assert(age===18)//true // 2. 实现了PartialEq/Equal的数据结构 可直接进行匹配 class User implements PartialEq { constructor(public name: string, public age: number) {} eq(other: this) { return other.name == this.name && other.age == this.age } } const User1 = new User('Tom', 18) const User2 = new User('Tom', 18) const res = match(User1, [User2, true], [Def, false]) assert(res)//true //tips: // 1. 匹配出现多个Def已最后一个为准 // 2. Def出现的位置不影响匹配结果,如果所有模式都匹配不上才使用Def作为结果 // 3. 默认匹配结果是null ``` @example Usage : 链式调用(没有匹配参数时) ```ts //1. 配合zod进行类型(校验)匹配 const pattern1 = zod.number().min(0).max(10) const pattern2 = zod.number().min(-10).max(-2) const res = match(10) .some([zod.validate(pattern1), zod.validate(pattern2)], true) .done(false) assert(res) //2. 实现了PartialEq/Equal的数据结构 可直接进行匹配 class Typer implements PartialEq { constructor(public name: string) {} eq(other: this) { return this.name === other.name } } const result = match(new Typer('jiojio')) .case(new Typer('dio'), false) .case(new Typer('jiojio'), true) .case(new Typer('jio - jio'), false) .done() .expect('test-error') assert(result) //3. `===` 和 `(val)=>boolean` 的普通匹配 , 同上 ``` @example Usage : 使用match替代`if-else`语句 ```ts const case1 = false const case2 = true const case3 = undefined // 使用Match替代if_else语句 const res = match(true) .case(case1, 'case1') .case(case2, 'case2') .case(case3, 'case3') .done('no_case') // 等价于下面语句 let ress = '' if (case1===true) ress = 'case1' else if (case2===true) ress = 'case2' else if (case3===true) ress = 'case3' else ress = 'no_case' assertEquals(res, ress) ``` @category Function */ export declare function match(match_value: NonNullable): Matcher; export declare function match(match_value: T, ab: [typeof Def, V]): V; export declare function match(match_value: T, ab: [Condition, V], bc: [typeof Def, C]): V | C; export declare function match(match_value: T, ab: [Condition, V], bc: [Condition, C], cd: [typeof Def, D]): V | C | D; export declare function match(match_value: T, ab: [Condition, V], bc: [Condition, C], cd: [Condition, D], de: [typeof Def, F]): V | C | D | F; export declare function match(match_value: T, ab: [Condition, V], bc: [Condition, C], cd: [Condition, D], de: [Condition, F], ef: [typeof Def, G]): V | C | D | F | G; export declare function match(match_value: T, ab: [Condition, V], bc: [Condition, C], cd: [Condition, D], de: [Condition, F], ef: [Condition, G], fg: [typeof Def, H]): V | C | D | F | G | H; export declare function match(match_value: T, ab: [Condition, V], bc: [Condition, C], cd: [Condition, D], de: [Condition, F], ef: [Condition, G], fg: [Condition, H], gh: [typeof Def, I]): V | C | D | F | G | H | I; export declare function match(match_value: T, ab: [Condition, V], bc: [Condition, C], cd: [Condition, D], de: [Condition, F], ef: [Condition, G], fg: [Condition, H], gh: [Condition, I], hi: [typeof Def, J]): V | C | D | F | G | H | I | J; export declare function match(match_value: T, ab: [Condition, V], bc: [Condition, C], cd: [Condition, D], de: [Condition, F], ef: [Condition, G], fg: [Condition, H], gh: [Condition, I], hi: [Condition, J], ij: [typeof Def, K]): V | C | D | F | G | H | I | J | K; export declare function match(match_value: T, ab: [Condition, V], bc: [Condition, C], cd: [Condition, D], de: [Condition, F], ef: [Condition, G], fg: [Condition, H], gh: [Condition, I], hi: [Condition, J], ij: [Condition, K], jk: [typeof Def, L]): V | C | D | F | G | H | I | J | K | L; //# sourceMappingURL=match.d.ts.map