summaryrefslogtreecommitdiffstats
path: root/src/util.ts
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2017-09-12 09:20:19 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2017-09-12 09:20:19 +0200
commit02758a69ac49cc437ed27628b64e08fd443758b8 (patch)
tree470d9980b9c2ec710f85a7c5b872d4b529e36a9e /src/util.ts
parenta5e69edc5a6f1a95618c04e214d39b397577d796 (diff)
downloadrpgedit-02758a69ac49cc437ed27628b64e08fd443758b8.tar
rpgedit-02758a69ac49cc437ed27628b64e08fd443758b8.zip
Implement simple map renderer
Diffstat (limited to 'src/util.ts')
-rw-r--r--src/util.ts33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/util.ts b/src/util.ts
new file mode 100644
index 0000000..337bf05
--- /dev/null
+++ b/src/util.ts
@@ -0,0 +1,33 @@
+import * as _ from 'lodash';
+
+
+export function mapFromObject<T>(obj: {[key: string]: T}): Map<string, T> {
+ return new Map(_.toPairs(obj));
+}
+
+export function mapValues<K, V1, V2>(f: (v: V1) => V2, map: Map<K, V1>): Map<K, V2> {
+ let ret: Map<K, V2> = new Map();
+
+ for (let [k, v] of map)
+ ret.set(k, f(v));
+
+ return ret;
+}
+
+export async function mapValuesAsync<K, V1, V2>(f: (v: V1) => Promise<V2>, map: Map<K, V1>): Promise<Map<K, V2>> {
+ let ret: Map<K, V2> = new Map();
+
+ for (let [k, v] of mapValues(f, map))
+ ret.set(k, await v);
+
+ return ret;
+}
+
+export function nextPowerOf2(n: number): number {
+ let i = 1;
+
+ while (i < n)
+ i *= 2;
+
+ return i;
+}