aboutsummaryrefslogtreecommitdiff
path: root/src/lib/dict.ts
diff options
context:
space:
mode:
authorJoris2022-06-21 07:59:57 +0200
committerJoris2022-06-21 07:59:57 +0200
commit46f39fa93fa99eef2691d6dc905b9d083eb170cb (patch)
tree1d39b1b358a49a4c194dbf852cf80b5f07629361 /src/lib/dict.ts
parent5944be94dfd221f41cfb5e60e007c159b617f5dc (diff)
Add more drum sounds
Diffstat (limited to 'src/lib/dict.ts')
-rw-r--r--src/lib/dict.ts21
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>)
+}