-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathgroupBy.test.js
More file actions
60 lines (49 loc) · 1.27 KB
/
groupBy.test.js
File metadata and controls
60 lines (49 loc) · 1.27 KB
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
// FIXME: npm test /src/utils/groupBy.test.js
// 어떤 것을 해볼까요?
const groupBy = (arr, callback) => {
return arr.reduce((acc, val) => {
const key = callback(val);
if (acc[key] === undefined) acc[key] = [];
acc[key].push(val);
return acc;
}, {});
};
describe("groupBy 테스트", () => {
describe("non-lazy", () => {
it("case: 1, Normal", () => {
const array = [6.1, 4.2, 6.3];
const grouped = groupBy(array, Math.floor);
expect(grouped).toEqual({ 4: [4.2], 6: [6.1, 6.3] });
});
it("case: 2, Advanced", () => {
const array = [
[1, "a"],
[2, "a"],
[2, "b"],
];
// 두 번째 인자가 index
const [groupedFirstIndex, groupedSecondIndex] = [
groupBy(array, 0),
groupBy(array, 1),
];
expect(groupedFirstIndex).toEqual({
1: [[1, "a"]],
2: [
[2, "a"],
[2, "b"],
],
});
expect(groupedSecondIndex).toEqual({
a: [
[1, "a"],
[2, "a"],
],
b: [[2, "b"]],
});
});
it("case: 3, Advanced", () => {
const grouped = groupBy({ a: 6.1, b: 4.2, c: 6.3 }, Math.floor);
expect(grouped).toEqual({ 4: [4.2], 6: [6.1, 6.3] });
});
});
});