UNPKG

ix

Version:

The Interactive Extensions for JavaScript

39 lines (37 loc) 1.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.max = void 0; const tslib_1 = require("tslib"); const comparer_js_1 = require("../util/comparer.js"); const identity_js_1 = require("../util/identity.js"); const withabort_js_1 = require("./operators/withabort.js"); const aborterror_js_1 = require("../aborterror.js"); /** * Returns the maximum element with the optional selector. * * @template TSource The type of the elements in the source sequence. * @param {AsyncIterable<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. */ function max(source, options) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const { ['comparer']: comparer = comparer_js_1.equalityComparerAsync, ['signal']: signal, ['selector']: selector = identity_js_1.identityAsync, } = options || {}; (0, aborterror_js_1.throwIfAborted)(signal); const it = (0, withabort_js_1.wrapWithAbort)(source, signal)[Symbol.asyncIterator](); let next = yield it.next(); if (next.done) { throw new Error('Sequence contains no elements'); } let maxValue = yield selector(next.value); while (!(next = yield it.next()).done) { const current = yield selector(next.value); if ((yield comparer(current, maxValue)) > 0) { maxValue = current; } } return maxValue; }); } exports.max = max; //# sourceMappingURL=max.js.map