aboutsummaryrefslogtreecommitdiff
path: root/test/internal/group-by.spec.ts
blob: 4f186e3dd2c36cd291aef89af77b96db0a15aa48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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],
    });
  });
});