-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/improve group by key types #143
Changes from 13 commits
94c6c1e
4e07d67
f15eefc
7d7e85c
33aa1b4
5a69bd4
893b537
60a5770
6bed375
0b308af
6d07266
77617b5
2ef925c
70438f6
83e04aa
b66c416
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,14 +13,14 @@ import { NonEmptyArray, PredIndexedOptional, PredIndexed } from './_types'; | |
* @indexed | ||
* @category Array | ||
*/ | ||
export function groupBy<T>( | ||
items: readonly T[], | ||
fn: (item: T) => PropertyKey | ||
): Record<PropertyKey, NonEmptyArray<T>>; | ||
export function groupBy<Item, Key extends PropertyKey>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not related to this PR, but I think we can replace this definition one day:
with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done: 83e04aa |
||
items: readonly Item[], | ||
fn: (item: Item) => Key | ||
): Partial<Record<Key, NonEmptyArray<Item>>>; | ||
|
||
export function groupBy<T>( | ||
fn: (item: T) => PropertyKey | ||
): (array: readonly T[]) => Record<PropertyKey, NonEmptyArray<T>>; | ||
export function groupBy<Item, Key extends PropertyKey>( | ||
fn: (item: Item) => Key | ||
): (array: readonly Item[]) => Partial<Record<Key, NonEmptyArray<Item>>>; | ||
|
||
/** | ||
* Splits a collection into sets, grouped by the result of running each value through `fn`. | ||
|
@@ -54,13 +54,13 @@ const _groupBy = (indexed: boolean) => <T>( | |
}; | ||
|
||
export namespace groupBy { | ||
export function indexed<T, K>( | ||
array: readonly T[], | ||
fn: PredIndexed<T, PropertyKey> | ||
): Record<string, NonEmptyArray<T>>; | ||
export function indexed<T, K>( | ||
fn: PredIndexed<T, PropertyKey> | ||
): (array: readonly T[]) => Record<string, NonEmptyArray<T>>; | ||
export function indexed<Item, Key extends PropertyKey>( | ||
array: readonly Item[], | ||
fn: PredIndexed<Item, Key> | ||
): Record<Key, NonEmptyArray<Item>>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not using the indexed version, but shouldn't it have same return type? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thx for seeing that, you are right 🙏 |
||
export function indexed<Item, Key extends PropertyKey>( | ||
fn: PredIndexed<Item, Key> | ||
): (array: readonly Item[]) => Record<Key, NonEmptyArray<Item>>; | ||
export function indexed() { | ||
return purry(_groupBy(true), arguments); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
70438f6