@byloth/core 2.0.0-rc.2 → 2.0.0-rc.3

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.
@@ -1,11 +1,14 @@
1
+ import AggregatedAsyncIterator from "../aggregators/aggregated-async-iterator.js";
1
2
  import { ValueException } from "../exceptions/index.js";
2
3
 
3
4
  import type {
4
- AsyncGeneratorFunction,
5
5
  GeneratorFunction,
6
+ AsyncGeneratorFunction,
7
+ MaybeAsyncGeneratorFunction,
6
8
  MaybeAsyncIteratee,
7
9
  MaybeAsyncReducer,
8
- MaybeAsyncIterLike,
10
+ MaybeAsyncIterable,
11
+ MaybeAsyncIteratorLike,
9
12
  MaybeAsyncTypeGuardIteratee
10
13
 
11
14
  } from "./types.js";
@@ -23,8 +26,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
23
26
  public constructor(iterator: AsyncIterator<T, R, N>);
24
27
  public constructor(generatorFn: GeneratorFunction<T, R, N>);
25
28
  public constructor(generatorFn: AsyncGeneratorFunction<T, R, N>);
26
- public constructor(argument: MaybeAsyncIterLike<T, R, N>);
27
- public constructor(argument: MaybeAsyncIterLike<T, R, N>)
29
+ public constructor(argument: MaybeAsyncIteratorLike<T, R, N> | MaybeAsyncGeneratorFunction<T, R, N>);
30
+ public constructor(argument: MaybeAsyncIteratorLike<T, R, N> | MaybeAsyncGeneratorFunction<T, R, N>)
28
31
  {
29
32
  if (argument instanceof Function)
30
33
  {
@@ -100,7 +103,7 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
100
103
  const result = await this._iterator.next();
101
104
 
102
105
  if (result.done) { return true; }
103
- if (!(predicate(result.value, index))) { return false; }
106
+ if (!(await predicate(result.value, index))) { return false; }
104
107
 
105
108
  index += 1;
106
109
  }
@@ -115,7 +118,7 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
115
118
  const result = await this._iterator.next();
116
119
 
117
120
  if (result.done) { return false; }
118
- if (predicate(result.value, index)) { return true; }
121
+ if (await predicate(result.value, index)) { return true; }
119
122
 
120
123
  index += 1;
121
124
  }
@@ -136,7 +139,7 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
136
139
  const result = await iterator.next();
137
140
 
138
141
  if (result.done) { return result.value; }
139
- if (predicate(result.value, index)) { yield result.value; }
142
+ if (await predicate(result.value, index)) { yield result.value; }
140
143
 
141
144
  index += 1;
142
145
  }
@@ -155,7 +158,7 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
155
158
  const result = await iterator.next();
156
159
  if (result.done) { return result.value; }
157
160
 
158
- yield iteratee(result.value, index);
161
+ yield await iteratee(result.value, index);
159
162
 
160
163
  index += 1;
161
164
  }
@@ -188,6 +191,94 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
188
191
  }
189
192
  }
190
193
 
194
+ public flatMap<V>(iteratee: MaybeAsyncIteratee<T, MaybeAsyncIterable<V>>): SmartAsyncIterator<V, R>
195
+ {
196
+ const iterator = this._iterator;
197
+
198
+ return new SmartAsyncIterator<V, R>(async function* ()
199
+ {
200
+ let index = 0;
201
+
202
+ while (true)
203
+ {
204
+ const result = await iterator.next();
205
+ if (result.done) { return result.value; }
206
+
207
+ const elements = await iteratee(result.value, index);
208
+
209
+ for await (const element of elements)
210
+ {
211
+ yield element;
212
+ }
213
+
214
+ index += 1;
215
+ }
216
+ });
217
+ }
218
+
219
+ public drop(count: number): SmartAsyncIterator<T, R | void>
220
+ {
221
+ const iterator = this._iterator;
222
+
223
+ return new SmartAsyncIterator<T, R | void>(async function* ()
224
+ {
225
+ let index = 0;
226
+
227
+ while (index < count)
228
+ {
229
+ const result = await iterator.next();
230
+ if (result.done) { return; }
231
+
232
+ index += 1;
233
+ }
234
+
235
+ while (true)
236
+ {
237
+ const result = await iterator.next();
238
+ if (result.done) { return result.value; }
239
+
240
+ yield result.value;
241
+ }
242
+ });
243
+ }
244
+ public take(limit: number): SmartAsyncIterator<T, R | void>
245
+ {
246
+ const iterator = this._iterator;
247
+
248
+ return new SmartAsyncIterator<T, R | void>(async function* ()
249
+ {
250
+ let index = 0;
251
+
252
+ while (index < limit)
253
+ {
254
+ const result = await iterator.next();
255
+ if (result.done) { return result.value; }
256
+
257
+ yield result.value;
258
+
259
+ index += 1;
260
+ }
261
+
262
+ return;
263
+ });
264
+ }
265
+
266
+ public async find(predicate: MaybeAsyncIteratee<T, boolean>): Promise<T | void>
267
+ {
268
+ let index = 0;
269
+
270
+ // eslint-disable-next-line no-constant-condition
271
+ while (true)
272
+ {
273
+ const result = await this._iterator.next();
274
+
275
+ if (result.done) { return; }
276
+ if (await predicate(result.value, index)) { return result.value; }
277
+
278
+ index += 1;
279
+ }
280
+ }
281
+
191
282
  public enumerate(): SmartAsyncIterator<[number, T], R>
192
283
  {
193
284
  return this.map((value, index) => [index, value]);
@@ -248,18 +339,19 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
248
339
  return this._iterator.next(...values);
249
340
  }
250
341
 
251
- public async toArray(): Promise<T[]>
342
+ public groupBy<K extends PropertyKey>(iteratee: MaybeAsyncIteratee<T, K>): AggregatedAsyncIterator<K, T>
252
343
  {
253
- const elements: T[] = [];
254
-
255
- // eslint-disable-next-line no-constant-condition
256
- while (true)
344
+ return new AggregatedAsyncIterator(this.map(async (element, index) =>
257
345
  {
258
- const result = await this._iterator.next();
259
- if (result.done) { return elements; }
346
+ const key = await iteratee(element, index);
260
347
 
261
- elements.push(result.value);
262
- }
348
+ return [key, element] as [K, T];
349
+ }));
350
+ }
351
+
352
+ public toArray(): Promise<T[]>
353
+ {
354
+ return Array.fromAsync(this as AsyncIterable<T>);
263
355
  }
264
356
 
265
357
  public get [Symbol.toStringTag]() { return "SmartAsyncIterator"; }
@@ -1,6 +1,7 @@
1
+ import AggregatedIterator from "../aggregators/aggregated-iterator.js";
1
2
  import { ValueException } from "../exceptions/index.js";
2
3
 
3
- import type { GeneratorFunction, Iteratee, TypeGuardIteratee, Reducer, IterLike } from "./types.js";
4
+ import type { GeneratorFunction, Iteratee, TypeGuardIteratee, Reducer, IteratorLike } from "./types.js";
4
5
 
5
6
  export default class SmartIterator<T, R = void, N = undefined> implements Iterator<T, R, N>
6
7
  {
@@ -9,11 +10,11 @@ export default class SmartIterator<T, R = void, N = undefined> implements Iterat
9
10
  public return?: (value?: R) => IteratorResult<T, R>;
10
11
  public throw?: (error?: unknown) => IteratorResult<T, R>;
11
12
 
12
- public constructor(iterable: Iterable<T>);
13
+ public constructor(iterable: Iterable<T, R, N>);
13
14
  public constructor(iterator: Iterator<T, R, N>);
14
15
  public constructor(generatorFn: GeneratorFunction<T, R, N>);
15
- public constructor(argument: IterLike<T, R, N>);
16
- public constructor(argument: IterLike<T, R, N>)
16
+ public constructor(argument: IteratorLike<T, R, N> | GeneratorFunction<T, R, N>);
17
+ public constructor(argument: IteratorLike<T, R, N> | GeneratorFunction<T, R, N>)
17
18
  {
18
19
  if (argument instanceof Function)
19
20
  {
@@ -28,8 +29,8 @@ export default class SmartIterator<T, R = void, N = undefined> implements Iterat
28
29
  this._iterator = argument;
29
30
  }
30
31
 
31
- if (this._iterator.return) { this.return = (value?: R) => this._iterator.return!(value); }
32
- if (this._iterator.throw) { this.throw = (error?: unknown) => this._iterator.throw!(error); }
32
+ if (this._iterator.return) { this.return = (value) => this._iterator.return!(value); }
33
+ if (this._iterator.throw) { this.throw = (error) => this._iterator.throw!(error); }
33
34
  }
34
35
 
35
36
  public every(predicate: Iteratee<T, boolean>): boolean
@@ -130,6 +131,93 @@ export default class SmartIterator<T, R = void, N = undefined> implements Iterat
130
131
  }
