Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | 1x 1x 2x 4x 4x 4x 16x 16x 16x 8x 16x 4x 1x 1x 2x | import { purry } from './purry';
import { PredIndexedOptional, PredIndexed } from './_types';
/**
* Splits a collection into sets, grouped by the result of running each value through `fn`.
* @param items the items to group
* @param fn the grouping function
* @signature
* R.groupBy(array, fn)
* @example
* R.groupBy(['one', 'two', 'three'], x => x.length) // => {3: ['one', 'two'], 5: ['three']}
* @data_first
* @indexed
* @category Array
*/
export function groupBy<T>(
items: T[],
fn: (item: T) => any
): Record<string, T[]>;
export function groupBy<T>(
fn: (item: T) => any
): (array: T[]) => Record<string, T[]>;
/**
* Splits a collection into sets, grouped by the result of running each value through `fn`.
* @param fn the grouping function
* @signature
* R.groupBy(fn)(array)
* @example
* R.pipe(['one', 'two', 'three'], R.groupBy(x => x.length)) // => {3: ['one', 'two'], 5: ['three']}
* @data_last
* @indexed
* @category Array
*/
export function groupBy() {
return purry(_groupBy(false), arguments);
}
const _groupBy = (indexed: boolean) => <T>(
array: T[],
fn: PredIndexedOptional<T, any>
) => {
const ret: Record<string, T[]> = {};
array.forEach((item, index) => {
const value = indexed ? fn(item, index, array) : fn(item);
const key = String(value);
if (!ret[key]) {
ret[key] = [];
}
ret[key].push(item);
});
return ret;
};
export namespace groupBy {
export function indexed<T, K>(
array: T[],
fn: PredIndexed<T, any>
): Record<string, T[]>;
export function indexed<T, K>(
fn: PredIndexed<T, any>
): (array: T[]) => Record<string, T[]>;
export function indexed() {
return purry(_groupBy(true), arguments);
}
}
|