diff options
| author | ST-DDT <[email protected]> | 2023-11-23 20:10:07 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-11-23 19:10:07 +0000 |
| commit | c2b952347e1f952cf379807a3c456fed65075d11 (patch) | |
| tree | a7b813f499c4dbf8420cf547b4b412993503b375 /test | |
| parent | 9b00fe9f7353df50c67966141a5f024ec9b95208 (diff) | |
| download | faker-c2b952347e1f952cf379807a3c456fed65075d11.tar.xz faker-c2b952347e1f952cf379807a3c456fed65075d11.zip | |
refactor: improve groupBy (#2532)
Diffstat (limited to 'test')
| -rw-r--r-- | test/internal/group-by.spec.ts | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/test/internal/group-by.spec.ts b/test/internal/group-by.spec.ts new file mode 100644 index 00000000..4f186e3d --- /dev/null +++ b/test/internal/group-by.spec.ts @@ -0,0 +1,41 @@ +import { describe, expect, it } from 'vitest'; +import { groupBy } from '../../src/internal/group-by'; + +describe('groupBy()', () => { + it('should group values by key', () => { + const values = [ + { id: 1, name: 'John' }, + { id: 2, name: 'Jane' }, + { id: 3, name: 'John' }, + ]; + + const result = groupBy(values, ({ name }) => name); + + expect(result).toEqual({ + John: [ + { id: 1, name: 'John' }, + { id: 3, name: 'John' }, + ], + Jane: [{ id: 2, name: 'Jane' }], + }); + }); + + it('should group by key and map values', () => { + const values = [ + { id: 1, name: 'John' }, + { id: 2, name: 'Jane' }, + { id: 3, name: 'John' }, + ]; + + const result = groupBy( + values, + ({ name }) => name, + ({ id }) => id + ); + + expect(result).toEqual({ + John: [1, 3], + Jane: [2], + }); + }); +}); |
