UNPKG

ts-prime

Version:

A utility library for JavaScript and Typescript.

34 lines (33 loc) 913 B
import { purry } from './purry'; export function groupBy() { return purry(_groupBy(false), arguments); } 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(_groupBy(true), arguments); } groupBy.indexed = indexed; })(groupBy || (groupBy = {}));