@byloth/core 1.5.0-rc.7 → 1.5.0-rc.8

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,17 +1,27 @@
1
1
  import AggregatedAsyncIterator from "./aggregated-async-iterator.js";
2
2
 
3
3
  import { SmartAsyncIterator } from "../iterators/index.js";
4
- import type { AsyncGeneratorFunction, MaybeAsyncIteratee, MaybeAsyncTypeGuardIteratee } from "../iterators/types.js";
4
+ import type {
5
+ AsyncGeneratorFunction,
6
+ GeneratorFunction,
7
+ MaybeAsyncIterables,
8
+ MaybeAsyncIteratee,
9
+ MaybeAsyncTypeGuardIteratee
10
+
11
+ } from "../iterators/types.js";
5
12
 
6
13
  export default class AsyncAggregator<T>
7
14
  {
8
15
  protected _elements: SmartAsyncIterator<T>;
9
16
 
17
+ public constructor(iterable: Iterable<T>);
10
18
  public constructor(iterable: AsyncIterable<T>);
19
+ public constructor(iterator: Iterator<T>);
11
20
  public constructor(iterator: AsyncIterator<T>);
21
+ public constructor(generatorFn: GeneratorFunction<T>);
12
22
  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>)
23
+ public constructor(argument: MaybeAsyncIterables<T>);
24
+ public constructor(argument: MaybeAsyncIterables<T>)
15
25
  {
16
26
  this._elements = new SmartAsyncIterator(argument);
17
27
  }
@@ -1,3 +1,4 @@
1
+ import { TypeException } from "../exceptions/index.js";
1
2
  import { SmartIterator } from "../iterators/index.js";
2
3
  import type { GeneratorFunction } from "../iterators/types.js";
3
4
 
@@ -61,7 +62,7 @@ export default class ReducedIterator<K extends PropertyKey, T>
61
62
  const firstElement = this._elements.next();
62
63
  if (firstElement.done)
63
64
  {
64
- throw new TypeError("Reduce of empty iterator with no initial value");
65
+ throw new TypeException("Reduce of empty iterator with no initial value");
65
66
  }
66
67
 
67
68
  index += 1;
