-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
74 lines (62 loc) · 4.03 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
export function multimethod<P, R>(
predicateFn: () => P,
defaultFn: () => R,
...fns: [predicate: P, fn: () => R][]
): () => R;
export function multimethod<P, R, T1>(
predicateFn: (arg1: T1) => P,
defaultFn: (arg1: T1) => R,
...fns: [predicate: P, fn: (arg1: T1) => R][]
): (arg1: T1) => R;
export function multimethod<P, R, T1, T2>(
predicateFn: (arg1: T1, arg2: T2) => P,
defaultFn: (arg1: T1, arg2: T2) => R,
...fns: [predicate: P, fn: (arg1: T1, arg2: T2) => R][]
): (arg1: T1, arg2: T2) => R;
export function multimethod<P, R, T1, T2, T3>(
predicateFn: (arg1: T1, arg2: T2, arg3: T3) => P,
defaultFn: (arg1: T1, arg2: T2, arg3: T3) => R,
...fns: [predicate: P, fn: (arg1: T1, arg2: T2, arg3: T3) => R][]
): (arg1: T1, arg2: T2, arg3: T3) => R;
export function multimethod<P, R, T1, T2, T3, T4>(
predicateFn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => P,
defaultFn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => R,
...fns: [predicate: P, fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => R][]
): (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => R;
export function multimethod<P, R, T1, T2, T3, T4, T5>(
predicateFn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => P,
defaultFn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => R,
...fns: [predicate: P, fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => R][]
): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => R;
export function multimethod<P, R, T1, T2, T3, T4, T5, T6>(
predicateFn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6) => P,
defaultFn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6) => R,
...fns: [predicate: P, fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6) => R][]
): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6) => R;
export function multimethod<P, R, T1, T2, T3, T4, T5, T6, T7>(
predicateFn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7) => P,
defaultFn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7) => R,
...fns: [predicate: P, fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7) => R][]
): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7) => R;
export function multimethod<P, R, T1, T2, T3, T4, T5, T6, T7, T8>(
predicateFn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8) => P,
defaultFn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8) => R,
...fns: [predicate: P, fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8) => R][]
): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8) => R;
export function multimethod<P, R, T1, T2, T3, T4, T5, T6, T7, T8, T9>(
predicateFn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9) => P,
defaultFn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9) => R,
...fns: [predicate: P, fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9) => R][]
): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9) => R;
export function multimethod<P, R, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(
predicateFn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10) => P,
defaultFn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10) => R,
...fns: [predicate: P, fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10) => R][]
): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10) => R;
export function multimethod(predicateFn: Function, defaultFn: Function, ...fns: [predicate: unknown, fn: Function][]) {
const dict = Object.fromEntries(fns);
return (...args: unknown[]) => {
const predicate = predicateFn(...args);
return (dict[predicate] ?? defaultFn)(...args);
};
};