summaryrefslogtreecommitdiffstats
path: root/src/util.ts
blob: 453966e2abba3669652e92ec301bb78b0ac7ff29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
export function mapFromObject<T>(obj: {[key: string]: T}): Map<string, T> {
	const ret = new Map();

	for (const k of Object.keys(obj))
		ret.set(k, obj[k]);

	return ret;
}

export function mapValues<K, V1, V2>(f: (v: V1) => V2, map: Map<K, V1>): Map<K, V2> {
	const ret: Map<K, V2> = new Map();

	for (const [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>> {
	const ret: Map<K, V2> = new Map();

	for (const [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;
}