Skip to content

Instantly share code, notes, and snippets.

@dsherret
Last active March 30, 2018 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsherret/68ae096bda88288a7cf1a5b0cc2eea91 to your computer and use it in GitHub Desktop.
Save dsherret/68ae096bda88288a7cf1a5b0cc2eea91 to your computer and use it in GitHub Desktop.
// only works with number enums...
class EnumEx {
private constructor() {
}
static getMembersCount(e: any) {
return EnumEx.getNames(e).length;
}
static getMemberValue(e: any, memberName: string) {
return e[memberName] as number;
}
static getNamesAndValues(e: any) {
return this.getNames(e).map(n => ({ name: n, value: e[n] as number }));
}
static getNames(e: any) {
return Object.keys(e).filter(k => typeof e[k] === "number") as string[];
}
static getValues<T extends number = number>(e: any) {
return Object.keys(e)
.map(k => e[k])
.filter(v => typeof v === "number") as T[];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment