aboutsummaryrefslogtreecommitdiff
path: root/src/lib/dict.ts
blob: 43b9f0290956e30985ab2318f7bb06964d8502ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
type Key = string | number | symbol

export function fromList<V>(xs: Array<{key: Key, value: V}>): Record<Key, V> {
  let res: any = {}
  xs.forEach(o => res[o.key] = o.value)
  return res as Record<Key, V>
}

export function toList<V>(record: Record<Key, V>): Array<{key: Key, value: V}> {
  return Object.keys(record)
    .map(key => ({ key: key, value: record[key] }))
}

type EnumObject = {[key: string]: number | string};
type EnumObjectEnum<E extends EnumObject> = E extends {[key: string]: infer ET | string} ? ET : never;

export function values<E extends EnumObject>(enumObject: E): EnumObjectEnum<E>[] {
  return Object.keys(enumObject)
    .filter(key => Number.isNaN(Number(key)))
    .map(key => enumObject[key] as EnumObjectEnum<E>)
}