-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f66dc0
commit 2942e8b
Showing
3 changed files
with
33 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@opentf/utils": minor | ||
--- | ||
|
||
Fixed groupBy types. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
export type GroupKey = (a: unknown) => string | string; | ||
export type GroupKey<T> = ((a: T) => string) | string; | ||
|
||
export default function groupBy(arr: unknown[], key: GroupKey) { | ||
return arr.reduce((acc: Record<string, unknown>, obj) => { | ||
export default function groupBy<T>( | ||
arr: T[], | ||
key: GroupKey<T> | ||
): Record<string, T[]> { | ||
return arr.reduce((acc: Record<string, T[]>, obj: T) => { | ||
const k = | ||
typeof key === 'function' ? key(obj) : (obj as Iterable<unknown>)[key]; | ||
typeof key === 'function' ? key(obj) : (obj[key as keyof T] as string); | ||
const curGroup = acc[k] ?? []; | ||
return { ...acc, [k]: [...(curGroup as unknown[]), obj] }; | ||
return { ...acc, [k]: [...curGroup, obj] }; | ||
}, {}); | ||
} |