@byloth/core 1.5.0-rc.5 → 1.5.0-rc.6

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.
@@ -0,0 +1,289 @@
1
+ import { SmartAsyncIterator } from "../iterators/index.js";
2
+ import type { AsyncGeneratorFunction } from "../iterators/types.js";
3
+
4
+ import ReducedIterator from "./reduced-iterator.js";
5
+ import type { MaybeAsyncKeyIteratee, MaybeAsyncKeyTypeGuardIteratee, MaybeAsyncKeyReducer } from "./types.js";
6
+
7
+ export default class AggregatedAsyncIterator<K extends PropertyKey, T>
8
+ {
9
+ protected _elements: SmartAsyncIterator<[K, T]>;
10
+
11
+ public constructor(iterable: AsyncIterable<[K, T]>);
12
+ public constructor(iterator: AsyncIterator<[K, T]>);
13
+ public constructor(generatorFn: AsyncGeneratorFunction<[K, T]>);
14
+ public constructor(argument: AsyncIterable<[K, T]> | AsyncIterator<[K, T]> | AsyncGeneratorFunction<[K, T]>);
15
+ public constructor(argument: AsyncIterable<[K, T]> | AsyncIterator<[K, T]> | AsyncGeneratorFunction<[K, T]>)
16
+ {
17
+ this._elements = new SmartAsyncIterator(argument);
18
+ }
19
+
20
+ public async every(predicate: MaybeAsyncKeyIteratee<K, T, boolean>): Promise<ReducedIterator<K, boolean>>
21
+ {
22
+ const indexes = new Map<K, [number, boolean]>();
23
+
24
+ for await (const [key, element] of this._elements)
25
+ {
26
+ const [index, result] = indexes.get(key) ?? [0, true];
27
+
28
+ if (!(result)) { continue; }
29
+
30
+ indexes.set(key, [index + 1, await predicate(key, element, index)]);
31
+ }
32
+
33
+ return new ReducedIterator(function* ()
34
+ {
35
+ for (const [key, [_, result]] of indexes)
36
+ {
37
+ yield [key, result];
38
+ }
39
+ });
40
+ }
41
+ public async some(predicate: MaybeAsyncKeyIteratee<K, T, boolean>): Promise<ReducedIterator<K, boolean>>
42
+ {
43
+ const indexes = new Map<K, [number, boolean]>();
44
+
45
+ for await (const [key, element] of this._elements)
46
+ {
47
+ const [index, result] = indexes.get(key) ?? [0, false];
48
+
49
+ if (result) { continue; }
50
+
51
+ indexes.set(key, [index + 1, await predicate(key, element, index)]);
52
+ }
53
+
54
+ return new ReducedIterator(function* ()
55
+ {
56
+ for (const [key, [_, result]] of indexes)
57
+ {
58
+ yield [key, result];
59
+ }
60
+ });
61
+ }
62
+
63
+ public filter(predicate: MaybeAsyncKeyIteratee<K, T, boolean>): AggregatedAsyncIterator<K, T>;
64
+ public filter<S extends T>(predicate: MaybeAsyncKeyTypeGuardIteratee<K, T, S>): AggregatedAsyncIterator<K, S>;
65
+ public filter(predicate: MaybeAsyncKeyIteratee<K, T, boolean>): AggregatedAsyncIterator<K, T>
66
+ {
67
+ const elements = this._elements;
68
+
69
+ return new AggregatedAsyncIterator(async function* ()
70
+ {
71
+ const indexes = new Map<K, number>();
72
+
73
+ for await (const [key, element] of elements)
74
+ {
75
+ const index = indexes.get(key) ?? 0;
76
+
77
+ indexes.set(key, index + 1);
78
+
79
+ if (predicate(key, element, index)) { yield [key, element]; }
80
+ }
81
+ });
82
+ }
83
+ public map<V>(iteratee: MaybeAsyncKeyIteratee<K, T, V>): AggregatedAsyncIterator<K, V>
84
+ {
85
+ const elements = this._elements;
86
+
87
+ return new AggregatedAsyncIterator(async function* ()
88
+ {
89
+ const indexes = new Map<K, number>();
90
+
91
+ for await (const [key, element] of elements)
92
+ {
93
+ const index = indexes.get(key) ?? 0;
94
+
95
+ indexes.set(key, index + 1);
96
+
97
+ yield [key, await iteratee(key, element, index)];
98
+ }
99
+ });
100
+ }
101
+ public async reduce(reducer: MaybeAsyncKeyReducer<K, T, T>): Promise<ReducedIterator<K, T>>;
102
+ public async reduce<A>(reducer: MaybeAsyncKeyReducer<K, T, A>, initialValue: (key: K) => A)
103
+ : Promise<ReducedIterator<K, A>>;
104
+ public async reduce<A>(reducer: MaybeAsyncKeyReducer<K, T, A>, initialValue?: (key: K) => A)
105
+ : Promise<ReducedIterator<K, A>>
106
+ {
107
+ const accumulators = new Map<K, [number, A]>();
108
+
109
+ for await (const [key, element] of this._elements)
110
+ {
111
+ let index: number;
112
+ let accumulator: A;
113
+
114
+ if (accumulators.has(key))
115
+ {
116
+ [index, accumulator] = accumulators.get(key)!;
117
+
118
+ index += 1;
119
+ }
120
+ else if (initialValue !== undefined)
121
+ {
122
+ index = 0;
123
+ accumulator = initialValue(key);
124
+ }
125
+ else
126
+ {
127
+ accumulators.set(key, [0, (element as unknown) as A]);
128
+
129
+ continue;
130
+ }
131
+
132
+ accumulator = await reducer(key, accumulator, element, index);
133
+
134
+ accumulators.set(key, [index, accumulator]);
135
+ }
136
+
137
+ return new ReducedIterator(function* ()
138
+ {
139
+ for (const [key, [_, accumulator]] of accumulators)
140
+ {
141
+ yield [key, accumulator];
142
+ }
143
+ });
144
+ }
145
+
146
+ public unique(): AggregatedAsyncIterator<K, T>
147
+ {
148
+ const elements = this._elements;
149
+
150
+ return new AggregatedAsyncIterator(async function* ()
151
+ {
152
+ const keys = new Map<K, Set<T>>();
153
+
154
+ for await (const [key, element] of elements)
155
+ {
156
+ const values = keys.get(key) ?? new Set<T>();
157
+
158
+ if (values.has(element)) { continue; }
159
+
160
+ values.add(element);
161
+ keys.set(key, values);
162
+
163
+ yield [key, element];
164
+ }
165
+ });
166
+ }
167
+
168
+ public async count(): Promise<ReducedIterator<K, number>>
169
+ {
170
+ const counters = new Map<K, number>();
171
+
172
+ for await (const [key] of this._elements)
173
+ {
174
+ const count = counters.get(key) ?? 0;
175
+
176
+ counters.set(key, count + 1);
177
+ }
178
+
179
+ return new ReducedIterator(function* ()
180
+ {
181
+ for (const [key, count] of counters)
182
+ {
183
+ yield [key, count];
184
+ }
185
+ });
186
+ }
187
+ public async first(): Promise<ReducedIterator<K, T>>
188
+ {
189
+ const firsts = new Map<K, T>();
190
+
191
+ for await (const [key, element] of this._elements)
192
+ {
193
+ if (firsts.has(key)) { continue; }
194
+
195
+ firsts.set(key, element);
196
+ }
197
+
198
+ return new ReducedIterator(function* ()
199
+ {
200
+ for (const [key, element] of firsts)
201
+ {
202
+ yield [key, element];
203
+ }
204
+ });
205
+ }
206
+ public async last(): Promise<ReducedIterator<K, T>>
207
+ {
208
+ const lasts = new Map<K, T>();
209
+
210
+ for await (const [key, element] of this._elements)
211
+ {
212
+ lasts.set(key, element);
213
+ }
214
+
215
+ return new ReducedIterator(function* ()
216
+ {
217
+ for (const [key, element] of lasts)
218
+ {
219
+ yield [key, element];
220
+ }
221
+ });
222
+ }
223
+
224
+ public keys(): SmartAsyncIterator<K>
225
+ {
226
+ const elements = this._elements;
227
+
228
+ return new SmartAsyncIterator<K>(async function* ()
229
+ {
230
+ for await (const [key] of elements)
231
+ {
232
+ yield key;
233
+ }
234
+ });
235
+ }
236
+ public items(): SmartAsyncIterator<[K, T]>
237
+ {
238
+ return this._elements;
239
+ }
240
+ public values(): SmartAsyncIterator<T>
241
+ {
242
+ const elements = this._elements;
243
+
244
+ return new SmartAsyncIterator<T>(async function* ()
245
+ {
246
+ for await (const [_, element] of elements)
247
+ {
248
+ yield element;
249
+ }
250
+ });
251
+ }
252
+
253
+ public async toArray(): Promise<T[][]>
254
+ {
255
+ const map = await this.toMap();
256
+
257
+ return Array.from(map.values());
258
+ }
259
+ public async toMap(): Promise<Map<K, T[]>>
260
+ {
261
+ const groups = new Map<K, T[]>();
262
+
263
+ for await (const [key, element] of this._elements)
264
+ {
265
+ const value = groups.get(key) ?? [];
266
+
267
+ value.push(element);
268
+ groups.set(key, value);
269
+ }
270
+
271
+ return groups;
272
+ }
273
+ public async toObject(): Promise<Record<K, T[]>>
274
+ {
275
+ const groups = { } as Record<K, T[]>;
276
+
277
+ for await (const [key, element] of this._elements)
278
+ {
279
+ const value = groups[key] ?? [];
280
+
281
+ value.push(element);
282
+ groups[key] = value;
283
+ }
284
+
285
+ return groups;
286
+ }
287
+
288
+ public get [Symbol.toStringTag]() { return "AggregatedAsyncIterator"; }
289
+ }
@@ -1,8 +1,8 @@
1
- import ReducedIterator from "./reduced-iterator.js";
2
- import SmartIterator from "../smart-iterator.js";
1
+ import { SmartIterator } from "../iterators/index.js";
2
+ import type { GeneratorFunction } from "../iterators/types.js";
3
3
 
