@reactivex/ix-es5-esm
Version:
The Interactive Extensions for JavaScript
29 lines (27 loc) • 1.13 kB
JavaScript
import { equalityComparer } from '../util/comparer.js';
import { identity } from '../util/identity.js';
/**
* Returns the maximum element with the optional selector.
*
* @template TSource The type of the elements in the source sequence.
* @param {Iterable<TSource>} source An async-iterable sequence to determine the maximum element of.
* @param {ExtremaByOptions<TKey>} [options] The options which include an optional comparer and abort signal.
* @returns {Promise<TResult>} The maximum element.
*/
export function max(source, options) {
var _a = options || {}, _b = _a["comparer"], comparer = _b === void 0 ? equalityComparer : _b, _c = _a["selector"], selector = _c === void 0 ? identity : _c;
var it = source[Symbol.iterator]();
var next = it.next();
if (next.done) {
throw new Error('Sequence contains no elements');
}
var maxValue = selector(next.value);
while (!(next = it.next()).done) {
var current = selector(next.value);
if (comparer(current, maxValue) > 0) {
maxValue = current;
}
}
return maxValue;
}
//# sourceMappingURL=max.js.map