-
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
e5b7279
commit 7bd2cc2
Showing
9 changed files
with
348 additions
and
410 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,27 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import { groupBy } from './groupBy.ts' | ||
|
||
describe('array groupBy', () => { | ||
|
||
test('empty array', () => { | ||
expect(groupBy([], () => 'p')).toEqual({}) | ||
}) | ||
|
||
test('selecting to same property', () => { | ||
expect(groupBy([1, 2, 3], () => 'p')).toEqual({ | ||
p: [1, 2, 3], | ||
}) | ||
}) | ||
|
||
test('group array of objects by property key', () => { | ||
expect( | ||
groupBy( | ||
[{ id: '1', value: 1 }, { id: '2', value: 2 }, { id: '1', value: 3 }], | ||
(item) => item.id | ||
) | ||
).toEqual({ | ||
1: [{ id: '1', value: 1 }, { id: '1', value: 3 }], | ||
2: [{ id: '2', value: 2 }], | ||
}) | ||
}) | ||
}) |
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,23 @@ | ||
import { NonEmptyArray } from '../types/index.js' | ||
|
||
export type Grouping<T> = Record<PropertyKey, NonEmptyArray<T>> | ||
|
||
/** | ||
* Groups an array of items by a property-key | ||
* Example: | ||
* groupBy( | ||
* [{ id: "1", value: 1 },{ id: "2", value: 2 },{ id: "1", value: 3 }], | ||
* (item) => item.id | ||
* ) === { | ||
* 1: [{ id: "1", value: 1 },{ id: "1", value: 3 }], | ||
* 2: [{ id: "2", value: 2 }] | ||
* } | ||
*/ | ||
export const groupBy = <T>(array: readonly T[], keySelector: (item: T) => PropertyKey): Grouping<T> => | ||
array.reduce<Grouping<T>>((group, item) => { | ||
const key = keySelector(item) | ||
return { | ||
...group, | ||
[key]: group[key] == null ? <NonEmptyArray<T>>[item] : <NonEmptyArray<T>>[...group[key], item], | ||
} | ||
}, {}) |
Empty file.
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
Oops, something went wrong.