项目作者: reforms

项目描述 :
TypeScript Enum like java.lang.Enum
高级语言: TypeScript
项目地址: git://github.com/reforms/ts-jenum.git
创建时间: 2018-08-10T06:40:05Z
项目社区:https://github.com/reforms/ts-jenum

开源协议:MIT License

下载


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

  1. import {Enum, EnumType} from "ts-jenum";
  2. @Enum("text")
  3. export class State extends EnumType<State>() {
  4. static readonly NEW = new State(1, "New");
  5. static readonly ACTIVE = new State(2, "Active");
  6. static readonly BLOCKED = new State(3, "Blocked");
  7. private constructor(readonly code: number, readonly text: string) {
  8. super();
  9. }
  10. }
  11. // Usage example
  12. console.log("" + State.ACTIVE); // "Active"
  13. console.log("" + State.BLOCKED); // "Blocked"
  14. console.log(State.values()); // [State.NEW, State.ACTIVE, State.BLOCKED]
  15. console.log(State.valueOf("New")); // State.NEW
  16. State.valueOf("Unknown") // throw Error(...)
  17. console.log(State.valueByName("NEW")); // State.NEW
  18. console.log(State.ACTIVE.enumName); // ACTIVE
  19. const first = state => state.code === 1;
  20. console.log(State.find("New")); // State.NEW
  21. console.log(State.find(first)); // State.NEW
  22. console.log(State.find("Unknown")); // null
  23. const last = state => state.code === 3;
  24. console.log(State.filter(last)) // [State.BLOCKED]
  25. console.log(State.keys()) // ["NEW", "ACTIVE", "BLOCKED"]
  26. // be "NEW" | "ACTIVE" | "BLOCKED"
  27. type StateNameUnion = EnumConstNames<typeof State>;

EnumTools powerful tool to comfortable work with plain json struct like enum

  1. import {EnumTools} from "ts-jenum";
  2. // plain json like enum
  3. const Colors = {
  4. WHITE: "#FFFFFF",
  5. GRAY: "#808080",
  6. BLACK: "#000000"
  7. };
  8. // to be ["WHITE", "GRAY", "BLACK"]
  9. const keys = EnumTools.keys(Colors);
  10. // to be ["#FFFFFF", "#808080", "#000000"]
  11. const values = EnumTools.values(Colors);
  12. /**
  13. * to be {
  14. * "#FFFFFF": "WHITE",
  15. * "#808080": "GRAY",
  16. * "#000000": "BLACK"
  17. * };
  18. */
  19. const rStruct = EnumTools.reverse(Colors);
  20. /**
  21. * to be: [
  22. * {key: "WHITE", value: "#FFFFFF"},
  23. * {key: "GRAY", value: "#808080"},
  24. * {key: "BLACK", value: "#000000"}
  25. * ]
  26. */
  27. const pairs = EnumTools.pairs(Colors);
  28. /**
  29. * To be class like:
  30. * @Enum<ColorEnum>("key")
  31. * class ColorEnum extends EnumType<ColorEnum>() {
  32. * static readonly WHITE = new ColorEnum("WHITE", "#FFFFFF");
  33. * static readonly GRAY = new ColorEnum("GRAY", "#808080");
  34. * static readonly BLACK = new ColorEnum("BLACK", "#000000");
  35. * private constructor(readonly key: string, readonly value: string | number) {
  36. * super();
  37. * }
  38. * }
  39. * ColorEnum has all IDE hint for developer, type checking and type safety
  40. */
  41. const ColorEnum = EnumTools.toClass(Colors);

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 to @Enum decorator

  1. import {Enum, EnumConstNames, EnumType} from "ts-jenum";
  2. @Enum<State>("text")
  3. export class State extends EnumType<State>() {
  4. static readonly NEW = new State(1, "New");
  5. static readonly ACTIVE = new State(2, "Active");
  6. static readonly BLOCKED = new State(3, "Blocked");
  7. private constructor(readonly code: number, readonly text: string) {
  8. super();
  9. }
  10. }
  11. // Get Enum Names
  12. // be "NEW" | "ACTIVE" | "BLOCKED"
  13. type StateNameUnion = EnumConstNames<typeof State>;

Powerful typing.

  1. @Enum<Person>("id")
  2. class Person<IdType extends 1 | 2 = 1 | 2,
  3. NameType extends "Ivan" | "John" = "Ivan" | "John"
  4. > extends EnumType<Person>() {
  5. static readonly IVAN = new Person(1 as const, "Ivan" as const);
  6. static readonly JOHN = new Person(2 as const, "John" as const);
  7. private constructor(readonly id: IdType, readonly name: NameType) {
  8. super();
  9. }
  10. static doSomeWork(): void {
  11. // type to be: "Ivan". Not string!
  12. const name = Person.IVAN.name;
  13. // to be: error
  14. // if (name === "cat")
  15. // ^ This condition will always return 'false' since the types '"Ivan"' and '"cat"' have no overlap.
  16. // type to be: 1. Not number!
  17. const id = Person.IVAN.id;
  18. // to be: error
  19. // if (id === 3)
  20. // ^ This condition will always return 'false' since the types '1' and '3' have no overlap
  21. }
  22. }