Created
August 29, 2018 05:57
-
-
Save tlumko/19a272d78f881e0b7a46dbf2d480fc1a to your computer and use it in GitHub Desktop.
Create enum
This file contains 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
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