Skip to content

Instantly share code, notes, and snippets.

@sillero
Created January 25, 2017 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sillero/ee79fb74c08807ac418656d2f6170066 to your computer and use it in GitHub Desktop.
Save sillero/ee79fb74c08807ac418656d2f6170066 to your computer and use it in GitHub Desktop.
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