ts-enum-next 1.0.5 → 1.0.6
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 +27 -1
- package/README.zh-CN.md +73 -39
- package/dist/index.d.ts +16 -0
- package/dist/index.js +10 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# ts-enum-next
|
|
2
2
|
|
|
3
|
+
[](https://codecov.io/github/ricoNext/ts-enum-next)
|
|
4
|
+
|
|
3
5
|
<b>English | <a href="./README.zh-CN.md">中文</a></b>
|
|
4
6
|
|
|
5
7
|
A TypeScript library that helps with the enum pattern.
|
|
@@ -91,5 +93,29 @@ const statusMessages = HttpStatus.enumMap({
|
|
|
91
93
|
NOT_FOUND: "The resource does not exist",
|
|
92
94
|
});
|
|
93
95
|
console.log(statusMessages.get(HttpStatus.NOT_FOUND)); // "The resource does not exist"
|
|
94
|
-
``;
|
|
95
96
|
```
|
|
97
|
+
|
|
98
|
+
- Generate options for Ant Design `Select`
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
import { Enum, enumOptions } from "ts-enum-next";
|
|
102
|
+
|
|
103
|
+
class HttpStatus extends Enum<number> {
|
|
104
|
+
static readonly OK = new HttpStatus(200, "OK", "Request succeeded");
|
|
105
|
+
static readonly BAD_REQUEST = new HttpStatus(
|
|
106
|
+
400,
|
|
107
|
+
"BAD_REQUEST",
|
|
108
|
+
"Error request"
|
|
109
|
+
);
|
|
110
|
+
static readonly NOT_FOUND = new HttpStatus(404, "NOT_FOUND");
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const options = enumOptions(HttpStatus);
|
|
114
|
+
// options:
|
|
115
|
+
// [
|
|
116
|
+
// { label: "OK", value: 200 },
|
|
117
|
+
// { label: "BAD_REQUEST", value: 400 },
|
|
118
|
+
// { label: "NOT_FOUND", value: 404 }
|
|
119
|
+
// ]
|
|
120
|
+
```
|
|
121
|
+
|
package/README.zh-CN.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# 使用 ts-enum-next 优雅的管理 typeScript enum
|
|
2
2
|
|
|
3
|
+
[](https://codecov.io/github/ricoNext/ts-enum-next)
|
|
4
|
+
|
|
3
5
|
在业务中,我们可能需要同时定义枚举值和枚举值说明,前者用来匹配,后者用来展示。
|
|
4
6
|
|
|
5
7
|
在 typescript 中可以使用 ***枚举 + 映射对象***的方式实现。
|
|
@@ -45,9 +47,9 @@ console.log(Status.PENDING.value); // 0
|
|
|
45
47
|
console.log(Status.PENDING.description); // "等待处理"
|
|
46
48
|
```
|
|
47
49
|
|
|
48
|
-
在业务中,我们除了需要这样简单的枚举定义类型, 我们还会需要处理很多`数据字典`的业务场景。尤其是在以 Node
|
|
50
|
+
在业务中,我们除了需要这样简单的枚举定义类型, 我们还会需要处理很多`数据字典`的业务场景。尤其是在以 Node 构建的服务端,可能需要使用类的能力来定义这些枚举值, 以方便包含其他字段、方法、构造函数、描述元数据等能力。
|
|
49
51
|
|
|
50
|
-
[ts-enum-next](
|
|
52
|
+
[ts-enum-next](https://github.com/ricoNext/ts-enum-next) 根据在 Java 中使用 enum 的场景,封装了一个抽象类 Enum 和一些辅助的工具类型, 来提供类似 Java 的枚举功能:
|
|
51
53
|
|
|
52
54
|
* Java 形式的数据定义
|
|
53
55
|
* 丰富的枚举元数据
|
|
@@ -76,11 +78,37 @@ console.log(HttpStatus.OK.value); // 200
|
|
|
76
78
|
console.log(HttpStatus.OK.name); // "OK"
|
|
77
79
|
console.log(HttpStatus.OK.description); // "请求成功"
|
|
78
80
|
```
|
|
81
|
+
|
|
82
|
+
在 Enum 接收的泛型参数中, 除了传入的是枚举值的类型, 如 number、string、boolean 等。 还可以传入一个联合类型, 如 `200 | 400 |404`, 这样就可以定义一个联合类型的枚举。
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
import { Enum } from 'ts-enum-next';
|
|
86
|
+
type HttpStatusValue = 200 | 400 | 404;
|
|
87
|
+
|
|
88
|
+
class HttpStatus extends Enum<HttpStatusValue> {
|
|
89
|
+
static readonly OK = new HttpStatus(200, "OK", "请求成功");
|
|
90
|
+
static readonly BAD_REQUEST = new HttpStatus(400, "BAD_REQUEST", "错误请求");
|
|
91
|
+
static readonly NOT_FOUND = new HttpStatus(404, "NOT_FOUND", "资源未找到");
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
// 使用示例
|
|
96
|
+
console.log(HttpStatus.OK.value); // 200
|
|
97
|
+
console.log(HttpStatus.OK.name); // "OK"
|
|
98
|
+
console.log(HttpStatus.OK.description); // "请求成功"
|
|
99
|
+
|
|
100
|
+
// status 使用 HttpStatusValue 类型
|
|
101
|
+
type ResultData = {
|
|
102
|
+
status: HttpStatusValue,
|
|
103
|
+
message: string,
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
79
107
|
#### 适用场景
|
|
80
108
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
109
|
+
* 需要为枚举值附加元信息(如状态码描述)
|
|
110
|
+
* 需要根据数值快速查找对应的枚举实例
|
|
111
|
+
* 需要保持严格的类型安全
|
|
84
112
|
|
|
85
113
|
### 场景2: 枚举集合操作
|
|
86
114
|
|
|
@@ -88,8 +116,6 @@ console.log(HttpStatus.OK.description); // "请求成功"
|
|
|
88
116
|
// 获取所有枚举实例, 返回的是所有的枚举类
|
|
89
117
|
console.log(HttpStatus.values()); // [HttpStatus, HttpStatus, HttpStatus, ....]
|
|
90
118
|
|
|
91
|
-
|
|
92
|
-
|
|
93
119
|
// 通过值获取枚举实例
|
|
94
120
|
console.log(HttpStatus.fromValue(200)); // HttpStatus(200, "OK", "请求成功")
|
|
95
121
|
|
|
@@ -97,8 +123,7 @@ console.log(HttpStatus.values()); // [HttpStatus, HttpStatus, HttpStatus, ....]
|
|
|
97
123
|
console.log(HttpStatus.fromName('OK')); // HttpStatus(200, "OK", "请求成功")
|
|
98
124
|
|
|
99
125
|
|
|
100
|
-
|
|
101
|
-
// 创建枚举集合
|
|
126
|
+
// 创建枚举集合, 对枚举值进行分组管理
|
|
102
127
|
const errorStatus = HttpStatus.setOf(
|
|
103
128
|
HttpStatus.BAD_REQUEST,
|
|
104
129
|
HttpStatus.NOT_FOUND
|
|
@@ -117,14 +142,13 @@ if (errorStatus.has(currentStatus))) {
|
|
|
117
142
|
|
|
118
143
|
**适用场景**:
|
|
119
144
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
- 需要遍历枚举的所有可能值
|
|
145
|
+
* 需要分组管理枚举值(如所有错误状态)
|
|
146
|
+
* 需要检查枚举值是否属于某个特定集合
|
|
147
|
+
* 需要遍历枚举的所有可能值
|
|
124
148
|
|
|
125
149
|
### 场景 3:枚举映射表
|
|
126
150
|
|
|
127
|
-
```
|
|
151
|
+
```ts
|
|
128
152
|
// 创建枚举到字符串的映射
|
|
129
153
|
const statusMessages = HttpStatus.enumMap<string>({
|
|
130
154
|
[HttpStatus.OK]: "操作成功完成",
|
|
@@ -135,11 +159,32 @@ const statusMessages = HttpStatus.enumMap<string>({
|
|
|
135
159
|
// 使用映射
|
|
136
160
|
console.log(statusMessages.get(HttpStatus.NOT_FOUND)); // “请求的资源不存在”
|
|
137
161
|
```
|
|
162
|
+
|
|
163
|
+
### 场景 7:为 Ant Design `Select` 生成枚举下拉项
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
import { Enum, enumOptions } from "ts-enum-next";
|
|
167
|
+
|
|
168
|
+
class HttpStatus extends Enum<number> {
|
|
169
|
+
static readonly OK = new HttpStatus(200, "OK", "请求成功");
|
|
170
|
+
static readonly BAD_REQUEST = new HttpStatus(400, "BAD_REQUEST", "错误请求");
|
|
171
|
+
static readonly NOT_FOUND = new HttpStatus(404, "NOT_FOUND", "资源未找到");
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const options = enumOptions(HttpStatus);
|
|
175
|
+
// options:
|
|
176
|
+
// [
|
|
177
|
+
// { label: "OK", value: 200 },
|
|
178
|
+
// { label: "BAD_REQUEST", value: 400 },
|
|
179
|
+
// { label: "NOT_FOUND", value: 404 }
|
|
180
|
+
// ]
|
|
181
|
+
```
|
|
182
|
+
|
|
138
183
|
**适用场景**:
|
|
139
184
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
185
|
+
* 需要为不同枚举值关联不同的数据
|
|
186
|
+
* 需要高效查找枚举对应的资源
|
|
187
|
+
* 需要类型安全的键值存储
|
|
143
188
|
|
|
144
189
|
### 场景 4:自定义枚举方法
|
|
145
190
|
|
|
@@ -174,9 +219,9 @@ console.log(current.canTransitionTo(OrderStatus.SHIPPED)); // false
|
|
|
174
219
|
|
|
175
220
|
**适用场景**:
|
|
176
221
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
222
|
+
* 需要为枚举添加业务逻辑
|
|
223
|
+
* 需要实现状态机或工作流
|
|
224
|
+
* 需要封装枚举相关的复杂判断逻辑
|
|
180
225
|
|
|
181
226
|
### 场景 5:枚举序列化/反序列化
|
|
182
227
|
|
|
@@ -215,9 +260,9 @@ console.log(parsedRole === UserRole.ADMIN); // true
|
|
|
215
260
|
|
|
216
261
|
**适用场景**:
|
|
217
262
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
263
|
+
* 需要将枚举与JSON相互转换
|
|
264
|
+
* 需要处理API响应中的枚举值
|
|
265
|
+
* 需要保持前后端枚举值的一致性
|
|
221
266
|
|
|
222
267
|
### 场景 6:获取原始枚举值类型
|
|
223
268
|
|
|
@@ -235,24 +280,13 @@ export type HttpStatusEnum = EnumValues<typeof HttpStatus>;
|
|
|
235
280
|
|
|
236
281
|
// 使用原始枚举值类型再去定义其他类型
|
|
237
282
|
export type ResponseData<T> = {
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
283
|
+
message: string;
|
|
284
|
+
data: T;
|
|
285
|
+
code: HttpStatusEnum;
|
|
241
286
|
};
|
|
242
287
|
```
|
|
243
288
|
|
|
244
289
|
**适用场景**:
|
|
245
290
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
291
|
+
* 需要获取原始枚举值类型
|
|
292
|
+
* 使用原始枚举值类型再去定义其他类型
|
package/dist/index.d.ts
CHANGED
|
@@ -27,3 +27,19 @@ export type EnumValues<T> = {
|
|
|
27
27
|
}[keyof T];
|
|
28
28
|
export { Enum };
|
|
29
29
|
export type { EnumValueType };
|
|
30
|
+
/**
|
|
31
|
+
* 枚举数组, 用于 Antd Select 组件的 options 参数
|
|
32
|
+
* @param enums
|
|
33
|
+
* @returns
|
|
34
|
+
*/
|
|
35
|
+
export declare const enumOptions: <T extends {
|
|
36
|
+
values: () => Array<{
|
|
37
|
+
name: string;
|
|
38
|
+
value: unknown;
|
|
39
|
+
[key: string]: unknown;
|
|
40
|
+
}>;
|
|
41
|
+
}>(enums: T) => {
|
|
42
|
+
label: string;
|
|
43
|
+
value: unknown;
|
|
44
|
+
name: string;
|
|
45
|
+
}[];
|
package/dist/index.js
CHANGED
|
@@ -78,3 +78,13 @@ Enum._values = new Map();
|
|
|
78
78
|
Enum._valueMap = new Map();
|
|
79
79
|
Enum._nameMap = new Map();
|
|
80
80
|
export { Enum };
|
|
81
|
+
/**
|
|
82
|
+
* 枚举数组, 用于 Antd Select 组件的 options 参数
|
|
83
|
+
* @param enums
|
|
84
|
+
* @returns
|
|
85
|
+
*/
|
|
86
|
+
export const enumOptions = (enums) => enums.values().map((i) => ({
|
|
87
|
+
...i,
|
|
88
|
+
label: i.name,
|
|
89
|
+
value: i.value,
|
|
90
|
+
}));
|