Skip to content

Instantly share code, notes, and snippets.

@ikasoba
Created March 7, 2022 13:04
Show Gist options
  • Save ikasoba/749805261a69d512a93de946cff82c40 to your computer and use it in GitHub Desktop.
Save ikasoba/749805261a69d512a93de946cff82c40 to your computer and use it in GitHub Desktop.
add enum to javascript
/**
* @example
* console.log($enum("A","B","C"),$enum(10)("A","B","C")) // {A:0,B:1,C:2} {A:10,B:11,C:12}
*/
export function $enum(...start){
const mkobj=(...keys)=>Object.fromEntries(keys.map((x,i)=>[x,typeof start[0] === "number" ? start[0]+i : i]))
return start.find(x=>typeof x !== "number") ? mkobj(...start) : mkobj
}
export default $enum;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment