-
-
Notifications
You must be signed in to change notification settings - Fork 934
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
infra(unicorn): no-array-reduce (#2479)
- Loading branch information
Showing
6 changed files
with
76 additions
and
60 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
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Groups the values by the key function. | ||
* | ||
* @internal | ||
* | ||
* @param values The values to group. | ||
* @param keyFunction The function to get the key from the value. | ||
*/ | ||
export function groupBy<TValue>( | ||
values: ReadonlyArray<TValue>, | ||
keyFunction: (value: TValue) => string | number | ||
): Record<string, TValue[]> { | ||
const result: Record<string, TValue[]> = {}; | ||
|
||
for (const value of values) { | ||
const key = keyFunction(value); | ||
if (result[key] === undefined) { | ||
result[key] = []; | ||
} | ||
|
||
result[key].push(value); | ||
} | ||
|
||
return result; | ||
} |
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
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