@byloth/core 1.5.0-rc.7 → 1.5.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/core.js +265 -188
- package/dist/core.js.map +1 -1
- package/dist/core.umd.cjs +2 -2
- package/dist/core.umd.cjs.map +1 -1
- package/package.json +13 -14
- package/src/index.ts +24 -9
- package/src/models/aggregators/aggregated-async-iterator.ts +14 -10
- package/src/models/aggregators/aggregator.ts +3 -3
- package/src/models/aggregators/async-aggregator.ts +13 -3
- package/src/models/aggregators/reduced-iterator.ts +2 -1
- package/src/models/aggregators/types.ts +1 -1
- package/src/models/iterators/smart-async-iterator.ts +65 -7
- package/src/models/iterators/smart-iterator.ts +4 -4
- package/src/models/iterators/types.ts +6 -0
- package/src/models/types.ts +14 -2
- package/src/utils/index.ts +1 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { SmartAsyncIterator } from "../iterators/index.js";
|
|
2
|
-
import type { AsyncGeneratorFunction } from "../iterators/types.js";
|
|
2
|
+
import type { AsyncGeneratorFunction, GeneratorFunction, MaybeAsyncIterLike } from "../iterators/types.js";
|
|
3
|
+
import type { MaybePromise } from "../types.js";
|
|
3
4
|
|
|
4
5
|
import ReducedIterator from "./reduced-iterator.js";
|
|
5
6
|
import type { MaybeAsyncKeyIteratee, MaybeAsyncKeyTypeGuardIteratee, MaybeAsyncKeyReducer } from "./types.js";
|
|
@@ -8,11 +9,14 @@ export default class AggregatedAsyncIterator<K extends PropertyKey, T>
|
|
|
8
9
|
{
|
|
9
10
|
protected _elements: SmartAsyncIterator<[K, T]>;
|
|
10
11
|
|
|
12
|
+
public constructor(iterable: Iterable<[K, T]>);
|
|
11
13
|
public constructor(iterable: AsyncIterable<[K, T]>);
|
|
14
|
+
public constructor(iterator: Iterator<[K, T]>);
|
|
12
15
|
public constructor(iterator: AsyncIterator<[K, T]>);
|
|
16
|
+
public constructor(generatorFn: GeneratorFunction<[K, T]>);
|
|
13
17
|
public constructor(generatorFn: AsyncGeneratorFunction<[K, T]>);
|
|
14
|
-
public constructor(argument:
|
|
15
|
-
public constructor(argument:
|
|
18
|
+
public constructor(argument: MaybeAsyncIterLike<[K, T]>);
|
|
19
|
+
public constructor(argument: MaybeAsyncIterLike<[K, T]>)
|
|
16
20
|
{
|
|
17
21
|
this._elements = new SmartAsyncIterator(argument);
|
|
18
22
|
}
|
|
@@ -66,7 +70,7 @@ export default class AggregatedAsyncIterator<K extends PropertyKey, T>
|
|
|
66
70
|
{
|
|
67
71
|
const elements = this._elements;
|
|
68
72
|
|
|
69
|
-
return new AggregatedAsyncIterator(async function* ()
|
|
73
|
+
return new AggregatedAsyncIterator(async function* (): AsyncGenerator<[K, T]>
|
|
70
74
|
{
|
|
71
75
|
const indexes = new Map<K, number>();
|
|
72
76
|
|
|
@@ -76,7 +80,7 @@ export default class AggregatedAsyncIterator<K extends PropertyKey, T>
|
|
|
76
80
|
|
|
77
81
|
indexes.set(key, index + 1);
|
|
78
82
|
|
|
79
|
-
if (predicate(key, element, index)) { yield [key, element]; }
|
|
83
|
+
if (await predicate(key, element, index)) { yield [key, element]; }
|
|
80
84
|
}
|
|
81
85
|
});
|
|
82
86
|
}
|
|
@@ -84,7 +88,7 @@ export default class AggregatedAsyncIterator<K extends PropertyKey, T>
|
|
|
84
88
|
{
|
|
85
89
|
const elements = this._elements;
|
|
86
90
|
|
|
87
|
-
return new AggregatedAsyncIterator(async function* ()
|
|
91
|
+
return new AggregatedAsyncIterator(async function* (): AsyncGenerator<[K, V]>
|
|
88
92
|
{
|
|
89
93
|
const indexes = new Map<K, number>();
|
|
90
94
|
|
|
@@ -99,9 +103,9 @@ export default class AggregatedAsyncIterator<K extends PropertyKey, T>
|
|
|
99
103
|
});
|
|
100
104
|
}
|
|
101
105
|
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)
|
|
106
|
+
public async reduce<A>(reducer: MaybeAsyncKeyReducer<K, T, A>, initialValue: (key: K) => MaybePromise<A>)
|
|
103
107
|
: Promise<ReducedIterator<K, A>>;
|
|
104
|
-
public async reduce<A>(reducer: MaybeAsyncKeyReducer<K, T, A>, initialValue?: (key: K) => A)
|
|
108
|
+
public async reduce<A>(reducer: MaybeAsyncKeyReducer<K, T, A>, initialValue?: (key: K) => MaybePromise<A>)
|
|
105
109
|
: Promise<ReducedIterator<K, A>>
|
|
106
110
|
{
|
|
107
111
|
const accumulators = new Map<K, [number, A]>();
|
|
@@ -120,7 +124,7 @@ export default class AggregatedAsyncIterator<K extends PropertyKey, T>
|
|
|
120
124
|
else if (initialValue !== undefined)
|
|
121
125
|
{
|
|
122
126
|
index = 0;
|
|
123
|
-
accumulator = initialValue(key);
|
|
127
|
+
accumulator = await initialValue(key);
|
|
124
128
|
}
|
|
125
129
|
else
|
|
126
130
|
{
|
|
@@ -147,7 +151,7 @@ export default class AggregatedAsyncIterator<K extends PropertyKey, T>
|
|
|
147
151
|
{
|
|
148
152
|
const elements = this._elements;
|
|
149
153
|
|
|
150
|
-
return new AggregatedAsyncIterator(async function* ()
|
|
154
|
+
return new AggregatedAsyncIterator(async function* (): AsyncGenerator<[K, T]>
|
|
151
155
|
{
|
|
152
156
|
const keys = new Map<K, Set<T>>();
|
|
153
157
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import AggregatedIterator from "./aggregated-iterator.js";
|
|
2
2
|
|
|
3
3
|
import { SmartIterator } from "../iterators/index.js";
|
|
4
|
-
import type { GeneratorFunction, Iteratee, TypeGuardIteratee } from "../iterators/types.js";
|
|
4
|
+
import type { GeneratorFunction, Iteratee, IterLike, TypeGuardIteratee } from "../iterators/types.js";
|
|
5
5
|
|
|
6
6
|
export default class Aggregator<T>
|
|
7
7
|
{
|
|
@@ -10,8 +10,8 @@ export default class Aggregator<T>
|
|
|
10
10
|
public constructor(iterable: Iterable<T>);
|
|
11
11
|
public constructor(iterator: Iterator<T>);
|
|
12
12
|
public constructor(generatorFn: GeneratorFunction<T>);
|
|
13
|
-
public constructor(argument:
|
|
14
|
-
public constructor(argument:
|
|
13
|
+
public constructor(argument: IterLike<T>);
|
|
14
|
+
public constructor(argument: IterLike<T>)
|
|
15
15
|
{
|
|
16
16
|
this._elements = new SmartIterator(argument);
|
|
17
17
|
}
|
|
@@ -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 {
|
|
4
|
+
import type {
|
|
5
|
+
AsyncGeneratorFunction,
|
|
6
|
+
GeneratorFunction,
|
|
7
|
+
MaybeAsyncIterLike,
|
|
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:
|
|
14
|
-
public constructor(argument:
|
|
23
|
+
public constructor(argument: MaybeAsyncIterLike<T>);
|
|
24
|
+
public constructor(argument: MaybeAsyncIterLike<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
|
|
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
|
|
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 {
|
|
1
|
+
import type {
|
|
2
|
+
AsyncGeneratorFunction,
|
|
3
|
+
GeneratorFunction,
|
|
4
|
+
MaybeAsyncIteratee,
|
|
5
|
+
MaybeAsyncReducer,
|
|
6
|
+
MaybeAsyncIterLike,
|
|
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:
|
|
13
|
-
public constructor(
|
|
14
|
-
public constructor(argument:
|
|
22
|
+
public constructor(generatorFn: GeneratorFunction<T, R, N>);
|
|
23
|
+
public constructor(generatorFn: AsyncGeneratorFunction<T, R, N>);
|
|
24
|
+
public constructor(argument: MaybeAsyncIterLike<T, R, N>);
|
|
25
|
+
public constructor(argument: MaybeAsyncIterLike<T, R, N>)
|
|
15
26
|
{
|
|
16
27
|
if (argument instanceof Function)
|
|
17
28
|
{
|
|
18
|
-
|
|
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 =
|
|
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:
|
|
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, IterLike } 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:
|
|
14
|
-
public constructor(argument:
|
|
13
|
+
public constructor(argument: IterLike<T, R, N>);
|
|
14
|
+
public constructor(argument: IterLike<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<
|
|
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 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>;
|
package/src/models/types.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
|
-
export type {
|
|
1
|
+
export type {
|
|
2
|
+
KeyIteratee,
|
|
3
|
+
MaybeAsyncKeyIteratee,
|
|
4
|
+
KeyTypeGuardIteratee,
|
|
5
|
+
MaybeAsyncKeyTypeGuardIteratee,
|
|
6
|
+
KeyReducer,
|
|
7
|
+
MaybeAsyncKeyReducer
|
|
8
|
+
|
|
9
|
+
} from "./aggregators/types.js";
|
|
10
|
+
|
|
2
11
|
export type {
|
|
3
12
|
GeneratorFunction,
|
|
4
13
|
AsyncGeneratorFunction,
|
|
@@ -7,7 +16,10 @@ export type {
|
|
|
7
16
|
TypeGuardIteratee,
|
|
8
17
|
MaybeAsyncTypeGuardIteratee,
|
|
9
18
|
Reducer,
|
|
10
|
-
MaybeAsyncReducer
|
|
19
|
+
MaybeAsyncReducer,
|
|
20
|
+
IterLike,
|
|
21
|
+
AsyncIterLike,
|
|
22
|
+
MaybeAsyncIterLike
|
|
11
23
|
|
|
12
24
|
} from "./iterators/types.js";
|
|
13
25
|
|
package/src/utils/index.ts
CHANGED
|
@@ -3,7 +3,7 @@ import Random from "./random.js";
|
|
|
3
3
|
export { delay, nextAnimationFrame } from "./async.js";
|
|
4
4
|
export { dateDifference, dateRange, dateRound, DateUnit } from "./date.js";
|
|
5
5
|
export { loadScript } from "./dom.js";
|
|
6
|
-
export { count, range, shuffle, unique, zip } from "./iterator.js";
|
|
6
|
+
export { chain, count, enumerate, range, shuffle, unique, zip } from "./iterator.js";
|
|
7
7
|
export { average, hash, sum } from "./math.js";
|
|
8
8
|
export { capitalize } from "./string.js";
|
|
9
9
|
|