From e4ec2b98a216cf61762aac73be83016b8e581e33 Mon Sep 17 00:00:00 2001 From: Daniel Cassidy Date: Fri, 19 Apr 2019 10:06:19 +0100 Subject: [PATCH] feat(groupBy): accept any ArrayLike as argument --- index.ts | 7 ++++--- tslint.json | 5 ++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/index.ts b/index.ts index fe47d0a6..7246e65a 100644 --- a/index.ts +++ b/index.ts @@ -38,11 +38,12 @@ export function reverseFn(): typeof reverse { return reverse; } -export function groupBy(array: ReadonlyArray, +export function groupBy(array: ArrayLike, keyOf: (element: TElement) => string): Dictionary { const grouped = {} as Dictionary; - 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); @@ -52,7 +53,7 @@ export function groupBy(array: ReadonlyArray, return grouped; } -export function groupByFn(keyOf: (element: T) => string): (array: ReadonlyArray) => Dictionary { +export function groupByFn(keyOf: (element: T) => string): (array: ArrayLike) => Dictionary { return array => groupBy(array, keyOf); } diff --git a/tslint.json b/tslint.json index b55edf6c..fbdb5274 100644 --- a/tslint.json +++ b/tslint.json @@ -1,3 +1,6 @@ { - "extends": "@softwareventures/tslint-rules" + "extends": "@softwareventures/tslint-rules", + "rules": { + "prefer-for-of": false + } } \ No newline at end of file