export function list2Group<G, K extends keyof G>(list: G[], key: K): Record<K, G[]> {
  return list.reduce(
    (group, item) => {
      const k = item[key] as K;
      const g = group[k] ?? (group[k] = []);
      g.push(item);
      return group;
    },
    {} as Record<K, G[]>
  );
}

export function list2Groups<G, K extends keyof G>(list: G[], key: K, _label?: (key: string) => string): { key: K; groups: G[] }[] {
  return Object.entries<G[]>(list2Group(list, key)).map(([key, groups]) => ({ groups, key: key as K, label: _label?.(key) ?? key }));
}
