Skip to content

Instantly share code, notes, and snippets.

@lil5
Created October 13, 2020 13:13
Show Gist options
  • Save lil5/a66f02c06c1a6ce43f54bb6ae225a5bd to your computer and use it in GitHub Desktop.
Save lil5/a66f02c06c1a6ce43f54bb6ae225a5bd to your computer and use it in GitHub Desktop.
TypeScript - convert enum to array
/**
* https://dirask.com/posts/TypeScript-convert-enum-to-array-ZDN4nj
*/
class EnumReflection {
private static REGEXP : RegExp = /^[0-9]+$/g;
private static isString<T>(name : string) : boolean {
if(name.match(this.REGEXP))
return false;
return true;
}
public static getNames<T>(object : T) : Array<string> {
let result = new Array<string>();
for(let name in object) {
if(this.isString(name))
result.push(name);
}
return result;
}
public static getValues<T>(object : T) : Array<string | number> {
let result = new Array<string | number>();
for(let name in object) {
if(this.isString(name))
result.push(<any>object[name]);
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment