Skip to content

Instantly share code, notes, and snippets.

@dnedrow
Created October 2, 2021 01:05
Show Gist options
  • Select an option

  • Save dnedrow/8ccb1c83be2c3fd7d8da0517caa612cd to your computer and use it in GitHub Desktop.

Select an option

Save dnedrow/8ccb1c83be2c3fd7d8da0517caa612cd to your computer and use it in GitHub Desktop.
JavaScript enum implementation
/**
* 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