import { IterableX } from '../iterablex.js'; import { OperatorFunction } from '../../interfaces.js'; /** @ignore */ export class TakeWhileIterable extends IterableX { private _source: Iterable; private _predicate: (value: TSource, index: number) => boolean; constructor(source: Iterable, predicate: (value: TSource, index: number) => boolean) { super(); this._source = source; this._predicate = predicate; } *[Symbol.iterator]() { let i = 0; for (const item of this._source) { if (!this._predicate(item, i++)) { break; } yield item; } } } /** * Returns elements from an iterable sequence as long as a specified condition is true. * * @template T The type of the elements in the source sequence. * @template S The result of the predicate that is truthy/falsy. * @param {(value: T, index: number) => value is S} predicate A function to test each element for a condition. * @returns {OperatorFunction} An iterable sequence that contains the elements from the input sequence that occur * before the element at which the test no longer passes. */ export function takeWhile( predicate: (value: T, index: number) => value is S ): OperatorFunction; /** * Returns elements from an iterable sequence as long as a specified condition is true. * * @template T The type of the elements in the source sequence. * @param {((value: T, index: number) => boolean)} predicate A function to test each element for a condition. * @returns {OperatorFunction} An iterable sequence that contains the elements from the input sequence that occur * before the element at which the test no longer passes. */ export function takeWhile( predicate: (value: T, index: number) => boolean ): OperatorFunction; /** * Returns elements from an iterable sequence as long as a specified condition is true. * * @template T The type of the elements in the source sequence. * @param {((value: T, index: number) => boolean)} predicate A function to test each element for a condition. * @returns {OperatorFunction} An iterable sequence that contains the elements from the input sequence that occur * before the element at which the test no longer passes. */ export function takeWhile( predicate: (value: T, index: number) => boolean ): OperatorFunction { return function takeWhileOperatorFunction(source: Iterable): IterableX { return new TakeWhileIterable(source, predicate); }; }