Files
twenty/packages/twenty-front/src/utils/array/groupArrayItemsBy.ts
Thaïs dd961209de feat: add event rows to Show Page Calendar tab (#4319)
* feat: add event rows to Show Page Calendar tab

Closes #4287

* refactor: use time as events group key instead of ISO string for easier sorting

* feat: implement data model changes

* refactor: improve sorting
2024-03-07 11:13:22 +01:00

33 lines
973 B
TypeScript

/**
* Groups an array of items by a key computed from each item.
*
* @param array - The array to group.
* @param computeGroupKey - A function that computes the group key to which the item belongs.
*
* @returns An object with items grouped by a computed key.
*
* @example
* groupArrayItemsBy(
* [{ id: '1', type: 'fruit' }, { id: '2', type: 'vegetable' }, { id: '3', type: 'fruit' }],
* ({ type }) => type,
* )
* ⬇️
* {
* fruit: [{ id: '1', type: 'fruit' }, { id: '3', type: 'fruit' }],
* vegetable: [{ id: '2', type: 'vegetable' }],
* }
*/
export const groupArrayItemsBy = <ArrayItem, Key extends string | number>(
array: ArrayItem[],
computeGroupKey: (item: ArrayItem) => Key,
) =>
array.reduce<Partial<Record<Key, ArrayItem[]>>>((result, item) => {
const groupKey = computeGroupKey(item);
const previousGroup = result[groupKey] || [];
return {
...result,
[groupKey]: [...previousGroup, item],
};
}, {});