Skip to content

Commit

Permalink
feat(groupBy): accept any ArrayLike as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
djcsdy committed Apr 19, 2019
1 parent 215fbb1 commit e4ec2b9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
7 changes: 4 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ export function reverseFn(): typeof reverse {
return reverse;
}

export function groupBy<TElement>(array: ReadonlyArray<TElement>,
export function groupBy<TElement>(array: ArrayLike<TElement>,
keyOf: (element: TElement) => string): Dictionary<TElement[]> {
const grouped = {} as Dictionary<TElement[]>;

for (const element of array) {
for (let i = 0; i < array.length; ++i) {
const element = array[i];
const key = keyOf(element);
const group = grouped[key] || [];
group.push(element);
Expand All @@ -52,7 +53,7 @@ export function groupBy<TElement>(array: ReadonlyArray<TElement>,
return grouped;
}

export function groupByFn<T>(keyOf: (element: T) => string): (array: ReadonlyArray<T>) => Dictionary<T[]> {
export function groupByFn<T>(keyOf: (element: T) => string): (array: ArrayLike<T>) => Dictionary<T[]> {
return array => groupBy(array, keyOf);
}

Expand Down
5 changes: 4 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "@softwareventures/tslint-rules"
"extends": "@softwareventures/tslint-rules",
"rules": {
"prefer-for-of": false
}
}

0 comments on commit e4ec2b9

Please sign in to comment.