File: groupBy.ts

package info (click to toggle)
node-immutable 4.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,676 kB
  • sloc: javascript: 8,303; sh: 17; makefile: 6
file content (98 lines) | stat: -rw-r--r-- 2,976 bytes parent folder | download
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import {
  Collection,
  Map,
  Seq,
  isOrdered,
  OrderedMap,
  List,
  OrderedSet,
  Set,
  Stack,
  Record,
} from 'immutable';

describe('groupBy', () => {
  it.each`
    constructor   | constructorIsOrdered | isObject
    ${Collection} | ${true}              | ${false}
    ${List}       | ${true}              | ${false}
    ${Seq}        | ${true}              | ${false}
    ${Set}        | ${false}             | ${false}
    ${Stack}      | ${true}              | ${false}
    ${OrderedSet} | ${true}              | ${false}
    ${Map}        | ${false}             | ${true}
    ${OrderedMap} | ${true}              | ${true}
  `(
    'groupBy returns ordered or unordered of the base type is ordered or not: $constructor.name',
    ({ constructor, constructorIsOrdered, isObject }) => {
      const iterableConstructor = ['a', 'b', 'a', 'c'];
      const objectConstructor = { a: 1, b: 2, c: 3, d: 1 };

      const col = constructor(
        isObject ? objectConstructor : iterableConstructor
      );

      const grouped = col.groupBy(v => v);

      // all groupBy should be instance of Map
      expect(grouped).toBeInstanceOf(Map);

      // ordered objects should be instance of OrderedMap
      expect(isOrdered(col)).toBe(constructorIsOrdered);
      expect(isOrdered(grouped)).toBe(constructorIsOrdered);
      if (constructorIsOrdered) {
        expect(grouped).toBeInstanceOf(OrderedMap);
      } else {
        expect(grouped).not.toBeInstanceOf(OrderedMap);
      }
    }
  );

  it('groups keyed sequence', () => {
    const grouped = Seq({ a: 1, b: 2, c: 3, d: 4 }).groupBy(x => x % 2);
    expect(grouped.toJS()).toEqual({ 1: { a: 1, c: 3 }, 0: { b: 2, d: 4 } });

    // Each group should be a keyed sequence, not an indexed sequence
    const firstGroup = grouped.get(1);
    expect(firstGroup && firstGroup.toArray()).toEqual([
      ['a', 1],
      ['c', 3],
    ]);
  });

  it('groups indexed sequence', () => {
    const group = Seq([1, 2, 3, 4, 5, 6]).groupBy(x => x % 2);

    expect(group.toJS()).toEqual({ 1: [1, 3, 5], 0: [2, 4, 6] });
  });

  it('groups to keys', () => {
    const group = Seq([1, 2, 3, 4, 5, 6]).groupBy(x =>
      x % 2 ? 'odd' : 'even'
    );
    expect(group.toJS()).toEqual({ odd: [1, 3, 5], even: [2, 4, 6] });
  });

  it('groups indexed sequences, maintaining indicies when keyed sequences', () => {
    const group = Seq([1, 2, 3, 4, 5, 6]).groupBy(x => x % 2);

    expect(group.toJS()).toEqual({ 1: [1, 3, 5], 0: [2, 4, 6] });

    const keyedGroup = Seq([1, 2, 3, 4, 5, 6])
      .toKeyedSeq()
      .groupBy(x => x % 2);

    expect(keyedGroup.toJS()).toEqual({
      1: { 0: 1, 2: 3, 4: 5 },
      0: { 1: 2, 3: 4, 5: 6 },
    });
  });

  it('has groups that can be mapped', () => {
    const mappedGroup = Seq([1, 2, 3, 4, 5, 6])
      .groupBy(x => x % 2)
      .map(group => group.map(value => value * 10));

    expect(mappedGroup.toJS()).toEqual({ 1: [10, 30, 50], 0: [20, 40, 60] });
  });
});