Created
March 7, 2022 13:04
-
-
Save ikasoba/749805261a69d512a93de946cff82c40 to your computer and use it in GitHub Desktop.
add enum to javascript
This file contains hidden or 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
/** | |
* @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