Created
October 13, 2020 13:13
-
-
Save lil5/a66f02c06c1a6ce43f54bb6ae225a5bd to your computer and use it in GitHub Desktop.
TypeScript - convert enum to array
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
/** | |
* 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