Skip to content
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

Add types to array2HashByKey #37

Merged
merged 4 commits into from
Jul 7, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions frontend/bidsoup/src/app/utils/sorting.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
// tslint:disable-next-line:no-any
export const array2HashByKey = (arr: any[], key: any) => {
return arr.reduce(
(ordered, el) => ({
...ordered,
[el[key]]: ordered[el[key]]
? [...ordered[el[key]], el]
: [el]
}),
type ObjOfObjs<T> = {
[key in string]: Array<T>
};

type HasString<K extends string> = {
[key in K]: string
};

export const array2HashByKey = <T extends HasString<K>, K extends keyof T & string>(arr: T[], key: K) => {
return arr.reduce<ObjOfObjs<T>>(
(ordered, el) => {
if (el[key] in ordered) {
ordered[el[key]].push(el);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spread didn't work. Maybe with microsoft/TypeScript#13288 but not sure. I tested this and it works well though.

} else {
ordered[el[key]] = [el];
}
return ordered;
},
{}
);
};