UNPKG

ix

Version:

The Interactive Extensions for JavaScript

47 lines (45 loc) 1.43 kB
import { IterableX } from '../iterablex.mjs'; import { isIterable } from '../../util/isiterable.mjs'; /** @ignore */ export class FlattenIterable extends IterableX { constructor(source, depth) { super(); this._source = source; this._depth = depth; } // eslint-disable-next-line consistent-return *_flatten(source, depth) { if (depth === 0) { for (const item of source) { yield item; } return undefined; } for (const item of source) { if (isIterable(item)) { for (const innerItem of this._flatten(item, depth - 1)) { yield innerItem; } } else { yield item; } } } [Symbol.iterator]() { return this._flatten(this._source, this._depth)[Symbol.iterator](); } } /** * Flattens the nested iterable by the given depth. * * @template T The type of elements in the source sequence. * @param {number} [depth=Infinity] The depth to flatten the iterable sequence if specified, otherwise infinite. * @returns {MonoTypeOperatorFunction<T>} An operator that flattens the iterable sequence. */ export function flat(depth = Infinity) { return function flattenOperatorFunction(source) { return new FlattenIterable(source, depth); }; } //# sourceMappingURL=flat.mjs.map