UNPKG

@codibre/fluent-iterable

Version:

Provides LINQ-like fluent api operations for iterables and async iterables (ES2018+).

42 lines (41 loc) 1.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.minRecipe = exports.maxRecipe = void 0; const types_internal_1 = require("../types-internal"); const prepare_1 = require("../types-internal/prepare"); const utils_1 = require("../utils"); function getPredicate(resolver, mapper, wrapper, accept) { return (c) => resolver(mapper(c), (mapped) => { if (wrapper.firstItem || accept(wrapper.lastMapped, mapped)) { wrapper.firstItem = false; wrapper.lastMapped = mapped; wrapper.lastItem = c; return true; } return wrapper.lastMapped === mapped; }); } function minMaxRecipe(accept, lastChecker, firstChecker) { return function ({ filter, resolver, first, last }) { return function (baseMapper = utils_1.identity) { const wrapper = { firstItem: true, }; const mapper = (0, prepare_1.prepare)(baseMapper); const predicate = getPredicate(resolver, mapper, wrapper, accept); let operation; if (lastChecker(mapper, this)) { operation = last.call(filter.call(this, (0, utils_1.assureOrder)(predicate))); } else if (firstChecker(mapper, this)) { operation = first.call(this, predicate); } else { operation = last.call(filter.call(this, predicate)); } return resolver(operation, () => wrapper.lastItem); }; }; } exports.maxRecipe = minMaxRecipe((last, current) => last < current, types_internal_1.isOrderAssured, types_internal_1.isDescendingOrderAssured); exports.minRecipe = minMaxRecipe((last, current) => last > current, types_internal_1.isDescendingOrderAssured, types_internal_1.isOrderAssured);