Commit 6f43db87 authored by Grand Francois's avatar Grand Francois
Browse files

#36 : ajout de méthodes (EnumEx) de manipulation des enums

Showing with 46 additions and 0 deletions
+46 -0
......@@ -23,3 +23,4 @@ export * from "./util/interval";
export * from "./pab/pab_dimension";
export * from "./pab/pab_puissance";
export * from "./util/iterator";
export * from "./util/enum";
/**
* classe d'itérateurs pour les enums
* cf. https://stackoverflow.com/questions/21293063/how-to-programmatically-enumerate-an-enum-type-in-typescript-0-9-5#21294925
utilisation :
for (const v of EnumEx.getValues(MonTypeEnum)) {
console.log(v);
}
for (const n of EnumEx.getNames(MonTypeEnum)) {
console.log(n);
}
for (const c of EnumEx.getNamesAndValues(MonTypeEnum)) {
console.log(c.name);
console.log(c.value);
}
*/
export class EnumEx {
/**
* retourne les noms et les valeurs d'un enum
*/
static getNamesAndValues<T extends number>(e: any) {
return EnumEx.getNames(e).map(n => ({ name: n, value: e[n] as T }));
}
/**
* retourne les noms d'un enum
*/
static getNames(e: any) {
return EnumEx.getObjValues(e).filter(v => typeof v === "string") as string[];
}
/**
* retourne les valeurs d'un enum
*/
static getValues<T extends number>(e: any) {
return EnumEx.getObjValues(e).filter(v => typeof v === "number") as T[];
}
private static getObjValues(e: any): (number | string)[] {
return Object.keys(e).map(k => e[k]);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment