test: improve utils coverage (#4230)

* test: improve utils coverage

* refactor: review - rename isDefined to isNonNullable, update tests and return statement
This commit is contained in:
Thaïs
2024-02-29 13:03:52 -03:00
committed by GitHub
parent 6ec0e5e995
commit 30df6c10ea
85 changed files with 396 additions and 240 deletions

View File

@ -1,8 +1,30 @@
export const groupArrayItemsBy = <Item, Key extends string>(
array: Item[],
computeGroupKey: (item: Item) => Key,
/**
* 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 extends Record<string, unknown>,
Key extends string,
>(
array: ArrayItem[],
computeGroupKey: (item: ArrayItem) => Key,
) =>
array.reduce<Partial<Record<Key, Item[]>>>((result, item) => {
array.reduce<Partial<Record<Key, ArrayItem[]>>>((result, item) => {
const groupKey = computeGroupKey(item);
const previousGroup = result[groupKey] || [];