iteragain
Version:
Javascript Iterable/Iterator/Generator-function utilities.
27 lines • 1.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.minmax = minmax;
const emptyIteratorError_1 = require("./internal/emptyIteratorError");
const toIterator_1 = require("./toIterator");
function minmax(...args) {
var _a;
if (!args.length || typeof args[0] === 'function')
return (it) => minmax(it, args[0]);
const it = (0, toIterator_1.default)(args[0]);
const iteratee = (_a = args[1]) !== null && _a !== void 0 ? _a : ((x) => x);
let next = it.next();
if (next.done)
throw new TypeError(emptyIteratorError_1.MINMAX_EMPTY_ERROR);
let min = { value: next.value, comparison: iteratee(next.value) };
let max = { value: next.value, comparison: iteratee(next.value) };
while (!(next = it.next()).done) {
const comparison = iteratee(next.value);
if (comparison < min.comparison)
min = { value: next.value, comparison };
if (comparison > max.comparison)
max = { value: next.value, comparison };
}
return [min.value, max.value];
}
exports.default = minmax;
//# sourceMappingURL=minmax.js.map