export function toGroups( groupByFn: (item: TItem) => PropertyKey, ) { type GroupKey = ReturnType type GroupResult = Record const reducer = ( groups: Record, current: TItem, index: number, arr: TItem[], ): GroupResult => { const key = groupByFn(current) const group = groups[key] ?? [] groups[key] = [...group, current] return groups } return [reducer, {} as GroupResult] as const }