UNPKG

ts-prime

Version:

A utility library for JavaScript and Typescript.

37 lines (36 loc) 1.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var purry_1 = require("./purry"); function groupBy() { return purry_1.purry(_groupBy(false), arguments); } exports.groupBy = groupBy; function isArray(data) { return Array.isArray(data); } var _groupBy = function (indexed) { return function (array, fn) { var ret = {}; array.forEach(function (item, index) { var value = indexed ? fn(item, index, array) : fn(item); var addToGroup = function (key, value) { if (!ret[key]) { ret[key] = []; } ret[key].push(value); }; if (isArray(value)) { value.forEach(function (key) { addToGroup(key, item); }); return; } addToGroup(value, item); }); return ret; }; }; (function (groupBy) { function indexed() { return purry_1.purry(_groupBy(true), arguments); } groupBy.indexed = indexed; })(groupBy = exports.groupBy || (exports.groupBy = {}));