-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linq.nut
224 lines (167 loc) · 4.97 KB
/
Linq.nut
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
if ("Linq" in getroottable() && typeof ::Linq == "class" )
return;
class Linq {
constructor (_arr) {
if (typeof _arr != "array")
throw "Invalid constructor parameter's type. Expected array."
arr = _arr;
}
arr = null;
static Error = {
NoMatch = "No match for predicate",
MoreThanOneMatch = "More than one match",
DuplicateKeys = "Duplicate keys in array element's attributes"
}
function Where(predicate) {
local result = [];
for (local i = 0; i < arr.len(); i++) {
if ( predicate(arr[i]) )
result.append(arr[i])
}
return Linq(result);
}
function First(predicate = null) {
if (!predicate)
return arr[0]
for (local i = 0; i < arr.len(); i++) {
if ( predicate(arr[i]) )
return arr[i]
}
throw Error.NoMatch;
}
function Last(predicate = null) {
if (!predicate)
return arr.top()
local result = null;
local found = false;
foreach (element in arr) {
if ( predicate(element) ) {
result = element;
found = true;
}
}
if (found) return result;
throw Error.NoMatch;
}
function Single(predicate) {
local result = null;
local count = 0;
foreach (element in arr) {
if ( predicate(element) ) {
result = element;
count++;
}
}
switch (count) {
case 0: throw Error.NoMatch;
case 1: return result;
}
throw Error.MoreThanOneMatch
}
function Any(predicate = null) {
if (predicate == null)
return arr.len() > 0
foreach (element in arr) {
if ( predicate(element) )
return true;
}
return false;
}
function All(predicate) {
foreach (element in arr) {
if ( !predicate(element) )
return false;
}
return true;
}
function Contains(value) {
foreach (element in arr) {
if (element == value)
return true;
}
return false;
}
function OrderBy(propertyName) {
local result = arr;
local modified = true;
while (modified) {
modified = false;
for (local i = 0; i < (result.len() - 1); i++) {
local currentVal = result[i];
local nextVal = result[i + 1];
if (currentVal[propertyName] > nextVal[propertyName]) {
result[i] = nextVal;
result[i + 1] = currentVal;
modified = true;
}
}
}
return Linq(result);
}
function OrderByDescending(propertyName) {
local result = arr;
local modified = true;
while (modified) {
modified = false;
for (local i = result.len(); i >= 1; i--) {
local currentVal = result[i];
local nextVal = result[i - 1];
if (currentVal[propertyName] < nextVal[propertyName]) {
result[i] = nextVal;
result[i - 1] = currentVal;
modified = true;
}
}
}
return Linq(result);
}
function Skip(count) {
return Linq(arr.slice(count))
}
function Take(count) {
arr.resize(count)
return Linq(arr)
}
function Max(propertyName = null) {
if (!propertyName) {
arr.sort()
return arr.top();
}
return OrderBy(propertyName).Last()
}
function Min(propertyName = null) {
if (!propertyName) {
arr.sort()
return arr[0];
}
return OrderBy(propertyName).First()
}
function Sum(propertyName = null) {
local result = 0;
foreach (element in arr) {
result += propertyName != null ? element[propertyName] : element
}
return result;
}
function Average(propertyName = null) {
return Sum(propertyName) / Count();
}
function Count(predicate = null) {
if (!predicate)
return arr.len();
return arr.Where(predicate).Count();
}
function ToTable(keyPropertyName, valuePropertyName = null) {
local result = {};
foreach (element in arr) {
local key = element[keyPropertyName];
if ( key in result )
throw Error.DuplicateKeys
result[key] <- valuePropertyName == null ? element : element[valuePropertyName];
}
return result;
}
function ToArray() {
return arr;
}
}