UNPKG

@reactivex/ix-esnext-esm

Version:

The Interactive Extensions for JavaScript

52 lines (50 loc) 1.41 kB
import { AsyncIterableX } from '../asynciterablex'; /** * @ignore */ export async function defaultCompareAsync(key, minValue) { // eslint-disable-next-line no-nested-ternary return key > minValue ? 1 : key < minValue ? -1 : 0; } /** * @ignore */ class ExtremaByAsyncIterator extends AsyncIterableX { constructor(source, keyFn, cmp) { super(); this._source = source; this._keyFn = keyFn; this._cmp = cmp; } async *[Symbol.asyncIterator]() { let result = []; let next; const it = this._source[Symbol.asyncIterator](); if ((next = await it.next()).done) { throw new Error('Sequence contains no elements'); } const current = next.value; let resKey = await this._keyFn(current); result.push(current); while (!(next = await it.next()).done) { const curr = next.value; const key = await this._keyFn(curr); const c = await this._cmp(key, resKey); if (c === 0) { result.push(curr); } else if (c > 0) { result = [curr]; resKey = key; } } yield* result; } } /** * @ignore */ export function extremaBy(source, keyFn, cmp) { return new ExtremaByAsyncIterator(source, keyFn, cmp); } //# sourceMappingURL=_extremaby.mjs.map