UNPKG

1.04 kBJavaScriptView Raw
1import { AsyncIterableX } from './asynciterablex';
2import { isAsyncIterable } from '../internal/isiterable';
3export class FlattenAsyncIterable extends AsyncIterableX {
4 constructor(source, depth) {
5 super();
6 this._source = source;
7 this._depth = depth;
8 }
9 async *_flatten(source, depth) {
10 if (depth === 0) {
11 for await (let item of source) {
12 yield item;
13 }
14 return undefined;
15 }
16 for await (let item of source) {
17 if (isAsyncIterable(item)) {
18 for await (let innerItem of this._flatten(item, depth - 1)) {
19 yield innerItem;
20 }
21 }
22 else {
23 yield item;
24 }
25 }
26 }
27 [Symbol.asyncIterator]() {
28 return this._flatten(this._source, this._depth)[Symbol.asyncIterator]();
29 }
30}
31export function flatten(source, depth = Infinity) {
32 return new FlattenAsyncIterable(source, depth);
33}
34
35//# sourceMappingURL=flatten.mjs.map