-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
266 lines (239 loc) · 7.77 KB
/
index.js
File metadata and controls
266 lines (239 loc) · 7.77 KB
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
export async function delayed(fn, ms = 10) {
await wait(ms);
return await fn();
}
export async function wait(ms = 10) {
const timer = accept => {
const time = setTimeout(accept, ms);
if (typeof time.unref === 'function') time.unref();
};
await new Promise(timer);
}
export async function attempt(fn, ...args) {
return await fn(...args);
}
export async function each(items, limit, iterator) {
if (typeof limit === 'function') {
[limit, iterator] = [Number.POSITIVE_INFINITY, limit];
}
const parts = Array.from(items);
const length = Math.min(limit, parts.length);
let i = 0;
const chainer = async (position, item) => {
await attempt(iterator, item, position, items);
return parts.length && chainer(i++, parts.shift());
};
const simultaneous = Array.from({ length }, () => chainer(i++, parts.shift()));
await Promise.all(simultaneous);
return items;
}
export async function collect(iterator) {
const result = [];
for await (const item of iterator) {
result.push(item);
}
return result;
}
export async function map(items, limit, iterator) {
if (typeof limit === 'function') {
[limit, iterator] = [Number.POSITIVE_INFINITY, limit];
}
const parts = Array.from(items);
const length = Math.min(limit, parts.length);
const results = [];
let i = 0;
const chainer = async (position, item) => {
results[position] = await attempt(iterator, item, position, items);
return parts.length && chainer(i++, parts.shift());
};
const simultaneous = Array.from({ length }, () => chainer(i++, parts.shift()));
await Promise.all(simultaneous);
return results;
}
export async function reduce(items, iterator, accumulator) {
const parts = Array.from(items);
let i = 0;
const chainer = async (value, item, position) => {
const previous = await attempt(iterator, value, item, position, items);
return parts.length ? chainer(previous, parts.shift(), i++) : previous;
};
return await chainer(accumulator, parts.shift(), i++);
}
export async function reduceRight(items, iterator, accumulator) {
const parts = Array.from(items);
let i = parts.length;
const chainer = async (value, item, position) => {
const previous = await attempt(iterator, value, item, position, items);
return parts.length ? chainer(previous, parts.pop(), --i) : previous;
};
return await chainer(accumulator, parts.pop(), --i);
}
export async function auto(tasks) {
const keys = Object.keys(tasks);
const promises = {};
const values = {};
const creator = async key => {
if (promises[key]) return promises[key];
const task = tasks[key];
if (Array.isArray(task)) {
promises[key] = task.reduce((promise, entry) => {
if (['string', 'symbol'].includes(typeof entry))
return promise.then(() => creator(entry));
return promise.then(() => entry(values));
}, Promise.resolve());
} else {
promises[key] = attempt(task, values);
}
return promises[key]
.then(value => {
values[key] = value;
return value;
});
};
await Promise.all(keys.map(creator));
return values;
}
export async function wrap(fn, ...args) {
return await new Promise((accept, reject) => {
fn(...args, (e, ...results) => {
if (e) return void reject(e);
if (results.length === 1) return accept(results[0]);
accept(results);
});
});
}
export async function find(items, limit, iterator) {
if (typeof limit === 'function') {
[limit, iterator] = [Number.POSITIVE_INFINITY, limit];
}
const parts = Array.from(items);
const length = Math.min(limit, parts.length);
let i = 0;
let found;
const chainer = async (position, item) => {
if (found) return;
if (await attempt(iterator, item, position, items)) {
if (found) return;
found = item;
return item;
}
if (parts.length) return chainer(i++, parts.shift());
};
const simultaneous = Array.from({ length }, () => chainer(i++, parts.shift()));
await Promise.all(simultaneous);
return found;
}
export async function findIndex(items, limit, iterator) {
return Array.from(items).indexOf(await find(items, limit, iterator));
}
export async function filter(items, limit, iterator) {
if (typeof limit === 'function') {
[limit, iterator] = [Number.POSITIVE_INFINITY, limit];
}
const parts = Array.from(items);
const length = Math.min(limit, parts.length);
let i = 0;
const found = [];
const chainer = async (position, item) => {
if (await attempt(iterator, item, position, items)) {
found.push(item);
}
if (parts.length) return chainer(i++, parts.shift());
};
const simultaneous = Array.from({ length }, () => chainer(i++, parts.shift()));
await Promise.all(simultaneous);
return found;
}
function comparator(a, b) {
return a.criteria - b.criteria;
}
export async function sortBy(items, limit, iterator) {
if (typeof limit === 'function') {
[limit, iterator] = [Number.POSITIVE_INFINITY, limit];
}
const meta = await map(items, limit, async (item, index) => {
return { item, criteria: await attempt(iterator, item, index, items) };
});
return meta.sort(comparator).map(entry => entry.item);
}
export async function some(items, limit, iterator) {
if (typeof limit === 'function') {
[limit, iterator] = [Number.POSITIVE_INFINITY, limit];
}
const parts = Array.from(items);
const length = Math.min(limit, parts.length);
let i = 0;
let passed = false;
const chainer = async (position, item) => {
if (passed) return true;
if (await attempt(iterator, item, position, items)) {
passed = true;
return true;
}
if (parts.length) return chainer(i++, parts.shift());
};
const simultaneous = Array.from({ length }, () => chainer(i++, parts.shift()));
await Promise.all(simultaneous);
return !!passed;
}
export async function anySettled(promises) {
const completed = arg => Promise.resolve(arg);
const list = [];
for (const promise of promises) {
list.push(promise.then(completed, completed));
}
return await Promise.race(list);
}
export async function allSettled(promises) {
const completed = arg => Promise.resolve(arg);
const list = [];
for (const promise of promises) {
list.push(promise.then(completed, completed));
}
return await Promise.all(list);
}
export async function every(items, limit, iterator) {
if (typeof limit === 'function') {
[limit, iterator] = [Number.POSITIVE_INFINITY, limit];
}
const parts = Array.from(items);
const length = Math.min(limit, parts.length);
let i = 0;
let passing = true;
const chainer = async (position, item) => {
if (!passing) return;
const passed = await attempt(iterator, item, position, items);
passing = passing && !!passed;
if (passing && parts.length) return chainer(i++, parts.shift());
};
const simultaneous = Array.from({ length }, () => chainer(i++, parts.shift()));
await Promise.all(simultaneous);
return passing;
}
export async function once(events, target, timeout = -1) {
events = [].concat(events);
return await new Promise(function (accept, reject) {
let timer, complete;
const on = target.addEventListener || target.addListener;
const off = target.removeEventListener || target.removeListener;
events.forEach(event => on.call(target, event, finish));
if (timeout > 0) {
timer = setTimeout(() => finish(new Error('timeout')), timeout);
if (typeof timer.unref === 'function') timer.unref();
}
function finish(...args) {
if (complete) return;
complete = true;
events.map(event => off.call(target, event, finish));
if (timer) clearTimeout(timer);
timer = null;
if (args.length === 1) {
if (args[0] instanceof Error) {
return void reject(args[0]);
}
return void accept(args[0]);
}
accept(args);
}
});
}