Skip to content

Commit

Permalink
fix: groups of groupBy should be instance of Collection
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoPedroAS51 committed Jan 7, 2021
1 parent 608ae35 commit 0a6c7b2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,14 +501,14 @@ export default class Collection<
*/
public groupBy<K extends KeyVariadic>(
key: keyof Item | K | ((item: Item, index: number) => Key)
): Obj<unknown> {
const collection: Obj<unknown[]> = {}
): Obj<Collection<Item>> {
const collection: Obj<Collection<Item>> = {}

this.items.forEach((item, index) => {
const resolvedKey: Key = (resolveValue(item, key, index) as Key) ?? ''

if (collection[resolvedKey] === undefined) {
collection[resolvedKey] = []
collection[resolvedKey] = this.newInstance<Item>([])
}

collection[resolvedKey].push(item)
Expand Down
4 changes: 3 additions & 1 deletion tests/methods/groupBy.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { collect } from '../../src'
import { collect, Collection } from '../../src'

describe('groupBy()', () => {
const products = [
Expand All @@ -14,6 +14,8 @@ describe('groupBy()', () => {
const grouped = collection.groupBy('manufacturer')

expect(Object.keys(grouped)).toEqual(['IKEA', 'Herman Miller'])
expect(grouped['IKEA']).toBeInstanceOf(Collection)
expect(grouped['Herman Miller']).toBeInstanceOf(Collection)
expect(collection).toEqual(products)
})

Expand Down

0 comments on commit 0a6c7b2

Please sign in to comment.