1 | import { toArray } from './toarray';
|
2 | export async function reduceRight(source, accumulator, ...seed) {
|
3 | const array = await toArray(source);
|
4 | const hasSeed = seed.length === 1;
|
5 | let hasValue = false, acc = seed[0];
|
6 | for (let offset = array.length - 1; offset >= 0; offset--) {
|
7 | const item = array[offset];
|
8 | if (hasValue || (hasValue = hasSeed)) {
|
9 | acc = await accumulator(acc, item, offset);
|
10 | }
|
11 | else {
|
12 | acc = item;
|
13 | hasValue = true;
|
14 | }
|
15 | }
|
16 | if (!(hasSeed || hasValue)) {
|
17 | throw new Error('Sequence contains no elements');
|
18 | }
|
19 | return acc;
|
20 | }
|
21 |
|
22 |
|