Created
November 13, 2018 19:59
-
-
Save jocafi/36660138c01e65184599bdeff1f228b4 to your computer and use it in GitHub Desktop.
The class TypeHelper was implemented to overcome some weakness of enum structures in TypeScript.
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
/** | |
* Created by Joao Araujo (jocafi) on November 2018 | |
* | |
* The class TypeHelper was implemented to overcome some weakness of enum structures in TypeScript. | |
* | |
* <h2>How to use it?</h2> | |
* | |
* 1) Declare all the keys like the example below: | |
* | |
* public static STATUS_ID = "STATUS_ID"; | |
* public static STATUS_NAME = "STATUS_NAME"; | |
* public static STATUS_ERROR = "STATUS_ERROR"; | |
* | |
* 2) Override the keys like the example below: | |
* | |
* public static KEY_VALUES: string[][] = [ | |
* [TypeHelper.STATUS_ID, "ID"], | |
* [TypeHelper.STATUS_NAME, "Status"], | |
* [TypeHelper.STATUS_ERROR, "Error"] | |
* ]; | |
* | |
*/ | |
export abstract class TypeHelper { | |
// Override the static variable below: | |
public static KEY_VALUES: string[][]; | |
/** | |
* Get the array of key/values for this type. | |
* | |
* <p/> The keys are stored in the first column and the values in the second. | |
*/ | |
public static getKeyValues(): string[][] { | |
return this.KEY_VALUES; | |
} | |
/** | |
* Get the value associated to the given key. | |
* @param key key | |
*/ | |
public static getValue(key: string): string { | |
const value = this.KEY_VALUES.find(obj => obj[0] === key); | |
return value[1]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment