// Copyright 2021 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import { reduce } from './iterables'; /** * Like Lodash's `groupBy`, but returns a `Map`. */ export const groupBy = ( iterable: Iterable, fn: (value: T) => ResultT ): Map> => reduce( iterable, (result: Map>, value: T) => { const key = fn(value); const existingGroup = result.get(key); if (existingGroup) { existingGroup.push(value); } else { result.set(key, [value]); } return result; }, new Map>() );