4
+ import ReducedIterator from "./reduced-iterator.js";
4
5
  import type { KeyIteratee, KeyTypeGuardIteratee, KeyReducer } from "./types.js";
5
- import type { GeneratorFunction } from "../../types.js";
6
6
 
7
7
  export default class AggregatedIterator<K extends PropertyKey, T>
8
8
  {
@@ -0,0 +1,46 @@
1
+ import AggregatedIterator from "./aggregated-iterator.js";
2
+
3
+ import { SmartIterator } from "../iterators/index.js";
4
+ import type { GeneratorFunction, Iteratee, TypeGuardIteratee } from "../iterators/types.js";
5
+
6
+ export default class Aggregator<T>
7
+ {
8
+ protected _elements: SmartIterator<T>;
9
+
10
+ public constructor(iterable: Iterable<T>);
11
+ public constructor(iterator: Iterator<T>);
12
+ public constructor(generatorFn: GeneratorFunction<T>);
13
+ public constructor(argument: Iterable<T> | Iterator<T> | GeneratorFunction<T>);
14
+ public constructor(argument: Iterable<T> | Iterator<T> | GeneratorFunction<T>)
15
+ {
16
+ this._elements = new SmartIterator(argument);
17
+ }
18
+
19
+ public filter(predicate: Iteratee<T, boolean>): Aggregator<T>;
20
+ public filter<S extends T>(predicate: TypeGuardIteratee<T, S>): Aggregator<S>;
21
+ public filter(predicate: Iteratee<T, boolean>): Aggregator<T>
22
+ {
23
+ return new Aggregator(this._elements.filter(predicate));
24
+ }
25
+ public map<V>(iteratee: Iteratee<T, V>): Aggregator<V>
26
+ {
27
+ return new Aggregator(this._elements.map(iteratee));
28
+ }
29
+
30
+ public unique(): Aggregator<T>
31
+ {
32
+ return new Aggregator(this._elements.unique());
33
+ }
34
+
35
+ public groupBy<K extends PropertyKey>(iteratee: Iteratee<T, K>): AggregatedIterator<K, T>
36
+ {
37
+ return new AggregatedIterator(this._elements.map((element, index) =>
38
+ {
39
+ const key = iteratee(element, index);
40
+
41
+ return [key, element] as [K, T];
42
+ }));
43
+ }
44
+
45
+ public get [Symbol.toStringTag]() { return "Aggregator"; }
46
+ }
@@ -0,0 +1,46 @@
1
+ import AggregatedAsyncIterator from "./aggregated-async-iterator.js";
2
+
3
+ import { SmartAsyncIterator } from "../iterators/index.js";
4
+ import type { AsyncGeneratorFunction, MaybeAsyncIteratee, MaybeAsyncTypeGuardIteratee } from "../iterators/types.js";
5
+
6
+ export default class AsyncAggregator<T>
7
+ {
8
+ protected _elements: SmartAsyncIterator<T>;
9
+
10
+ public constructor(iterable: AsyncIterable<T>);
11
+ public constructor(iterator: AsyncIterator<T>);
12
+ public constructor(generatorFn: AsyncGeneratorFunction<T>);
13
+ public constructor(argument: AsyncIterable<T> | AsyncIterator<T> | AsyncGeneratorFunction<T>);
14
+ public constructor(argument: AsyncIterable<T> | AsyncIterator<T> | AsyncGeneratorFunction<T>)
15
+ {
16
+ this._elements = new SmartAsyncIterator(argument);
17
+ }
18
+
19
+ public filter(predicate: MaybeAsyncIteratee<T, boolean>): AsyncAggregator<T>;
20
+ public filter<S extends T>(predicate: MaybeAsyncTypeGuardIteratee<T, S>): AsyncAggregator<S>;
21
+ public filter(predicate: MaybeAsyncIteratee<T, boolean>): AsyncAggregator<T>
22
+ {
23
+ return new AsyncAggregator(this._elements.filter(predicate));
24
+ }
25
+ public map<V>(iteratee: MaybeAsyncIteratee<T, V>): AsyncAggregator<V>
26
+ {
27
+ return new AsyncAggregator(this._elements.map(iteratee));
28
+ }
29
+
30
+ public unique(): AsyncAggregator<T>
31
+ {
32
+ return new AsyncAggregator(this._elements.unique());
33
+ }
34
+
35
+ public groupBy<K extends PropertyKey>(iteratee: MaybeAsyncIteratee<T, K>): AggregatedAsyncIterator<K, T>
36
+ {
37
+ return new AggregatedAsyncIterator(this._elements.map(async (element, index) =>
38
+ {
39
+ const key = await iteratee(element, index);
40
+
41
+ return [key, element] as [K, T];
42
+ }));
43
+ }
44
+
45
+ public get [Symbol.toStringTag]() { return "AsyncAggregator"; }
46
+ }
@@ -1,50 +1,7 @@
1
+ import Aggregator from "./aggregator.js";
2
+ import AsyncAggregator from "./async-aggregator.js";
1
3
  import AggregatedIterator from "./aggregated-iterator.js";
4
+ import AggregatedAsyncIterator from "./aggregated-async-iterator.js";
2
5
  import ReducedIterator from "./reduced-iterator.js";
3
- import SmartIterator from "../smart-iterator.js";
4
6
 
5
- import type { GeneratorFunction, Iteratee, TypeGuardIteratee } from "../../types.js";
6
-
7
- export default class Aggregator<T>
8
- {
9
- protected _elements: SmartIterator<T>;
10
-
11
- public constructor(iterable: Iterable<T>);
12
- public constructor(iterator: Iterator<T>);
13
- public constructor(generatorFn: GeneratorFunction<T>);
14
- public constructor(argument: Iterable<T> | Iterator<T> | GeneratorFunction<T>);
15
- public constructor(argument: Iterable<T> | Iterator<T> | GeneratorFunction<T>)
16
- {
17
- this._elements = new SmartIterator(argument);
18
- }
19
-
20
- public filter(predicate: Iteratee<T, boolean>): Aggregator<T>;
21
- public filter<S extends T>(predicate: TypeGuardIteratee<T, S>): Aggregator<S>;
22
- public filter(predicate: Iteratee<T, boolean>): Aggregator<T>
23
- {
24
- return new Aggregator(this._elements.filter(predicate));
25
- }
26
- public map<V>(iteratee: Iteratee<T, V>): Aggregator<V>
27
- {
28
- return new Aggregator(this._elements.map(iteratee));
29
- }
30
-
31
- public unique(): Aggregator<T>
32
- {
33
- return new Aggregator(this._elements.unique());
34
- }
35
-
36
- public groupBy<K extends PropertyKey>(iteratee: Iteratee<T, K>): AggregatedIterator<K, T>
37
- {
38
- return new AggregatedIterator(this._elements.map((element, index) =>
39
- {
40
- const key = iteratee(element, index);
41
-
42
- return [key, element] as [K, T];
43
- }));
44
- }
45
-
46
- public get [Symbol.toStringTag]() { return "Aggregator"; }
47
- }
48
-
49
- export { AggregatedIterator, ReducedIterator };
50
- export type { KeyIteratee, KeyReducer, KeyTypeGuardIteratee } from "./types.js";
7
+ export { Aggregator, AsyncAggregator, AggregatedIterator, AggregatedAsyncIterator, ReducedIterator };
@@ -1,7 +1,7 @@
1
- import SmartIterator from "../smart-iterator.js";
1
+ import { SmartIterator } from "../iterators/index.js";
2
+ import type { GeneratorFunction } from "../iterators/types.js";
2
3
 
3
4
  import type { KeyIteratee, KeyTypeGuardIteratee } from "./types.js";
4
- import type { GeneratorFunction } from "../../types.js";
5
5
 
6
6
  export default class ReducedIterator<K extends PropertyKey, T>
7
7
  {
@@ -1,5 +1,14 @@
1
+ import type { MaybePromise } from "../promises/types.js";
2
+
1
3
  export type KeyIteratee<K extends PropertyKey, T, R = void> = (key: K, value: T, index: number) => R;
4
+ export type MaybeAsyncKeyIteratee<K extends PropertyKey, T, R = void> = (key: K, value: T, index: number) =>
5
+ MaybePromise<R>;
6
+
2
7
  export type KeyTypeGuardIteratee<K extends PropertyKey, T, R extends T> =
3
8
  (key: K, value: T, index: number) => value is R;
9
+ export type MaybeAsyncKeyTypeGuardIteratee<K extends PropertyKey, T, R extends T> =
10
+ (key: K, value: MaybePromise<T>, index: number) => value is MaybePromise<R>;
4
11
 
5
12
  export type KeyReducer<K extends PropertyKey, T, A> = (key: K, accumulator: A, value: T, index: number) => A;
13
+ export type MaybeAsyncKeyReducer<K extends PropertyKey, T, A> =
14
+ (key: K, accumulator: A, value: T, index: number) => MaybePromise<A>;
@@ -1,18 +1,18 @@
1
- import Aggregator, { AggregatedIterator, ReducedIterator } from "./aggregators/index.js";
2
- export type { KeyIteratee, KeyReducer, KeyTypeGuardIteratee } from "./aggregators/index.js";
1
+ export {
2
+ Aggregator,
3
+ AsyncAggregator,
4
+ AggregatedIterator,
5
+ AggregatedAsyncIterator,
6
+ ReducedIterator
7
+
8
+ } from "./aggregators/index.js";
3
9
 
4
10
  export { Exception, ReferenceException, TimeoutException, ValueException } from "./exceptions/index.js";
11
+ export { SmartIterator, SmartAsyncIterator } from "./iterators/index.js";
5
12
 
6
13
  import JsonStorage from "./json-storage.js";
7
- import SmartIterator from "./smart-iterator.js";
8
14
  import Subscribers from "./subscribers.js";
9
15
 
10
16
  export { DeferredPromise, SmartPromise, TimedPromise } from "./promises/index.js";
11
- export {
12
- AggregatedIterator,
13
- Aggregator,
14
- JsonStorage,
15
- ReducedIterator,
16
- SmartIterator,
17
- Subscribers
18
- };
17
+
18
+ export { JsonStorage, Subscribers };
@@ -0,0 +1,4 @@
1
+ import SmartIterator from "./smart-iterator.js";
2
+ import SmartAsyncIterator from "./smart-async-iterator.js";
3
+
4
+ export { SmartIterator, SmartAsyncIterator };