ts-enum-next 1.0.3 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,25 +2,27 @@
2
2
 
3
3
  <b>English | <a href="./README.zh-CN.md">中文</a></b>
4
4
 
5
+ A TypeScript library that helps with the enum pattern.
6
+
5
7
  - The ultimate solution for next-generation enumeration in TypeScript
6
8
 
7
9
  - Define and use enum in typescript like using enum in java.
8
10
 
9
- ## Why?
11
+ ## Why?
10
12
 
11
13
  It is very convenient to use enum to define numeric enums and string enums in typescript, but when you are defining some enums of numeric dictionary types, you are a little overwhelmed. For example, when we define a set of states, we need to define the name of the state at the same time. We can combine enumerations and mapping objects in typescript to implement it.
12
14
 
13
15
  ```ts
14
16
  enum Status {
15
- PENDING = 0,
16
- APPROVED = 1,
17
- REJECTED = 2
17
+ PENDING = 0,
18
+ APPROVED = 1,
19
+ REJECTED = 2,
18
20
  }
19
21
 
20
22
  const StatusDescriptions = {
21
- [Status.PENDING]: "waiting",
22
- [Status.APPROVED]: "Approved",
23
- [Status.REJECTED]: "reject"
23
+ [Status.PENDING]: "waiting",
24
+ [Status.APPROVED]: "Approved",
25
+ [Status.REJECTED]: "reject",
24
26
  };
25
27
 
26
28
  console.log(Status.PENDING); // 0
@@ -40,17 +42,22 @@ pnpm add ts-enum-next
40
42
  ```
41
43
 
42
44
  ### Define data dictionary
45
+
43
46
  ```ts
44
47
  class HttpStatus extends Enum<number> {
45
- static readonly OK = new HttpStatus(200, 'OK', 'Request succeeded');
46
- static readonly BAD_REQUEST = new HttpStatus(400, 'BAD_REQUEST', 'Error request');
47
- static readonly NOT_FOUND = new HttpStatus(404, 'NOT_FOUND');
48
+ static readonly OK = new HttpStatus(200, "OK", "Request succeeded");
49
+ static readonly BAD_REQUEST = new HttpStatus(
50
+ 400,
51
+ "BAD_REQUEST",
52
+ "Error request"
53
+ );
54
+ static readonly NOT_FOUND = new HttpStatus(404, "NOT_FOUND");
48
55
  }
49
56
  ```
50
57
 
51
58
  ### Using a data dictionary
52
59
 
53
- * Get dictionary items
60
+ - Get dictionary items
54
61
 
55
62
  ```ts
56
63
  console.log(HttpStatus.OK.description); // "Request succeeded"
@@ -58,32 +65,31 @@ console.log(HttpStatus.fromValue(404).name); // "NOT_FOUND"
58
65
  console.log(HttpStatus.fromName("BAD_REQUEST").value); // 400
59
66
  ```
60
67
 
61
- * Get all enum values
68
+ - Get all enum values
62
69
 
63
70
  ```ts
64
71
  const allStatuses = HttpStatus.values();
65
- console.log(allStatuses.map(s => s.name)); // ["OK", "BAD_REQUEST", "NOT_FOUND"]
72
+ console.log(allStatuses.map((s) => s.name)); // ["OK", "BAD_REQUEST", "NOT_FOUND"]
66
73
  ```
67
74
 
68
- * Using enumeration collection
75
+ - Using enumeration collection
69
76
 
70
77
  ```ts
71
78
  const errorStatuses = HttpStatus.setOf(
72
- HttpStatus.BAD_REQUEST,
73
- HttpStatus.NOT_FOUND
79
+ HttpStatus.BAD_REQUEST,
80
+ HttpStatus.NOT_FOUND
74
81
  );
75
82
  console.log(errorStatuses.has(HttpStatus.fromValue(400))); // true
76
83
  ```
77
84
 
78
- * Using enumeration map
85
+ - Using enumeration map
79
86
 
80
87
  ```ts
81
88
  const statusMessages = HttpStatus.enumMap({
82
- [HttpStatus.OK.value]: "Operation is successful",
83
- [HttpStatus.BAD_REQUEST.value]: "Request error",
84
- "NOT_FOUND": "The resource does not exist"
89
+ [HttpStatus.OK.value]: "Operation is successful",
90
+ [HttpStatus.BAD_REQUEST.value]: "Request error",
91
+ NOT_FOUND: "The resource does not exist",
85
92
  });
86
93
  console.log(statusMessages.get(HttpStatus.NOT_FOUND)); // "The resource does not exist"
87
- ``
88
-
89
-
94
+ ``;
95
+ ```
package/dist/index.d.ts CHANGED
@@ -2,11 +2,18 @@ type EnumValueType = number | string;
2
2
  declare abstract class Enum<T extends string | number = string | number> {
3
3
  readonly value: T;
4
4
  readonly name: string;
5
- readonly description?: string | undefined;
5
+ /**
6
+ * @description 附加说明
7
+ */
8
+ readonly description?: unknown | undefined;
6
9
  private static _values;
7
10
  private static _valueMap;
8
11
  private static _nameMap;
9
- constructor(value: T, name: string, description?: string | undefined);
12
+ constructor(value: T, name: string,
13
+ /**
14
+ * @description 附加说明
15
+ */
16
+ description?: unknown | undefined);
10
17
  static values<T extends Enum>(): T[];
11
18
  static fromValue<T extends Enum>(this: any, value: T['value']): T;
12
19
  static fromName<T extends Enum>(this: any, name: string): T;
package/dist/index.js CHANGED
@@ -1,5 +1,9 @@
1
1
  class Enum {
2
- constructor(value, name, description) {
2
+ constructor(value, name,
3
+ /**
4
+ * @description 附加说明
5
+ */
6
+ description) {
3
7
  this.value = value;
4
8
  this.name = name;
5
9
  this.description = description;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-enum-next",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "Ultimate Enum Enhancement for TypeScript",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",