UNPKG

fast-iterable

Version:

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

28 lines (27 loc) 1.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toMapRecipe = exports.chooseToMapRecipe = void 0; const prepare_1 = require("../types-internal/prepare"); function chooseToMapRecipe({ forEach, resolver }) { return function distinct(getKey, mapper, choose) { const map = new Map(); return resolver(forEach.call(this, (x) => { const k = getKey(x); const old = map.get(k); const newOne = old === undefined ? mapper(x) : (choose === null || choose === void 0 ? void 0 : choose(old, x)) || old; if (old !== newOne) { map.set(k, newOne); } }), () => map); }; } exports.chooseToMapRecipe = chooseToMapRecipe; function toMapRecipe(ingredients) { const choose = chooseToMapRecipe(ingredients); return function distinct(getKey, mapper, getReduced) { const preparedGetKey = (0, prepare_1.prepare)(getKey); const preparedMapper = (0, prepare_1.prepare)(mapper); return choose.call(this, preparedGetKey, preparedMapper, getReduced); }; } exports.toMapRecipe = toMapRecipe;