Skip to content

Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️

495 B
Raw Blame
export interface ConsecutiveGroup<T> {
    key: string;
    items: T[];
}

export const groupConsecutive = <T>(items: T[], keyOf: (item: T) => string): ConsecutiveGroup<T>[] =>
    items.reduce<ConsecutiveGroup<T>[]>((groups, item) => {
        const key = keyOf(item);
        const last = groups[groups.length - 1];
        if (last?.key === key) {
            last.items.push(item);
        } else {
            groups.push({key, items: [item]});
        }
        return groups;
    }, []);