@@ -9,21 +9,42 @@ import { Coalesce } from './tsUtils'
9
9
* High-level abstraction over async generator functions of the form `async function*` {@link AsyncGenerator}
10
10
*/
11
11
export interface AsyncCollection < T > extends AsyncIterable < T > {
12
- /** Flattens the collection 1-level deep */
12
+ /**
13
+ * Flattens the collection 1-level deep.
14
+ */
13
15
flatten ( ) : AsyncCollection < SafeUnboxIterable < T > >
14
- /** Applies a mapping transform to the output generator */
15
- map < U > ( fn : ( obj : T ) => U ) : AsyncCollection < U >
16
- /** Filters out results which will _not_ be passed on to further transformations. */
17
- filter < U extends T > ( predicate : Predicate < T , U > ) : AsyncCollection < U >
18
- /** Uses only the first 'count' number of values returned by the generator. */
19
- take ( count : number ) : AsyncCollection < T >
20
- /** Converts the collection to a Promise, resolving to an array of all values returned by the generator. */
16
+
17
+ /**
18
+ * Applies a mapping transform to the output generator.
19
+ */
20
+ map < U > ( fn : ( obj : T ) => Promise < U > | U ) : AsyncCollection < U >
21
+
22
+ /**
23
+ * Filters out results which will _not_ be passed on to further transformations.
24
+ */
25
+ filter < U extends T > ( predicate : ( item : T ) => item is U ) : AsyncCollection < U >
26
+ filter < U extends T > ( predicate : ( item : T ) => boolean ) : AsyncCollection < U >
27
+
28
+ /**
29
+ * Uses only the first 'count' number of values returned by the generator.
30
+ */
31
+ limit ( count : number ) : AsyncCollection < T >
32
+
33
+ /**
34
+ * Converts the collection to a Promise, resolving to an array of all values returned by the generator.
35
+ */
21
36
promise ( ) : Promise < T [ ] >
22
- /** Converts the collection to a Map, using either a property of the item or a function to select keys. */
37
+
38
+ /**
39
+ * Converts the collection to a Map, using either a property of the item or a function to select keys
40
+ */
23
41
toMap < K extends StringProperty < T > , U extends string = never > (
24
42
selector : KeySelector < T , U > | K
25
43
) : Promise < Map < Coalesce < U , T [ K ] > , T > >
26
- /** Returns an iterator directly from the underlying generator, preserving values returned. */
44
+
45
+ /**
46
+ * Returns an iterator directly from the underlying generator, preserving values returned.
47
+ */
27
48
iterator ( ) : AsyncIterator < T , T | void >
28
49
}
29
50
@@ -55,8 +76,8 @@ export function toCollection<T>(generator: () => AsyncGenerator<T, T | undefined
55
76
flatten : ( ) => toCollection < SafeUnboxIterable < T > > ( ( ) => delegateGenerator ( generator ( ) , flatten ) ) ,
56
77
filter : < U extends T > ( predicate : Predicate < T , U > ) =>
57
78
toCollection < U > ( ( ) => filterGenerator < T , U > ( generator ( ) , predicate ) ) ,
58
- map : < U > ( fn : ( item : T ) => U ) => toCollection < U > ( ( ) => mapGenerator ( generator ( ) , fn ) ) ,
59
- take : ( count : number ) => toCollection ( ( ) => delegateGenerator ( generator ( ) , takeFrom ( count ) ) ) ,
79
+ map : < U > ( fn : ( item : T ) => Promise < U > | U ) => toCollection < U > ( ( ) => mapGenerator ( generator ( ) , fn ) ) ,
80
+ limit : ( count : number ) => toCollection ( ( ) => delegateGenerator ( generator ( ) , takeFrom ( count ) ) ) ,
60
81
promise : ( ) => promise ( iterable ) ,
61
82
toMap : < U extends string = never , K extends StringProperty < T > = never > ( selector : KeySelector < T , U > | K ) =>
62
83
asyncIterableToMap ( iterable , selector ) ,
@@ -66,7 +87,7 @@ export function toCollection<T>(generator: () => AsyncGenerator<T, T | undefined
66
87
67
88
async function * mapGenerator < T , U , R = T > (
68
89
generator : AsyncGenerator < T , R | undefined | void > ,
69
- fn : ( item : T | R ) => U
90
+ fn : ( item : T | R ) => Promise < U > | U
70
91
) : AsyncGenerator < U , U | undefined > {
71
92
while ( true ) {
72
93
const { value, done } = await generator . next ( )
@@ -79,9 +100,11 @@ async function* mapGenerator<T, U, R = T>(
79
100
}
80
101
}
81
102
103
+ type Predicate < T , U extends T > = ( item : T ) => item is U
104
+
82
105
async function * filterGenerator < T , U extends T , R = T > (
83
106
generator : AsyncGenerator < T , R | undefined | void > ,
84
- predicate : Predicate < T | R , U >
107
+ predicate : Predicate < T | R , U > | ( ( item : T | R ) => boolean )
85
108
) : AsyncGenerator < U , U | void > {
86
109
while ( true ) {
87
110
const { value, done } = await generator . next ( )
@@ -159,8 +182,6 @@ async function promise<T>(iterable: AsyncIterable<T>): Promise<T[]> {
159
182
return result
160
183
}
161
184
162
- type Predicate < T , U extends T > = ( ( item : T ) => item is U ) | ( ( item : T ) => boolean )
163
-
164
185
function addToMap < T , U extends string > ( map : Map < string , T > , selector : KeySelector < T , U > | StringProperty < T > , item : T ) {
165
186
const key = typeof selector === 'function' ? selector ( item ) : item [ selector ]
166
187
if ( key ) {
0 commit comments