-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.ts
More file actions
31 lines (27 loc) · 1.05 KB
/
map.ts
File metadata and controls
31 lines (27 loc) · 1.05 KB
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
/**
* Node.js GraphQL API Starter Kit
* https://github.com/kriasoft/nodejs-api-starter
* Copyright © 2016-present Kriasoft | MIT License
*/
type Func<R, K> = (row: R) => K;
export function mapTo<K, R>(keys: ReadonlyArray<K>, keyFn: Func<R, K>) {
return (rows: Array<R>) => {
const group = new Map<K, R | null>(keys.map(key => [key, null]));
rows.forEach(row => group.set(keyFn(row), row));
return Array.from(group.values());
};
}
export function mapToMany<K, R>(keys: ReadonlyArray<K>, keyFn: Func<R, K>) {
return (rows: Array<R>) => {
const group = new Map<K, R[]>(keys.map(key => [key, []]));
rows.forEach(row => (group.get(keyFn(row)) || []).push(row));
return Array.from(group.values());
};
}
export function mapToValues<K, R, V>(keys: ReadonlyArray<K>, keyFn: Func<R, K>, valueFn: Func<R, V>, defaultValue = null) {
return (rows: Array<R>) => {
const group = new Map<K, V | null>(keys.map(key => [key, defaultValue]));
rows.forEach(row => group.set(keyFn(row), valueFn(row)));
return Array.from(group.values());
};
}