1 | import { IterableX } from './iterablex';
|
2 | import { isIterable } from '../internal/isiterable';
|
3 | export class FlattenIterable extends IterableX {
|
4 | constructor(source, depth) {
|
5 | super();
|
6 | this._source = source;
|
7 | this._depth = depth;
|
8 | }
|
9 | *_flatten(source, depth) {
|
10 | if (depth === 0) {
|
11 | for (let item of source) {
|
12 | yield item;
|
13 | }
|
14 | return undefined;
|
15 | }
|
16 | for (let item of source) {
|
17 | if (isIterable(item)) {
|
18 | for (let innerItem of this._flatten(item, depth - 1)) {
|
19 | yield innerItem;
|
20 | }
|
21 | }
|
22 | else {
|
23 | yield item;
|
24 | }
|
25 | }
|
26 | }
|
27 | [Symbol.iterator]() {
|
28 | return this._flatten(this._source, this._depth)[Symbol.iterator]();
|
29 | }
|
30 | }
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | export function flatten(source, depth = Infinity) {
|
38 | return new FlattenIterable(source, depth);
|
39 | }
|
40 |
|
41 |
|