diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/dict.ts | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/lib/dict.ts b/src/lib/dict.ts new file mode 100644 index 0000000..43b9f02 --- /dev/null +++ b/src/lib/dict.ts @@ -0,0 +1,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>) +} |