/
GroupByWithSel.ts
108 lines (83 loc) · 4.49 KB
/
GroupByWithSel.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
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
99
100
101
102
103
104
105
106
107
108
import { EqualityComparer } from "linq-to-typescript"
import { asAsync, itAsync, itEnumerable, itParallel } from "../TestHelpers"
// tslint:disable:variable-name
describe("groupByWithSel", () => {
itEnumerable<{ key: string, value: number }>("ObjectSelect", (asEnumerable) => {
const array = asEnumerable([{ key: "foo", value: 0 }, { key: "foo", value: 1 }, { key: "bar", value: 3}])
const grouping = array.groupByWithSel((x) => x.key, (x) => x.value)
const groupingArray = grouping.toArray()
expect(groupingArray[0].key).toBe("foo")
expect(groupingArray[0].toArray()).toEqual([0, 1])
expect(groupingArray[1].key).toBe("bar")
expect(groupingArray[1].toArray()).toEqual([3])
})
itAsync("ObjectSelect", async () => {
const array = asAsync([{ key: "foo", value: 0 }, { key: "foo", value: 1 }, { key: "bar", value: 3}])
const grouping = array.groupByWithSel((x) => x.key, (x) => x.value)
const groupingArray = await grouping.toArray()
expect(groupingArray[0].key).toBe("foo")
expect(groupingArray[0].toArray()).toEqual([0, 1])
expect(groupingArray[1].key).toBe("bar")
expect(groupingArray[1].toArray()).toEqual([3])
})
itParallel<{ key: string, value: number }>("ObjectSelect", async (asParallel) => {
const array = asParallel([{ key: "foo", value: 0 }, { key: "foo", value: 1 }, { key: "bar", value: 3}])
const grouping = array.groupByWithSel((x) => x.key, (x) => x.value)
const groupingArray = await grouping.toArray()
expect(groupingArray[0].key).toBe("foo")
expect(groupingArray[0].toArray()).toEqual([0, 1])
expect(groupingArray[1].key).toBe("bar")
expect(groupingArray[1].toArray()).toEqual([3])
})
itEnumerable<{ key: string, value: any }>("ObjectSelectWithComparer", (asEnumerable) => {
const array = asEnumerable([{ key: "foo", value: "0" }, { key: "foo", value: 1 }, { key: "bar", value: 3}])
const grouping = array.groupByWithSel((x) => x.key, (x) => x.value, EqualityComparer)
const groupingArray = grouping.toArray()
expect(groupingArray[0].key).toBe("foo")
expect(groupingArray[0].toArray()).toEqual(["0", 1])
expect(groupingArray[1].key).toBe("bar")
expect(groupingArray[1].toArray()).toEqual([3])
})
itAsync("ObjectSelectWithComparer", async () => {
const array = asAsync([{ key: "foo", value: "0" }, { key: "foo", value: 1 }, { key: "bar", value: 3}])
const grouping = array.groupByWithSel((x) => x.key, (x) => x.value, EqualityComparer)
const groupingArray = await grouping.toArray()
expect(groupingArray[0].key).toBe("foo")
expect(groupingArray[0].toArray()).toEqual(["0", 1])
expect(groupingArray[1].key).toBe("bar")
expect(groupingArray[1].toArray()).toEqual([3])
})
itParallel<{ key: string, value: number | string }>("ObjectSelectWithComparer", async (asParallel) => {
const array = asParallel([{ key: "foo", value: "0" }, { key: "foo", value: 1 }, { key: "bar", value: 3}])
const grouping = array.groupByWithSel((x) => x.key, (x) => x.value, EqualityComparer)
const groupingArray = await grouping.toArray()
expect(groupingArray[0].key).toBe("foo")
expect(groupingArray[0].toArray()).toEqual(["0", 1])
expect(groupingArray[1].key).toBe("bar")
expect(groupingArray[1].toArray()).toEqual([3])
})
itEnumerable<number>("SingleKey", (asEnumerable) => {
const singleKey = "singleKey"
const grouping = asEnumerable([1, 2, 3]).groupByWithSel((_x) => singleKey, (x) => x.toString())
for (const group of grouping) {
expect(group.key).toBe(singleKey)
expect(group.toArray()).toEqual(["1", "2", "3"])
}
})
itAsync("SingleKey", async () => {
const singleKey = "singleKey"
const grouping = asAsync([1, 2, 3]).groupByWithSel((_x) => singleKey, (x) => x.toString())
for await (const group of grouping) {
expect(group.key).toBe(singleKey)
expect(group.toArray()).toEqual(["1", "2", "3"])
}
})
itParallel("SingleKey", async (asParallel) => {
const singleKey = "singleKey"
const grouping = asParallel([1, 2, 3]).groupByWithSel((_x) => singleKey, (x) => x.toString())
for await (const group of grouping) {
expect(group.key).toBe(singleKey)
expect(group.toArray()).toEqual(["1", "2", "3"])
}
})
})