Created
January 25, 2017 22:34
-
-
Save sillero/ee79fb74c08807ac418656d2f6170066 to your computer and use it in GitHub Desktop.
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
export default class Enum { | |
constructor(arr) { | |
const enumMap = new Map(enumerize(arr)) | |
for (const [key, value] of enumMap) { | |
this[key] = value | |
this[value] = key | |
} | |
[ | |
'entries', | |
'forEach', | |
'get', | |
'has', | |
'keys', | |
'values', | |
].forEach(method => | |
this[method] = enumMap[method].bind(enumMap) | |
) | |
this.toArray = () => Array.from(enumMap) | |
} | |
} | |
function enumerize (arr) { | |
return arr.map((item, i) => Array.isArray(item) ? item : [item, i]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment