Skip to content

Instantly share code, notes, and snippets.

@rauschma
Last active December 30, 2019 00:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rauschma/71f93ef6ddcfb4c4fb5ff8b68a8b14d5 to your computer and use it in GitHub Desktop.
Save rauschma/71f93ef6ddcfb4c4fb5ff8b68a8b14d5 to your computer and use it in GitHub Desktop.
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