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 User = (function () {
function User() {
}
return User;
}());
enum Names {
}
var Names;
(function (Names) {
})(Names || (Names = {}));
/*
You can skip values initialization into enum definition.
TypeScript use it as default enum declaration.
*/
enum Names {
Alex = 0,
Denis = 1
}
var Names;
(function (Names) {
Names[Names["Alex"] = 0] = "Alex";
Names[Names["Denis"] = 1] = "Denis";
})(Names || (Names = {}));
/*
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 = {
"0": "Alex",
"1": "Denis",
"Alex": 0,
"Denis": 1
};
class EnumHelpers {
/**
* No instances guard.
*/
private constructor() { }
/**
* Get all keys from enumeration.
*/
public static keys(enumType: object) {
/**
* Get key-value array from enumeration.
*/
public static toKeyValueArray(enumType: object) {
return EnumHelpers.keys(enumType).map(key => ({ key, value: enumType[key] }));
}
/**
* Get values from enumeration.
*/
public static values(enumType: object) {
return EnumHelpers.toKeyValueArray(enumType).map(kv => kv.value);
}