UNPKG

1.41 kBJavaScriptView Raw
1import { AsyncIterableX } from './asynciterablex';
2/**
3 * @ignore
4 */
5export async function defaultCompareAsync(key, minValue) {
6 return key > minValue ? 1 : key < minValue ? -1 : 0;
7}
8/**
9 * @ignore
10 */
11class ExtremaByAsyncIterator extends AsyncIterableX {
12 constructor(source, keyFn, cmp) {
13 super();
14 this._source = source;
15 this._keyFn = keyFn;
16 this._cmp = cmp;
17 }
18 async *[Symbol.asyncIterator]() {
19 let result = [], done, next;
20 const it = this._source[Symbol.asyncIterator]();
21 done = (next = await it.next()).done;
22 if (done) {
23 throw new Error('Sequence contains no elements');
24 }
25 let current = next.value;
26 let resKey = await this._keyFn(current);
27 done = (next = await it.next()).done;
28 while (!done) {
29 let curr = next.value;
30 let key = await this._keyFn(curr);
31 const c = await this._cmp(key, resKey);
32 if (c === 0) {
33 result.push(curr);
34 }
35 else if (c > 0) {
36 result = [curr];
37 resKey = key;
38 }
39 done = (next = await it.next()).done;
40 }
41 yield* result;
42 }
43}
44/**
45 * @ignore
46 */
47export function extremaBy(source, keyFn, cmp) {
48 return new ExtremaByAsyncIterator(source, keyFn, cmp);
49}
50
51//# sourceMappingURL=_extremaby.mjs.map