summaryrefslogtreecommitdiffstats
path: root/src/util.ts
blob: 337bf05e5d48b5cf671ec5cbe262eb1a6e22cb2c (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
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;
}