@codibre/fluent-iterable
Version:
Provides LINQ-like fluent api operations for iterables and async iterables (ES2018+).
35 lines (34 loc) • 1.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.chainRecipeFactory = chainRecipeFactory;
exports.toObjectChainReduceRecipe = toObjectChainReduceRecipe;
const prepare_1 = require("../types-internal/prepare");
function unwindAndGroup(it, ing, key) {
it = ing.flatten.call(it, (x) => {
const current = key(x);
return typeof current !== 'string' &&
current &&
typeof current[Symbol.iterator] === 'function'
? ing.map.call(current, (item) => [item, x])
: [[current, x]];
});
return ing.group.call(it, (x) => x[0], (_k, v) => [v[1]]);
}
function toNode(it, keys, index, ing, reducer, initial, dictFunc) {
const key = keys[index];
it = unwindAndGroup(it, ing, key);
return dictFunc.call(it, 'key', index < keys.length - 1
? (subIt) => toNode(subIt.values, keys, index + 1, ing, reducer, initial, dictFunc)
: (subIt) => ing.reduce.call(subIt.values, reducer, initial()));
}
function chainRecipeFactory(reduce, ing, toDictFunc) {
return function (initial, reducer, ...keys) {
const prepared = keys.map((x) => (0, prepare_1.prepare)(x));
return keys.length === 0
? reduce.call(this, reducer, initial())
: toNode(this, prepared, 0, ing, reducer, initial, toDictFunc);
};
}
function toObjectChainReduceRecipe(ing) {
return chainRecipeFactory(ing.reduce, ing, ing.toObject);
}