import { IterableX } from '../iterablex.js'; import { OperatorFunction } from '../../interfaces.js'; /** @ignore */ export class FilterIterable extends IterableX { private _source: Iterable; private _predicate: (value: TSource, index: number) => boolean; private _thisArg?: any; constructor( source: Iterable, predicate: (value: TSource, index: number) => boolean, thisArg?: any ) { super(); this._source = source; this._predicate = predicate; this._thisArg = thisArg; } *[Symbol.iterator]() { let i = 0; for (const item of this._source) { if (this._predicate.call(this._thisArg, item, i++)) { yield item; } } } } export function filter( predicate: (value: T, index: number) => value is S, thisArg?: any ): OperatorFunction; export function filter( predicate: (value: T, index: number) => boolean, thisArg?: any ): OperatorFunction; /** * Filters the elements of an iterable sequence based on a predicate. * * @template TSource The type of the elements in the source sequence. * @param {((value: TSource, index: number) => boolean)} predicate A function to test each source element for a condition. * @param {*} [thisArg] Optional this for binding. * @returns {OperatorFunction} An operator which returns an iterable * sequence that contains elements from the input sequence that satisfy the condition. */ export function filter( predicate: (value: TSource, index: number) => boolean, thisArg?: any ): OperatorFunction { return function filterOperatorFunction(source: Iterable): IterableX { return new FilterIterable(source, predicate, thisArg); }; }