semantic-typescript 0.2.7 → 0.3.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/collectable.d.ts +12 -7
- package/dist/collectable.js +80 -237
- package/dist/collector.d.ts +99 -1
- package/dist/collector.js +422 -1
- package/dist/hook.d.ts +2 -0
- package/dist/hook.js +36 -1
- package/dist/optional.d.ts +2 -0
- package/dist/optional.js +11 -0
- package/dist/semantic.d.ts +5 -0
- package/dist/semantic.js +13 -1
- package/dist/utility.d.ts +6 -0
- package/package.json +3 -5
- package/readme.md +49 -11
package/dist/collectable.d.ts
CHANGED
|
@@ -17,6 +17,11 @@ export declare abstract class Collectable<E> {
|
|
|
17
17
|
collect<A, R>(identity: Supplier<A>, interruptor: BiPredicate<E, bigint>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): R;
|
|
18
18
|
collect<A, R>(identity: Supplier<A>, interruptor: TriPredicate<E, bigint, A>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): R;
|
|
19
19
|
count(): bigint;
|
|
20
|
+
error(): void;
|
|
21
|
+
error(accumulator: BiFunctional<string, E, string>): void;
|
|
22
|
+
error(accumulator: TriFunctional<string, E, bigint, string>): void;
|
|
23
|
+
error(prefix: string, accumulator: BiFunctional<string, E, string>, suffix: string): void;
|
|
24
|
+
error(prefix: string, accumulator: TriFunctional<string, E, bigint, string>, suffix: string): void;
|
|
20
25
|
isEmpty(): boolean;
|
|
21
26
|
findAny(): Optional<E>;
|
|
22
27
|
findFirst(): Optional<E>;
|
|
@@ -32,7 +37,7 @@ export declare abstract class Collectable<E> {
|
|
|
32
37
|
join(prefiex: string, accumulator: TriFunctional<string, E, bigint, string>, suffix: string): string;
|
|
33
38
|
log(): void;
|
|
34
39
|
log(accumulator: BiFunctional<string, E, string>): void;
|
|
35
|
-
log(accumulator:
|
|
40
|
+
log(accumulator: TriFunctional<string, E, bigint, string>): void;
|
|
36
41
|
log(prefix: string, accumulator: BiFunctional<string, E, string>, suffix: string): void;
|
|
37
42
|
log(prefix: string, accumulator: TriFunctional<string, E, bigint, string>, suffix: string): void;
|
|
38
43
|
nonMatch(predicate: Predicate<E>): boolean;
|
|
@@ -42,16 +47,16 @@ export declare abstract class Collectable<E> {
|
|
|
42
47
|
reduce(accumulator: TriFunctional<E, E, bigint, E>): Optional<E>;
|
|
43
48
|
reduce(identity: E, accumulator: BiFunctional<E, E, E>): E;
|
|
44
49
|
reduce(identity: E, accumulator: TriFunctional<E, E, bigint, E>): E;
|
|
45
|
-
reduce<R>(identity: R, accumulator: BiFunctional<R, E, R>, finisher:
|
|
46
|
-
reduce<R>(identity: R, accumulator: TriFunctional<R, E, bigint, R>, finisher:
|
|
50
|
+
reduce<R>(identity: R, accumulator: BiFunctional<R, E, R>, finisher: Functional<R, R>): R;
|
|
51
|
+
reduce<R>(identity: R, accumulator: TriFunctional<R, E, bigint, R>, finisher: Functional<R, R>): R;
|
|
47
52
|
semantic(): Semantic<E>;
|
|
48
|
-
|
|
53
|
+
abstract source(): Generator<E> | Iterable<E>;
|
|
49
54
|
toArray(): Array<E>;
|
|
50
55
|
toMap<K, V>(keyExtractor: Functional<E, K>, valueExtractor: Functional<E, V>): Map<K, V>;
|
|
51
56
|
toSet(): Set<E>;
|
|
52
|
-
write(stream: WritableStream<
|
|
53
|
-
write(stream: WritableStream<
|
|
54
|
-
write(stream: WritableStream<
|
|
57
|
+
write<S = string>(stream: WritableStream<S>): Collector<E, Promise<WritableStream<S>>, Promise<WritableStream<S>>>;
|
|
58
|
+
write<S = string>(stream: WritableStream<S>, accumulator: BiFunctional<WritableStream<S>, E, WritableStream<S>>): Collector<E, Promise<WritableStream<S>>, Promise<WritableStream<S>>>;
|
|
59
|
+
write<S = string>(stream: WritableStream<S>, accumulator: TriFunctional<WritableStream<S>, E, bigint, WritableStream<S>>): Collector<E, Promise<WritableStream<S>>, Promise<WritableStream<S>>>;
|
|
55
60
|
}
|
|
56
61
|
export declare class UnorderedCollectable<E> extends Collectable<E> {
|
|
57
62
|
protected readonly UnorderedCollectable: symbol;
|
package/dist/collectable.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Collector } from "./collector";
|
|
1
|
+
import { Collector, useAllMatch, useAnyMatch, useCollect, useCount, useError, useFindAny, useFindFirst, useFindLast, useForEach, useGroup, useGroupBy, useJoin, useLog, useNoneMatch, usePartition, useReduce, useToArray, useToMap, useToSet, useWrite } from "./collector";
|
|
2
2
|
import { from } from "./factory";
|
|
3
|
-
import { isCollector, isFunction, isIterable, isObject, isString } from "./guard";
|
|
3
|
+
import { isBigInt, isCollector, isFunction, isIterable, isObject, isString } from "./guard";
|
|
4
4
|
import { useCompare } from "./hook";
|
|
5
5
|
import { Optional } from "./optional";
|
|
6
6
|
import { Semantic } from "./semantic";
|
|
@@ -11,226 +11,138 @@ export class Collectable {
|
|
|
11
11
|
constructor() {
|
|
12
12
|
}
|
|
13
13
|
anyMatch(predicate) {
|
|
14
|
-
|
|
15
|
-
return
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
}, (result, element) => {
|
|
19
|
-
return result || predicate(element);
|
|
20
|
-
}, (result) => {
|
|
21
|
-
return result;
|
|
22
|
-
});
|
|
14
|
+
if (isFunction(predicate)) {
|
|
15
|
+
return useAnyMatch(predicate).collect(this);
|
|
16
|
+
}
|
|
17
|
+
throw new TypeError("Predicate must be a function.");
|
|
23
18
|
}
|
|
24
19
|
allMatch(predicate) {
|
|
25
|
-
return
|
|
26
|
-
return true;
|
|
27
|
-
}, (_element, _index, accumulator) => {
|
|
28
|
-
return !accumulator;
|
|
29
|
-
}, (result, element) => {
|
|
30
|
-
return result && predicate(element);
|
|
31
|
-
}, (result) => {
|
|
32
|
-
return result;
|
|
33
|
-
});
|
|
20
|
+
return useAllMatch(predicate).collect(this);
|
|
34
21
|
}
|
|
35
22
|
collect(argument1, argument2, argument3, argument4) {
|
|
36
|
-
let source = this.source();
|
|
37
23
|
if (isCollector(argument1)) {
|
|
38
24
|
let collector = argument1;
|
|
39
|
-
return collector.collect(
|
|
25
|
+
return collector.collect(this);
|
|
40
26
|
}
|
|
41
27
|
if (isFunction(argument1) && isFunction(argument2) && isFunction(argument3)) {
|
|
42
28
|
let identity = argument1;
|
|
43
29
|
let accumulator = argument2;
|
|
44
30
|
let finisher = argument3;
|
|
45
|
-
|
|
46
|
-
return collector.collect(source);
|
|
31
|
+
return useCollect(identity, accumulator, finisher).collect(this);
|
|
47
32
|
}
|
|
48
33
|
if (isFunction(argument1) && isFunction(argument2) && isFunction(argument3) && isFunction(argument4)) {
|
|
49
34
|
let identity = argument1;
|
|
50
35
|
let interrupt = argument2;
|
|
51
36
|
let accumulator = argument3;
|
|
52
37
|
let finisher = argument4;
|
|
53
|
-
|
|
54
|
-
return collector.collect(source);
|
|
38
|
+
return useCollect(identity, interrupt, accumulator, finisher).collect(this);
|
|
55
39
|
}
|
|
56
40
|
throw new TypeError("Invalid arguments.");
|
|
57
41
|
}
|
|
58
42
|
count() {
|
|
59
|
-
return
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
43
|
+
return useCount().collect(this);
|
|
44
|
+
}
|
|
45
|
+
error(argument1, argument2, argument3) {
|
|
46
|
+
if (invalidate(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
47
|
+
useError().collect(this);
|
|
48
|
+
}
|
|
49
|
+
else if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
50
|
+
let accumulator = argument1;
|
|
51
|
+
useError(accumulator).collect(this);
|
|
52
|
+
}
|
|
53
|
+
else if (isString(argument1) && isFunction(argument2) && isString(argument3)) {
|
|
54
|
+
let prefix = argument1;
|
|
55
|
+
let accumulator = argument2;
|
|
56
|
+
let suffix = argument3;
|
|
57
|
+
useError(prefix, accumulator, suffix).collect(this);
|
|
58
|
+
}
|
|
59
|
+
{
|
|
60
|
+
throw new TypeError("Invalid arguments.");
|
|
61
|
+
}
|
|
66
62
|
}
|
|
67
63
|
isEmpty() {
|
|
68
64
|
return this.count() === 0n;
|
|
69
65
|
}
|
|
70
66
|
findAny() {
|
|
71
|
-
return
|
|
72
|
-
return Optional.empty();
|
|
73
|
-
}, (_element, _index, accumulator) => {
|
|
74
|
-
return accumulator.isPresent();
|
|
75
|
-
}, (result, element) => {
|
|
76
|
-
if (Math.random() < 0.5) {
|
|
77
|
-
return Optional.of(element);
|
|
78
|
-
}
|
|
79
|
-
return result;
|
|
80
|
-
}, (result) => {
|
|
81
|
-
return result;
|
|
82
|
-
});
|
|
67
|
+
return useFindAny().collect(this);
|
|
83
68
|
}
|
|
84
69
|
findFirst() {
|
|
85
|
-
return
|
|
86
|
-
return Optional.empty();
|
|
87
|
-
}, (_element, _index, accumulator) => {
|
|
88
|
-
return accumulator.isPresent();
|
|
89
|
-
}, (result, element) => {
|
|
90
|
-
return result.isPresent() ? result : Optional.of(element);
|
|
91
|
-
}, (result) => {
|
|
92
|
-
return result;
|
|
93
|
-
});
|
|
70
|
+
return useFindFirst().collect(this);
|
|
94
71
|
}
|
|
95
72
|
findLast() {
|
|
96
|
-
return
|
|
97
|
-
return Optional.empty();
|
|
98
|
-
}, () => {
|
|
99
|
-
return false;
|
|
100
|
-
}, (result, element) => {
|
|
101
|
-
return result.isPresent() ? result : Optional.of(element);
|
|
102
|
-
}, (result) => {
|
|
103
|
-
return result;
|
|
104
|
-
});
|
|
73
|
+
return useFindLast().collect(this);
|
|
105
74
|
}
|
|
106
75
|
forEach(action) {
|
|
107
76
|
if (isFunction(action)) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
return count + 1n;
|
|
113
|
-
}, (count) => {
|
|
114
|
-
return count;
|
|
115
|
-
});
|
|
77
|
+
useForEach(action).collect(this);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
throw new TypeError("Action must be a function.");
|
|
116
81
|
}
|
|
117
82
|
}
|
|
118
83
|
group(classifier) {
|
|
119
84
|
if (isFunction(classifier)) {
|
|
120
|
-
return
|
|
121
|
-
return new Map();
|
|
122
|
-
}, (map, element) => {
|
|
123
|
-
let key = classifier(element);
|
|
124
|
-
let raw = map.get(key);
|
|
125
|
-
let array = validate(raw) ? raw : [];
|
|
126
|
-
array.push(element);
|
|
127
|
-
map.set(key, array);
|
|
128
|
-
return map;
|
|
129
|
-
}, (map) => {
|
|
130
|
-
return map;
|
|
131
|
-
});
|
|
85
|
+
return useGroup(classifier).collect(this);
|
|
132
86
|
}
|
|
133
|
-
throw new TypeError("
|
|
87
|
+
throw new TypeError("Classifier must be a function.");
|
|
134
88
|
}
|
|
135
89
|
groupBy(keyExtractor, valueExtractor) {
|
|
136
|
-
|
|
137
|
-
return
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
let value = valueExtractor(element);
|
|
141
|
-
let group = (validate(map.get(key)) ? map.get(key) : []);
|
|
142
|
-
group.push(value);
|
|
143
|
-
map.set(key, group);
|
|
144
|
-
return map;
|
|
145
|
-
}, (map) => {
|
|
146
|
-
return map;
|
|
147
|
-
});
|
|
90
|
+
if (isFunction(keyExtractor) && isFunction(valueExtractor)) {
|
|
91
|
+
return useGroupBy(keyExtractor, valueExtractor).collect(this);
|
|
92
|
+
}
|
|
93
|
+
throw new TypeError("Key and value extractors must be functions.");
|
|
148
94
|
}
|
|
149
95
|
join(argument1, argument2, argument3) {
|
|
150
96
|
if (invalidate(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
151
|
-
return
|
|
152
|
-
return "[";
|
|
153
|
-
}, (text, element) => {
|
|
154
|
-
return text + element + ",";
|
|
155
|
-
}, (text) => {
|
|
156
|
-
return text.substring(0, text.length - 1) + "]";
|
|
157
|
-
});
|
|
97
|
+
return useJoin().collect(this);
|
|
158
98
|
}
|
|
159
99
|
if (isString(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
160
100
|
let delimiter = argument1;
|
|
161
|
-
return
|
|
162
|
-
return "[";
|
|
163
|
-
}, (text, element) => {
|
|
164
|
-
return text + element + delimiter;
|
|
165
|
-
}, (text) => {
|
|
166
|
-
return text.substring(0, text.length - 1) + "]";
|
|
167
|
-
});
|
|
101
|
+
return useJoin(delimiter).collect(this);
|
|
168
102
|
}
|
|
169
103
|
if (isString(argument1) && isFunction(argument2) && isString(argument3)) {
|
|
170
104
|
let prefix = argument1;
|
|
171
105
|
let accumulator = argument2;
|
|
172
106
|
let suffix = argument3;
|
|
173
|
-
return
|
|
174
|
-
return prefix;
|
|
175
|
-
}, (text, element, index) => {
|
|
176
|
-
return text + accumulator(text, element, index);
|
|
177
|
-
}, (text) => {
|
|
178
|
-
return text + suffix;
|
|
179
|
-
});
|
|
107
|
+
return useJoin(prefix, accumulator, suffix).collect(this);
|
|
180
108
|
}
|
|
181
109
|
if (isString(argument1) && isString(argument2) && isString(argument3)) {
|
|
182
110
|
let prefix = argument1;
|
|
183
111
|
let delimiter = argument2;
|
|
184
112
|
let suffix = argument3;
|
|
185
|
-
return
|
|
186
|
-
return prefix;
|
|
187
|
-
}, (text, element) => {
|
|
188
|
-
return text + element + delimiter;
|
|
189
|
-
}, (text) => {
|
|
190
|
-
return text + suffix;
|
|
191
|
-
});
|
|
113
|
+
return useJoin(prefix, delimiter, suffix).collect(this);
|
|
192
114
|
}
|
|
193
115
|
throw new TypeError("Invalid arguments.");
|
|
194
116
|
}
|
|
195
117
|
log(argument1, argument2, argument3) {
|
|
196
118
|
if (invalidate(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
197
|
-
|
|
198
|
-
console.log(text);
|
|
119
|
+
useLog().collect(this);
|
|
199
120
|
}
|
|
200
121
|
else if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
201
122
|
let accumulator = argument1;
|
|
202
|
-
|
|
203
|
-
console.log(text);
|
|
123
|
+
useLog(accumulator).collect(this);
|
|
204
124
|
}
|
|
205
|
-
else {
|
|
125
|
+
else if (isString(argument1) && isFunction(argument2) && isString(argument3)) {
|
|
126
|
+
let prefix = argument1;
|
|
127
|
+
let accumulator = argument2;
|
|
128
|
+
let suffix = argument3;
|
|
129
|
+
useLog(prefix, accumulator, suffix).collect(this);
|
|
130
|
+
}
|
|
131
|
+
{
|
|
206
132
|
throw new TypeError("Invalid arguments.");
|
|
207
133
|
}
|
|
208
134
|
}
|
|
209
135
|
nonMatch(predicate) {
|
|
210
|
-
|
|
211
|
-
return
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
}, (result, element) => {
|
|
215
|
-
return result || !predicate(element);
|
|
216
|
-
}, (result) => {
|
|
217
|
-
return result;
|
|
218
|
-
});
|
|
136
|
+
if (isFunction(predicate)) {
|
|
137
|
+
return useNoneMatch(predicate).collect(this);
|
|
138
|
+
}
|
|
139
|
+
throw new TypeError("Predicate must be a function.");
|
|
219
140
|
}
|
|
220
141
|
partition(count) {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
let index = limited % BigInt(array.length);
|
|
226
|
-
if (index === 0n) {
|
|
227
|
-
array.push([]);
|
|
228
|
-
}
|
|
229
|
-
array[Number(index)].push(element);
|
|
230
|
-
return array;
|
|
231
|
-
}, (result) => {
|
|
232
|
-
return result;
|
|
233
|
-
});
|
|
142
|
+
if (isBigInt(count)) {
|
|
143
|
+
return usePartition(count).collect(this);
|
|
144
|
+
}
|
|
145
|
+
throw new TypeError("Count must be a BigInt.");
|
|
234
146
|
}
|
|
235
147
|
partitionBy(classifier) {
|
|
236
148
|
return this.collect(() => {
|
|
@@ -249,30 +161,18 @@ export class Collectable {
|
|
|
249
161
|
reduce(argument1, argument2, argument3) {
|
|
250
162
|
if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
251
163
|
let accumulator = argument1;
|
|
252
|
-
return
|
|
253
|
-
if (result.isEmpty()) {
|
|
254
|
-
return Optional.of(element);
|
|
255
|
-
}
|
|
256
|
-
else {
|
|
257
|
-
let current = result.get();
|
|
258
|
-
return Optional.of(accumulator(current, element, index));
|
|
259
|
-
}
|
|
260
|
-
}, (result) => result);
|
|
164
|
+
return useReduce(accumulator).collect(this);
|
|
261
165
|
}
|
|
262
166
|
else if (validate(argument1) && isFunction(argument2) && invalidate(argument3)) {
|
|
263
167
|
let identity = argument1;
|
|
264
168
|
let accumulator = argument2;
|
|
265
|
-
return
|
|
266
|
-
return accumulator(result, element, index);
|
|
267
|
-
}, (result) => result);
|
|
169
|
+
return useReduce(identity, accumulator).collect(this);
|
|
268
170
|
}
|
|
269
171
|
else if (validate(argument1) && isFunction(argument2) && isFunction(argument3)) {
|
|
270
172
|
let identity = argument1;
|
|
271
173
|
let accumulator = argument2;
|
|
272
174
|
let finisher = argument3;
|
|
273
|
-
return
|
|
274
|
-
return accumulator(result, element, index);
|
|
275
|
-
}, (result) => finisher(result, result));
|
|
175
|
+
return useReduce(identity, accumulator, finisher).collect(this);
|
|
276
176
|
}
|
|
277
177
|
else {
|
|
278
178
|
throw new TypeError("Invalid arguments.");
|
|
@@ -291,83 +191,26 @@ export class Collectable {
|
|
|
291
191
|
}
|
|
292
192
|
}
|
|
293
193
|
toArray() {
|
|
294
|
-
return
|
|
295
|
-
return [];
|
|
296
|
-
}, (array, element) => {
|
|
297
|
-
array.push(element);
|
|
298
|
-
return array;
|
|
299
|
-
}, (result) => {
|
|
300
|
-
return result;
|
|
301
|
-
});
|
|
194
|
+
return useToArray().collect(this);
|
|
302
195
|
}
|
|
303
196
|
toMap(keyExtractor, valueExtractor) {
|
|
304
|
-
return
|
|
305
|
-
return new Map();
|
|
306
|
-
}, (map, element) => {
|
|
307
|
-
let key = keyExtractor(element);
|
|
308
|
-
let value = valueExtractor(element);
|
|
309
|
-
map.set(key, value);
|
|
310
|
-
return map;
|
|
311
|
-
}, (map) => {
|
|
312
|
-
return map;
|
|
313
|
-
});
|
|
197
|
+
return useToMap(keyExtractor, valueExtractor).collect(this);
|
|
314
198
|
}
|
|
315
199
|
toSet() {
|
|
316
|
-
return
|
|
317
|
-
return new Set();
|
|
318
|
-
}, (set, element) => {
|
|
319
|
-
set.add(element);
|
|
320
|
-
return set;
|
|
321
|
-
}, (result) => {
|
|
322
|
-
return result;
|
|
323
|
-
});
|
|
200
|
+
return useToSet().collect(this);
|
|
324
201
|
}
|
|
325
|
-
write(
|
|
326
|
-
if (isObject(
|
|
327
|
-
let
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
});
|
|
336
|
-
}
|
|
337
|
-
catch (reason) {
|
|
338
|
-
return Optional.empty();
|
|
339
|
-
}
|
|
340
|
-
}, (a) => {
|
|
341
|
-
return a;
|
|
342
|
-
});
|
|
343
|
-
return new Promise((resolve, reject) => {
|
|
344
|
-
optional.ifPresent(resolve, reject);
|
|
345
|
-
});
|
|
346
|
-
}
|
|
347
|
-
else if (isObject(stream) && isFunction(accumulator)) {
|
|
348
|
-
let optional = this.collect(() => {
|
|
349
|
-
return Optional.ofNonNull(stream);
|
|
350
|
-
}, (result, element, index) => {
|
|
351
|
-
try {
|
|
352
|
-
return result.map((stream) => {
|
|
353
|
-
let writer = stream.getWriter();
|
|
354
|
-
writer.write(accumulator(element, index));
|
|
355
|
-
return stream;
|
|
356
|
-
});
|
|
357
|
-
}
|
|
358
|
-
catch (reason) {
|
|
359
|
-
return Optional.empty();
|
|
360
|
-
}
|
|
361
|
-
}, (a) => {
|
|
362
|
-
return a;
|
|
363
|
-
});
|
|
364
|
-
return new Promise((resolve, reject) => {
|
|
365
|
-
optional.ifPresent(resolve, reject);
|
|
366
|
-
});
|
|
367
|
-
}
|
|
368
|
-
else {
|
|
369
|
-
throw new TypeError("Invalid arguments.");
|
|
202
|
+
write(argument1, argument2) {
|
|
203
|
+
if (isObject(argument1)) {
|
|
204
|
+
let stream = argument1;
|
|
205
|
+
if (isFunction(argument2)) {
|
|
206
|
+
let accumulator = argument2;
|
|
207
|
+
return useWrite(stream, accumulator);
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
return useWrite(stream);
|
|
211
|
+
}
|
|
370
212
|
}
|
|
213
|
+
throw new TypeError("Invalid arguments.");
|
|
371
214
|
}
|
|
372
215
|
}
|
|
373
216
|
;
|
package/dist/collector.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import type { Collectable } from "./collectable";
|
|
2
|
+
import { Optional } from "./optional";
|
|
1
3
|
import type { Semantic } from "./semantic";
|
|
2
|
-
import type
|
|
4
|
+
import { type BiFunctional, type BiPredicate, type Functional, type Predicate, type Supplier, type TriFunctional, type Generator, type TriPredicate, type Consumer, type BiConsumer } from "./utility";
|
|
3
5
|
export declare class Collector<E, A, R> {
|
|
4
6
|
protected identity: Supplier<A>;
|
|
5
7
|
protected interrupt: Predicate<E> | BiPredicate<E, bigint> | TriPredicate<E, bigint, A>;
|
|
@@ -13,6 +15,7 @@ export declare class Collector<E, A, R> {
|
|
|
13
15
|
collect(generator: Generator<E>): R;
|
|
14
16
|
collect(iterable: Iterable<E>): R;
|
|
15
17
|
collect(semantic: Semantic<E>): R;
|
|
18
|
+
collect(collectable: Collectable<E>): R;
|
|
16
19
|
collect(start: number, end: number): R;
|
|
17
20
|
collect(start: bigint, end: bigint): R;
|
|
18
21
|
static full<E, A, R>(identity: Supplier<A>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
@@ -24,3 +27,98 @@ export declare class Collector<E, A, R> {
|
|
|
24
27
|
static shortable<E, A, R>(identity: Supplier<A>, interruptor: TriPredicate<E, bigint, A>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
25
28
|
static shortable<E, A, R>(identity: Supplier<A>, interruptor: TriPredicate<E, bigint, A>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
26
29
|
}
|
|
30
|
+
export declare let useAnyMatch: <E>(predicate: Predicate<E>) => Collector<E, boolean, boolean>;
|
|
31
|
+
export declare let useAllMatch: <E>(predicate: Predicate<E>) => Collector<E, boolean, boolean>;
|
|
32
|
+
interface UseCollect {
|
|
33
|
+
<E, A, R>(identity: Supplier<A>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
34
|
+
<E, A, R>(identity: Supplier<A>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
35
|
+
<E, A, R>(identity: Supplier<A>, accumulator: BiFunctional<A, E, A> | TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
36
|
+
<E, A, R>(identity: Supplier<A>, interruptor: Predicate<E>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
37
|
+
<E, A, R>(identity: Supplier<A>, interruptor: Predicate<E>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
38
|
+
<E, A, R>(identity: Supplier<A>, interruptor: BiPredicate<E, bigint>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
39
|
+
<E, A, R>(identity: Supplier<A>, interruptor: BiPredicate<E, bigint>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
40
|
+
<E, A, R>(identity: Supplier<A>, interruptor: TriPredicate<E, bigint, A>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
41
|
+
<E, A, R>(identity: Supplier<A>, interruptor: TriPredicate<E, bigint, A>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
42
|
+
}
|
|
43
|
+
export declare let useCollect: UseCollect;
|
|
44
|
+
export declare let useCount: <E = unknown>() => Collector<E, bigint, bigint>;
|
|
45
|
+
export interface UseError {
|
|
46
|
+
<E = unknown>(): Collector<E, string, string>;
|
|
47
|
+
<E = unknown>(accumulator: BiFunctional<string, E, string>): Collector<E, string, string>;
|
|
48
|
+
<E = unknown>(accumulator: TriFunctional<string, E, bigint, string>): Collector<E, string, string>;
|
|
49
|
+
<E = unknown>(prefix: string, accumulator: BiFunctional<string, E, string>, suffix: string): Collector<E, string, string>;
|
|
50
|
+
<E = unknown>(prefix: string, accumulator: TriFunctional<string, E, bigint, string>, suffix: string): Collector<E, string, string>;
|
|
51
|
+
}
|
|
52
|
+
export declare let useError: UseLog;
|
|
53
|
+
export declare let useFindFirst: <E>() => Collector<E, Optional<E>, Optional<E>>;
|
|
54
|
+
export declare let useFindAny: <E>() => Collector<E, Optional<E>, Optional<E>>;
|
|
55
|
+
export declare let useFindLast: <E>() => Collector<E, Optional<E>, Optional<E>>;
|
|
56
|
+
export interface UseForEach {
|
|
57
|
+
<E>(action: Consumer<E>): Collector<E, bigint, bigint>;
|
|
58
|
+
<E>(action: BiConsumer<E, bigint>): Collector<E, bigint, bigint>;
|
|
59
|
+
}
|
|
60
|
+
export declare let useForEach: UseForEach;
|
|
61
|
+
export declare let useNoneMatch: <E>(predicate: Predicate<E>) => Collector<E, boolean, boolean>;
|
|
62
|
+
export declare let useGroup: <E, K>(classifier: Functional<E, K>) => Collector<E, Map<K, E[]>, Map<K, E[]>>;
|
|
63
|
+
export declare let useGroupBy: <E, K, V>(keyExtractor: Functional<E, K>, valueExtractor: Functional<E, V>) => Collector<E, Map<K, V[]>, Map<K, V[]>>;
|
|
64
|
+
export interface UseJoin {
|
|
65
|
+
<E = unknown>(): Collector<E, string, string>;
|
|
66
|
+
<E = unknown>(delimiter: string): Collector<E, string, string>;
|
|
67
|
+
<E = unknown>(prefix: string, delimiter: string, suffix: string): Collector<E, string, string>;
|
|
68
|
+
<E = unknown>(prefiex: string, accumulator: BiFunctional<string, E, string>, suffix: string): Collector<E, string, string>;
|
|
69
|
+
<E = unknown>(prefiex: string, accumulator: TriFunctional<string, E, bigint, string>, suffix: string): Collector<E, string, string>;
|
|
70
|
+
}
|
|
71
|
+
export declare let useJoin: UseJoin;
|
|
72
|
+
export interface UseLog {
|
|
73
|
+
<E = unknown>(): Collector<E, string, string>;
|
|
74
|
+
<E = unknown>(accumulator: BiFunctional<string, E, string>): Collector<E, string, string>;
|
|
75
|
+
<E = unknown>(accumulator: TriFunctional<string, E, bigint, string>): Collector<E, string, string>;
|
|
76
|
+
<E = unknown>(prefix: string, accumulator: BiFunctional<string, E, string>, suffix: string): Collector<E, string, string>;
|
|
77
|
+
<E = unknown>(prefix: string, accumulator: TriFunctional<string, E, bigint, string>, suffix: string): Collector<E, string, string>;
|
|
78
|
+
}
|
|
79
|
+
export declare let useLog: UseLog;
|
|
80
|
+
export declare let usePartition: <E>(count: bigint) => Collector<E, Array<Array<E>>, Array<Array<E>>>;
|
|
81
|
+
export declare let usePartitionBy: <E>(classifier: Functional<E, bigint>) => Collector<E, Array<E[]>, Array<E[]>>;
|
|
82
|
+
export interface UseReduce {
|
|
83
|
+
<E>(accumulator: BiFunctional<E, E, E>): Collector<E, Optional<E>, Optional<E>>;
|
|
84
|
+
<E>(accumulator: TriFunctional<E, E, bigint, E>): Collector<E, Optional<E>, Optional<E>>;
|
|
85
|
+
<E>(identity: E, accumulator: BiFunctional<E, E, E>): Collector<E, E, E>;
|
|
86
|
+
<E>(identity: E, accumulator: TriFunctional<E, E, bigint, E>): Collector<E, E, E>;
|
|
87
|
+
<E, R>(identity: R, accumulator: BiFunctional<R, E, R>, finisher: Functional<R, R>): Collector<E, R, R>;
|
|
88
|
+
<E, R>(identity: R, accumulator: TriFunctional<R, E, bigint, R>, finisher: Functional<R, R>): Collector<E, R, R>;
|
|
89
|
+
}
|
|
90
|
+
export declare let useReduce: UseReduce;
|
|
91
|
+
export declare let useToArray: <E>() => Collector<E, E[], E[]>;
|
|
92
|
+
export declare let useToMap: <E, K, V>(keyExtractor: Functional<E, K>, valueExtractor: Functional<E, V>) => Collector<E, Map<K, V>, Map<K, V>>;
|
|
93
|
+
export declare let useToSet: <E>() => Collector<E, Set<E>, Set<E>>;
|
|
94
|
+
export interface UseWrite {
|
|
95
|
+
<E, S = string>(stream: WritableStream<S>): Collector<E, Promise<WritableStream<S>>, Promise<WritableStream<S>>>;
|
|
96
|
+
<E, S = string>(stream: WritableStream<S>, accumulator: BiFunctional<WritableStream<S>, E, WritableStream<S>>): Collector<E, Promise<WritableStream<S>>, Promise<WritableStream<S>>>;
|
|
97
|
+
<E, S = string>(stream: WritableStream<S>, accumulator: TriFunctional<WritableStream<S>, E, bigint, WritableStream<S>>): Collector<E, Promise<WritableStream<S>>, Promise<WritableStream<S>>>;
|
|
98
|
+
}
|
|
99
|
+
export declare let useWrite: UseWrite;
|
|
100
|
+
export type NumericAverageInformation = {
|
|
101
|
+
summate: number;
|
|
102
|
+
count: number;
|
|
103
|
+
};
|
|
104
|
+
export interface UseNumericAverage {
|
|
105
|
+
(): Collector<number, NumericAverageInformation, number>;
|
|
106
|
+
<E>(mapper: Functional<E, number>): Collector<E, NumericAverageInformation, number>;
|
|
107
|
+
}
|
|
108
|
+
export declare let useNumericAverage: UseNumericAverage;
|
|
109
|
+
export type BigIntAverageInformation = {
|
|
110
|
+
summate: bigint;
|
|
111
|
+
count: bigint;
|
|
112
|
+
};
|
|
113
|
+
export interface UseBigIntAverage {
|
|
114
|
+
(): Collector<bigint, BigIntAverageInformation, bigint>;
|
|
115
|
+
<E>(mapper: Functional<E, bigint>): Collector<E, BigIntAverageInformation, bigint>;
|
|
116
|
+
}
|
|
117
|
+
export declare let useBigIntAverage: UseBigIntAverage;
|
|
118
|
+
export declare let useFrequency: <E>() => Collector<E, Map<E, bigint>, Map<E, bigint>>;
|
|
119
|
+
export interface UseNumericSummate {
|
|
120
|
+
(): Collector<number, number, number>;
|
|
121
|
+
<E>(mapper: Functional<E, number>): Collector<E, number, number>;
|
|
122
|
+
}
|
|
123
|
+
export declare let useSummate: UseNumericSummate;
|
|
124
|
+
export {};
|
package/dist/collector.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { isBigInt, isFunction, isIterable, isNumber, isSemantic } from "./guard";
|
|
1
|
+
import { isBigInt, isBoolean, isCollectable, isFunction, isIterable, isNumber, isObject, isSemantic, isString } from "./guard";
|
|
2
|
+
import { Optional } from "./optional";
|
|
2
3
|
import { CollectableSymbol } from "./symbol";
|
|
4
|
+
import { validate, invalidate } from "./utility";
|
|
3
5
|
export class Collector {
|
|
4
6
|
identity;
|
|
5
7
|
interrupt;
|
|
@@ -52,6 +54,29 @@ export class Collector {
|
|
|
52
54
|
throw new TypeError("Invalid arguments");
|
|
53
55
|
}
|
|
54
56
|
}
|
|
57
|
+
else if (isCollectable(argument1)) {
|
|
58
|
+
let collectable = argument1;
|
|
59
|
+
let source = collectable.source();
|
|
60
|
+
if (isIterable(source)) {
|
|
61
|
+
let iterable = source;
|
|
62
|
+
let index = 0n;
|
|
63
|
+
for (let element of iterable) {
|
|
64
|
+
if (this.interrupt(element, index, accumulator)) {
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
accumulator = this.accumulator(accumulator, element, count);
|
|
68
|
+
count++;
|
|
69
|
+
index++;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
else if (isFunction(source)) {
|
|
73
|
+
let generator = source;
|
|
74
|
+
generator((element, index) => {
|
|
75
|
+
accumulator = this.accumulator(accumulator, element, index);
|
|
76
|
+
count++;
|
|
77
|
+
}, (element, index) => this.interrupt(element, index, accumulator));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
55
80
|
else if (isNumber(argument1) && isNumber(argument2)) {
|
|
56
81
|
let start = argument1 < argument2 ? argument1 : argument2;
|
|
57
82
|
let end = argument1 > argument2 ? argument1 : argument2;
|
|
@@ -84,3 +109,399 @@ export class Collector {
|
|
|
84
109
|
}
|
|
85
110
|
}
|
|
86
111
|
;
|
|
112
|
+
export let useAnyMatch = (predicate) => {
|
|
113
|
+
if (isFunction(predicate)) {
|
|
114
|
+
return Collector.shortable(() => false, (_element, _index, accumulator) => isBoolean(accumulator) && accumulator, (accumulator, element) => accumulator || predicate(element), (accumulator) => accumulator);
|
|
115
|
+
}
|
|
116
|
+
throw new TypeError("Predicate must be a function.");
|
|
117
|
+
};
|
|
118
|
+
export let useAllMatch = (predicate) => {
|
|
119
|
+
if (isFunction(predicate)) {
|
|
120
|
+
return Collector.shortable(() => true, (_element, _index, accumulator) => isBoolean(accumulator) && !accumulator, (accumulator, element) => accumulator && predicate(element), (accumulator) => accumulator);
|
|
121
|
+
}
|
|
122
|
+
throw new TypeError("Predicate must be a function.");
|
|
123
|
+
};
|
|
124
|
+
;
|
|
125
|
+
export let useCollect = (argument1, argument2, argument3, argument4) => {
|
|
126
|
+
if (isFunction(argument1) && isFunction(argument2) && isFunction(argument3) && isFunction(argument4)) {
|
|
127
|
+
let identity = argument1;
|
|
128
|
+
let interrupt = argument2;
|
|
129
|
+
let accumulator = argument3;
|
|
130
|
+
let finisher = argument4;
|
|
131
|
+
return Collector.shortable(identity, interrupt, accumulator, finisher);
|
|
132
|
+
}
|
|
133
|
+
if (isFunction(argument1) && isFunction(argument2) && isFunction(argument3)) {
|
|
134
|
+
let identity = argument1;
|
|
135
|
+
let accumulator = argument2;
|
|
136
|
+
let finisher = argument3;
|
|
137
|
+
return Collector.full(identity, accumulator, finisher);
|
|
138
|
+
}
|
|
139
|
+
throw new TypeError("Identity, accumulator, and finisher must be functions.");
|
|
140
|
+
};
|
|
141
|
+
export let useCount = () => {
|
|
142
|
+
return Collector.full(() => 0n, (count) => count + 1n, (count) => count);
|
|
143
|
+
};
|
|
144
|
+
;
|
|
145
|
+
export let useError = (argument1, argument2, argument3) => {
|
|
146
|
+
if (invalidate(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
147
|
+
return Collector.full(() => "[", (accumulator, element) => {
|
|
148
|
+
if (isString(accumulator) && isString(element)) {
|
|
149
|
+
return accumulator + element + ",";
|
|
150
|
+
}
|
|
151
|
+
return String(accumulator) + String(element) + ",";
|
|
152
|
+
}, (text) => {
|
|
153
|
+
let result = text.substring(0, text.length - 1) + "]";
|
|
154
|
+
console.error(result);
|
|
155
|
+
return result;
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
else if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
159
|
+
let accumulator = argument1;
|
|
160
|
+
return Collector.full(() => "[", accumulator, (text) => {
|
|
161
|
+
let result = text.substring(0, text.length - 1) + "]";
|
|
162
|
+
console.error(result);
|
|
163
|
+
return result;
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
else if (isString(argument1) && isFunction(argument2) && isString(argument3)) {
|
|
167
|
+
let prefix = argument1;
|
|
168
|
+
let accumulator = argument2;
|
|
169
|
+
let suffix = argument3;
|
|
170
|
+
return Collector.full(() => prefix, accumulator, (text) => {
|
|
171
|
+
let result = text + suffix;
|
|
172
|
+
console.error(result);
|
|
173
|
+
return result;
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
throw new TypeError("Invalid arguments.");
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
export let useFindFirst = () => {
|
|
181
|
+
return Collector.shortable(() => Optional.empty(), (_element, _index, accumulator) => validate(accumulator) && accumulator.isPresent(), (accumulator, element) => {
|
|
182
|
+
if (validate(accumulator) && accumulator.isPresent()) {
|
|
183
|
+
return accumulator;
|
|
184
|
+
}
|
|
185
|
+
return Optional.ofNullable(element);
|
|
186
|
+
}, (accumulator) => accumulator);
|
|
187
|
+
};
|
|
188
|
+
export let useFindAny = () => {
|
|
189
|
+
return Collector.shortable(() => Optional.empty(), (_element, _index, accumulator) => validate(accumulator) && accumulator.isPresent(), (accumulator, element) => {
|
|
190
|
+
if (validate(accumulator) && accumulator.isPresent() && Math.random() < 0.5) {
|
|
191
|
+
return accumulator;
|
|
192
|
+
}
|
|
193
|
+
return Optional.ofNullable(element);
|
|
194
|
+
}, (accumulator) => accumulator);
|
|
195
|
+
};
|
|
196
|
+
export let useFindLast = () => {
|
|
197
|
+
return Collector.full(() => Optional.empty(), (accumulator, element) => {
|
|
198
|
+
if (validate(accumulator) && accumulator.isPresent()) {
|
|
199
|
+
return Optional.ofNullable(element);
|
|
200
|
+
}
|
|
201
|
+
return accumulator;
|
|
202
|
+
}, (accumulator) => accumulator);
|
|
203
|
+
};
|
|
204
|
+
;
|
|
205
|
+
export let useForEach = (action) => {
|
|
206
|
+
if (isFunction(action)) {
|
|
207
|
+
return Collector.full(() => 0n, (accumulator, element, index) => {
|
|
208
|
+
action(element, index);
|
|
209
|
+
return accumulator + 1n;
|
|
210
|
+
}, (accumulator) => accumulator);
|
|
211
|
+
}
|
|
212
|
+
throw new TypeError("Action must be a function.");
|
|
213
|
+
};
|
|
214
|
+
export let useNoneMatch = (predicate) => {
|
|
215
|
+
if (isFunction(predicate)) {
|
|
216
|
+
return Collector.shortable(() => true, (_element, _index, accumulator) => !accumulator, (accumulator, element) => accumulator && !predicate(element), (accumulator) => !accumulator);
|
|
217
|
+
}
|
|
218
|
+
throw new TypeError("Predicate must be a function.");
|
|
219
|
+
};
|
|
220
|
+
export let useGroup = (classifier) => {
|
|
221
|
+
if (isFunction(classifier)) {
|
|
222
|
+
return Collector.full(() => new Map(), (accumulator, element) => {
|
|
223
|
+
let key = classifier(element);
|
|
224
|
+
let group = accumulator.get(key) || [];
|
|
225
|
+
group.push(element);
|
|
226
|
+
accumulator.set(key, group);
|
|
227
|
+
return accumulator;
|
|
228
|
+
}, (accumulator) => accumulator);
|
|
229
|
+
}
|
|
230
|
+
throw new TypeError("Classifier must be a function.");
|
|
231
|
+
};
|
|
232
|
+
export let useGroupBy = (keyExtractor, valueExtractor) => {
|
|
233
|
+
if (isFunction(keyExtractor) && isFunction(valueExtractor)) {
|
|
234
|
+
return Collector.full(() => new Map(), (accumulator, element) => {
|
|
235
|
+
let key = keyExtractor(element);
|
|
236
|
+
let group = accumulator.get(key) || [];
|
|
237
|
+
group.push(valueExtractor(element));
|
|
238
|
+
accumulator.set(key, group);
|
|
239
|
+
return accumulator;
|
|
240
|
+
}, (accumulator) => accumulator);
|
|
241
|
+
}
|
|
242
|
+
throw new TypeError("Key extractor and value extractor must be functions.");
|
|
243
|
+
};
|
|
244
|
+
;
|
|
245
|
+
export let useJoin = (argument1, argument2, argument3) => {
|
|
246
|
+
if (invalidate(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
247
|
+
return Collector.full(() => "[", (text, element) => text + element + ",", (text) => text.substring(0, text.length - 1) + "]");
|
|
248
|
+
}
|
|
249
|
+
if (isString(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
250
|
+
let delimiter = argument1;
|
|
251
|
+
return Collector.full(() => "[", (text, element) => text + element + delimiter, (text) => text.substring(0, text.length - delimiter.length) + "]");
|
|
252
|
+
}
|
|
253
|
+
if (isString(argument1) && isFunction(argument2) && isString(argument3)) {
|
|
254
|
+
let prefix = argument1;
|
|
255
|
+
let accumulator = argument2;
|
|
256
|
+
let suffix = argument3;
|
|
257
|
+
return Collector.full(() => prefix, accumulator, (text) => text + suffix);
|
|
258
|
+
}
|
|
259
|
+
if (isString(argument1) && isString(argument2) && isString(argument3)) {
|
|
260
|
+
let prefix = argument1;
|
|
261
|
+
let delimiter = argument2;
|
|
262
|
+
let suffix = argument3;
|
|
263
|
+
return Collector.full(() => prefix, (text, element) => text + element + delimiter, (text) => text + suffix);
|
|
264
|
+
}
|
|
265
|
+
throw new TypeError("Invalid arguments.");
|
|
266
|
+
};
|
|
267
|
+
;
|
|
268
|
+
export let useLog = (argument1, argument2, argument3) => {
|
|
269
|
+
if (invalidate(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
270
|
+
return Collector.full(() => "[", (accumulator, element) => {
|
|
271
|
+
if (isString(accumulator) && isString(element)) {
|
|
272
|
+
return accumulator + element + ",";
|
|
273
|
+
}
|
|
274
|
+
return String(accumulator) + String(element) + ",";
|
|
275
|
+
}, (text) => {
|
|
276
|
+
let result = text.substring(0, text.length - 1) + "]";
|
|
277
|
+
console.log(result);
|
|
278
|
+
return result;
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
else if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
282
|
+
let accumulator = argument1;
|
|
283
|
+
return Collector.full(() => "[", accumulator, (text) => {
|
|
284
|
+
let result = text.substring(0, text.length - 1) + "]";
|
|
285
|
+
console.log(result);
|
|
286
|
+
return result;
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
else if (isString(argument1) && isFunction(argument2) && isString(argument3)) {
|
|
290
|
+
let prefix = argument1;
|
|
291
|
+
let accumulator = argument2;
|
|
292
|
+
let suffix = argument3;
|
|
293
|
+
return Collector.full(() => prefix, accumulator, (text) => {
|
|
294
|
+
let result = text + suffix;
|
|
295
|
+
console.log(result);
|
|
296
|
+
return result;
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
throw new TypeError("Invalid arguments.");
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
export let usePartition = (count) => {
|
|
304
|
+
if (isBigInt(count)) {
|
|
305
|
+
let limited = count > 1n ? count : 1n;
|
|
306
|
+
return Collector.full(() => {
|
|
307
|
+
return [];
|
|
308
|
+
}, (array, element) => {
|
|
309
|
+
let index = limited % BigInt(array.length);
|
|
310
|
+
if (index === 0n) {
|
|
311
|
+
array.push([]);
|
|
312
|
+
}
|
|
313
|
+
array[Number(index)].push(element);
|
|
314
|
+
return array;
|
|
315
|
+
}, (result) => {
|
|
316
|
+
return result;
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
throw new TypeError("Count must be a BigInt.");
|
|
320
|
+
};
|
|
321
|
+
export let usePartitionBy = (classifier) => {
|
|
322
|
+
if (isFunction(classifier)) {
|
|
323
|
+
return Collector.full(() => {
|
|
324
|
+
return [];
|
|
325
|
+
}, (array, element) => {
|
|
326
|
+
let index = classifier(element);
|
|
327
|
+
while (index > BigInt(array.length) - 1n) {
|
|
328
|
+
array.push([]);
|
|
329
|
+
}
|
|
330
|
+
array[Number(index)].push(element);
|
|
331
|
+
return array;
|
|
332
|
+
}, (result) => {
|
|
333
|
+
return result;
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
throw new TypeError("Classifier must be a function.");
|
|
337
|
+
};
|
|
338
|
+
;
|
|
339
|
+
export let useReduce = (argument1, argument2, argument3) => {
|
|
340
|
+
if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
341
|
+
let accumulator = argument1;
|
|
342
|
+
return Collector.full(() => Optional.ofNullable(), (result, element, index) => {
|
|
343
|
+
if (result.isEmpty()) {
|
|
344
|
+
return Optional.of(element);
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
let current = result.get();
|
|
348
|
+
return Optional.of(accumulator(current, element, index));
|
|
349
|
+
}
|
|
350
|
+
}, (result) => result);
|
|
351
|
+
}
|
|
352
|
+
else if (validate(argument1) && isFunction(argument2) && invalidate(argument3)) {
|
|
353
|
+
let identity = argument1;
|
|
354
|
+
let accumulator = argument2;
|
|
355
|
+
return Collector.full(() => identity, accumulator, (result) => result);
|
|
356
|
+
}
|
|
357
|
+
else if (validate(argument1) && isFunction(argument2) && isFunction(argument3)) {
|
|
358
|
+
let identity = argument1;
|
|
359
|
+
let accumulator = argument2;
|
|
360
|
+
let finisher = argument3;
|
|
361
|
+
return Collector.full(() => identity, accumulator, finisher);
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
throw new TypeError("Invalid arguments.");
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
export let useToArray = () => {
|
|
368
|
+
return Collector.full(() => [], (array, element) => {
|
|
369
|
+
array.push(element);
|
|
370
|
+
return array;
|
|
371
|
+
}, (array) => array);
|
|
372
|
+
};
|
|
373
|
+
export let useToMap = (keyExtractor, valueExtractor) => {
|
|
374
|
+
if (isFunction(keyExtractor) && isFunction(valueExtractor)) {
|
|
375
|
+
return Collector.full(() => new Map(), (map, element) => {
|
|
376
|
+
let key = keyExtractor(element);
|
|
377
|
+
let value = valueExtractor(element);
|
|
378
|
+
map.set(key, value);
|
|
379
|
+
return map;
|
|
380
|
+
}, (map) => map);
|
|
381
|
+
}
|
|
382
|
+
throw new TypeError("Key extractor and value extractor must be functions.");
|
|
383
|
+
};
|
|
384
|
+
export let useToSet = () => {
|
|
385
|
+
return Collector.full(() => new Set(), (set, element) => {
|
|
386
|
+
set.add(element);
|
|
387
|
+
return set;
|
|
388
|
+
}, (set) => set);
|
|
389
|
+
};
|
|
390
|
+
;
|
|
391
|
+
export let useWrite = (argument1, argument2) => {
|
|
392
|
+
if (isObject(argument1)) {
|
|
393
|
+
if (isFunction(argument2)) {
|
|
394
|
+
let stream = argument1;
|
|
395
|
+
let accumulator = argument2;
|
|
396
|
+
return Collector.full(() => Promise.resolve(stream), (promise, element, index) => {
|
|
397
|
+
return new Promise((resolve, reject) => {
|
|
398
|
+
promise.then((stream) => {
|
|
399
|
+
try {
|
|
400
|
+
resolve(accumulator(stream, element, index));
|
|
401
|
+
}
|
|
402
|
+
catch (error) {
|
|
403
|
+
reject(error);
|
|
404
|
+
}
|
|
405
|
+
}).catch(reject);
|
|
406
|
+
});
|
|
407
|
+
}, (promise) => promise);
|
|
408
|
+
}
|
|
409
|
+
else {
|
|
410
|
+
let stream = argument1;
|
|
411
|
+
return Collector.full(() => Promise.resolve(stream), (promise, element) => {
|
|
412
|
+
return new Promise((resolve, reject) => {
|
|
413
|
+
promise.then((stream) => {
|
|
414
|
+
try {
|
|
415
|
+
let writer = stream.getWriter();
|
|
416
|
+
writer.write(String(element));
|
|
417
|
+
resolve(stream);
|
|
418
|
+
}
|
|
419
|
+
catch (error) {
|
|
420
|
+
reject(error);
|
|
421
|
+
}
|
|
422
|
+
}).catch(reject);
|
|
423
|
+
});
|
|
424
|
+
}, (promise) => promise);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
throw new TypeError("Invalid arguments.");
|
|
428
|
+
};
|
|
429
|
+
;
|
|
430
|
+
export let useNumericAverage = (mapper) => {
|
|
431
|
+
if (isFunction(mapper)) {
|
|
432
|
+
return Collector.full(() => {
|
|
433
|
+
return {
|
|
434
|
+
summate: 0,
|
|
435
|
+
count: 0
|
|
436
|
+
};
|
|
437
|
+
}, (information, element) => {
|
|
438
|
+
let value = mapper(element);
|
|
439
|
+
information.summate += value;
|
|
440
|
+
information.count++;
|
|
441
|
+
return information;
|
|
442
|
+
}, (information) => {
|
|
443
|
+
return information.summate / information.count;
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
return Collector.full(() => {
|
|
447
|
+
return {
|
|
448
|
+
summate: 0,
|
|
449
|
+
count: 0
|
|
450
|
+
};
|
|
451
|
+
}, (information, element) => {
|
|
452
|
+
information.summate += element;
|
|
453
|
+
information.count++;
|
|
454
|
+
return information;
|
|
455
|
+
}, (information) => {
|
|
456
|
+
return information.summate / information.count;
|
|
457
|
+
});
|
|
458
|
+
};
|
|
459
|
+
;
|
|
460
|
+
export let useBigIntAverage = (mapper) => {
|
|
461
|
+
if (isFunction(mapper)) {
|
|
462
|
+
return Collector.full(() => {
|
|
463
|
+
return {
|
|
464
|
+
summate: 0n,
|
|
465
|
+
count: 0n
|
|
466
|
+
};
|
|
467
|
+
}, (information, element) => {
|
|
468
|
+
let value = mapper(element);
|
|
469
|
+
information.summate += value;
|
|
470
|
+
information.count++;
|
|
471
|
+
return information;
|
|
472
|
+
}, (information) => {
|
|
473
|
+
return information.summate / information.count;
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
return Collector.full(() => {
|
|
477
|
+
return {
|
|
478
|
+
summate: 0n,
|
|
479
|
+
count: 0n
|
|
480
|
+
};
|
|
481
|
+
}, (information, element) => {
|
|
482
|
+
information.summate += element;
|
|
483
|
+
information.count++;
|
|
484
|
+
return information;
|
|
485
|
+
}, (information) => {
|
|
486
|
+
return information.summate / information.count;
|
|
487
|
+
});
|
|
488
|
+
};
|
|
489
|
+
export let useFrequency = () => {
|
|
490
|
+
return Collector.full(() => new Map(), (map, element) => {
|
|
491
|
+
let count = map.get(element) || 0n;
|
|
492
|
+
map.set(element, count + 1n);
|
|
493
|
+
return map;
|
|
494
|
+
}, (map) => map);
|
|
495
|
+
};
|
|
496
|
+
;
|
|
497
|
+
export let useSummate = (mapper) => {
|
|
498
|
+
if (isFunction(mapper)) {
|
|
499
|
+
return Collector.full(() => 0, (summate, element) => {
|
|
500
|
+
let value = mapper(element);
|
|
501
|
+
return summate + value;
|
|
502
|
+
}, (summate) => summate);
|
|
503
|
+
}
|
|
504
|
+
return Collector.full(() => 0, (summate, element) => {
|
|
505
|
+
return summate + element;
|
|
506
|
+
}, (summate) => summate);
|
|
507
|
+
};
|
package/dist/hook.d.ts
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
|
+
import { type BiConsumer, type DeepPropertyKey, type DeepPropertyValue } from "./utility";
|
|
1
2
|
export declare let useCompare: <T>(t1: T, t2: T) => number;
|
|
2
3
|
export declare let useRandom: <T = number | bigint>(index: T) => T;
|
|
4
|
+
export declare let useTraverse: <T extends object>(t: T, callback: BiConsumer<DeepPropertyKey<T>, DeepPropertyValue<T>>) => void;
|
package/dist/hook.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { isFunction, isNumber, isPrimitive } from "./guard";
|
|
1
|
+
import { isFunction, isIterable, isNumber, isObject, isPrimitive } from "./guard";
|
|
2
|
+
import { validate } from "./utility";
|
|
2
3
|
export let useCompare = (t1, t2) => {
|
|
3
4
|
if (t1 === t2 || Object.is(t1, t2)) {
|
|
4
5
|
return 0;
|
|
@@ -62,3 +63,37 @@ export let useRandom = (index) => {
|
|
|
62
63
|
}
|
|
63
64
|
throw new TypeError("Invalid input type");
|
|
64
65
|
};
|
|
66
|
+
export let useTraverse = (t, callback) => {
|
|
67
|
+
if (isObject(t)) {
|
|
68
|
+
let seen = new WeakSet();
|
|
69
|
+
let traverse = (target) => {
|
|
70
|
+
if (!seen.has(target)) {
|
|
71
|
+
seen.add(target);
|
|
72
|
+
let properties = Reflect.ownKeys(target);
|
|
73
|
+
for (let property of properties) {
|
|
74
|
+
let value = target[property];
|
|
75
|
+
if (validate(value)) {
|
|
76
|
+
if (isObject(value)) {
|
|
77
|
+
if (isIterable(value)) {
|
|
78
|
+
for (let item of value) {
|
|
79
|
+
if (validate(item)) {
|
|
80
|
+
if (isObject(item)) {
|
|
81
|
+
traverse(item);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
callback(property, item);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
callback(property, value);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
traverse(t);
|
|
98
|
+
}
|
|
99
|
+
};
|
package/dist/optional.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Semantic } from "./semantic";
|
|
1
2
|
import { type Consumer, type Functional, type MaybeInvalid, type Predicate, type Runnable } from "./utility";
|
|
2
3
|
export declare class Optional<T> {
|
|
3
4
|
protected value: MaybeInvalid<T>;
|
|
@@ -11,6 +12,7 @@ export declare class Optional<T> {
|
|
|
11
12
|
isEmpty(): boolean;
|
|
12
13
|
isPresent(): boolean;
|
|
13
14
|
map<R>(mapper: Functional<T, R>): Optional<R>;
|
|
15
|
+
semantic(): Semantic<T>;
|
|
14
16
|
static empty<T>(): Optional<T>;
|
|
15
17
|
static of<T>(value: MaybeInvalid<T>): Optional<T>;
|
|
16
18
|
static ofNullable<T>(value?: MaybeInvalid<T>): Optional<T>;
|
package/dist/optional.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { empty, generate } from "./factory";
|
|
1
2
|
import { isFunction } from "./guard";
|
|
2
3
|
import { OptionalSymbol } from "./symbol";
|
|
3
4
|
import { invalidate, validate } from "./utility";
|
|
@@ -46,6 +47,16 @@ export class Optional {
|
|
|
46
47
|
}
|
|
47
48
|
return new Optional(null);
|
|
48
49
|
}
|
|
50
|
+
semantic() {
|
|
51
|
+
if (this.isPresent()) {
|
|
52
|
+
return generate(() => {
|
|
53
|
+
return this.value;
|
|
54
|
+
}, () => {
|
|
55
|
+
return this.isEmpty();
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return empty();
|
|
59
|
+
}
|
|
49
60
|
static empty() {
|
|
50
61
|
return new Optional(null);
|
|
51
62
|
}
|
package/dist/semantic.d.ts
CHANGED
|
@@ -40,3 +40,8 @@ export declare class Semantic<E> {
|
|
|
40
40
|
translate(offset: bigint): Semantic<E>;
|
|
41
41
|
translate(translator: BiFunctional<E, bigint, bigint>): Semantic<E>;
|
|
42
42
|
}
|
|
43
|
+
export interface UseTransform {
|
|
44
|
+
<E, R>(generator: Generator<E>, mapper: Functional<E, R>): Generator<R>;
|
|
45
|
+
<E, R>(generator: Generator<E>, mapper: BiFunctional<E, bigint, R>): Generator<R>;
|
|
46
|
+
}
|
|
47
|
+
export declare let useTransform: UseTransform;
|
package/dist/semantic.js
CHANGED
|
@@ -18,7 +18,8 @@ export class Semantic {
|
|
|
18
18
|
accept(element, index);
|
|
19
19
|
count++;
|
|
20
20
|
}, interrupt);
|
|
21
|
-
other.
|
|
21
|
+
let otherGenerator = Reflect.has(other, "generator") ? Reflect.get(other, "generator") : () => { };
|
|
22
|
+
otherGenerator((element, index) => {
|
|
22
23
|
accept(element, index + count);
|
|
23
24
|
}, interrupt);
|
|
24
25
|
});
|
|
@@ -337,3 +338,14 @@ export class Semantic {
|
|
|
337
338
|
}
|
|
338
339
|
}
|
|
339
340
|
;
|
|
341
|
+
;
|
|
342
|
+
export let useTransform = (generator, mapper) => {
|
|
343
|
+
return (accept, interrupt) => {
|
|
344
|
+
generator((element, index) => {
|
|
345
|
+
let resolved = mapper(element, index);
|
|
346
|
+
accept(resolved, index);
|
|
347
|
+
}, (element, index) => {
|
|
348
|
+
return interrupt(mapper(element, index), index);
|
|
349
|
+
});
|
|
350
|
+
};
|
|
351
|
+
};
|
package/dist/utility.d.ts
CHANGED
|
@@ -6,6 +6,12 @@ export declare let invalidate: <T>(t: MaybeInvalid<T>) => t is (null | undefined
|
|
|
6
6
|
export type Primitive = string | number | boolean | symbol | bigint | Function | ((...args: any[]) => any);
|
|
7
7
|
export type MaybePrimitive<T> = T | Primitive;
|
|
8
8
|
export type AsyncFunction = (...args: any[]) => Promise<unknown>;
|
|
9
|
+
export type DeepPropertyKey<T extends object> = {
|
|
10
|
+
[K in keyof T]: T[K] extends object ? DeepPropertyKey<T[K]> : K;
|
|
11
|
+
};
|
|
12
|
+
export type DeepPropertyValue<T extends object> = {
|
|
13
|
+
[K in keyof T]: T[K] extends object ? DeepPropertyValue<T[K]> : T[K];
|
|
14
|
+
};
|
|
9
15
|
export interface Runnable {
|
|
10
16
|
(): void;
|
|
11
17
|
}
|
package/package.json
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
"url": "https://github.com/eloyhere"
|
|
7
7
|
},
|
|
8
8
|
"description": "A modern type-safe stream processing library inspired by JavaScript Generator, Java Stream, and MySQL Index. Supports lazy evaluation, async streams, statistics, and IO-like operations.",
|
|
9
|
-
"version": "0.
|
|
9
|
+
"version": "0.3.0",
|
|
10
10
|
"type": "module",
|
|
11
11
|
"readme": "readme.md",
|
|
12
12
|
"main": "dist/index.js",
|
|
@@ -52,12 +52,10 @@
|
|
|
52
52
|
},
|
|
53
53
|
"scripts": {
|
|
54
54
|
"build": "tsc",
|
|
55
|
+
"dev": "vite",
|
|
55
56
|
"prepublishOnly": "npm run build"
|
|
56
57
|
},
|
|
57
58
|
"devDependencies": {
|
|
58
59
|
"typescript": "~5.9.3"
|
|
59
|
-
},
|
|
60
|
-
"peerDependencies": {},
|
|
61
|
-
"dependencies": {
|
|
62
60
|
}
|
|
63
|
-
}
|
|
61
|
+
}
|
package/readme.md
CHANGED
|
@@ -148,6 +148,42 @@ console.log(empty.get(100)); // Outputs 100
|
|
|
148
148
|
|------|------|------------|------------|
|
|
149
149
|
| `Collector.full(identity, accumulator, finisher)` | Create a full collector | O(1) | O(1) |
|
|
150
150
|
| `Collector.shortable(identity, interruptor, accumulator, finisher)` | Create an interruptible collector | O(1) | O(1) |
|
|
151
|
+
| `useAnyMatch<E>(predicate)` | Create a shortable collector returning true if any element matches the predicate | O(n) | O(1) |
|
|
152
|
+
| `useAllMatch<E>(predicate)` | Create a shortable collector returning true if all elements match the predicate | O(n) | O(1) |
|
|
153
|
+
| `useCollect<E, A, R>(identity, accumulator, finisher)` | Create a full collector with identity, accumulator, finisher | O(1) | O(1) |
|
|
154
|
+
| `useCollect<E, A, R>(identity, interruptor, accumulator, finisher)` | Create a shortable collector with identity, interruptor, accumulator, finisher | O(1) | O(1) |
|
|
155
|
+
| `useCount<E>()` | Create a full collector counting elements | O(n) | O(1) |
|
|
156
|
+
| `useFindFirst<E>()` | Create a shortable collector returning the first element | O(n) | O(1) |
|
|
157
|
+
| `useFindAny<E>()` | Create a shortable collector returning any element | O(n) | O(1) |
|
|
158
|
+
| `useFindLast<E>()` | Create a full collector returning the last element | O(n) | O(1) |
|
|
159
|
+
| `useForEach<E>(action)` | Create a full collector executing an action for each element | O(n) | O(1) |
|
|
160
|
+
| `useNoneMatch<E>(predicate)` | Create a shortable collector returning true if no element matches the predicate | O(n) | O(1) |
|
|
161
|
+
| `useGroup<E, K>(classifier)` | Create a full collector grouping elements by classifier key | O(n) | O(n) |
|
|
162
|
+
| `useGroupBy<E, K, V>(keyExtractor, valueExtractor)` | Create a full collector grouping elements by key with extracted values | O(n) | O(n) |
|
|
163
|
+
| `useJoin<E>()` | Create a full collector joining elements into a string with default format | O(n) | O(1) |
|
|
164
|
+
| `useJoin<E>(delimiter)` | Create a full collector joining elements with delimiter | O(n) | O(1) |
|
|
165
|
+
| `useJoin<E>(prefix, delimiter, suffix)` | Create a full collector joining elements with prefix, delimiter, suffix | O(n) | O(1) |
|
|
166
|
+
| `useJoin<E>(prefix, accumulator, suffix)` | Create a full collector joining elements via custom accumulator | O(n) | O(1) |
|
|
167
|
+
| `useLog<E>()` | Create a full collector logging elements to console with default format | O(n) | O(1) |
|
|
168
|
+
| `useLog<E>(accumulator)` | Create a full collector logging elements via custom accumulator | O(n) | O(1) |
|
|
169
|
+
| `useLog<E>(prefix, accumulator, suffix)` | Create a full collector logging elements with prefix/suffix via accumulator | O(n) | O(1) |
|
|
170
|
+
| `usePartition<E>(count)` | Create a full collector partitioning elements into chunks of specified size | O(n) | O(n) |
|
|
171
|
+
| `usePartitionBy<E>(classifier)` | Create a full collector partitioning elements by classifier result | O(n) | O(n) |
|
|
172
|
+
| `useReduce<E>(accumulator)` | Create a full collector reducing elements without identity | O(n) | O(1) |
|
|
173
|
+
| `useReduce<E>(identity, accumulator)` | Create a full collector reducing elements with identity | O(n) | O(1) |
|
|
174
|
+
| `useReduce<E, R>(identity, accumulator, finisher)` | Create a full collector reducing elements with identity, accumulator, finisher | O(n) | O(1) |
|
|
175
|
+
| `useToArray<E>()` | Create a full collector gathering elements into an array | O(n) | O(n) |
|
|
176
|
+
| `useToMap<E, K, V>(keyExtractor, valueExtractor)` | Create a full collector gathering elements into a Map | O(n) | O(n) |
|
|
177
|
+
| `useToSet<E>()` | Create a full collector gathering elements into a Set | O(n) | O(n) |
|
|
178
|
+
| `useWrite<E, S>(stream)` | Create a full collector writing elements to a stream | O(n) | O(1) |
|
|
179
|
+
| `useWrite<E, S>(stream, accumulator)` | Create a full collector writing elements via custom accumulator | O(n) | O(1) |
|
|
180
|
+
| `useNumericAverage<E>(mapper)` | Create a full collector computing numeric average with mapper | O(n) | O(1) |
|
|
181
|
+
| `useNumericAverage<E>()` | Create a full collector computing numeric average | O(n) | O(1) |
|
|
182
|
+
| `useBigIntAverage<E>(mapper)` | Create a full collector computing bigint average with mapper | O(n) | O(1) |
|
|
183
|
+
| `useBigIntAverage<E>()` | Create a full collector computing bigint average | O(n) | O(1) |
|
|
184
|
+
| `useFrequency<E>()` | Create a full collector counting element frequencies | O(n) | O(n) |
|
|
185
|
+
| `useSummate<E>(mapper)` | Create a full collector summing mapped numeric values | O(n) | O(1) |
|
|
186
|
+
| `useSummate<E>()` | Create a full collector summing numeric elements | O(n) | O(1) |
|
|
151
187
|
|
|
152
188
|
```typescript
|
|
153
189
|
// Collector conversion examples
|
|
@@ -163,22 +199,24 @@ let ordered: OrderedCollectable<number> = from([3, 1, 4, 1, 5, 9, 2, 6, 5])
|
|
|
163
199
|
.sorted();
|
|
164
200
|
|
|
165
201
|
// Counts the number of elements
|
|
166
|
-
let count: Collector<number, number, number> =
|
|
167
|
-
(): number => 0, // Initial value
|
|
168
|
-
(accumulator: number, element: number): number => accumulator + element, // Accumulate
|
|
169
|
-
(accumulator: number): number => accumulator // Finish
|
|
170
|
-
);
|
|
202
|
+
let count: Collector<number, number, number> = useCount();
|
|
171
203
|
count.collect(from([1,2,3,4,5])); // Counts from a stream
|
|
172
204
|
count.collect([1,2,3,4,5]); // Counts from an iterable object
|
|
173
205
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
(element: number, index: bigint, accumulator: Optional<number>): Optional<number> => accumulator.isPresent(), // Interrupt
|
|
177
|
-
(accumulator: Optional<number>, element: number, index: bigint): Optional<number> => Optional.of(element), // Accumulate
|
|
178
|
-
(accumulator: Optional<number>): Optional<number> => accumulator // Finish
|
|
179
|
-
);
|
|
206
|
+
// Finds the first element
|
|
207
|
+
let findFirst: Collector<number, number, number> = useFindFirst();
|
|
180
208
|
find.collect(from([1,2,3,4,5])); // Finds the first element
|
|
181
209
|
find.collect([1,2,3,4,5]); // Finds the first element
|
|
210
|
+
|
|
211
|
+
// Calculates the sum of elements
|
|
212
|
+
let sum: Collector<number, number, number> = useSummate();
|
|
213
|
+
sum.collect(from([1,2,3,4,5])); // Sums from a stream
|
|
214
|
+
sum.collect([1,2,3,4,5]); // Sums from an iterable object
|
|
215
|
+
|
|
216
|
+
// Calculates the average of elements
|
|
217
|
+
let average: Collector<number, number, number> = useNumericAverage();
|
|
218
|
+
average.collect(from([1,2,3,4,5])); // Averages from a stream
|
|
219
|
+
average.collect([1,2,3,4,5]); // Averages from an iterable object
|
|
182
220
|
```
|
|
183
221
|
|
|
184
222
|
### Semantic Factory Methods
|