Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reuse
groupBy
in validation rules (#3307)
- Loading branch information
1 parent
96b146d
commit 71c7a14
Showing
6 changed files
with
62 additions
and
67 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,19 @@ | ||
/** | ||
* Groups array items into a Map, given a function to produce grouping key. | ||
*/ | ||
export function groupBy<K, T>( | ||
list: ReadonlyArray<T>, | ||
keyFn: (item: T) => K, | ||
): Map<K, ReadonlyArray<T>> { | ||
const result = new Map<K, Array<T>>(); | ||
for (const item of list) { | ||
const key = keyFn(item); | ||
const group = result.get(key); | ||
if (group === undefined) { | ||
result.set(key, [item]); | ||
} else { | ||
group.push(item); | ||
} | ||
} | ||
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
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