All files / apps/huella-legal/layers/archive/app/utils groupByLetter.ts

100% Statements 9/9
100% Branches 2/2
100% Functions 1/1
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  2x 7x   7x 16x 16x   16x 11x     7x    
/** Splits an already sorted list by the first letter of each name, in the order the list has. */
export const groupByLetter = <T>(items: T[], name: (item: T) => string): [string, T[]][] => {
    const groups = new Map<string, T[]>();
 
    for (const item of items) {
        const letter = normalizeText(name(item)).charAt(0).toLocaleUpperCase("es");
        const group = groups.get(letter);
 
        if (group) group.push(item);
        else groups.set(letter, [item]);
    }
 
    return [...groups.entries()];
};