/
groupByOmit.test.ts
49 lines (45 loc) · 1.17 KB
/
groupByOmit.test.ts
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
import { describe, expect, it } from "vitest"
import { groupByOmit } from "../groupByOmit"
describe("groupByOmit", () => {
it("should group by single key and omit it in result", () => {
const data = [
{ id: 1, name: "Alice", age: 30 },
{ id: 2, name: "Bob", age: 30 },
{ id: 3, name: "Alice", age: 25 },
]
const result = groupByOmit(data, ["name"])
expect(result).toEqual({
Alice: [
{ id: 1, age: 30 },
{ id: 3, age: 25 },
],
Bob: [{ id: 2, age: 30 }],
})
})
it("should group by multiple keys and omit them in results", () => {
type TestData = {
prop1: string
prop2: string
prop3: string
prop4: string
}
const data: TestData[] = [
{ prop1: "a", prop2: "b", prop3: "c", prop4: "d" },
{ prop1: "a", prop2: "b", prop3: "c", prop4: "d" },
{ prop1: "a", prop2: "c", prop3: "c", prop4: "d" },
{ prop1: "a", prop2: "c", prop3: "d", prop4: "d" },
]
const result = groupByOmit(data, ["prop1", "prop2"])
const expected = {
ab: [
{ prop3: "c", prop4: "d" },
{ prop3: "c", prop4: "d" },
],
ac: [
{ prop3: "c", prop4: "d" },
{ prop3: "d", prop4: "d" },
],
}
expect(result).toEqual(expected)
})
})