diff options
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], + }); + }); +}); |
