ts-enum-next 1.0.2 → 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 +29 -23
- package/dist/index.d.ts +9 -2
- package/dist/index.js +5 -1
- package/package.json +10 -2
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
PENDING = 0,
|
|
18
|
+
APPROVED = 1,
|
|
19
|
+
REJECTED = 2,
|
|
18
20
|
}
|
|
19
21
|
|
|
20
22
|
const StatusDescriptions = {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
75
|
+
- Using enumeration collection
|
|
69
76
|
|
|
70
77
|
```ts
|
|
71
78
|
const errorStatuses = HttpStatus.setOf(
|
|
72
|
-
|
|
73
|
-
|
|
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
|
-
|
|
85
|
+
- Using enumeration map
|
|
79
86
|
|
|
80
87
|
```ts
|
|
81
88
|
const statusMessages = HttpStatus.enumMap({
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
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,
|
|
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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-enum-next",
|
|
3
|
-
"version": "1.0.
|
|
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",
|
|
@@ -21,7 +21,15 @@
|
|
|
21
21
|
"README.md",
|
|
22
22
|
"README.zh-CN.md"
|
|
23
23
|
],
|
|
24
|
-
"
|
|
24
|
+
"homepage": "https://github.com/ricoNext/ts-enum-next",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "https://github.com/ricoNext/ts-enum-next.git"
|
|
28
|
+
},
|
|
29
|
+
"authors": {
|
|
30
|
+
"name": "ricoNext",
|
|
31
|
+
"email": "18028514355@163.com"
|
|
32
|
+
},
|
|
25
33
|
"license": "ISC",
|
|
26
34
|
"devDependencies": {
|
|
27
35
|
"typescript": "^5.8.3"
|