Skip to content

Instantly share code, notes, and snippets.

@tlumko
Created August 29, 2018 05:57
Show Gist options
  • Save tlumko/19a272d78f881e0b7a46dbf2d480fc1a to your computer and use it in GitHub Desktop.
Save tlumko/19a272d78f881e0b7a46dbf2d480fc1a to your computer and use it in GitHub Desktop.
Create enum
function createEnum(arr) {
const obj = arr.reduce((accum, item, index) => {
index = index.toString();
accum[index] = item;
accum[item] = index;
return accum;
}, {});
Object.defineProperties(obj, {
names: {
configurable: false,
enumerable: false,
get: () => () => arr,
set: undefined
},
numericals: {
configurable: false,
enumerable: false,
get: () => () => arr.map((_, i) => i),
set: undefined
}
});
Object.freeze(obj);
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment