Created
October 2, 2021 01:05
-
-
Save dnedrow/8ccb1c83be2c3fd7d8da0517caa612cd to your computer and use it in GitHub Desktop.
JavaScript enum implementation
This file contains hidden or 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
| /** | |
| * Creates a frozen "enumeration" | |
| * @see https://masteringjs.io/tutorials/fundamentals/enum | |
| * @example createEnum(['Up', 'Down', 'Left', 'Right']) returns { Up: 'Up', Down: 'Down', Left: 'Left', Right: 'Right' } | |
| * @param values Array of items with which to create enum | |
| * @returns {Readonly<{}>} | |
| */ | |
| export function createEnum(values) { | |
| const enumObject = {}; | |
| for (const val of values) { | |
| enumObject[val] = val; | |
| } | |
| return Object.freeze(enumObject); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment