import { ensureEntryInMap } from "@sanjo/ensure-entry-in-map" export function groupByToMap( array: T[], predicate: (element: T) => GroupingType, ): Map { const groups = new Map() const createDefaultValue = (): T[] => [] for (const element of array) { const groupValue = predicate(element) ensureEntryInMap(groups, groupValue, createDefaultValue) const group = groups.get(groupValue)! group.push(element) } return groups }