@@ -7,7 +7,7 @@ export type MaybeAsyncKeyIteratee<K extends PropertyKey, T, R = void> = (key: K,
7
7
  export type KeyTypeGuardIteratee<K extends PropertyKey, T, R extends T> =
8
8
  (key: K, value: T, index: number) => value is R;
9
9
  export type MaybeAsyncKeyTypeGuardIteratee<K extends PropertyKey, T, R extends T> =
10
- (key: K, value: MaybePromise<T>, index: number) => value is MaybePromise<R>;
10
+ (key: K, value: MaybePromise<T>, index: number) => value is Awaited<R>;
11
11
 
12
12
  export type KeyReducer<K extends PropertyKey, T, A> = (key: K, accumulator: A, value: T, index: number) => A;
13
13
  export type MaybeAsyncKeyReducer<K extends PropertyKey, T, A> =
@@ -1,4 +1,12 @@
1
- import type { AsyncGeneratorFunction, MaybeAsyncIteratee, MaybeAsyncReducer, TypeGuardIteratee } from "./types.js";
1
+ import type {
2
+ AsyncGeneratorFunction,
3
+ GeneratorFunction,
4
+ MaybeAsyncIteratee,
5
+ MaybeAsyncReducer,
6
+ MaybeAsyncIterables,
7
+ MaybeAsyncTypeGuardIteratee
8
+
9
+ } from "./types.js";
2
10
 
3
11
  export default class SmartAsyncIterator<T, R = void, N = undefined> implements AsyncIterator<T, R, N>
4
12
  {
@@ -7,23 +15,73 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
7
15
  public return?: (value?: R) => Promise<IteratorResult<T, R>>;
8
16
  public throw?: (error?: unknown) => Promise<IteratorResult<T, R>>;
9
17
 
18
+ public constructor(iterable: Iterable<T>);
10
19
  public constructor(iterable: AsyncIterable<T>);
20
+ public constructor(iterator: Iterator<T, R, N>);
11
21
  public constructor(iterator: AsyncIterator<T, R, N>);
12
- public constructor(generatorFn: () => AsyncGenerator<T, R, N>);
13
- public constructor(argument: AsyncIterable<T> | AsyncIterator<T, R, N> | AsyncGeneratorFunction<T, R, N>);
14
- public constructor(argument: AsyncIterable<T> | AsyncIterator<T, R, N> | AsyncGeneratorFunction<T, R, N>)
22
+ public constructor(generatorFn: GeneratorFunction<T, R, N>);
23
+ public constructor(generatorFn: AsyncGeneratorFunction<T, R, N>);
24
+ public constructor(argument: MaybeAsyncIterables<T, R, N>);
25
+ public constructor(argument: MaybeAsyncIterables<T, R, N>)
15
26
  {
16
27
  if (argument instanceof Function)
17
28
  {
18
- this._iterator = argument();
29
+ const generator = argument();
30
+ if (Symbol.asyncIterator in generator)
31
+ {
32
+ this._iterator = generator;
33
+ }
34
+ else
35
+ {
36
+ this._iterator = (async function* ()
37
+ {
38
+ let next: [] | [N] = [];
39
+
40
+ while (true)
41
+ {
42
+ const result = generator.next(...next);
43
+ if (result.done) { return result.value; }
44
+
45
+ next = [yield result.value];
46
+ }
47
+
48
+ })();
49
+ }
19
50
  }
20
51
  else if (Symbol.asyncIterator in argument)
21
52
  {
22
53
  this._iterator = argument[Symbol.asyncIterator]() as AsyncIterator<T, R, N>;
23
54
  }
55
+ else if (Symbol.iterator in argument)
56
+ {
57
+ const iterator = argument[Symbol.iterator]();
58
+ this._iterator = (async function* ()
59
+ {
60
+ while (true)
61
+ {
62
+ const result = iterator.next();
63
+ if (result.done) { return result.value; }
64
+
65
+ yield result.value;
66
+ }
67
+
68
+ })();
69
+ }
24
70
  else
25
71
  {
26
- this._iterator = argument;
72
+ this._iterator = (async function* ()
73
+ {
74
+ let next: [] | [N] = [];
75
+
76
+ while (true)
77
+ {
78
+ const result: IteratorResult<T, R> = await argument.next(...next);
79
+ if (result.done) { return result.value; }
80
+
81
+ next = [yield result.value];
82
+ }
83
+
84
+ })();
27
85
  }
28
86
 
29
87
  if (this._iterator.return) { this.return = (value?: R) => this._iterator.return!(value); }
@@ -62,7 +120,7 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
62
120
  }
63
121
 
64
122
  public filter(predicate: MaybeAsyncIteratee<T, boolean>): SmartAsyncIterator<T, R>;
65
- public filter<S extends T>(predicate: TypeGuardIteratee<T, S>): SmartAsyncIterator<T, S>;
123
+ public filter<S extends T>(predicate: MaybeAsyncTypeGuardIteratee<T, S>): SmartAsyncIterator<S, R>;
66
124
  public filter(predicate: MaybeAsyncIteratee<T, boolean>): SmartAsyncIterator<T, R>
67
125
  {
68
126
  const iterator = this._iterator;
@@ -1,4 +1,4 @@
1
- import type { GeneratorFunction, Iteratee, TypeGuardIteratee, Reducer } from "./types.js";
1
+ import type { GeneratorFunction, Iteratee, TypeGuardIteratee, Reducer, Iterables } from "./types.js";
2
2
 
3
3
  export default class SmartIterator<T, R = void, N = undefined> implements Iterator<T, R, N>
4
4
  {
@@ -10,8 +10,8 @@ export default class SmartIterator<T, R = void, N = undefined> implements Iterat
10
10
  public constructor(iterable: Iterable<T>);
11
11
  public constructor(iterator: Iterator<T, R, N>);
12
12
  public constructor(generatorFn: GeneratorFunction<T, R, N>);
13
- public constructor(argument: Iterable<T> | Iterator<T, R, N> | GeneratorFunction<T, R, N>);
14
- public constructor(argument: Iterable<T> | Iterator<T, R, N> | GeneratorFunction<T, R, N>)
13
+ public constructor(argument: Iterables<T, R, N>);
14
+ public constructor(argument: Iterables<T, R, N>)
15
15
  {
16
16
  if (argument instanceof Function)
17
17
  {
@@ -62,7 +62,7 @@ export default class SmartIterator<T, R = void, N = undefined> implements Iterat
62
62
  }
63
63
 
64
64
  public filter(predicate: Iteratee<T, boolean>): SmartIterator<T, R>;
65
- public filter<S extends T>(predicate: TypeGuardIteratee<T, S>): SmartIterator<T, S>;
65
+ public filter<S extends T>(predicate: TypeGuardIteratee<T, S>): SmartIterator<S, R>;
66
66
  public filter(predicate: Iteratee<T, boolean>): SmartIterator<T, R>
67
67
  {
68
68
  const iterator = this._iterator;
@@ -12,3 +12,9 @@ export type MaybeAsyncTypeGuardIteratee<T, R extends T> = (value: MaybePromise<T
12
12
 
13
13
  export type Reducer<T, A> = (accumulator: A, value: T, index: number) => A;
14
14
  export type MaybeAsyncReducer<T, A> = (accumulator: A, value: T, index: number) => MaybePromise<A>;
15
+
16
+ export type Iterables<T, R = void, N = undefined> = Iterable<T> | Iterator<T, R, N> | GeneratorFunction<T, R, N>;
17
+ export type AsyncIterables<T, R = void, N = undefined> =
18
+ AsyncIterable<T> | AsyncIterator<T, R, N> | AsyncGeneratorFunction<T, R, N>;
19
+
20
+ export type MaybeAsyncIterables<T, R = void, N = undefined> = Iterables<T, R, N> | AsyncIterables<T, R, N>;