Guest User

Untitled

a guest
Nov 13th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. /**
  2. * Created by Joao Araujo (jocafi) on November 2018
  3. *
  4. * The class TypeHelper was implemented to overcome some weakness of enum structures in TypeScript.
  5. *
  6. * <h2>How to use it?</h2>
  7. *
  8. * 1) Declare all the keys like the example below:
  9. *
  10. * public static STATUS_ID = "STATUS_ID";
  11. * public static STATUS_NAME = "STATUS_NAME";
  12. * public static STATUS_ERROR = "STATUS_ERROR";
  13. *
  14. * 2) Override the keys like the example below:
  15. *
  16. * public static KEY_VALUES: string[][] = [
  17. * [TypeHelper.STATUS_ID, "ID"],
  18. * [TypeHelper.STATUS_NAME, "Status"],
  19. * [TypeHelper.STATUS_ERROR, "Error"]
  20. * ];
  21. *
  22. */
  23. export abstract class TypeHelper {
  24. // Override the static variable below:
  25. public static KEY_VALUES: string[][];
  26.  
  27. /**
  28. * Get the array of key/values for this type.
  29. *
  30. * <p/> The keys are stored in the first column and the values in the second.
  31. */
  32. public static getKeyValues(): string[][] {
  33. return this.KEY_VALUES;
  34. }
  35.  
  36. /**
  37. * Get the value associated to the given key.
  38. * @param key key
  39. */
  40. public static getValue(key: string): string {
  41. const value = this.KEY_VALUES.find(obj => obj[0] === key);
  42. return value[1];
  43. }
  44.  
  45. }
Add Comment
Please, Sign In to add comment