2021-03-11 15:29:31 -06:00
|
|
|
// Copyright 2019-2021 Signal Messenger, LLC
|
2020-10-30 15:34:04 -05:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-01-14 13:49:58 -08:00
|
|
|
export function makeLookup<T>(
|
2021-03-11 15:29:31 -06:00
|
|
|
items: ReadonlyArray<T>,
|
2020-09-14 14:56:35 -07:00
|
|
|
key: keyof T
|
2021-01-06 07:41:43 -08:00
|
|
|
): Record<string, T> {
|
|
|
|
return (items || []).reduce((lookup, item) => {
|
2021-01-27 14:39:45 -08:00
|
|
|
if (item !== undefined && item[key] !== undefined) {
|
2021-01-06 07:41:43 -08:00
|
|
|
// The force cast is necessary if we want the keyof T above, and the flexibility
|
|
|
|
// to pass anything in. And of course we're modifying a parameter!
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
lookup[String(item[key])] = item;
|
|
|
|
}
|
|
|
|
return lookup;
|
|
|
|
}, {} as Record<string, T>);
|
2019-01-14 13:49:58 -08:00
|
|
|
}
|