UNPKG

927 BJavaScriptView Raw
1import { throwIfAborted } from '../aborterror';
2import { wrapWithAbort } from './operators/withabort';
3export async function extremaBy(source, selector, comparer, signal) {
4 throwIfAborted(signal);
5 let result = [];
6 const it = wrapWithAbort(source, signal)[Symbol.asyncIterator]();
7 const { value, done } = await it.next();
8 if (done) {
9 throw new Error('Sequence contains no elements');
10 }
11 let resKey = await selector(value, signal);
12 result.push(value);
13 let next;
14 while (!(next = await it.next()).done) {
15 const current = next.value;
16 const key = await selector(current, signal);
17 const cmp = await comparer(key, resKey, signal);
18 if (cmp === 0) {
19 result.push(current);
20 }
21 else if (cmp > 0) {
22 result = [current];
23 resKey = key;
24 }
25 }
26 return result;
27}
28
29//# sourceMappingURL=_extremaby.mjs.map