blob: e9b0acd5139d966e95e8588222f0355f43a4443f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
export function groupByCategory<
TItem extends { categoryName: string | null },
TCategory extends { name: string },
>(items: TItem[], categories: TCategory[]): [string, TItem[]][] {
const groups = new Map<string, TItem[]>();
for (const item of items) {
const key = item.categoryName ?? 'Non catégorisé';
const list = groups.get(key);
if (list) list.push(item);
else groups.set(key, [item]);
}
const result: [string, TItem[]][] = [];
for (const cat of categories) {
const list = groups.get(cat.name);
if (list?.length) result.push([cat.name, list]);
}
const uncategorized = groups.get('Non catégorisé');
if (uncategorized?.length) {
result.unshift(['Non catégorisé', uncategorized]);
}
return result;
}
|