Created
September 22, 2020 07:19
-
-
Save mynameislau/1e985016edd75e5c863006d96710dcaa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface StringBasedIndexedEntry { | |
id: string; | |
} | |
export interface NumberBasedIndexedEntry { | |
id: number; | |
} | |
type AnyIndexedEntry = StringBasedIndexedEntry | NumberBasedIndexedEntry; | |
export type Indexed<T> = T extends StringBasedIndexedEntry | |
? { | |
[key: string]: T; | |
} // tslint:disable-line: indent | |
: { | |
[key: number]: T; | |
}; // tslint:disable-line: indent | |
export const arrayToIndexed = <T extends AnyIndexedEntry>(a: T[]): Indexed<T> => | |
a.reduce((acc, val) => { | |
return { ...acc, [val.id]: val }; | |
}, {} as Indexed<T>); | |
export const filterIndexed = <T extends AnyIndexedEntry>( | |
predicateFn: (entry: T) => boolean, | |
indexed: Indexed<T> | |
): Indexed<T> => arrayToIndexed(Object.values<T>(indexed).filter(predicateFn)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment