Created
April 19, 2019 19:41
-
-
Save DDzia/cf0a74e13bb015373f17d9104e96a741 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
/** | |
* Check to numeral enumeration. | |
*/ | |
private static isNumeral(enumType: object) { | |
const members = Object.keys(enumType); | |
if (!members.some((x) => true)) { | |
throw new TypeError("Invalid enumeration type."); | |
} | |
let parsedCount = 0; | |
members.forEach(x => { | |
const parsedValue = parseInt(x, 10); | |
if (!Number.isNaN(parsedValue)) { | |
parsedCount++; | |
} | |
}); | |
return parsedCount === members.length / 2; | |
} | |
/** | |
* Get all keys from enumeration. | |
*/ | |
public static keys(enumType: object) { | |
const members = Object.keys(enumType); | |
if (!EnumHelpers.isNumeral(enumType)) { | |
return members; | |
} | |
const keys: string[] = []; | |
members.forEach(x => { | |
const parsedValue = parseInt(x, 10); | |
if (Number.isNaN(parsedValue)) { | |
keys.push(x); | |
} | |
}); | |
return keys; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment