ts-jenum
- TypeScript Enum like java.lang.Enum
- EnumType
- Enum
- Powerful tool to comfortable work with plain json struct like enum
- EnumTools
Installation
npm i ts-jenum
Example in TypeScript
TypeScript Enum like java.lang.Enum
; // Usage exampleconsole.log"" + State.ACTIVE; // "Active"console.log"" + State.BLOCKED; // "Blocked"console.logState.values; // [State.NEW, State.ACTIVE, State.BLOCKED]console.logState.valueOf"New"; // State.NEWState.valueOf"Unknown" // throw Error(...)console.logState.valueByName"NEW"; // State.NEWconsole.logState.ACTIVE.enumName; // ACTIVE ;console.logState.find"New"; // State.NEWconsole.logState.findfirst; // State.NEWconsole.logState.find"Unknown"; // null;console.logState.filterlast // [State.BLOCKED]console.logState.keys // ["NEW", "ACTIVE", "BLOCKED"] // be "NEW" | "ACTIVE" | "BLOCKED";
EnumTools powerful tool to comfortable work with plain json struct like enum
;// plain json like enum; // to be ["WHITE", "GRAY", "BLACK"]; // to be ["#FFFFFF", "#808080", "#000000"]; /** * to be { * "#FFFFFF": "WHITE", * "#808080": "GRAY", * "#000000": "BLACK" * }; */; /** * to be: [ * {key: "WHITE", value: "#FFFFFF"}, * {key: "GRAY", value: "#808080"}, * {key: "BLACK", value: "#000000"} * ] */; /** * To be class like: * @Enum<ColorEnum>("key") * class ColorEnum extends EnumType<ColorEnum>() { * static readonly WHITE = new ColorEnum("WHITE", "#FFFFFF"); * static readonly GRAY = new ColorEnum("GRAY", "#808080"); * static readonly BLACK = new ColorEnum("BLACK", "#000000"); * private constructor(readonly key: string, readonly value: string | number) { * super(); * } * } * ColorEnum has all IDE hint for developer, type checking and type safety */;
Details. Type safety. In example above, you can write "tExt" or "txt" instead of "text" as @Enum decorator argument and no exception happen. In example below this problem is absent. Add an expression <State> to @Enum decorator
; // Get Enum Names// be "NEW" | "ACTIVE" | "BLOCKED";
Powerful typing.