UNPKG

550 BJavaScriptView Raw
1export async function reduce(source, accumulator, ...seed) {
2 const hasSeed = seed.length === 1;
3 let i = 0, hasValue = false, acc = seed[0];
4 for await (let item of source) {
5 if (hasValue || (hasValue = hasSeed)) {
6 acc = await accumulator(acc, item, i++);
7 }
8 else {
9 acc = item;
10 hasValue = true;
11 i++;
12 }
13 }
14 if (!(hasSeed || hasValue)) {
15 throw new Error('Sequence contains no elements');
16 }
17 return acc;
18}
19
20//# sourceMappingURL=reduce.mjs.map