UNPKG

1.02 kBJavaScriptView Raw
1import { AsyncIterableX } from './asynciterablex';
2import { toArray } from './toarray';
3export class ScanRightAsyncIterable extends AsyncIterableX {
4 constructor(source, fn, seed) {
5 super();
6 this._source = source;
7 this._fn = fn;
8 this._hasSeed = seed.length === 1;
9 this._seed = seed[0];
10 }
11 async *[Symbol.asyncIterator]() {
12 let hasValue = false, acc = this._seed;
13 const source = await toArray(this._source);
14 for (let offset = source.length - 1; offset >= 0; offset--) {
15 const item = source[offset];
16 if (hasValue || (hasValue = this._hasSeed)) {
17 acc = await this._fn(acc, item, offset);
18 yield acc;
19 }
20 else {
21 acc = item;
22 hasValue = true;
23 }
24 }
25 }
26}
27export function scanRight(source, accumulator, ...seed) {
28 return new ScanRightAsyncIterable(source, accumulator, seed);
29}
30
31//# sourceMappingURL=scanright.mjs.map