UNPKG

@codibre/fluent-iterable

Version:

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

30 lines (29 loc) 1.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.chooseToMapRecipe = chooseToMapRecipe; exports.toMapRecipe = toMapRecipe; 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) => { return resolver(getKey(x), (k) => { const old = map.get(k); const newOneP = old === undefined ? mapper(x) : (choose === null || choose === void 0 ? void 0 : choose(old, x)) || old; return resolver(newOneP, (newOne) => { if (old !== newOne) { map.set(k, newOne); } }); }); }), () => map); }; } 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); }; }