summaryrefslogtreecommitdiff
path: root/src/utils.ts
blob: 7e3009f4832539099303e0bf10a965d0dbe465b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
export const eq =
  <T>(a: T) =>
  (b: T): boolean =>
    a === b

export const match =
  <R, K extends string>(pattern: { [key in K | '_']: () => R }) =>
  (k: K): R =>
    (pattern[k] || pattern._)()

type Tag<N, V> = { tag: N; value: V }
export type Union<T> = { [N in keyof T]: Tag<N, T[N]> }[keyof T]

export const constructors = <T extends Record<string, any>>(): {
  [N in keyof T]: (value: T[N]) => Union<T> // Tag<N, T[N]>
} =>
  new Proxy(
    {},
    {
      get(_, k) {
        return (value: any) => ({ tag: k, value })
      },
    },
  ) as any