UNPKG

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