Skip to content

Commit

Permalink
Added groupBy
Browse files Browse the repository at this point in the history
  • Loading branch information
djalmajr committed Apr 16, 2023
1 parent 8b15e5a commit 660ac2a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/groupBy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { curry } from './curry';
import { Obj } from './types';

interface GroupByFn {
<T = unknown>(fn: (a: T) => unknown): (arr: T[]) => Obj<T[]>;
<T = unknown>(fn: (a: T) => unknown, arr: T[]): Obj<T[]>;
}

/**
* TODO: adicionar documentação
*/
export const groupBy = curry((fn: Function, items: unknown[]) => {
const res = {} as Obj;
for (const item of items) {
const key = fn(item);
res[key] ||= [];
res[key].push(item);
}
return res;
}) as GroupByFn;
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export * from './flow';
export * from './formatCurrency';
export * from './formatPhone';
export * from './get';
export * from './groupBy';
export * from './groupByEveryN';
export * from './includes';
export * from './isArray';
Expand Down

0 comments on commit 660ac2a

Please sign in to comment.