deno.land / x / linq@4.0.2 / test / grouping.js

نووسراو ببینە
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
109
110
111
112
113
import { test, testModule, deepEqual } from './testutils.js'import Enumerable from '../linq.js'
testModule("Grouping");
var fileList = ["temp.xls", "temp2.xls", "temp.pdf", "temp.jpg", "temp2.pdf", "temp3.xls"];
test("groupBy", function (){ let actual = Enumerable.from(fileList) .groupBy("file=>file.match(/\\.(.+$)/)[1]") .select("{key:$.key(),value:$.toArray()}") .toArray(); let expected = [{ key: "xls", value: ["temp.xls", "temp2.xls", "temp3.xls"] }, { key: "pdf", value: ["temp.pdf", "temp2.pdf"] }, { key: "jpg", value: ["temp.jpg"]}]; deepEqual(actual, expected);
actual = Enumerable.from(fileList) .groupBy("file=>file.match(/\\.(.+$)/)[1]", "file=>file.match(/(^.+)\\..+$/)[1]") .select("{key:$.key(),value:$.toArray()}") .toArray(); expected = [{ key: "xls", value: ["temp", "temp2", "temp3"] }, { key: "pdf", value: ["temp", "temp2"] }, { key: "jpg", value: ["temp"]}]; deepEqual(actual, expected);
actual = Enumerable.from(fileList).groupBy("file=>file.match(/\\.(.+$)/)[1]", "file=>file", "ext,group => {extension:ext,count:group.count(),files:group.toArray()}") .toArray(); expected = [{ extension: "xls", count: 3, files: ["temp.xls", "temp2.xls", "temp3.xls"] }, { extension: "pdf", count: 2, files: ["temp.pdf", "temp2.pdf"] }, { extension: "jpg", count: 1, files: ["temp.jpg"]}]; deepEqual(actual, expected);
var objects = [ { Date: new Date(2000, 1, 1), Id: 1 }, { Date: new Date(2010, 5, 5), Id: 2 }, { Date: new Date(2000, 1, 1), Id: 3 } ] actual = Enumerable.from(objects) .groupBy("$.Date", "$.Id", function (key, group) { return key.getFullYear() + "-" + group.toJoinedString(',') }, function (key) { return key.toString() }) .toArray(); expected = ["2000-1,3", "2010-2"] deepEqual(actual, expected);});
test("partitionBy", function (){ let actual = Enumerable.from(fileList) .partitionBy("file=>file.match(/\\.(.+$)/)[1]") .select("{key:$.key(),value:$.toArray()}") .toArray(); let expected = [{ key: "xls", value: ["temp.xls", "temp2.xls"] }, { key: "pdf", value: ["temp.pdf"] }, { key: "jpg", value: ["temp.jpg"] }, { key: "pdf", value: ["temp2.pdf"] }, { key: "xls", value: ["temp3.xls"] } ]; deepEqual(actual, expected);
actual = Enumerable.from(fileList) .partitionBy("file=>file.match(/\\.(.+$)/)[1]", "file=>file.match(/(^.+)\\..+$/)[1]") .select("{key:$.key(),value:$.toArray()}") .toArray(); expected = [{ key: "xls", value: ["temp", "temp2"] }, { key: "pdf", value: ["temp"] }, { key: "jpg", value: ["temp"] }, { key: "pdf", value: ["temp2"] }, { key: "xls", value: ["temp3"] } ]; deepEqual(actual, expected);
actual = Enumerable.from(fileList) .partitionBy("file=>file.match(/\\.(.+$)/)[1]", "file=>file", "ext,group=>{extension:ext,count:group.count(),files:group.toArray()}") .toArray(); expected = [{ extension: "xls", count: 2, files: ["temp.xls", "temp2.xls"] }, { extension: "pdf", count: 1, files: ["temp.pdf"] }, { extension: "jpg", count: 1, files: ["temp.jpg"] }, { extension: "pdf", count: 1, files: ["temp2.pdf"] }, { extension: "xls", count: 1, files: ["temp3.xls"] } ]; deepEqual(actual, expected);
var objects = [ { Date: new Date(2000, 1, 1), Id: 1 }, { Date: new Date(2000, 1, 1), Id: 2 }, { Date: new Date(2010, 5, 5), Id: 3 }, { Date: new Date(2000, 1, 1), Id: 4 }, { Date: new Date(2010, 5, 5), Id: 5 }, { Date: new Date(2010, 5, 5), Id: 6 } ] actual = Enumerable.from(objects) .partitionBy("$.Date", "$.Id", function (key, group) { return key.getFullYear() + "-" + group.toJoinedString(',') }, function (key) { return key.toString() }) .toArray(); expected = ["2000-1,2", "2010-3", "2000-4", "2010-5,6"] deepEqual(actual, expected);});
test("buffer", function (){ let actual = Enumerable.range(1, 10).buffer("3").toArray(); let expected = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]; deepEqual(actual, expected);});
linq

Version Info

Tagged at
7 months ago