Last active
December 30, 2019 00:49
-
-
Save rauschma/71f93ef6ddcfb4c4fb5ff8b68a8b14d5 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
const re_startsWithCapitalLetter = /^[A-Z]/; | |
class Enum { | |
static enumKeys = new Set<string>(); | |
static enumValues = new Set<Enum>(); | |
static setup() { | |
for (const [key, value] of Object.entries(this)) { | |
if (!re_startsWithCapitalLetter.test(key[0])) continue; | |
value.key = key; | |
this.enumKeys.add(key); | |
this.enumValues.add(value); | |
} | |
for (const key of Object.keys(this)) { | |
const value = this | |
} | |
} | |
key!: string; | |
toString() { | |
return `${this.constructor.name}.${this.key}`; | |
} | |
} | |
class YesNo extends Enum { | |
static Yes = new YesNo(); | |
static No = new YesNo(); | |
} | |
YesNo.setup(); | |
console.log(String(YesNo.Yes)); // 'YesNo.Yes' | |
console.log(YesNo.enumKeys.has('Yes')); // true | |
console.log(YesNo.enumKeys.has('Yesss!')); // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment