Created
December 8, 2022 11:07
-
-
Save sergiocarracedo/ac219a9b3f700b2e721cc9c2964b36c9 to your computer and use it in GitHub Desktop.
Typescript enum to KV object
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
/* | |
enum Test { | |
A: 1, | |
B: 2 | |
} | |
tsEnumToKeyValue(Test) | |
// Output | |
{ | |
1: A, | |
2: B | |
} | |
*/ | |
function tsEnumToKeyValue (e: Record<string, any>, exclude: string[] = []): Record<string, any> => { | |
return Object.fromEntries( | |
Object.entries(e) | |
.filter((item: [string, unknown]) => (exclude || []).indexOf((item[1] as string).toString()) === -1) | |
.map(([v, k]) => ([k, v])) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment