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;
|
6 | let acc = seed[0];
|
7 | for (let offset = array.length - 1; offset >= 0; offset--) {
|
8 | const item = array[offset];
|
9 | if (hasValue || (hasValue = hasSeed)) {
|
10 | acc = await accumulator(acc, item, offset);
|
11 | }
|
12 | else {
|
13 | acc = item;
|
14 | hasValue = true;
|
15 | }
|
16 | }
|
17 | if (!(hasSeed || hasValue)) {
|
18 | throw new Error('Sequence contains no elements');
|
19 | }
|
20 | return acc;
|
21 | }
|
22 |
|
23 |
|