UNPKG

@arrows/array

Version:
29 lines (28 loc) 767 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.groupBy_ = void 0; const curry_1 = require("@arrows/composition/curry"); const _groupBy_ = (groupingFn, arr) => { const groups = {}; arr.forEach((item) => { const key = groupingFn(item); if (groups[key] !== undefined) { groups[key].push(item); } else { groups[key] = [item]; } }); return groups; }; /** * Creates an object that groups array items * by field specified by grouping functions. * * @param groupingFn Grouping function * @param arr Initial array of objects * @returns New array */ const groupBy_ = curry_1.default(_groupBy_); exports.groupBy_ = groupBy_; exports.default = groupBy_;