import { IterableX } from '../iterablex.js'; import { MonoTypeOperatorFunction } from '../../interfaces.js'; /** @ignore */ export class TakeIterable extends IterableX { private _source: Iterable; private _count: number; constructor(source: Iterable, count: number) { super(); this._source = source; this._count = count; } *[Symbol.iterator]() { let i = this._count; if (i > 0) { for (const item of this._source) { yield item; if (--i === 0) { break; } } } } } /** * Returns a specified number of contiguous elements from the start of an iterable sequence. * * @template TSource The type of the elements in the source sequence. * @param {number} count The number of elements to return. * @returns {MonoTypeOperatorFunction} An iterable sequence that contains the specified * number of elements from the start of the input sequence. */ export function take(count: number): MonoTypeOperatorFunction { return function takeOperatorFunction(source: Iterable): IterableX { return new TakeIterable(source, count); }; }