import { IterableX } from '../iterablex.js'; import { identity } from '../../util/identity.js'; import { arrayIndexOf } from '../../util/arrayindexof.js'; import { comparer as defaultComparer } from '../../util/comparer.js'; import { MonoTypeOperatorFunction } from '../../interfaces.js'; import { DistinctOptions } from './distinctoptions.js'; /** @ignore */ export class DistinctIterable extends IterableX { private _source: Iterable; private _keySelector: (value: TSource) => TKey; private _cmp: (x: TKey, y: TKey) => boolean; constructor( source: Iterable, keySelector: (value: TSource) => TKey, cmp: (x: TKey, y: TKey) => boolean ) { super(); this._source = source; this._keySelector = keySelector; this._cmp = cmp; } *[Symbol.iterator]() { const set = [] as TKey[]; for (const item of this._source) { const key = this._keySelector(item); if (arrayIndexOf(set, key, this._cmp) === -1) { set.push(key); yield item; } } } } /** * Returns an iterable sequence that contains only distinct elements according to the keySelector and comparer. * * @template TSource The type of the elements in the source sequence. * @template TKey The type of the discriminator key computed for each element in the source sequence. * @param {DistinctOptions} [options] The optional arguments for a key selector and comparer function. * @returns {MonoTypeOperatorFunction} An operator that returns distinct elements according to the keySelector and options. */ export function distinct( options?: DistinctOptions ): MonoTypeOperatorFunction { return function distinctOperatorFunction(source: Iterable): IterableX { const { ['keySelector']: keySelector = identity, ['comparer']: comparer = defaultComparer } = options || {}; return new DistinctIterable(source, keySelector!, comparer!); }; }