UNPKG

1.09 kBJavaScriptView Raw
1import { equalityComparer } from '../util/comparer';
2import { identity } from '../util/identity';
3/**
4 * Returns the maximum element with the optional selector.
5 *
6 * @export
7 * @template TSource The type of the elements in the source sequence.
8 * @param {Iterable<TSource>} source An async-iterable sequence to determine the maximum element of.
9 * @param {ExtremaByOptions<TKey>} [options] The options which include an optional comparer and abort signal.
10 * @returns {Promise<TResult>} The maximum element.
11 */
12export function max(source, options) {
13 const { ['comparer']: comparer = equalityComparer, ['selector']: selector = identity } = options || {};
14 const it = source[Symbol.iterator]();
15 let next = it.next();
16 if (next.done) {
17 throw new Error('Sequence contains no elements');
18 }
19 let maxValue = selector(next.value);
20 while (!(next = it.next()).done) {
21 const current = selector(next.value);
22 if (comparer(current, maxValue) > 0) {
23 maxValue = current;
24 }
25 }
26 return maxValue;
27}
28
29//# sourceMappingURL=max.mjs.map