@byloth/core 2.0.0-rc.2 → 2.0.0-rc.4
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/core.js +1070 -862
- package/dist/core.js.map +1 -1
- package/dist/core.umd.cjs +3 -3
- package/dist/core.umd.cjs.map +1 -1
- package/package.json +7 -11
- package/src/helpers.ts +7 -0
- package/src/index.ts +10 -12
- package/src/models/aggregators/aggregated-async-iterator.ts +134 -81
- package/src/models/aggregators/aggregated-iterator.ts +133 -82
- package/src/models/aggregators/index.ts +1 -3
- package/src/models/aggregators/reduced-iterator.ts +118 -30
- package/src/models/aggregators/types.ts +15 -10
- package/src/models/exceptions/core.ts +3 -3
- package/src/models/exceptions/index.ts +13 -13
- package/src/models/game-loop.ts +2 -0
- package/src/models/index.ts +0 -2
- package/src/models/iterators/smart-async-iterator.ts +109 -23
- package/src/models/iterators/smart-iterator.ts +105 -12
- package/src/models/iterators/types.ts +17 -7
- package/src/models/json/json-storage.ts +2 -3
- package/src/models/json/types.ts +5 -1
- package/src/models/promises/deferred-promise.ts +1 -1
- package/src/models/promises/smart-promise.ts +6 -1
- package/src/models/promises/timed-promise.ts +1 -1
- package/src/models/publisher.ts +1 -1
- package/src/models/timers/clock.ts +3 -1
- package/src/models/timers/countdown.ts +3 -1
- package/src/models/types.ts +9 -9
- package/src/utils/async.ts +4 -4
- package/src/utils/date.ts +3 -0
- package/src/utils/random.ts +4 -3
- package/src/models/aggregators/aggregator.ts +0 -46
- package/src/models/aggregators/async-aggregator.ts +0 -56
package/src/helpers.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
2
|
+
|
|
3
|
+
// @ts-ignore
|
|
1
4
|
export const isBrowser = ((typeof window !== "undefined") && (typeof window.document !== "undefined"));
|
|
5
|
+
|
|
6
|
+
// @ts-ignore
|
|
2
7
|
export const isNode = ((typeof process !== "undefined") && (process.versions?.node));
|
|
8
|
+
|
|
9
|
+
// @ts-ignore
|
|
3
10
|
export const isWebWorker = ((typeof self === "object") && (self.constructor?.name === "DedicatedWorkerGlobalScope"));
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export const VERSION = "2.0.0-rc.
|
|
1
|
+
export const VERSION = "2.0.0-rc.4";
|
|
2
2
|
|
|
3
3
|
export type { Constructor, Interval, Timeout } from "./core/types.js";
|
|
4
4
|
|
|
@@ -7,8 +7,6 @@ export { isBrowser, isNode, isWebWorker } from "./helpers.js";
|
|
|
7
7
|
export {
|
|
8
8
|
AggregatedIterator,
|
|
9
9
|
AggregatedAsyncIterator,
|
|
10
|
-
Aggregator,
|
|
11
|
-
AsyncAggregator,
|
|
12
10
|
Clock,
|
|
13
11
|
Countdown,
|
|
14
12
|
DeferredPromise,
|
|
@@ -40,23 +38,23 @@ export {
|
|
|
40
38
|
|
|
41
39
|
export type {
|
|
42
40
|
AsyncGeneratorFunction,
|
|
43
|
-
|
|
41
|
+
AsyncIteratorLike,
|
|
44
42
|
FulfilledHandler,
|
|
45
43
|
GeneratorFunction,
|
|
46
44
|
Iteratee,
|
|
47
|
-
|
|
45
|
+
IteratorLike,
|
|
48
46
|
JSONArray,
|
|
49
47
|
JSONObject,
|
|
50
48
|
JSONValue,
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
49
|
+
KeyedIteratee,
|
|
50
|
+
KeyedReducer,
|
|
51
|
+
KeyedTypeGuardIteratee,
|
|
52
|
+
MaybeAsyncKeyedIteratee,
|
|
53
|
+
MaybeAsyncKeyedReducer,
|
|
54
|
+
MaybeAsyncKeyedTypeGuardIteratee,
|
|
57
55
|
MaybeAsyncReducer,
|
|
58
56
|
MaybeAsyncIteratee,
|
|
59
|
-
|
|
57
|
+
MaybeAsyncIteratorLike,
|
|
60
58
|
MaybeAsyncTypeGuardIteratee,
|
|
61
59
|
MaybePromise,
|
|
62
60
|
PromiseExecutor,
|
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import { SmartAsyncIterator } from "../iterators/index.js";
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
GeneratorFunction,
|
|
4
|
+
AsyncGeneratorFunction,
|
|
5
|
+
MaybeAsyncGeneratorFunction,
|
|
6
|
+
MaybeAsyncIteratorLike
|
|
7
|
+
|
|
8
|
+
} from "../iterators/types.js";
|
|
3
9
|
import type { MaybePromise } from "../types.js";
|
|
4
10
|
|
|
5
11
|
import ReducedIterator from "./reduced-iterator.js";
|
|
6
|
-
import type {
|
|
12
|
+
import type { MaybeAsyncKeyedIteratee, MaybeAsyncKeyedTypeGuardIteratee, MaybeAsyncKeyedReducer } from "./types.js";
|
|
7
13
|
|
|
8
14
|
export default class AggregatedAsyncIterator<K extends PropertyKey, T>
|
|
9
15
|
{
|
|
@@ -15,58 +21,52 @@ export default class AggregatedAsyncIterator<K extends PropertyKey, T>
|
|
|
15
21
|
public constructor(iterator: AsyncIterator<[K, T]>);
|
|
16
22
|
public constructor(generatorFn: GeneratorFunction<[K, T]>);
|
|
17
23
|
public constructor(generatorFn: AsyncGeneratorFunction<[K, T]>);
|
|
18
|
-
public constructor(argument:
|
|
19
|
-
public constructor(argument:
|
|
24
|
+
public constructor(argument: MaybeAsyncIteratorLike<[K, T]> | MaybeAsyncGeneratorFunction<[K, T]>);
|
|
25
|
+
public constructor(argument: MaybeAsyncIteratorLike<[K, T]> | MaybeAsyncGeneratorFunction<[K, T]>)
|
|
20
26
|
{
|
|
21
27
|
this._elements = new SmartAsyncIterator(argument);
|
|
22
28
|
}
|
|
23
29
|
|
|
24
|
-
public async every(predicate:
|
|
30
|
+
public async every(predicate: MaybeAsyncKeyedIteratee<K, T, boolean>): Promise<ReducedIterator<K, boolean>>
|
|
25
31
|
{
|
|
26
|
-
const
|
|
32
|
+
const values = new Map<K, [number, boolean]>();
|
|
27
33
|
|
|
28
34
|
for await (const [key, element] of this._elements)
|
|
29
35
|
{
|
|
30
|
-
const [index, result] =
|
|
36
|
+
const [index, result] = values.get(key) ?? [0, true];
|
|
31
37
|
|
|
32
38
|
if (!(result)) { continue; }
|
|
33
39
|
|
|
34
|
-
|
|
40
|
+
values.set(key, [index + 1, await predicate(key, element, index)]);
|
|
35
41
|
}
|
|
36
42
|
|
|
37
43
|
return new ReducedIterator(function* ()
|
|
38
44
|
{
|
|
39
|
-
for (const [key, [_, result]] of
|
|
40
|
-
{
|
|
41
|
-
yield [key, result];
|
|
42
|
-
}
|
|
45
|
+
for (const [key, [_, result]] of values) { yield [key, result]; }
|
|
43
46
|
});
|
|
44
47
|
}
|
|
45
|
-
public async some(predicate:
|
|
48
|
+
public async some(predicate: MaybeAsyncKeyedIteratee<K, T, boolean>): Promise<ReducedIterator<K, boolean>>
|
|
46
49
|
{
|
|
47
|
-
const
|
|
50
|
+
const values = new Map<K, [number, boolean]>();
|
|
48
51
|
|
|
49
52
|
for await (const [key, element] of this._elements)
|
|
50
53
|
{
|
|
51
|
-
const [index, result] =
|
|
54
|
+
const [index, result] = values.get(key) ?? [0, false];
|
|
52
55
|
|
|
53
56
|
if (result) { continue; }
|
|
54
57
|
|
|
55
|
-
|
|
58
|
+
values.set(key, [index + 1, await predicate(key, element, index)]);
|
|
56
59
|
}
|
|
57
60
|
|
|
58
61
|
return new ReducedIterator(function* ()
|
|
59
62
|
{
|
|
60
|
-
for (const [key, [_, result]] of
|
|
61
|
-
{
|
|
62
|
-
yield [key, result];
|
|
63
|
-
}
|
|
63
|
+
for (const [key, [_, result]] of values) { yield [key, result]; }
|
|
64
64
|
});
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
public filter(predicate:
|
|
68
|
-
public filter<S extends T>(predicate:
|
|
69
|
-
public filter(predicate:
|
|
67
|
+
public filter(predicate: MaybeAsyncKeyedIteratee<K, T, boolean>): AggregatedAsyncIterator<K, T>;
|
|
68
|
+
public filter<S extends T>(predicate: MaybeAsyncKeyedTypeGuardIteratee<K, T, S>): AggregatedAsyncIterator<K, S>;
|
|
69
|
+
public filter(predicate: MaybeAsyncKeyedIteratee<K, T, boolean>): AggregatedAsyncIterator<K, T>
|
|
70
70
|
{
|
|
71
71
|
const elements = this._elements;
|
|
72
72
|
|
|
@@ -78,13 +78,13 @@ export default class AggregatedAsyncIterator<K extends PropertyKey, T>
|
|
|
78
78
|
{
|
|
79
79
|
const index = indexes.get(key) ?? 0;
|
|
80
80
|
|
|
81
|
-
indexes.set(key, index + 1);
|
|
82
|
-
|
|
83
81
|
if (await predicate(key, element, index)) { yield [key, element]; }
|
|
82
|
+
|
|
83
|
+
indexes.set(key, index + 1);
|
|
84
84
|
}
|
|
85
85
|
});
|
|
86
86
|
}
|
|
87
|
-
public map<V>(iteratee:
|
|
87
|
+
public map<V>(iteratee: MaybeAsyncKeyedIteratee<K, T, V>): AggregatedAsyncIterator<K, V>
|
|
88
88
|
{
|
|
89
89
|
const elements = this._elements;
|
|
90
90
|
|
|
@@ -96,31 +96,26 @@ export default class AggregatedAsyncIterator<K extends PropertyKey, T>
|
|
|
96
96
|
{
|
|
97
97
|
const index = indexes.get(key) ?? 0;
|
|
98
98
|
|
|
99
|
-
indexes.set(key, index + 1);
|
|
100
|
-
|
|
101
99
|
yield [key, await iteratee(key, element, index)];
|
|
100
|
+
|
|
101
|
+
indexes.set(key, index + 1);
|
|
102
102
|
}
|
|
103
103
|
});
|
|
104
104
|
}
|
|
105
|
-
public async reduce(reducer:
|
|
106
|
-
public async reduce<A>(reducer:
|
|
105
|
+
public async reduce(reducer: MaybeAsyncKeyedReducer<K, T, T>): Promise<ReducedIterator<K, T>>;
|
|
106
|
+
public async reduce<A>(reducer: MaybeAsyncKeyedReducer<K, T, A>, initialValue: (key: K) => MaybePromise<A>)
|
|
107
107
|
: Promise<ReducedIterator<K, A>>;
|
|
108
|
-
public async reduce<A>(reducer:
|
|
108
|
+
public async reduce<A>(reducer: MaybeAsyncKeyedReducer<K, T, A>, initialValue?: (key: K) => MaybePromise<A>)
|
|
109
109
|
: Promise<ReducedIterator<K, A>>
|
|
110
110
|
{
|
|
111
|
-
const
|
|
111
|
+
const values = new Map<K, [number, A]>();
|
|
112
112
|
|
|
113
113
|
for await (const [key, element] of this._elements)
|
|
114
114
|
{
|
|
115
115
|
let index: number;
|
|
116
116
|
let accumulator: A;
|
|
117
117
|
|
|
118
|
-
if (
|
|
119
|
-
{
|
|
120
|
-
[index, accumulator] = accumulators.get(key)!;
|
|
121
|
-
|
|
122
|
-
index += 1;
|
|
123
|
-
}
|
|
118
|
+
if (values.has(key)) { [index, accumulator] = values.get(key)!; }
|
|
124
119
|
else if (initialValue !== undefined)
|
|
125
120
|
{
|
|
126
121
|
index = 0;
|
|
@@ -128,103 +123,164 @@ export default class AggregatedAsyncIterator<K extends PropertyKey, T>
|
|
|
128
123
|
}
|
|
129
124
|
else
|
|
130
125
|
{
|
|
131
|
-
|
|
126
|
+
values.set(key, [0, (element as unknown) as A]);
|
|
132
127
|
|
|
133
128
|
continue;
|
|
134
129
|
}
|
|
135
130
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
accumulators.set(key, [index, accumulator]);
|
|
131
|
+
values.set(key, [index + 1, await reducer(key, accumulator, element, index)]);
|
|
139
132
|
}
|
|
140
133
|
|
|
141
134
|
return new ReducedIterator(function* ()
|
|
142
135
|
{
|
|
143
|
-
for (const [key, [_, accumulator]] of
|
|
136
|
+
for (const [key, [_, accumulator]] of values) { yield [key, accumulator]; }
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
public flatMap<V>(iteratee: MaybeAsyncKeyedIteratee<K, T, Iterable<V>>): AggregatedAsyncIterator<K, V>
|
|
141
|
+
{
|
|
142
|
+
const elements = this._elements;
|
|
143
|
+
|
|
144
|
+
return new AggregatedAsyncIterator(async function* (): AsyncGenerator<[K, V]>
|
|
145
|
+
{
|
|
146
|
+
const indexes = new Map<K, number>();
|
|
147
|
+
|
|
148
|
+
for await (const [key, element] of elements)
|
|
144
149
|
{
|
|
145
|
-
|
|
150
|
+
const index = indexes.get(key) ?? 0;
|
|
151
|
+
const values = await iteratee(key, element, index);
|
|
152
|
+
|
|
153
|
+
for await (const value of values) { yield [key, value]; }
|
|
154
|
+
|
|
155
|
+
indexes.set(key, index + 1);
|
|
146
156
|
}
|
|
147
157
|
});
|
|
148
158
|
}
|
|
149
159
|
|
|
150
|
-
public
|
|
160
|
+
public drop(count: number): AggregatedAsyncIterator<K, T>
|
|
151
161
|
{
|
|
152
162
|
const elements = this._elements;
|
|
153
163
|
|
|
154
164
|
return new AggregatedAsyncIterator(async function* (): AsyncGenerator<[K, T]>
|
|
155
165
|
{
|
|
156
|
-
const
|
|
166
|
+
const indexes = new Map<K, number>();
|
|
157
167
|
|
|
158
168
|
for await (const [key, element] of elements)
|
|
159
169
|
{
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
|
|
170
|
+
const index = indexes.get(key) ?? 0;
|
|
171
|
+
if (index < count)
|
|
172
|
+
{
|
|
173
|
+
indexes.set(key, index + 1);
|
|
163
174
|
|
|
164
|
-
|
|
165
|
-
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
166
177
|
|
|
167
178
|
yield [key, element];
|
|
168
179
|
}
|
|
169
180
|
});
|
|
170
181
|
}
|
|
171
|
-
|
|
172
|
-
public async count(): Promise<ReducedIterator<K, number>>
|
|
182
|
+
public take(limit: number): AggregatedAsyncIterator<K, T>
|
|
173
183
|
{
|
|
174
|
-
const
|
|
184
|
+
const elements = this._elements;
|
|
175
185
|
|
|
176
|
-
|
|
186
|
+
return new AggregatedAsyncIterator(async function* (): AsyncGenerator<[K, T]>
|
|
177
187
|
{
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
-
counters.set(key, count + 1);
|
|
181
|
-
}
|
|
188
|
+
const indexes = new Map<K, number>();
|
|
182
189
|
|
|
183
|
-
|
|
184
|
-
{
|
|
185
|
-
for (const [key, count] of counters)
|
|
190
|
+
for await (const [key, element] of elements)
|
|
186
191
|
{
|
|
187
|
-
|
|
192
|
+
const index = indexes.get(key) ?? 0;
|
|
193
|
+
if (index >= limit)
|
|
194
|
+
{
|
|
195
|
+
if (indexes.values().every((value) => value >= limit)) { break; }
|
|
196
|
+
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
yield [key, element];
|
|
201
|
+
|
|
202
|
+
indexes.set(key, index + 1);
|
|
188
203
|
}
|
|
189
204
|
});
|
|
190
205
|
}
|
|
191
|
-
|
|
206
|
+
|
|
207
|
+
public async find(predicate: MaybeAsyncKeyedIteratee<K, T, boolean>): Promise<ReducedIterator<K, T | undefined>>;
|
|
208
|
+
public async find<S extends T>(predicate: MaybeAsyncKeyedTypeGuardIteratee<K, T, S>)
|
|
209
|
+
: Promise<ReducedIterator<K, S | undefined>>;
|
|
210
|
+
|
|
211
|
+
public async find(predicate: MaybeAsyncKeyedIteratee<K, T, boolean>): Promise<ReducedIterator<K, T | undefined>>
|
|
192
212
|
{
|
|
193
|
-
const
|
|
213
|
+
const values = new Map<K, [number, T | undefined]>();
|
|
194
214
|
|
|
195
215
|
for await (const [key, element] of this._elements)
|
|
196
216
|
{
|
|
197
|
-
|
|
217
|
+
let [index, finding] = values.get(key) ?? [0, undefined];
|
|
198
218
|
|
|
199
|
-
|
|
219
|
+
if (finding !== undefined) { continue; }
|
|
220
|
+
if (await predicate(key, element, index)) { finding = element; }
|
|
221
|
+
|
|
222
|
+
values.set(key, [index + 1, finding]);
|
|
200
223
|
}
|
|
201
224
|
|
|
202
225
|
return new ReducedIterator(function* ()
|
|
203
226
|
{
|
|
204
|
-
for (const [key,
|
|
227
|
+
for (const [key, [_, finding]] of values) { yield [key, finding]; }
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
public unique(): AggregatedAsyncIterator<K, T>
|
|
232
|
+
{
|
|
233
|
+
const elements = this._elements;
|
|
234
|
+
|
|
235
|
+
return new AggregatedAsyncIterator(async function* (): AsyncGenerator<[K, T]>
|
|
236
|
+
{
|
|
237
|
+
const keys = new Map<K, Set<T>>();
|
|
238
|
+
|
|
239
|
+
for await (const [key, element] of elements)
|
|
205
240
|
{
|
|
241
|
+
const values = keys.get(key) ?? new Set<T>();
|
|
242
|
+
|
|
243
|
+
if (values.has(element)) { continue; }
|
|
244
|
+
|
|
245
|
+
values.add(element);
|
|
246
|
+
keys.set(key, values);
|
|
247
|
+
|
|
206
248
|
yield [key, element];
|
|
207
249
|
}
|
|
208
250
|
});
|
|
209
251
|
}
|
|
210
|
-
|
|
252
|
+
|
|
253
|
+
public async count(): Promise<ReducedIterator<K, number>>
|
|
211
254
|
{
|
|
212
|
-
const
|
|
255
|
+
const counters = new Map<K, number>();
|
|
213
256
|
|
|
214
|
-
for await (const [key
|
|
257
|
+
for await (const [key] of this._elements)
|
|
215
258
|
{
|
|
216
|
-
|
|
259
|
+
const count = counters.get(key) ?? 0;
|
|
260
|
+
|
|
261
|
+
counters.set(key, count + 1);
|
|
217
262
|
}
|
|
218
263
|
|
|
219
264
|
return new ReducedIterator(function* ()
|
|
220
265
|
{
|
|
221
|
-
for (const [key,
|
|
222
|
-
{
|
|
223
|
-
yield [key, element];
|
|
224
|
-
}
|
|
266
|
+
for (const [key, count] of counters) { yield [key, count]; }
|
|
225
267
|
});
|
|
226
268
|
}
|
|
227
269
|
|
|
270
|
+
public async forEach(iteratee: MaybeAsyncKeyedIteratee<K, T>): Promise<void>
|
|
271
|
+
{
|
|
272
|
+
const indexes = new Map<K, number>();
|
|
273
|
+
|
|
274
|
+
for await (const [key, element] of this._elements)
|
|
275
|
+
{
|
|
276
|
+
const index = indexes.get(key) ?? 0;
|
|
277
|
+
|
|
278
|
+
iteratee(key, element, index);
|
|
279
|
+
|
|
280
|
+
indexes.set(key, index + 1);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
228
284
|
public keys(): SmartAsyncIterator<K>
|
|
229
285
|
{
|
|
230
286
|
const elements = this._elements;
|
|
@@ -252,10 +308,7 @@ export default class AggregatedAsyncIterator<K extends PropertyKey, T>
|
|
|
252
308
|
|
|
253
309
|
return new SmartAsyncIterator<T>(async function* ()
|
|
254
310
|
{
|
|
255
|
-
for await (const [_, element] of elements)
|
|
256
|
-
{
|
|
257
|
-
yield element;
|
|
258
|
-
}
|
|
311
|
+
for await (const [_, element] of elements) { yield element; }
|
|
259
312
|
});
|
|
260
313
|
}
|
|
261
314
|
|
|
@@ -294,5 +347,5 @@ export default class AggregatedAsyncIterator<K extends PropertyKey, T>
|
|
|
294
347
|
return groups;
|
|
295
348
|
}
|
|
296
349
|
|
|
297
|
-
public
|
|
350
|
+
public readonly [Symbol.toStringTag]: string = "AggregatedAsyncIterator";
|
|
298
351
|
}
|