Skip to content

Instantly share code, notes, and snippets.

@rostegg
Last active May 28, 2021 12:36
Show Gist options
  • Select an option

  • Save rostegg/024f6c643a8581065c18cc6cf0e5aadc to your computer and use it in GitHub Desktop.

Select an option

Save rostegg/024f6c643a8581065c18cc6cf0e5aadc to your computer and use it in GitHub Desktop.
Simple example of how to replace the lack of enums in JS
function* generateIds() {
let i = 0;
while (true) { yield i; i++ }
}
const generator = generateIds();
const getId = () => { return Object.freeze(generator.next().value) }
const typesList = [
`TEST`,
`GET`,
`BLA`
];
const typesObject = typesList.reduce((o, key) => ({ ...o, [key]: getId()}), {});
const Types = Object.freeze(typesObject);
console.log(Types.GET);
Types.GET = 15;
console.log(Types.GET);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment