-
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
Showing
4 changed files
with
54 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { groupBy } from './groupBy' | ||
|
||
describe('groupBy', () => { | ||
it('should group by a string', () => { | ||
const array = [ | ||
{ name: 'John', age: 20 }, | ||
{ name: 'Jane', age: 25 }, | ||
{ name: 'Jack', age: 20 }, | ||
] | ||
|
||
const result = groupBy(array, item => `${item.age}`) | ||
expect(result).toEqual({ | ||
20: [ | ||
{ name: 'John', age: 20 }, | ||
{ name: 'Jack', age: 20 }, | ||
], | ||
25: [{ name: 'Jane', age: 25 }], | ||
}) | ||
|
||
const arr2 = [4.2, 6.3, 6.5] | ||
const result2 = groupBy(arr2, v => `${Math.floor(v)}`) | ||
expect(result2).toEqual({ | ||
4: [4.2], | ||
6: [6.3, 6.5], | ||
}) | ||
}) | ||
}) |
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 @@ | ||
/** | ||
* group an array by a string | ||
* @param {T[]} array - The array to iterate over. | ||
* @param iteratee - (item: T) => string | ||
* @returns {Record<string, T[]>} Returns the composed aggregate object. | ||
* @example | ||
* ``` | ||
groupBy([4.2, 6.3, 6.5], v => `${Math.floor(v)}`) | ||
// { '4': [ 4.2 ], '6': [ 6.3, 6.5 ] } | ||
groupBy([{ name: 'John', age: 20 }, { name: 'Jane', age: 25 }], item => `${item.age}`) | ||
// { '20': [ { name: 'John', age: 20 } ], '25': [ { name: 'Jane', age: 25 } ] } | ||
* ``` | ||
*/ | ||
export function groupBy<T>(array: T[], iteratee: (item: T) => string): Record<string, T[]> { | ||
return array.reduce<Record<string, T[]>>((acc, item) => { | ||
const key = iteratee(item) | ||
if (acc[key]) | ||
acc[key].push(item) | ||
else | ||
acc[key] = [item] | ||
|
||
return acc | ||
}, {}) | ||
} |
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