import { IterableX } from '../iterablex.js'; import { arrayIndexOf } from '../../util/arrayindexof.js'; import { comparer as defaultComparer } from '../../util/comparer.js'; import { MonoTypeOperatorFunction } from '../../interfaces.js'; /** @ignore */ export class ExceptIterable extends IterableX { private _first: Iterable; private _second: Iterable; private _comparer: (x: TSource, y: TSource) => boolean; constructor( first: Iterable, second: Iterable, comparer: (x: TSource, y: TSource) => boolean ) { super(); this._first = first; this._second = second; this._comparer = comparer; } *[Symbol.iterator]() { const map = [] as TSource[]; for (const secondItem of this._second) { map.push(secondItem); } for (const firstItem of this._first) { if (arrayIndexOf(map, firstItem, this._comparer) === -1) { map.push(firstItem); yield firstItem; } } } } /** * Produces the set difference of two iterable sequences by using the specified equality comparer to compare values. * * @template TSource The type of the elements of the input sequences. * @param {Iterable} second An iterable sequence whose elements that also occur in the * operator sequence will cause those elements to be removed from the returned sequence. * @param {((x: TSource, y: TSource) => boolean} [comparer=defaultComparer] An equality comparer to compare values * @returns {MonoTypeOperatorFunction} An operator that returns a sequence that contains the set * difference of the elements of two sequences. */ export function except( second: Iterable, comparer: (x: TSource, y: TSource) => boolean = defaultComparer ): MonoTypeOperatorFunction { return function exceptOperatorFunction(first: Iterable): IterableX { return new ExceptIterable(first, second, comparer); }; }