ix
Version:
The Interactive Extensions for JavaScript
39 lines (37 loc) • 1.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.min = 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 minimum 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 minimum element of.
* @param {ExtremaOptions<TSource, TKey>} [options] The options which include an optional comparer and abort signal.
* @returns {Promise<TSource>} A promise containing the minimum element.
*/
function min(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 minValue = yield selector(next.value);
while (!(next = yield it.next()).done) {
const current = yield selector(next.value);
if ((yield comparer(current, minValue)) < 0) {
minValue = current;
}
}
return minValue;
});
}
exports.min = min;
//# sourceMappingURL=min.js.map