Created
February 2, 2023 14:24
-
-
Save smitroshin/64cb52c25d25395560c802a8a3020aec to your computer and use it in GitHub Desktop.
Get from a numeric `enum` just `keys` or just 'values'
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
/** | |
* Get from a numeric `enum` just `keys` or just 'values' | |
* | |
* Source: https://www.sharooq.com/how-to-access-keys-and-values-of-an-enum-in-typescript | |
* | |
* @param target | |
* @param NumericEnum | |
* @returns | |
*/ | |
export const getNumericEnum = (target: "keys" | "values", NumericEnum: Record<any, any>) => | |
(target === "values" && Object.values(NumericEnum).filter((v) => !isNaN(Number(v)))) || | |
(target === "keys" && Object.keys(NumericEnum).filter((v) => isNaN(Number(v)))) || | |
[]; | |
// enum NumericEnum { | |
// One = 1, | |
// Two, | |
// } | |
// getEnum('keys', NumericEnum) // # [ 'One', 'Two' ] | |
// getEnum('keys', NumericEnum) // # [ 1, 2 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment