1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
2// See the LICENCE file in the repository root for full licence text.
3
4export function mapBy<T, K extends keyof T>(array: T[], key: K) {
5 const map = new Map<T[K], T>();
6
7 for (const value of array) {
8 map.set(value[key], value);
9 }
10
11 return map;
12}
13
14export function mapByWithNulls<T, K extends keyof T>(array: T[], key: K) {
15 const map = new Map<T[K] | null | undefined, T>();
16
17 for (const value of array) {
18 map.set(value[key], value);
19 }
20
21 return map;
22}