UNPKG

ix

Version:

The Interactive Extensions for JavaScript

35 lines (33 loc) 1.49 kB
import { __awaiter } from "tslib"; import { equalityComparerAsync } from '../util/comparer.mjs'; import { identityAsync } from '../util/identity.mjs'; import { wrapWithAbort } from './operators/withabort.mjs'; import { throwIfAborted } from '../aborterror.mjs'; /** * 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. */ export function max(source, options) { return __awaiter(this, void 0, void 0, function* () { const { ['comparer']: comparer = equalityComparerAsync, ['signal']: signal, ['selector']: selector = identityAsync, } = options || {}; throwIfAborted(signal); const it = 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; }); } //# sourceMappingURL=max.mjs.map