Skip to content

Instantly share code, notes, and snippets.

View DDzia's full-sized avatar
:octocat:

Dziarkach Dzianis DDzia

:octocat:
  • Social Discovery Ventures
  • Belarus, Minsk
View GitHub Profile
var isValueProperty = !Number.isNaN(parseInt(enumMember, 10));
/**
* 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]);
}
enum Names {
Alex = 0,
Denis = -1
}
/**
* Get values from enumeration.
*/
public static values(enumType: object) {
return EnumHelpers.toKeyValueArray(enumType).map(kv => kv.value);
}
/**
* Get key-value array from enumeration.
*/
public static toKeyValueArray(enumType: object) {
return EnumHelpers.keys(enumType).map(key => ({ key, value: enumType[key] }));
}
class EnumHelpers {
/**
* No instances guard.
*/
private constructor() { }
/**
* Get all keys from enumeration.
*/
public static keys(enumType: object) {
var Names = {
"0": "Alex",
"1": "Denis",
"Alex": 0,
"Denis": 1
};
/*
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 />
var Names;
(function (Names) {
Names[Names["Alex"] = 0] = "Alex";
Names[Names["Denis"] = 1] = "Denis";
})(Names || (Names = {}));
/*
You can skip values initialization into enum definition.
TypeScript use it as default enum declaration.
*/
enum Names {
Alex = 0,
Denis = 1
}