semantic-typescript 0.5.3 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/asynchronous/collector.d.ts +231 -0
- package/dist/asynchronous/collector.js +800 -0
- package/dist/asynchronous/semantic.d.ts +257 -0
- package/dist/asynchronous/semantic.js +1853 -0
- package/dist/factory.d.ts +71 -37
- package/dist/factory.js +443 -262
- package/dist/guard.d.ts +24 -14
- package/dist/guard.js +73 -19
- package/dist/hook.d.ts +6 -6
- package/dist/hook.js +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/optional.d.ts +2 -2
- package/dist/symbol.d.ts +19 -10
- package/dist/symbol.js +19 -10
- package/dist/synchronous/collector.d.ts +232 -0
- package/dist/{collector.js → synchronous/collector.js} +160 -151
- package/dist/{semantic.d.ts → synchronous/semantic.d.ts} +111 -120
- package/dist/{semantic.js → synchronous/semantic.js} +299 -337
- package/dist/utility.d.ts +7 -1
- package/dist/utility.js +1 -0
- package/package.json +1 -1
- package/readme.cn.md +158 -697
- package/readme.de.md +163 -432
- package/readme.es.md +163 -433
- package/readme.fr.md +162 -444
- package/readme.jp.md +162 -442
- package/readme.kr.md +161 -430
- package/readme.md +157 -1009
- package/readme.ru.md +161 -426
- package/readme.tw.md +161 -436
- package/dist/collector.d.ts +0 -236
- package/dist/main.d.ts +0 -1
- package/dist/main.js +0 -4
- package/dist/map.d.ts +0 -76
- package/dist/map.js +0 -245
- package/dist/node.d.ts +0 -182
- package/dist/node.js +0 -918
- package/dist/set.d.ts +0 -19
- package/dist/set.js +0 -65
- package/dist/tree.d.ts +0 -82
- package/dist/tree.js +0 -257
|
@@ -0,0 +1,800 @@
|
|
|
1
|
+
import { isFunction, isNumber, isBigInt, isBoolean, isString, isObject, isAsyncIterable, isAsyncFunction } from "../guard";
|
|
2
|
+
import { useCompare, useToBigInt, useToNumber } from "../hook";
|
|
3
|
+
import { AsynchronousCollectorSymbol } from "../symbol";
|
|
4
|
+
import { invalidate, validate } from "../utility";
|
|
5
|
+
export class AsynchronousCollector {
|
|
6
|
+
identity;
|
|
7
|
+
interrupt;
|
|
8
|
+
accumulator;
|
|
9
|
+
finisher;
|
|
10
|
+
AsynchronousCollector = AsynchronousCollectorSymbol;
|
|
11
|
+
constructor(identity, interrupt, accumulator, finisher) {
|
|
12
|
+
if (isFunction(identity) && isFunction(interrupt) && isFunction(accumulator) && isFunction(finisher)) {
|
|
13
|
+
this.identity = identity;
|
|
14
|
+
this.interrupt = interrupt;
|
|
15
|
+
this.accumulator = accumulator;
|
|
16
|
+
this.finisher = finisher;
|
|
17
|
+
Object.defineProperties(this, {
|
|
18
|
+
"identity": {
|
|
19
|
+
value: identity,
|
|
20
|
+
writable: false,
|
|
21
|
+
enumerable: true,
|
|
22
|
+
configurable: false
|
|
23
|
+
},
|
|
24
|
+
"interrupt": {
|
|
25
|
+
value: interrupt,
|
|
26
|
+
writable: false,
|
|
27
|
+
enumerable: true,
|
|
28
|
+
configurable: false
|
|
29
|
+
},
|
|
30
|
+
"accumulator": {
|
|
31
|
+
value: accumulator,
|
|
32
|
+
writable: false,
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: false
|
|
35
|
+
},
|
|
36
|
+
"finisher": {
|
|
37
|
+
value: finisher,
|
|
38
|
+
writable: false,
|
|
39
|
+
enumerable: true,
|
|
40
|
+
configurable: false
|
|
41
|
+
},
|
|
42
|
+
"AsynchronousCollector": {
|
|
43
|
+
value: AsynchronousCollectorSymbol,
|
|
44
|
+
writable: false,
|
|
45
|
+
enumerable: false,
|
|
46
|
+
configurable: false
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
Object.freeze(this);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
throw new TypeError("Invalid arguments");
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async collect(argument1, argument2) {
|
|
56
|
+
if (isAsyncFunction(argument1) || isFunction(argument1)) {
|
|
57
|
+
console.log("Function");
|
|
58
|
+
return await new Promise(async (resolve, reject) => {
|
|
59
|
+
try {
|
|
60
|
+
let generator = argument1;
|
|
61
|
+
let accumulator = this.identity();
|
|
62
|
+
let count = 0n;
|
|
63
|
+
await generator((element, index) => {
|
|
64
|
+
accumulator = this.accumulator(accumulator, element, index);
|
|
65
|
+
console.log(accumulator);
|
|
66
|
+
count++;
|
|
67
|
+
}, (element, index) => this.interrupt(element, index, accumulator));
|
|
68
|
+
resolve(this.finisher(accumulator));
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
reject(error);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
else if (isAsyncIterable(argument1)) {
|
|
76
|
+
return new Promise(async (resolve, reject) => {
|
|
77
|
+
try {
|
|
78
|
+
let iterable = argument1;
|
|
79
|
+
let accumulator = this.identity();
|
|
80
|
+
let count = 0n;
|
|
81
|
+
for await (let element of iterable) {
|
|
82
|
+
if (this.interrupt(element, count, accumulator)) {
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
accumulator = this.accumulator(accumulator, element, count);
|
|
86
|
+
count++;
|
|
87
|
+
}
|
|
88
|
+
resolve(this.finisher(accumulator));
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
reject(error);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
else if (isNumber(argument1) && isNumber(argument2)) {
|
|
96
|
+
return new Promise(async (resolve, reject) => {
|
|
97
|
+
try {
|
|
98
|
+
let start = argument1 < argument2 ? argument1 : argument2;
|
|
99
|
+
let end = argument1 > argument2 ? argument1 : argument2;
|
|
100
|
+
let accumulator = this.identity();
|
|
101
|
+
let count = 0n;
|
|
102
|
+
for (let i = start; i < end; i++) {
|
|
103
|
+
if (this.interrupt(i, count, accumulator)) {
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
accumulator = this.accumulator(accumulator, i, count);
|
|
107
|
+
count++;
|
|
108
|
+
}
|
|
109
|
+
resolve(this.finisher(accumulator));
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
reject(error);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
else if (isBigInt(argument1) && isBigInt(argument2)) {
|
|
117
|
+
return new Promise(async (resolve, reject) => {
|
|
118
|
+
try {
|
|
119
|
+
let start = argument1 < argument2 ? argument1 : argument2;
|
|
120
|
+
let end = argument1 > argument2 ? argument1 : argument2;
|
|
121
|
+
let accumulator = this.identity();
|
|
122
|
+
let count = 0n;
|
|
123
|
+
for (let i = start; i < end; i++) {
|
|
124
|
+
if (this.interrupt(i, count, accumulator)) {
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
accumulator = this.accumulator(accumulator, i, count);
|
|
128
|
+
count++;
|
|
129
|
+
}
|
|
130
|
+
resolve(this.finisher(accumulator));
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
reject(error);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
throw new Error("Invalid arguments.");
|
|
138
|
+
}
|
|
139
|
+
static full(identity, accumulator, finisher) {
|
|
140
|
+
return new AsynchronousCollector(identity, () => false, accumulator, finisher);
|
|
141
|
+
}
|
|
142
|
+
static shortable(identity, interrupt, accumulator, finisher) {
|
|
143
|
+
return new AsynchronousCollector(identity, interrupt, accumulator, finisher);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
;
|
|
147
|
+
;
|
|
148
|
+
export let useAsynchronousAnyMatch = (predicate) => {
|
|
149
|
+
if (isFunction(predicate)) {
|
|
150
|
+
return AsynchronousCollector.shortable(() => false, (_element, _index, accumulator) => isBoolean(accumulator) && accumulator, (accumulator, element, index) => accumulator || predicate(element, index), (accumulator) => accumulator);
|
|
151
|
+
}
|
|
152
|
+
throw new TypeError("Predicate must be a function.");
|
|
153
|
+
};
|
|
154
|
+
;
|
|
155
|
+
export let useAsynchronousAllMatch = (predicate) => {
|
|
156
|
+
if (isFunction(predicate)) {
|
|
157
|
+
return AsynchronousCollector.shortable(() => true, (_element, _index, accumulator) => isBoolean(accumulator) && !accumulator, (accumulator, element, index) => accumulator && predicate(element, index), (accumulator) => accumulator);
|
|
158
|
+
}
|
|
159
|
+
throw new TypeError("Predicate must be a function.");
|
|
160
|
+
};
|
|
161
|
+
;
|
|
162
|
+
export let useAsynchronousCollect = (argument1, argument2, argument3, argument4) => {
|
|
163
|
+
if (isFunction(argument1) && isFunction(argument2) && isFunction(argument3) && isFunction(argument4)) {
|
|
164
|
+
let identity = argument1;
|
|
165
|
+
let interrupt = argument2;
|
|
166
|
+
let accumulator = argument3;
|
|
167
|
+
let finisher = argument4;
|
|
168
|
+
return AsynchronousCollector.shortable(identity, interrupt, accumulator, finisher);
|
|
169
|
+
}
|
|
170
|
+
if (isFunction(argument1) && isFunction(argument2) && isFunction(argument3)) {
|
|
171
|
+
let identity = argument1;
|
|
172
|
+
let accumulator = argument2;
|
|
173
|
+
let finisher = argument3;
|
|
174
|
+
return AsynchronousCollector.full(identity, accumulator, finisher);
|
|
175
|
+
}
|
|
176
|
+
throw new TypeError("Identity, accumulator, and finisher must be functions.");
|
|
177
|
+
};
|
|
178
|
+
export let useAsynchronousCount = () => {
|
|
179
|
+
return AsynchronousCollector.full(() => 0n, (count) => count + 1n, (count) => count);
|
|
180
|
+
};
|
|
181
|
+
;
|
|
182
|
+
export let useAsynchronousError = (argument1, argument2, argument3) => {
|
|
183
|
+
if (isString(argument1) && isFunction(argument2) && isString(argument3)) {
|
|
184
|
+
let prefix = argument1;
|
|
185
|
+
let accumulator = argument2;
|
|
186
|
+
let suffix = argument3;
|
|
187
|
+
return AsynchronousCollector.full(() => prefix, accumulator, (text) => {
|
|
188
|
+
let result = text + suffix;
|
|
189
|
+
console.error(result);
|
|
190
|
+
return result;
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
else if (isFunction(argument1)) {
|
|
194
|
+
let prefix = "[";
|
|
195
|
+
let accumulator = argument2;
|
|
196
|
+
let suffix = "]";
|
|
197
|
+
return AsynchronousCollector.full(() => prefix, accumulator, (text) => {
|
|
198
|
+
let result = text + suffix;
|
|
199
|
+
console.error(result);
|
|
200
|
+
return result;
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
return AsynchronousCollector.full(() => "[", (accumulator, element) => {
|
|
205
|
+
if (isString(accumulator) && isString(element)) {
|
|
206
|
+
return accumulator + element + ",";
|
|
207
|
+
}
|
|
208
|
+
return String(accumulator) + String(element) + ",";
|
|
209
|
+
}, (text) => {
|
|
210
|
+
let result = text.substring(0, Math.max(1, text.length - 1)) + "]";
|
|
211
|
+
console.error(result);
|
|
212
|
+
return result;
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
;
|
|
217
|
+
export let useAsynchronousFindAt = (index) => {
|
|
218
|
+
let target = useToBigInt(index);
|
|
219
|
+
if (target < 0n) {
|
|
220
|
+
return AsynchronousCollector.full(() => [], (accumulator, element) => {
|
|
221
|
+
accumulator.push(element);
|
|
222
|
+
return accumulator;
|
|
223
|
+
}, (accumulator) => {
|
|
224
|
+
if (accumulator.length === 0) {
|
|
225
|
+
return Promise.reject(new Error("No element found."));
|
|
226
|
+
}
|
|
227
|
+
let limited = (((BigInt(accumulator.length)) % target) + target) % target;
|
|
228
|
+
return Promise.resolve(accumulator[Number(limited)]);
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
return AsynchronousCollector.shortable(() => [], (_element, _index, accumulator) => BigInt(accumulator.length) - 1n === target, (accumulator, element) => {
|
|
232
|
+
accumulator.push(element);
|
|
233
|
+
return accumulator;
|
|
234
|
+
}, (accumulator) => {
|
|
235
|
+
if (accumulator.length === 0) {
|
|
236
|
+
return Promise.reject(new Error("No element found."));
|
|
237
|
+
}
|
|
238
|
+
return Promise.resolve(accumulator[Number(target)]);
|
|
239
|
+
});
|
|
240
|
+
};
|
|
241
|
+
export let useAsynchronousFindFirst = () => {
|
|
242
|
+
return AsynchronousCollector.shortable(() => [], (_element, _index, accumulator) => validate(accumulator) && accumulator.length === 1, (accumulator, element) => {
|
|
243
|
+
if (validate(accumulator) && accumulator.length === 0) {
|
|
244
|
+
accumulator.push(element);
|
|
245
|
+
return accumulator;
|
|
246
|
+
}
|
|
247
|
+
return accumulator;
|
|
248
|
+
}, (accumulator) => {
|
|
249
|
+
if (validate(accumulator) && accumulator.length > 0) {
|
|
250
|
+
return Promise.resolve(accumulator[0]);
|
|
251
|
+
}
|
|
252
|
+
return Promise.reject(new Error("No element found."));
|
|
253
|
+
});
|
|
254
|
+
};
|
|
255
|
+
export let useAsynchronousFindAny = () => {
|
|
256
|
+
return AsynchronousCollector.shortable(() => [], (_element, _index, accumulator) => validate(accumulator) && accumulator.length == 1, (accumulator, element) => {
|
|
257
|
+
if (validate(accumulator) && accumulator.length === 0 && Math.random() < 0.5) {
|
|
258
|
+
accumulator.push(element);
|
|
259
|
+
return accumulator;
|
|
260
|
+
}
|
|
261
|
+
return accumulator;
|
|
262
|
+
}, (accumulator) => Promise.resolve(accumulator[0]));
|
|
263
|
+
};
|
|
264
|
+
export let useAsynchronousFindLast = () => {
|
|
265
|
+
return AsynchronousCollector.full(() => [], (accumulator, element) => {
|
|
266
|
+
if (validate(accumulator)) {
|
|
267
|
+
accumulator.push(element);
|
|
268
|
+
}
|
|
269
|
+
return accumulator;
|
|
270
|
+
}, (accumulator) => Promise.resolve(accumulator[accumulator.length - 1]));
|
|
271
|
+
};
|
|
272
|
+
export let useAsynchronousFindMaximum = (comparator = (useCompare)) => {
|
|
273
|
+
if (isFunction(comparator)) {
|
|
274
|
+
return AsynchronousCollector.full(() => Promise.reject(new Error("No element found.")), async (accumulator, element) => {
|
|
275
|
+
if (validate(accumulator)) {
|
|
276
|
+
return accumulator.then((accumulator) => {
|
|
277
|
+
if (comparator(accumulator, element) < 0) {
|
|
278
|
+
return element;
|
|
279
|
+
}
|
|
280
|
+
else {
|
|
281
|
+
return accumulator;
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
return Promise.reject(new Error("No element found."));
|
|
286
|
+
}, (result) => result);
|
|
287
|
+
}
|
|
288
|
+
throw new TypeError("Invalid argument.");
|
|
289
|
+
};
|
|
290
|
+
export let useAsynchronousFindMinimum = (comparator = (useCompare)) => {
|
|
291
|
+
if (isFunction(comparator)) {
|
|
292
|
+
return AsynchronousCollector.full(() => Promise.reject(new Error("No element found.")), async (accumulator, element) => {
|
|
293
|
+
if (validate(accumulator)) {
|
|
294
|
+
return accumulator.then((accumulator) => {
|
|
295
|
+
if (comparator(accumulator, element) > 0) {
|
|
296
|
+
return element;
|
|
297
|
+
}
|
|
298
|
+
else {
|
|
299
|
+
return accumulator;
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
return Promise.reject(new Error("No element found."));
|
|
304
|
+
}, (result) => result);
|
|
305
|
+
}
|
|
306
|
+
throw new TypeError("Invalid argument.");
|
|
307
|
+
};
|
|
308
|
+
;
|
|
309
|
+
export let useAsynchronousForEach = (action) => {
|
|
310
|
+
if (isFunction(action)) {
|
|
311
|
+
return AsynchronousCollector.full(() => 0n, (accumulator, element, index) => {
|
|
312
|
+
action(element, index);
|
|
313
|
+
return accumulator + 1n;
|
|
314
|
+
}, (accumulator) => accumulator);
|
|
315
|
+
}
|
|
316
|
+
throw new TypeError("Action must be a function.");
|
|
317
|
+
};
|
|
318
|
+
;
|
|
319
|
+
export let useAsynchronousNoneMatch = (predicate) => {
|
|
320
|
+
if (isFunction(predicate)) {
|
|
321
|
+
return AsynchronousCollector.shortable(() => true, (_element, _index, accumulator) => !accumulator, (accumulator, element, index) => accumulator && !predicate(element, index), (accumulator) => !accumulator);
|
|
322
|
+
}
|
|
323
|
+
throw new TypeError("Predicate must be a function.");
|
|
324
|
+
};
|
|
325
|
+
;
|
|
326
|
+
export let useAsynchronousGroup = (classifier) => {
|
|
327
|
+
if (isFunction(classifier)) {
|
|
328
|
+
return AsynchronousCollector.full(() => new Map(), (accumulator, element, index) => {
|
|
329
|
+
let key = classifier(element, index);
|
|
330
|
+
let group = accumulator.get(key) || [];
|
|
331
|
+
group.push(element);
|
|
332
|
+
accumulator.set(key, group);
|
|
333
|
+
return accumulator;
|
|
334
|
+
}, (accumulator) => accumulator);
|
|
335
|
+
}
|
|
336
|
+
throw new TypeError("Classifier must be a function.");
|
|
337
|
+
};
|
|
338
|
+
export let useAsynchronousGroupBy = (keyExtractor, valueExtractor = (element) => element) => {
|
|
339
|
+
if (isFunction(keyExtractor) && isFunction(valueExtractor)) {
|
|
340
|
+
return AsynchronousCollector.full(() => new Map(), (accumulator, element, index) => {
|
|
341
|
+
let key = keyExtractor(element, index);
|
|
342
|
+
let group = accumulator.get(key) || [];
|
|
343
|
+
group.push(valueExtractor(element, index));
|
|
344
|
+
accumulator.set(key, group);
|
|
345
|
+
return accumulator;
|
|
346
|
+
}, (accumulator) => accumulator);
|
|
347
|
+
}
|
|
348
|
+
throw new TypeError("Key extractor and value extractor must be functions.");
|
|
349
|
+
};
|
|
350
|
+
;
|
|
351
|
+
export let useAsynchronousJoin = (argument1, argument2, argument3) => {
|
|
352
|
+
if (invalidate(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
353
|
+
return AsynchronousCollector.full(() => "[", (text, element) => text + element + ",", (text) => text.substring(0, text.length - 1) + "]");
|
|
354
|
+
}
|
|
355
|
+
if (isString(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
356
|
+
let delimiter = argument1;
|
|
357
|
+
return AsynchronousCollector.full(() => "[", (text, element) => text + element + delimiter, (text) => text.substring(0, text.length - delimiter.length) + "]");
|
|
358
|
+
}
|
|
359
|
+
if (isString(argument1) && isFunction(argument2) && isString(argument3)) {
|
|
360
|
+
let prefix = argument1;
|
|
361
|
+
let accumulator = argument2;
|
|
362
|
+
let suffix = argument3;
|
|
363
|
+
return AsynchronousCollector.full(() => prefix, accumulator, (text) => text + suffix);
|
|
364
|
+
}
|
|
365
|
+
if (isString(argument1) && isString(argument2) && isString(argument3)) {
|
|
366
|
+
let prefix = argument1;
|
|
367
|
+
let delimiter = argument2;
|
|
368
|
+
let suffix = argument3;
|
|
369
|
+
return AsynchronousCollector.full(() => prefix, (text, element) => text + element + delimiter, (text) => text + suffix);
|
|
370
|
+
}
|
|
371
|
+
throw new TypeError("Invalid arguments.");
|
|
372
|
+
};
|
|
373
|
+
;
|
|
374
|
+
export let useAsynchronousLog = (argument1, argument2, argument3) => {
|
|
375
|
+
if (isString(argument1) && isFunction(argument2) && isString(argument3)) {
|
|
376
|
+
let prefix = argument1;
|
|
377
|
+
let accumulator = argument2;
|
|
378
|
+
let suffix = argument3;
|
|
379
|
+
return AsynchronousCollector.full(() => prefix, accumulator, (text) => {
|
|
380
|
+
let result = text + suffix;
|
|
381
|
+
console.log(result);
|
|
382
|
+
return result;
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
else if (isFunction(argument1)) {
|
|
386
|
+
let prefix = "[";
|
|
387
|
+
let accumulator = argument2;
|
|
388
|
+
let suffix = "]";
|
|
389
|
+
return AsynchronousCollector.full(() => prefix, accumulator, (text) => {
|
|
390
|
+
let result = text + suffix;
|
|
391
|
+
console.log(result);
|
|
392
|
+
return result;
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
else {
|
|
396
|
+
return AsynchronousCollector.full(() => "[", (accumulator, element) => {
|
|
397
|
+
console.log(element);
|
|
398
|
+
if (isString(accumulator) && isString(element)) {
|
|
399
|
+
return accumulator + element + ",";
|
|
400
|
+
}
|
|
401
|
+
return String(accumulator) + String(element) + ",";
|
|
402
|
+
}, (text) => {
|
|
403
|
+
let result = text.substring(0, Math.max(1, text.length - 1)) + "]";
|
|
404
|
+
console.log(result);
|
|
405
|
+
return result;
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
export let useAsynchronousPartition = (count) => {
|
|
410
|
+
if (isBigInt(count)) {
|
|
411
|
+
let limited = count > 1n ? count : 1n;
|
|
412
|
+
return AsynchronousCollector.full(() => {
|
|
413
|
+
return [];
|
|
414
|
+
}, (array, element) => {
|
|
415
|
+
let index = limited % BigInt(array.length);
|
|
416
|
+
if (index === 0n) {
|
|
417
|
+
array.push([]);
|
|
418
|
+
}
|
|
419
|
+
array[Number(index)].push(element);
|
|
420
|
+
return array;
|
|
421
|
+
}, (result) => {
|
|
422
|
+
return result;
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
throw new TypeError("Count must be a BigInt.");
|
|
426
|
+
};
|
|
427
|
+
;
|
|
428
|
+
export let useAsynchronousPartitionBy = (classifier) => {
|
|
429
|
+
if (isFunction(classifier)) {
|
|
430
|
+
return AsynchronousCollector.full(() => {
|
|
431
|
+
return [];
|
|
432
|
+
}, (array, element, index) => {
|
|
433
|
+
let resolved = classifier(element, index);
|
|
434
|
+
while (resolved > BigInt(array.length) - 1n) {
|
|
435
|
+
array.push([]);
|
|
436
|
+
}
|
|
437
|
+
array[Number(index)].push(element);
|
|
438
|
+
return array;
|
|
439
|
+
}, (result) => {
|
|
440
|
+
return result;
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
throw new TypeError("Classifier must be a function.");
|
|
444
|
+
};
|
|
445
|
+
;
|
|
446
|
+
export let useAsynchronousReduce = (argument1, argument2, argument3) => {
|
|
447
|
+
if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
448
|
+
let accumulator = argument1;
|
|
449
|
+
return AsynchronousCollector.full(() => Promise.reject(), async (result, element, index) => {
|
|
450
|
+
if (validate(result)) {
|
|
451
|
+
return result.then((result) => {
|
|
452
|
+
return accumulator(result, element, index);
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
return Promise.reject();
|
|
456
|
+
}, (result) => result);
|
|
457
|
+
}
|
|
458
|
+
else if (validate(argument1) && isFunction(argument2) && invalidate(argument3)) {
|
|
459
|
+
let identity = argument1;
|
|
460
|
+
let accumulator = argument2;
|
|
461
|
+
return AsynchronousCollector.full(() => identity, accumulator, (result) => result);
|
|
462
|
+
}
|
|
463
|
+
else if (validate(argument1) && isFunction(argument2) && isFunction(argument3)) {
|
|
464
|
+
let identity = argument1;
|
|
465
|
+
let accumulator = argument2;
|
|
466
|
+
let finisher = argument3;
|
|
467
|
+
return AsynchronousCollector.full(() => identity, accumulator, finisher);
|
|
468
|
+
}
|
|
469
|
+
else {
|
|
470
|
+
throw new TypeError("Invalid arguments.");
|
|
471
|
+
}
|
|
472
|
+
};
|
|
473
|
+
export let useAsynchronousToArray = () => {
|
|
474
|
+
return AsynchronousCollector.full(() => [], (array, element) => {
|
|
475
|
+
array.push(element);
|
|
476
|
+
return array;
|
|
477
|
+
}, (array) => array);
|
|
478
|
+
};
|
|
479
|
+
;
|
|
480
|
+
export let useAsynchronousToMap = (keyExtractor, valueExtractor = (element) => element) => {
|
|
481
|
+
if (isFunction(keyExtractor) && isFunction(valueExtractor)) {
|
|
482
|
+
return AsynchronousCollector.full(() => new Map(), (map, element, index) => {
|
|
483
|
+
let key = keyExtractor(element, index);
|
|
484
|
+
let value = valueExtractor(element, index);
|
|
485
|
+
map.set(key, value);
|
|
486
|
+
return map;
|
|
487
|
+
}, (map) => map);
|
|
488
|
+
}
|
|
489
|
+
throw new TypeError("Key extractor and value extractor must be functions.");
|
|
490
|
+
};
|
|
491
|
+
export let useAsynchronousToSet = () => {
|
|
492
|
+
return AsynchronousCollector.full(() => new Set(), (set, element) => {
|
|
493
|
+
set.add(element);
|
|
494
|
+
return set;
|
|
495
|
+
}, (set) => set);
|
|
496
|
+
};
|
|
497
|
+
;
|
|
498
|
+
export let useAsynchronousWrite = (argument1, argument2) => {
|
|
499
|
+
if (isObject(argument1)) {
|
|
500
|
+
if (isFunction(argument2)) {
|
|
501
|
+
let stream = argument1;
|
|
502
|
+
let accumulator = argument2;
|
|
503
|
+
return AsynchronousCollector.full(() => Promise.resolve(stream), (promise, element, index) => {
|
|
504
|
+
return new Promise((resolve, reject) => {
|
|
505
|
+
promise.then((stream) => {
|
|
506
|
+
try {
|
|
507
|
+
resolve(accumulator(stream, element, index));
|
|
508
|
+
}
|
|
509
|
+
catch (error) {
|
|
510
|
+
reject(error);
|
|
511
|
+
}
|
|
512
|
+
}).catch(reject);
|
|
513
|
+
});
|
|
514
|
+
}, (promise) => promise);
|
|
515
|
+
}
|
|
516
|
+
else {
|
|
517
|
+
let stream = argument1;
|
|
518
|
+
return AsynchronousCollector.full(() => Promise.resolve(stream), (promise, element) => {
|
|
519
|
+
return new Promise((resolve, reject) => {
|
|
520
|
+
promise.then((stream) => {
|
|
521
|
+
try {
|
|
522
|
+
let writer = stream.getWriter();
|
|
523
|
+
writer.write(String(element));
|
|
524
|
+
resolve(stream);
|
|
525
|
+
}
|
|
526
|
+
catch (error) {
|
|
527
|
+
reject(error);
|
|
528
|
+
}
|
|
529
|
+
}).catch(reject);
|
|
530
|
+
});
|
|
531
|
+
}, (promise) => promise);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
throw new TypeError("Invalid arguments.");
|
|
535
|
+
};
|
|
536
|
+
;
|
|
537
|
+
export let useAsynchronousNumericSummate = (mapper = useToNumber) => {
|
|
538
|
+
return AsynchronousCollector.full(() => 0, (accumulator, element) => {
|
|
539
|
+
let resolved = isNumber(element) ? element : mapper(element);
|
|
540
|
+
return accumulator + (isNumber(resolved) ? resolved : 0);
|
|
541
|
+
}, (result) => result);
|
|
542
|
+
};
|
|
543
|
+
;
|
|
544
|
+
export let useAsynchronousBigIntSummate = (mapper = useToBigInt) => {
|
|
545
|
+
return AsynchronousCollector.full(() => 0n, (accumulator, element) => {
|
|
546
|
+
let resolved = isBigInt(element) ? element : mapper(element);
|
|
547
|
+
return accumulator + (isBigInt(resolved) ? resolved : 0n);
|
|
548
|
+
}, (result) => result);
|
|
549
|
+
};
|
|
550
|
+
;
|
|
551
|
+
;
|
|
552
|
+
export let useAsynchronousNumericAverage = (mapper = useToNumber) => {
|
|
553
|
+
return AsynchronousCollector.full(() => {
|
|
554
|
+
return {
|
|
555
|
+
summate: 0,
|
|
556
|
+
count: 0
|
|
557
|
+
};
|
|
558
|
+
}, (accumulator, element) => {
|
|
559
|
+
let resolved = isNumber(element) ? element : mapper(element);
|
|
560
|
+
return {
|
|
561
|
+
summate: accumulator.summate + (isNumber(resolved) ? resolved : 0),
|
|
562
|
+
count: accumulator.count + 1
|
|
563
|
+
};
|
|
564
|
+
}, (result) => {
|
|
565
|
+
if (result.count === 0) {
|
|
566
|
+
return 0;
|
|
567
|
+
}
|
|
568
|
+
return result.summate / result.count;
|
|
569
|
+
});
|
|
570
|
+
};
|
|
571
|
+
;
|
|
572
|
+
;
|
|
573
|
+
export let useAsynchronousBigIntAverage = (mapper = useToBigInt) => {
|
|
574
|
+
return AsynchronousCollector.full(() => {
|
|
575
|
+
return {
|
|
576
|
+
summate: 0n,
|
|
577
|
+
count: 0n
|
|
578
|
+
};
|
|
579
|
+
}, (accumulator, element) => {
|
|
580
|
+
let resolved = isBigInt(element) ? element : mapper(element);
|
|
581
|
+
return {
|
|
582
|
+
summate: accumulator.summate + (isBigInt(resolved) ? resolved : 0n),
|
|
583
|
+
count: accumulator.count + 1n
|
|
584
|
+
};
|
|
585
|
+
}, (result) => {
|
|
586
|
+
if (result.count === 0n) {
|
|
587
|
+
return 0n;
|
|
588
|
+
}
|
|
589
|
+
return result.summate / result.count;
|
|
590
|
+
});
|
|
591
|
+
};
|
|
592
|
+
export let useAsynchronousFrequency = () => {
|
|
593
|
+
return AsynchronousCollector.full(() => new Map(), (map, element) => {
|
|
594
|
+
let count = map.get(element) || 0n;
|
|
595
|
+
map.set(element, count + 1n);
|
|
596
|
+
return map;
|
|
597
|
+
}, (map) => map);
|
|
598
|
+
};
|
|
599
|
+
;
|
|
600
|
+
export let useAsynchronousNumericMode = (mapper = useToNumber) => {
|
|
601
|
+
return AsynchronousCollector.full(() => new Map(), (map, element) => {
|
|
602
|
+
let resolved = isNumber(element) ? element : mapper(element);
|
|
603
|
+
let count = map.get(resolved) || 0n;
|
|
604
|
+
map.set(resolved, count + 1n);
|
|
605
|
+
return map;
|
|
606
|
+
}, (map) => {
|
|
607
|
+
let maxCount = 0n;
|
|
608
|
+
let mode = 0;
|
|
609
|
+
for (let [key, value] of map) {
|
|
610
|
+
if (value > maxCount) {
|
|
611
|
+
maxCount = value;
|
|
612
|
+
mode = key;
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
return mode;
|
|
616
|
+
});
|
|
617
|
+
};
|
|
618
|
+
;
|
|
619
|
+
export let useAsynchronousBigIntMode = (mapper = useToBigInt) => {
|
|
620
|
+
return AsynchronousCollector.full(() => new Map(), (map, element) => {
|
|
621
|
+
let resolved = isBigInt(element) ? element : mapper(element);
|
|
622
|
+
let count = map.get(resolved) || 0n;
|
|
623
|
+
map.set(resolved, count + 1n);
|
|
624
|
+
return map;
|
|
625
|
+
}, (map) => {
|
|
626
|
+
let maxCount = 0n;
|
|
627
|
+
let mode = 0n;
|
|
628
|
+
for (let [key, value] of map) {
|
|
629
|
+
if (value > maxCount) {
|
|
630
|
+
maxCount = value;
|
|
631
|
+
mode = key;
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
return mode;
|
|
635
|
+
});
|
|
636
|
+
};
|
|
637
|
+
;
|
|
638
|
+
;
|
|
639
|
+
export let useAsynchronousNumericVariance = (mapper = useToNumber) => {
|
|
640
|
+
return AsynchronousCollector.full(() => {
|
|
641
|
+
return {
|
|
642
|
+
summate: 0,
|
|
643
|
+
summateOfSquares: 0,
|
|
644
|
+
count: 0
|
|
645
|
+
};
|
|
646
|
+
}, (accumulator, element) => {
|
|
647
|
+
let resolved = isNumber(element) ? element : mapper(element);
|
|
648
|
+
return {
|
|
649
|
+
summate: accumulator.summate + (isNumber(resolved) ? resolved : 0),
|
|
650
|
+
summateOfSquares: accumulator.summateOfSquares + (isNumber(resolved) ? Math.pow(resolved, 2) : 0),
|
|
651
|
+
count: accumulator.count + 1
|
|
652
|
+
};
|
|
653
|
+
}, (result) => {
|
|
654
|
+
if (result.count < 2) {
|
|
655
|
+
return 0;
|
|
656
|
+
}
|
|
657
|
+
let mean = result.summate / result.count;
|
|
658
|
+
let variance = (result.summateOfSquares / result.count) - Math.pow(mean, 2);
|
|
659
|
+
return variance;
|
|
660
|
+
});
|
|
661
|
+
};
|
|
662
|
+
;
|
|
663
|
+
;
|
|
664
|
+
export let useAsynchronousBigIntVariance = (mapper = useToBigInt) => {
|
|
665
|
+
return AsynchronousCollector.full(() => {
|
|
666
|
+
return {
|
|
667
|
+
summate: 0n,
|
|
668
|
+
summateOfSquares: 0n,
|
|
669
|
+
count: 0n
|
|
670
|
+
};
|
|
671
|
+
}, (accumulator, element) => {
|
|
672
|
+
let resolved = isBigInt(element) ? element : mapper(element);
|
|
673
|
+
return {
|
|
674
|
+
summate: accumulator.summate + (isBigInt(resolved) ? resolved : 0n),
|
|
675
|
+
summateOfSquares: accumulator.summateOfSquares + (isBigInt(resolved) ? resolved * resolved : 0n),
|
|
676
|
+
count: accumulator.count + 1n
|
|
677
|
+
};
|
|
678
|
+
}, (result) => {
|
|
679
|
+
if (result.count < 2n) {
|
|
680
|
+
return 0n;
|
|
681
|
+
}
|
|
682
|
+
let mean = result.summate / result.count;
|
|
683
|
+
let variance = (result.summateOfSquares / result.count) - (mean * mean);
|
|
684
|
+
return variance;
|
|
685
|
+
});
|
|
686
|
+
};
|
|
687
|
+
;
|
|
688
|
+
;
|
|
689
|
+
export let useAsynchronousNumericStandardDeviation = (mapper = useToNumber) => {
|
|
690
|
+
return AsynchronousCollector.full(() => {
|
|
691
|
+
return {
|
|
692
|
+
summate: 0,
|
|
693
|
+
summateOfSquares: 0,
|
|
694
|
+
count: 0
|
|
695
|
+
};
|
|
696
|
+
}, (accumulator, element) => {
|
|
697
|
+
let resolved = isNumber(element) ? element : mapper(element);
|
|
698
|
+
return {
|
|
699
|
+
summate: accumulator.summate + (isNumber(resolved) ? resolved : 0),
|
|
700
|
+
summateOfSquares: accumulator.summateOfSquares + (isNumber(resolved) ? Math.pow(resolved, 2) : 0),
|
|
701
|
+
count: accumulator.count + 1
|
|
702
|
+
};
|
|
703
|
+
}, (result) => {
|
|
704
|
+
if (result.count < 2) {
|
|
705
|
+
return 0;
|
|
706
|
+
}
|
|
707
|
+
let mean = result.summate / result.count;
|
|
708
|
+
let variance = (result.summateOfSquares / result.count) - Math.pow(mean, 2);
|
|
709
|
+
let standardDeviation = Math.sqrt(variance);
|
|
710
|
+
return standardDeviation;
|
|
711
|
+
});
|
|
712
|
+
};
|
|
713
|
+
;
|
|
714
|
+
;
|
|
715
|
+
export let useAsynchronousBigIntStandardDeviation = (mapper = useToBigInt) => {
|
|
716
|
+
return AsynchronousCollector.full(() => {
|
|
717
|
+
return {
|
|
718
|
+
summate: 0n,
|
|
719
|
+
summateOfSquares: 0n,
|
|
720
|
+
count: 0n
|
|
721
|
+
};
|
|
722
|
+
}, (accumulator, element) => {
|
|
723
|
+
let resolved = isBigInt(element) ? element : mapper(element);
|
|
724
|
+
return {
|
|
725
|
+
summate: accumulator.summate + (isBigInt(resolved) ? resolved : 0n),
|
|
726
|
+
summateOfSquares: accumulator.summateOfSquares + (isBigInt(resolved) ? resolved * resolved : 0n),
|
|
727
|
+
count: accumulator.count + 1n
|
|
728
|
+
};
|
|
729
|
+
}, (result) => {
|
|
730
|
+
if (result.count < 2n) {
|
|
731
|
+
return 0n;
|
|
732
|
+
}
|
|
733
|
+
let mean = result.summate / result.count;
|
|
734
|
+
let variance = (result.summateOfSquares / result.count) - (mean * mean);
|
|
735
|
+
let standardDeviation = BigInt(Math.sqrt(Number(variance)));
|
|
736
|
+
return standardDeviation;
|
|
737
|
+
});
|
|
738
|
+
};
|
|
739
|
+
;
|
|
740
|
+
export let useAsynchronousNumericMedian = (mapper = useToNumber) => {
|
|
741
|
+
return AsynchronousCollector.full(() => [], (array, element) => {
|
|
742
|
+
let resolved = isNumber(element) ? element : mapper(element);
|
|
743
|
+
array.push(resolved);
|
|
744
|
+
array.sort((a, b) => a - b);
|
|
745
|
+
return array;
|
|
746
|
+
}, (array) => {
|
|
747
|
+
let length = array.length;
|
|
748
|
+
if (length % 2 === 0) {
|
|
749
|
+
let mid = length / 2;
|
|
750
|
+
return (array[mid - 1] + array[mid]) / 2;
|
|
751
|
+
}
|
|
752
|
+
else {
|
|
753
|
+
let mid = Math.floor(length / 2);
|
|
754
|
+
return array[mid];
|
|
755
|
+
}
|
|
756
|
+
});
|
|
757
|
+
};
|
|
758
|
+
;
|
|
759
|
+
export let useAsynchronousBigIntMedian = (mapper = useToBigInt) => {
|
|
760
|
+
return AsynchronousCollector.full(() => [], (array, element) => {
|
|
761
|
+
let resolved = isBigInt(element) ? element : mapper(element);
|
|
762
|
+
array.push(resolved);
|
|
763
|
+
array.sort((a, b) => Number(a - b));
|
|
764
|
+
return array;
|
|
765
|
+
}, (array) => {
|
|
766
|
+
let length = array.length;
|
|
767
|
+
if (length % 2 === 0) {
|
|
768
|
+
let mid = length / 2;
|
|
769
|
+
return (array[Number(mid - 1)] + array[mid]) / 2n;
|
|
770
|
+
}
|
|
771
|
+
else {
|
|
772
|
+
let mid = Math.floor(length / 2);
|
|
773
|
+
return array[mid];
|
|
774
|
+
}
|
|
775
|
+
});
|
|
776
|
+
};
|
|
777
|
+
export let useAsynchronousToGeneratorFunction = () => {
|
|
778
|
+
return AsynchronousCollector.full(() => [], (array, element) => {
|
|
779
|
+
array.push(element);
|
|
780
|
+
return array;
|
|
781
|
+
}, (array) => {
|
|
782
|
+
return (function* () {
|
|
783
|
+
for (let element of array) {
|
|
784
|
+
yield element;
|
|
785
|
+
}
|
|
786
|
+
})();
|
|
787
|
+
});
|
|
788
|
+
};
|
|
789
|
+
export let useAsynchronousToAsyncGeneratorFunction = () => {
|
|
790
|
+
return AsynchronousCollector.full(() => [], (array, element) => {
|
|
791
|
+
array.push(element);
|
|
792
|
+
return array;
|
|
793
|
+
}, (array) => {
|
|
794
|
+
return (async function* () {
|
|
795
|
+
for (let element of array) {
|
|
796
|
+
yield element;
|
|
797
|
+
}
|
|
798
|
+
})();
|
|
799
|
+
});
|
|
800
|
+
};
|