Skip to content

Instantly share code, notes, and snippets.

@DDzia
Created April 19, 2019 19:41
Show Gist options
  • Save DDzia/cf0a74e13bb015373f17d9104e96a741 to your computer and use it in GitHub Desktop.
Save DDzia/cf0a74e13bb015373f17d9104e96a741 to your computer and use it in GitHub Desktop.
/**
* 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