131
132
  }
132
133
 
134
+ public flatMap<V>(iteratee: Iteratee<T, Iterable<V>>): SmartIterator<V, R>
135
+ {
136
+ const iterator = this._iterator;
137
+
138
+ return new SmartIterator<V, R>(function* ()
139
+ {
140
+ let index = 0;
141
+
142
+ while (true)
143
+ {
144
+ const result = iterator.next();
145
+ if (result.done) { return result.value; }
146
+
147
+ const iterable = iteratee(result.value, index);
148
+ for (const value of iterable)
149
+ {
150
+ yield value;
151
+ }
152
+
153
+ index += 1;
154
+ }
155
+ });
156
+ }
157
+
158
+ public drop(count: number): SmartIterator<T, R | void>
159
+ {
160
+ const iterator = this._iterator;
161
+
162
+ return new SmartIterator<T, R | void>(function* ()
163
+ {
164
+ let index = 0;
165
+ while (index < count)
166
+ {
167
+ const result = iterator.next();
168
+ if (result.done) { return; }
169
+
170
+ index += 1;
171
+ }
172
+
173
+ while (true)
174
+ {
175
+ const result = iterator.next();
176
+ if (result.done) { return result.value; }
177
+
178
+ yield result.value;
179
+ }
180
+ });
181
+ }
182
+ public take(limit: number): SmartIterator<T, R | void>
183
+ {
184
+ const iterator = this._iterator;
185
+
186
+ return new SmartIterator<T, R | void>(function* ()
187
+ {
188
+ let index = 0;
189
+ while (index < limit)
190
+ {
191
+ const result = iterator.next();
192
+ if (result.done) { return result.value; }
193
+
194
+ yield result.value;
195
+
196
+ index += 1;
197
+ }
198
+
199
+ return;
200
+ });
201
+ }
202
+
203
+ public find(predicate: Iteratee<T, boolean>): T | void;
204
+ public find<S extends T>(predicate: TypeGuardIteratee<T, S>): S | void;
205
+ public find(predicate: Iteratee<T, boolean>): T | void
206
+ {
207
+ let index = 0;
208
+
209
+ // eslint-disable-next-line no-constant-condition
210
+ while (true)
211
+ {
212
+ const result = this._iterator.next();
213
+
214
+ if (result.done) { return; }
215
+ if (predicate(result.value, index)) { return result.value; }
216
+
217
+ index += 1;
218
+ }
219
+ }
220
+
133
221
  public enumerate(): SmartIterator<[number, T], R>
134
222
  {
135
223
  return this.map((value, index) => [index, value]);
@@ -169,6 +257,7 @@ export default class SmartIterator<T, R = void, N = undefined> implements Iterat
169
257
  index += 1;
170
258
  }
171
259
  }
260
+
172
261
  public forEach(iteratee: Iteratee<T>): void
173
262
  {
174
263
  let index = 0;
@@ -190,6 +279,16 @@ export default class SmartIterator<T, R = void, N = undefined> implements Iterat
190
279
  return this._iterator.next(...values);
191
280
  }
192
281
 
282
+ public groupBy<K extends PropertyKey>(iteratee: Iteratee<T, K>): AggregatedIterator<K, T>
283
+ {
284
+ return new AggregatedIterator(this.map((element, index) =>
285
+ {
286
+ const key = iteratee(element, index);
287
+
288
+ return [key, element] as [K, T];
289
+ }));
290
+ }
291
+
193
292
  public toArray(): T[]
194
293
  {
195
294
  return Array.from(this as Iterable<T>);
@@ -1,20 +1,31 @@
1
+ /* eslint-disable max-len */
2
+
1
3
  import type { MaybePromise } from "../promises/types.js";
2
4
 
5
+ export type MaybeAsyncIterable<T, R = void, N = undefined> = Iterable<T, R, N> | AsyncIterable<T, R, N>;
6
+ export type MaybeAsyncIterator<T, R = void, N = undefined> = Iterator<T, R, N> | AsyncIterator<T, R, N>;
7
+ export type MaybeAsyncGenerator<T, R = void, N = undefined> = Generator<T, R, N> | AsyncGenerator<T, R, N>;
8
+
3
9
  export type GeneratorFunction<T, R = void, N = undefined> = () => Generator<T, R, N>;
4
10
  export type AsyncGeneratorFunction<T, R = void, N = undefined> = () => AsyncGenerator<T, R, N>;
11
+ export type MaybeAsyncGeneratorFunction<T, R = void, N = undefined> = () => MaybeAsyncGenerator<T, R, N>;
5
12
 
6
13
  export type Iteratee<T, R = void> = (value: T, index: number) => R;
14
+ export type AsyncIteratee<T, R = void> = (value: T, index: number) => Promise<R>;
7
15
  export type MaybeAsyncIteratee<T, R = void> = (value: T, index: number) => MaybePromise<R>;
8
16
 
9
17
  export type TypeGuardIteratee<T, R extends T> = (value: T, index: number) => value is R;
10
- export type MaybeAsyncTypeGuardIteratee<T, R extends T> = (value: MaybePromise<T>, index: number) =>
11
- value is MaybePromise<R>;
18
+
19
+ // @ts-expect-error - This is an asyncronous type guard iteratee that guarantees the return value is a promise.
20
+ export type AsyncTypeGuardIteratee<T, R extends T> = (value: T, index: number) => value is Promise<R>;
21
+
22
+ // @ts-expect-error - This may be an asyncronous type guard iteratee that guarantees the return value may be a promise.
23
+ export type MaybeAsyncTypeGuardIteratee<T, R extends T> = (value: T, index: number) => value is MaybePromise<R>;
12
24
 
13
25
  export type Reducer<T, A> = (accumulator: A, value: T, index: number) => A;
26
+ export type AsyncReducer<T, A> = (accumulator: A, value: T, index: number) => Promise<A>;
14
27
  export type MaybeAsyncReducer<T, A> = (accumulator: A, value: T, index: number) => MaybePromise<A>;
15
28
 
16
- export type IterLike<T, R = void, N = undefined> = Iterable<T> | Iterator<T, R, N> | GeneratorFunction<T, R, N>;
17
- export type AsyncIterLike<T, R = void, N = undefined> =
18
- AsyncIterable<T> | AsyncIterator<T, R, N> | AsyncGeneratorFunction<T, R, N>;
19
-
20
- export type MaybeAsyncIterLike<T, R = void, N = undefined> = IterLike<T, R, N> | AsyncIterLike<T, R, N>;
29
+ export type IteratorLike<T, R = void, N = undefined> = Iterable<T> | Iterator<T, R, N>;
30
+ export type AsyncIteratorLike<T, R = void, N = undefined> = AsyncIterable<T> | AsyncIterator<T, R, N>;
31
+ export type MaybeAsyncIteratorLike<T, R = void, N = undefined> = IteratorLike<T, R, N> | AsyncIteratorLike<T, R, N>;
@@ -2,6 +2,11 @@ import type { FulfilledHandler, PromiseExecutor, RejectedHandler } from "./types
2
2
 
3
3
  export default class SmartPromise<T = void> implements Promise<T>
4
4
  {
5
+ public static FromPromise<T>(promise: Promise<T>): SmartPromise<T>
6
+ {
7
+ return new SmartPromise((resolve, reject) => promise.then(resolve, reject));
8
+ }
9
+
5
10
  protected _isPending: boolean;
6
11
  protected _isFulfilled: boolean;
7
12
  protected _isRejected: boolean;
@@ -6,7 +6,7 @@ import Publisher from "../publisher.js";
6
6
 
7
7
  export default class Clock extends GameLoop
8
8
  {
9
- protected _publisher: Publisher<[number], void>;
9
+ protected _publisher: Publisher<[number]>;
10
10
 
11
11
  public constructor(fpsIfNotBrowser: number = TimeUnit.Second)
12
12
  {
@@ -9,7 +9,7 @@ import Publisher from "../publisher.js";
9
9
  export default class Countdown extends GameLoop
10
10
  {
11
11
  protected _deferrer?: DeferredPromise<void>;
12
- protected _publisher: Publisher<[number], void>;
12
+ protected _publisher: Publisher<[number]>;
13
13
 
14
14
  protected _duration: number;
15
15
  public get duration(): number
@@ -1,10 +1,10 @@
1
1
  export type {
2
- KeyIteratee,
3
- MaybeAsyncKeyIteratee,
4
- KeyTypeGuardIteratee,
5
- MaybeAsyncKeyTypeGuardIteratee,
6
- KeyReducer,
7
- MaybeAsyncKeyReducer
2
+ KeyedIteratee,
3
+ MaybeAsyncKeyedIteratee,
4
+ KeyedTypeGuardIteratee,
5
+ MaybeAsyncKeyedTypeGuardIteratee,
6
+ KeyedReducer,
7
+ MaybeAsyncKeyedReducer
8
8
 
9
9
  } from "./aggregators/types.js";
10
10
 
@@ -17,9 +17,9 @@ export type {
17
17
  MaybeAsyncTypeGuardIteratee,
18
18
  Reducer,
19
19
  MaybeAsyncReducer,
20
- IterLike,
21
- AsyncIterLike,
22
- MaybeAsyncIterLike
20
+ IteratorLike,
21
+ AsyncIteratorLike,
22
+ MaybeAsyncIteratorLike
23
23
 
24
24
  } from "./iterators/types.js";
25
25
 
@@ -1,9 +1,9 @@
1
- export async function delay(milliseconds: number): Promise<void>
1
+ export function delay(milliseconds: number): Promise<void>
2
2
  {
3
- return new Promise<void>((resolve, reject) => setTimeout(resolve, milliseconds));
3
+ return new Promise<void>((resolve) => setTimeout(resolve, milliseconds));
4
4
  }
5
5
 
6
- export async function nextAnimationFrame(): Promise<void>
6
+ export function nextAnimationFrame(): Promise<void>
7
7
  {
8
- return new Promise<void>((resolve, reject) => requestAnimationFrame(() => resolve()));
8
+ return new Promise<void>((resolve) => requestAnimationFrame(() => resolve()));
9
9
  }
@@ -1,46 +0,0 @@
1
- import AggregatedIterator from "./aggregated-iterator.js";
2
-
3
- import { SmartIterator } from "../iterators/index.js";
4
- import type { GeneratorFunction, Iteratee, IterLike, 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: IterLike<T>);
14
- public constructor(argument: IterLike<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
- }
@@ -1,56 +0,0 @@
1
- import AggregatedAsyncIterator from "./aggregated-async-iterator.js";
2
-
3
- import { SmartAsyncIterator } from "../iterators/index.js";
4
- import type {
5
- AsyncGeneratorFunction,
6
- GeneratorFunction,
7
- MaybeAsyncIterLike,
8
- MaybeAsyncIteratee,
9
- MaybeAsyncTypeGuardIteratee
10
-
11
- } from "../iterators/types.js";
12
-
13
- export default class AsyncAggregator<T>
14
- {
15
- protected _elements: SmartAsyncIterator<T>;
16
-
17
- public constructor(iterable: Iterable<T>);
18
- public constructor(iterable: AsyncIterable<T>);
19
- public constructor(iterator: Iterator<T>);
20
- public constructor(iterator: AsyncIterator<T>);
21
- public constructor(generatorFn: GeneratorFunction<T>);
22
- public constructor(generatorFn: AsyncGeneratorFunction<T>);
23
- public constructor(argument: MaybeAsyncIterLike<T>);
24
- public constructor(argument: MaybeAsyncIterLike<T>)
25
- {
26
- this._elements = new SmartAsyncIterator(argument);
27
- }
28
-
29
- public filter(predicate: MaybeAsyncIteratee<T, boolean>): AsyncAggregator<T>;
30
- public filter<S extends T>(predicate: MaybeAsyncTypeGuardIteratee<T, S>): AsyncAggregator<S>;
31
- public filter(predicate: MaybeAsyncIteratee<T, boolean>): AsyncAggregator<T>
32
- {
33
- return new AsyncAggregator(this._elements.filter(predicate));
34
- }
35
- public map<V>(iteratee: MaybeAsyncIteratee<T, V>): AsyncAggregator<V>
36
- {
37
- return new AsyncAggregator(this._elements.map(iteratee));
38
- }
39
-
40
- public unique(): AsyncAggregator<T>
41
- {
42
- return new AsyncAggregator(this._elements.unique());
43
- }
44
-
45
- public groupBy<K extends PropertyKey>(iteratee: MaybeAsyncIteratee<T, K>): AggregatedAsyncIterator<K, T>
46
- {
47
- return new AggregatedAsyncIterator(this._elements.map(async (element, index) =>
48
- {
49
- const key = await iteratee(element, index);
50
-
51
- return [key, element] as [K, T];
52
- }));
53
- }
54
-
55
- public get [Symbol.toStringTag]() { return "AsyncAggregator"; }
56
- }