-
Notifications
You must be signed in to change notification settings - Fork 0
/
async.js
286 lines (238 loc) · 6.61 KB
/
async.js
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
export function isAsyncIterable(value) {
return value != null && typeof value[Symbol.asyncIterator] === "function";
}
export function map(source, callback) {
return callback
? from(async function* () {
for await (const value of source) yield callback(value);
})
: (anotherSource) => map(anotherSource, source);
}
export function chain(source, callback) {
return callback
? from(async function* () {
for await (const value of source) yield* callback(value);
})
: (anotherSource) => chain(anotherSource, source);
}
export function filter(source, predicate) {
return predicate
? from(async function* () {
for await (const value of source) if (predicate(value)) yield value;
})
: (anotherSource) => filter(anotherSource, source);
}
export function forEach(source, callback) {
return callback
? (async () => {
for await (const value of source) callback(value);
})()
: (anotherSource) => forEach(anotherSource, source);
}
export function of(...values) {
return from(values);
}
export function from(value) {
if (isAsyncIterable(value)) return value;
if (Symbol.iterator in value)
return from(async function* () {
yield* value;
});
if (typeof value === "function") return (value[Symbol.asyncIterator] = value);
return from(async function* () {
for (const key in value) Number.isNaN(+key) || (yield value[key]);
});
}
export function fold(source, accumulator, reduce) {
return arguments.length === 3
? (async () => {
for await (const value of source)
accumulator = await reduce(accumulator, value);
return accumulator;
})()
: (anotherSource) => fold(anotherSource, source, accumulator);
}
export function concat(source, other) {
return other
? from(async function* () {
yield* source;
yield* other;
})
: (anotherSource) => concat(anotherSource, source);
}
export function all(source, predicate) {
return predicate
? (async () => {
for await (const item of source) if (!predicate(item)) return false;
return true;
})()
: (anotherSource) => all(anotherSource, source);
}
export function any(source, predicate) {
return predicate
? (async () => {
for await (const item of source) if (predicate(item)) return true;
return false;
})()
: (anotherSource) => any(anotherSource, source);
}
export function take(source, amount) {
if (arguments.length === 2) {
let _amount = amount;
return takeWhile(
source,
() => _amount-- > 0 || ((_amount = amount), false),
);
} else return (anotherSource) => take(anotherSource, source);
}
export function takeWhile(source, predicate) {
return predicate
? from(async function* () {
for await (const value of source) {
if (predicate(value)) yield value;
else return;
}
})
: (anotherSource) => takeWhile(anotherSource, source);
}
export function skip(source, amount) {
return arguments.length === 2
? from(async function* () {
let _amount = amount;
for await (const value of source)
_amount <= 0 ? yield value : _amount--;
})
: (anotherSource) => skip(anotherSource, source);
}
export function skipWhile(source, predicate) {
return predicate
? from(async function* () {
let skipping = true;
for await (const value of source)
skipping
? !(skipping = predicate(value)) && (yield value)
: yield value;
})
: (anotherSource) => skipWhile(anotherSource, source);
}
export function enumerate(source) {
return from(async function* () {
let index = 0;
for await (const value of source) yield [value, index++];
});
}
export function sort(source, compare) {
return compare
? from(async function* () {
const items = [];
for await (const item of source) items.push(item);
yield* items.sort(compare);
})
: (anotherSource) => sort(anotherSource, source);
}
export function count(source) {
return fold(source, 0, (amount) => amount + 1);
}
export function scan(source, accumulator, reduce) {
return arguments.length === 3
? from(async function* () {
let intermediate = accumulator;
for await (const value of source)
yield (intermediate = await reduce(intermediate, value));
})
: (anotherSource) => scan(anotherSource, source, accumulator);
}
export function first(source) {
return source[Symbol.asyncIterator]()
.next()
.then(({ value }) => value);
}
export function isEmpty(source) {
return source[Symbol.asyncIterator]()
.next()
.then(({ done }) => Boolean(done));
}
export function last(source) {
return fold(source, (_, value) => value);
}
export function zip(source, other) {
return other
? from(async function* () {
const otherIterator = other[Symbol.asyncIterator]();
for await (const item of source) {
const { done, value } = await otherIterator.next();
if (done) return;
yield [item, value];
}
})
: (anotherSource) => zip(anotherSource, source);
}
export function unzip(source) {
return [
from(async function* () {
for await (const [first] of source) yield first;
}),
from(async function* () {
for await (const [, second] of source) yield second;
}),
];
}
export function find(source, predicate) {
return predicate
? (async () => {
for await (const item of source) if (predicate(item)) return item;
})()
: (anotherSource) => find(anotherSource, source);
}
export function group(source, callback) {
return callback
? from(async function* () {
const groups = new Map();
await forEach(source, (value) => {
const key = callback(value);
groups.set(key, concat(groups.get(key) ?? of(), of(value)));
});
for (const pair of groups) yield pair;
})
: (anotherSource) => group(anotherSource, source);
}
export function unique(source, callback = (item) => item) {
return isAsyncIterable(source)
? from(async function* () {
const cache = new Set();
for await (const item of source) {
const key = callback(item);
cache.has(key) || (cache.add(key), yield item);
}
})
: (anotherSource) => unique(anotherSource, source);
}
export default {
of,
is: isAsyncIterable,
all,
zip,
any,
map,
skip,
last,
from,
fold,
find,
take,
sort,
scan,
unzip,
count,
group,
first,
chain,
filter,
concat,
unique,
isEmpty,
forEach,
takeWhile,
skipWhile,
enumerate,
};