Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/array/groupBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
* @template K - The type of keys.
* @param {T[]} arr - The array to group.
* @param {(item: T) => K} getKeyFromItem - A function that generates a key from an element.
* @returns {Record<K, T[]>} An object where each key is associated with an array of elements that
* share that key.
* @returns {Partial<Record<K, T[]>>} An object where each key is associated with an array of elements that
* share that key. If a specific key exists in K but at runtime no array element groups to that specific key,
* then that key maps to undefined rather than T[].
*
* @example
* const array = [
Expand All @@ -30,8 +31,11 @@
* // ]
* // }
*/
export function groupBy<T, K extends PropertyKey>(arr: readonly T[], getKeyFromItem: (item: T) => K): Record<K, T[]> {
const result = {} as Record<K, T[]>;
export function groupBy<T, K extends PropertyKey>(
arr: readonly T[],
getKeyFromItem: (item: T) => K
): Partial<Record<K, T[]>> {
const result = {} as Partial<Record<K, T[]>>;

for (let i = 0; i < arr.length; i++) {
const item = arr[i];
Expand All @@ -41,7 +45,7 @@ export function groupBy<T, K extends PropertyKey>(arr: readonly T[], getKeyFromI
result[key] = [];
}

result[key].push(item);
result[key]!.push(item);
}

return result;
Expand Down
11 changes: 6 additions & 5 deletions src/compat/array/groupBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function groupBy<T>(
export function groupBy<T extends object>(
collection: T | null | undefined,
iteratee?: ValueIteratee<T[keyof T]>
): Record<string, Array<T[keyof T]>>;
): Partial<Record<string, Array<T[keyof T]>>>;

/**
* Groups the elements of an array or object based on a provided key-generating function.
Expand All @@ -58,8 +58,9 @@ export function groupBy<T extends object>(
* - If a property name (string) is provided, that property of each element is used as the key.
* - If a property-value pair (array) is provided, elements with matching property values are used.
* - If a partial object is provided, elements with matching properties are used.
* @returns {Record<K, T>} An object where each key is associated with an array of elements that
* share that key.
* @returns {Partial<Record<K, T>>} An object where each key is associated with an array of elements that
* share that key. If a specific key exists in K but at runtime no array element groups to that specific key,
* then that key maps to undefined rather than T[].
*
* @example
* // Using an array
Expand All @@ -81,9 +82,9 @@ export function groupBy<T, K extends PropertyKey>(
| [keyof T, unknown]
| PropertyKey
| null
): Record<K, T[]> {
): Partial<Record<K, T[]>> {
if (source == null) {
return {} as Record<K, T[]>;
return {} as Partial<Record<K, T[]>>;
}

const items = isArrayLike(source) ? Array.from(source) : Object.values(source);
Expand Down