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
var isValueProperty = !Number.isNaN(parseInt(enumMember, 10)); |
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
/** | |
* Sample from stack overflow. | |
* @see https://stackoverflow.com/a/18112157 | |
*/ | |
function getValues(enumType: object) { | |
for (var enumMember in enumType) { | |
var isValueProperty = parseInt(enumMember, 10) >= 0 | |
if (isValueProperty) { | |
console.log("enum member: ", enumType[enumMember]); | |
} |
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
enum Names { | |
Alex = 0, | |
Denis = -1 | |
} |
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
/** | |
* Get values from enumeration. | |
*/ | |
public static values(enumType: object) { | |
return EnumHelpers.toKeyValueArray(enumType).map(kv => kv.value); | |
} |
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
/** | |
* Get key-value array from enumeration. | |
*/ | |
public static toKeyValueArray(enumType: object) { | |
return EnumHelpers.keys(enumType).map(key => ({ key, value: enumType[key] })); | |
} |
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
class EnumHelpers { | |
/** | |
* No instances guard. | |
*/ | |
private constructor() { } | |
/** | |
* Get all keys from enumeration. | |
*/ | |
public static keys(enumType: object) { |
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
var Names = { | |
"0": "Alex", | |
"1": "Denis", | |
"Alex": 0, | |
"Denis": 1 | |
}; |
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
/* | |
No step. Only for example. | |
This is function will be created at run-time in memory really. | |
*/ | |
function selfInvokable(enumType) { | |
// step 3: enumType["Alex"] = 0; | |
// step 4: enumType[enumType["Alex"]] = "Alex"; | |
// step 5: enumType["Alex"] = 1; | |
// step 6: enumType[enumType["Denis"]] = "Denis"; | |
}<br /> |
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
var Names; | |
(function (Names) { | |
Names[Names["Alex"] = 0] = "Alex"; | |
Names[Names["Denis"] = 1] = "Denis"; | |
})(Names || (Names = {})); |
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
/* | |
You can skip values initialization into enum definition. | |
TypeScript use it as default enum declaration. | |
*/ | |
enum Names { | |
Alex = 0, | |
Denis = 1 | |
} |