-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasynciter.ts
210 lines (187 loc) · 5.48 KB
/
asynciter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { collect } from "./collect.ts";
import { concurrentMap, concurrentUnorderedMap } from "./concurrent-map.ts";
import { filter } from "./filter.ts";
import { first } from "./first.ts";
import { flatten } from "./flatten.ts";
import { forEach } from "./for-each.ts";
import { map } from "./map.ts";
import { reduce } from "./reduce.ts";
/**
* Convert an array into an {@link AsyncIterableIterator}.
* @param items An array of items.
*/
async function* toAsyncIterable<T>(
items: Iterable<T> | AsyncIterable<T> | Array<T>,
): AsyncIterableIterator<T> {
yield* items;
}
export interface IAsyncIter<T> extends AsyncIterable<T> {
map<U>(mapFn: (item: T) => U | Promise<U>): IAsyncIter<U>;
concurrentMap<U>(
mapFn: (item: T) => Promise<U>,
concurrency?: number,
): IAsyncIter<U>;
concurrentUnorderedMap<U>(
mapFn: (item: T) => Promise<U>,
concurrency?: number,
): IAsyncIter<U>;
filter(
filterFn: (item: T) => boolean | Promise<boolean>,
): IAsyncIter<T>;
reduce<U>(
zero: U,
reduceFn: (acc: U, item: T) => U | Promise<U>,
): Promise<U>;
forEach(
forEachFn: (item: T) => void | Promise<void>,
): Promise<void>;
first(): Promise<T | null>;
collect(): Promise<T[]>;
}
/**
* Convert an array or a standard {@link AsyncIterable} to an {@link AsyncIter}.
* @param items A collection of items.
* @returns Items as an {@link AsyncIter}.
*/
export function asynciter<T>(
items: Iterable<T> | AsyncIterable<T> | Array<T>,
): IAsyncIter<T> {
return new AsyncIter(items);
}
export abstract class AbstractAsyncIter<T> implements IAsyncIter<T> {
protected abstract readonly iterator: AsyncIterable<T>;
public async *[Symbol.asyncIterator](): AsyncGenerator<T, void, unknown> {
for await (const item of this.iterator) {
yield item;
}
}
/**
* Map the sequence from one type to another.
* @param mapFn The mapping function.
* @returns An iterable of mapped values.
*/
public map<U>(mapFn: (item: T) => U | Promise<U>): AsyncIter<U> {
const iterable = this.iterator;
return new AsyncIter({
async *[Symbol.asyncIterator]() {
yield* map(iterable, mapFn);
},
});
}
/**
* Flatten the iterable.
* @returns An iterator where a level of indirection has been removed.
*/
public flatten<U>(): AsyncIter<U> {
const iterable = this.iterator;
return new AsyncIter(
flatten(iterable as AsyncIterable<AsyncIterable<U> | Iterable<U>>),
);
}
/**
* Map the sequence from one type to another, concurrently.
*
* Results are returned in order.
*
* @param mapFn The mapping function.
* @param concurrency The maximum concurrency.
* @returns An iterable of mapped values.
*/
public concurrentMap<U>(
mapFn: (item: T) => Promise<U>,
concurrency?: number,
): AsyncIter<U> {
const iterable = this.iterator;
return new AsyncIter({
async *[Symbol.asyncIterator]() {
yield* concurrentMap(iterable, mapFn, concurrency);
},
});
}
/**
* Map the sequence from one type to another, concurrently.
*
* Items are iterated out of order. This allows maximum concurrency
* at all times, but the output order cannot be assumed to be the
* same as the input order.
*
* @param mapFn The mapping function.
* @param concurrency The maximum concurrency.
* @returns An iterable of mapped values.
*/
public concurrentUnorderedMap<U>(
mapFn: (item: T) => Promise<U>,
concurrency?: number,
): AsyncIter<U> {
const iterable = this.iterator;
return new AsyncIter({
async *[Symbol.asyncIterator]() {
yield* concurrentUnorderedMap(iterable, mapFn, concurrency);
},
});
}
/**
* Filter the sequence to contain just the items that pass a test.
* @param filterFn The filter function.
* @returns An iterator returning the values that passed the filter function.
*/
public filter(
filterFn: (item: T) => boolean | Promise<boolean>,
): AsyncIter<T> {
const iterable = this.iterator;
return new AsyncIter({
async *[Symbol.asyncIterator]() {
yield* filter(iterable, filterFn);
},
});
}
/**
* Reduce a sequence to a single value.
* @param reduce The reducing function.
* @returns The result of applying the reducing function to each item and accumulating the result.
*/
public async reduce<U>(
zero: U,
reduceFn: (acc: U, item: T) => U | Promise<U>,
): Promise<U> {
return await reduce(this.iterator, zero, reduceFn);
}
/**
* Perform an operation for each item in the sequence.
* @param forEachFn The forEach function.
*/
public async forEach(
forEachFn: (item: T) => void | Promise<void>,
): Promise<void> {
await forEach(this.iterator, forEachFn);
}
/**
* Return the first item of the sequence.
* @return The first item of the sequence.
*/
public async first(): Promise<T | null> {
return await first(this.iterator);
}
/**
* Collect the items in this iterator to an array.
* @returns The items of this iterator collected to an array.
*/
public async collect(): Promise<T[]> {
return await collect(this.iterator);
}
}
/**
* A decorator for `AsyncIterable`.
*/
export class AsyncIter<T> extends AbstractAsyncIter<T>
implements IAsyncIter<T> {
protected iterator: AsyncIterable<T>;
/**
* Constructor.
* @param iterable The wrapped iterable.
*/
constructor(iterable: Iterable<T> | AsyncIterable<T> | Array<T>) {
super();
this.iterator = toAsyncIterable(iterable);
}
}