@airpower/enum 2.5.1 → 2.7.0

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.
Files changed (2) hide show
  1. package/README.md +40 -565
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,615 +1,90 @@
1
1
  <p align="center">
2
- <img width="300" src="./assets/airpower-bg.svg" alt="AirPower-Enum Logo"/>
3
- </p>
4
-
5
- <h1 align="center">AirPower-Enum</h1>
6
-
7
- <p align="center">
8
- <strong>基于 TypeScript 的 Java 风格枚举字典处理工具</strong>
2
+ <img width="300" src="./assets/airpower-bg.svg"/>
9
3
  </p>
10
4
 
11
5
  <p align="center">
12
- <a href="https://www.npmjs.com/package/@airpower/enum">
13
- <img src="https://img.shields.io/npm/v/@airpower/enum" alt="NPM Version"/>
14
- </a>
15
- <a href="https://www.npmjs.com/package/@airpower/enum">
16
- <img src="https://img.shields.io/npm/dm/@airpower/enum" alt="NPM Downloads"/>
17
- </a>
18
- <a href="https://github.com/AirPowerTeam/AirPower-Enum/blob/main/LICENSE">
19
- <img src="https://img.shields.io/npm/l/@airpower/enum" alt="License"/>
20
- </a>
21
- <a href="https://www.npmjs.com/package/@airpower/enum">
22
- <img src="https://img.shields.io/bundlephobia/min/@airpower/enum" alt="Minified Size"/>
23
- </a>
6
+ <a href="https://www.npmjs.com/@airpower/enum">
7
+ <img src="https://img.shields.io/npm/v/@airpower/enum"/>
8
+ </a>
9
+ <a href="https://www.npmjs.com/@airpower/enum">
10
+ <img src="https://img.shields.io/npm/dm/@airpower/enum"/>
11
+ </a>
24
12
  </p>
25
13
 
26
14
  <p align="center">
27
- <a href="https://github.com/AirPowerTeam/AirPower-Enum">GitHub</a> ·
28
- <a href="https://gitee.com/air-power/AirPower-Enum">Gitee</a> ·
29
- <a href="https://www.npmjs.com/package/@airpower/enum">NPM</a>
15
+ <a href="https://github.com/AirPowerTeam/AirPower-Enum">Github</a> /
16
+ <a href="https://gitee.com/air-power/AirPower-Enum">Gitee</a> /
17
+ <a href="https://www.npmjs.com/package/@airpower/enum">NPM</a>
30
18
  </p>
31
19
 
32
- ---
33
-
34
- ## 📖 目录
35
-
36
- - [项目介绍](#-项目介绍)
37
- - [特性亮点](#-特性亮点)
38
- - [安装](#-安装)
39
- - [快速开始](#-快速开始)
40
- - [API 文档](#-api-文档)
41
- - [高级用法](#-高级用法)
42
- - [类型定义](#-类型定义)
43
- - [构建与开发](#-构建与开发)
44
- - [常见问题](#-常见问题)
45
- - [贡献指南](#-贡献指南)
46
- - [许可证](#-许可证)
47
-
48
- ---
49
-
50
20
  ## 🎉 项目介绍
51
21
 
52
- **AirPower-Enum** 是一个基于 TypeScript 开发的轻量级枚举字典处理工具库,采用类似 Java 枚举的设计模式,为 TypeScript
53
- 项目提供类型安全、功能丰富的枚举操作体验。
54
-
55
- 通过面向对象的封装,AirPower-Enum 让 TypeScript 枚举不再局限于简单的键值对,而是支持自定义属性、扩展方法、类型推断等高级特性,使代码更加优雅、可维护。
56
-
57
- ---
58
-
59
- ## ✨ 特性亮点
60
-
61
- - 🎯 **Java 风格枚举**:采用经典的 Java 枚举设计模式,熟悉 Java 的开发者可快速上手
62
- - 🔒 **类型安全**:完整的 TypeScript 泛型支持,提供精确的类型推断和检查
63
- - 🎨 **多类型支持**:支持 `number`、`string`、`boolean` 三种类型的枚举键
64
- - 🧩 **可扩展性**:支持自定义属性、方法,可灵活扩展枚举功能
65
- - 📦 **零依赖**:纯 TypeScript 实现,无任何运行时依赖
66
- - 🚀 **轻量级**:极小的包体积,不影响项目打包大小
67
- - 📝 **完整类型声明**:提供完整的 `.d.ts` 类型定义文件
68
- - 🛠️ **现代化工具链**:基于 Vite 构建,支持 ES Modules
69
-
70
- ---
22
+ **AirPower-Enum** 是一个基于 `TypeScript` 的封装的类似 `Java` 枚举类的枚举字典处理工具。
71
23
 
72
- ## 📥 安装
24
+ ## 💻 如何安装
73
25
 
74
- ### 使用 npm
75
-
76
- ```bash
26
+ ```shell
77
27
  npm install @airpower/enum
78
- ```
79
-
80
- ### 使用 yarn
81
-
82
- ```bash
28
+ # or
83
29
  yarn add @airpower/enum
84
- ```
85
-
86
- ### 使用 cnpm
87
-
88
- ```bash
30
+ # or
89
31
  cnpm install @airpower/enum
32
+ # or ...
90
33
  ```
91
34
 
92
- ### 使用 pnpm
93
-
94
- ```bash
95
- pnpm add @airpower/enum
96
- ```
97
-
98
- ---
99
-
100
- ## 🚀 快速开始
101
-
102
- ### 基础用法
35
+ ## 📖 如何使用
103
36
 
104
- #### 数字枚举
105
-
106
- ```typescript
107
- import { Enum } from '@airpower/enum'
37
+ ```ts
38
+ import type { EnumConstructor } from './enum'
39
+ import { Enum } from './enum'
108
40
 
41
+ // 普通数字枚举
109
42
  class UserStatus extends Enum {
110
43
  static readonly NORMAL = new UserStatus(0, '正常')
111
44
  static readonly DISABLED = new UserStatus(1, '禁用')
112
45
  }
113
46
 
114
- // 使用示例
115
- console.log(UserStatus.NORMAL.key) // 输出:0
116
- console.log(UserStatus.NORMAL.label) // 输出:正常
117
- ```
118
-
119
- #### 字符串枚举
120
-
121
- ```typescript
122
- import { Enum } from '@airpower/enum'
123
-
47
+ // 字符串枚举(支持数字、字符串、布尔值)
124
48
  class UserGender extends Enum<string> {
125
49
  static readonly MALE = new UserGender('MALE', '男')
126
50
  static readonly FEMALE = new UserGender('FEMALE', '女')
127
51
  }
128
52
 
129
- // 使用示例
130
- console.log(UserGender.MALE.key) // 输出:MALE
131
- console.log(UserGender.FEMALE.label) // 输出:女
132
- ```
133
-
134
- #### 布尔值枚举
135
-
136
- ```typescript
137
- import { Enum } from '@airpower/enum'
138
-
139
- class SwitchState extends Enum<boolean> {
140
- static readonly ON = new SwitchState(true, '开启')
141
- static readonly OFF = new SwitchState(false, '关闭')
142
- }
143
- ```
144
-
145
- ---
146
-
147
- ## 📚 API 文档
148
-
149
- ### 类:`Enum<K>`
150
-
151
- 枚举基类,所有自定义枚举类需继承此类。
152
-
153
- #### 构造函数
154
-
155
- ```typescript
156
- constructor(key
157
- :
158
- K, label ? : string
159
- )
160
- ```
161
-
162
- | 参数 | 类型 | 必填 | 说明 |
163
- |---------|----------|----|-------------------------------------|
164
- | `key` | `K` | 是 | 枚举键值,支持 `number`、`string`、`boolean` |
165
- | `label` | `string` | 否 | 枚举描述标签 |
166
-
167
- #### 实例属性
168
-
169
- | 属性 | 类型 | 说明 |
170
- |---------|----------|-----------|
171
- | `key` | `K` | 枚举的值(只读) |
172
- | `label` | `string` | 枚举的描述(可选) |
173
-
174
- #### 实例方法
175
-
176
- ##### `equalsKey(key: EnumKey): boolean`
177
-
178
- 判断实例的 `key` 是否与传入的键值相等。
179
-
180
- ```typescript
181
- UserStatus.NORMAL.equalsKey(0) // true
182
- UserStatus.NORMAL.equalsKey(1) // false
183
- ```
184
-
185
- ---
186
-
187
- ### 静态方法
188
-
189
- #### `Enum.get<K, E>(key: K): E | null`
190
-
191
- 通过键值查找对应的枚举实例。
192
-
193
- ```typescript
194
- const status = UserStatus.get(0)
195
- console.log(status?.label) // 输出:正常
196
- ```
197
-
198
- **泛型参数:**
199
-
200
- - `K`:枚举键类型
201
- - `E`:枚举实例类型(继承自 `Enum<K>`)
202
-
203
- ---
204
-
205
- #### `Enum.getLabel<K, E>(key: K, defaultLabel?: string): string`
206
-
207
- 获取指定键值的枚举标签,如不存在则返回默认值。
208
-
209
- ```typescript
210
- UserStatus.getLabel(0) // 输出:正常
211
- UserStatus.getLabel(999, '未知') // 输出:未知
212
- UserStatus.getLabel(999) // 输出:-
213
- ```
214
-
215
- **参数说明:**
216
-
217
- | 参数 | 类型 | 必填 | 默认值 | 说明 |
218
- |----------------|----------|----|-------|------|
219
- | `key` | `K` | 是 | - | 枚举键值 |
220
- | `defaultLabel` | `string` | 否 | `'-'` | 默认标签 |
221
-
222
- ---
223
-
224
- #### `Enum.toArray<K, E>(): E[]`
225
-
226
- 将所有枚举实例转换为数组。
227
-
228
- ```typescript
229
- const allStatus = UserStatus.toArray()
230
- // 输出:[UserStatus { key: 0, label: '正常' }, UserStatus { key: 1, label: '禁用' }]
231
-
232
- // 常用于遍历或下拉选项
233
- allStatus.forEach((item) => {
234
- console.log(`${item.key}: ${item.label}`)
235
- })
236
- ```
237
-
238
- ---
239
-
240
- ## 🔧 高级用法
241
-
242
- ### 自定义属性
243
-
244
- 通过扩展构造函数,可以为枚举添加自定义属性:
245
-
246
- ```typescript
247
- import type { EnumConstructor } from '@airpower/enum'
248
- import { Enum } from '@airpower/enum'
249
-
53
+ // 扩展自定义属性
250
54
  class Platform extends Enum<number> {
251
- static readonly MAC = new Platform(1, 'macOS', 'apple.png', '#000')
252
- static readonly WINDOWS = new Platform(2, 'Windows', 'windows.png', '#0078D4')
253
- static readonly ANDROID = new Platform(3, 'Android', 'android.png', '#3DDC84')
254
- static readonly LINUX = new Platform(4, 'Linux', 'linux.png', '#FCC624')
55
+ static readonly MAC = new Platform(1, 'mac', 'apple.png')
56
+ static readonly WINDOWS = new Platform(2, 'windows', 'windows.png')
57
+ static readonly ANDROID = new Platform(3, 'android', 'android.png')
255
58
 
256
59
  // 自定义属性
257
60
  icon!: string
258
- color!: string
259
61
 
260
- constructor(key: number, label?: string, icon?: string, color?: string) {
62
+ // 1. 通过构造初始化(此时可以设置icon为readonly)
63
+ constructor(key: number, label?: string, icon?: string) {
261
64
  super(key, label)
262
- if (icon)
65
+ if (icon) {
263
66
  this.icon = icon
264
- if (color)
265
- this.color = color
266
- }
267
- }
268
-
269
- // 使用自定义属性
270
- console.log(Platform.MAC.icon) // 输出:apple.png
271
- console.log(Platform.WINDOWS.color) // 输出:#0078D4
272
- ```
273
-
274
- ---
275
-
276
- ### 自定义方法
277
-
278
- 可以在枚举类中定义实例方法和静态方法:
279
-
280
- ```typescript
281
- class Priority extends Enum<number> {
282
- static readonly LOW = new Priority(1, '低优先级')
283
- static readonly MEDIUM = new Priority(2, '中优先级')
284
- static readonly HIGH = new Priority(3, '高优先级')
285
- static readonly CRITICAL = new Priority(4, '紧急')
286
-
287
- // 实例方法:判断是否为高优先级
288
- isHighPriority(): boolean {
289
- return this.key >= 3
290
- }
291
-
292
- // 实例方法:获取优先级图标
293
- getIcon(): string {
294
- const icons: Record<number, string> = {
295
- 1: '🟢',
296
- 2: '🟡',
297
- 3: '🟠',
298
- 4: '🔴'
299
67
  }
300
- return icons[this.key] || '⚪'
301
- }
302
-
303
- // 静态方法:获取所有高优先级项
304
- static getHighPriorities(): Priority[] {
305
- return this.toArray().filter(p => p.isHighPriority())
306
- }
307
- }
308
-
309
- // 使用示例
310
- console.log(Priority.HIGH.getIcon()) // 输出:🟠
311
- console.log(Priority.LOW.isHighPriority()) // 输出:false
312
- console.log(Priority.getHighPriorities()) // 输出:[HIGH, CRITICAL]
313
- ```
314
-
315
- ---
316
-
317
- ### 链式调用
318
-
319
- 通过返回 `this` 实现链式调用:
320
-
321
- ```typescript
322
- class Config extends Enum<string> {
323
- static readonly DEBUG = new Config('DEBUG', '调试模式')
324
- static readonly RELEASE = new Config('RELEASE', '发布模式')
325
-
326
- enabled!: boolean
327
- timeout!: number
328
-
329
- setEnabled(enabled: boolean): this {
330
- this.enabled = enabled
331
- return this
332
68
  }
333
69
 
334
- setTimeout(timeout: number): this {
335
- this.timeout = timeout
70
+ // 2. 通过 set 方法初始化
71
+ setIcon(icon: string) {
72
+ this.icon = icon
336
73
  return this
337
74
  }
338
- }
339
-
340
- // 链式调用
341
- Config.DEBUG.setEnabled(true).setTimeout(3000)
342
- ```
343
-
344
- ---
345
-
346
- ### 类型安全的高级用法
347
-
348
- 使用 `EnumConstructor` 类型来编写泛型工具函数:
349
-
350
- ```typescript
351
- import type { EnumConstructor } from '@airpower/enum'
352
- import { Enum } from '@airpower/enum'
353
-
354
- /**
355
- * 通用的枚举选择器组件(以 Vue 3 为例)
356
- */
357
- function useEnumSelector<K extends string | number | boolean, E extends Enum<K>>(
358
- EnumClass: EnumConstructor<K, E>
359
- ) {
360
- const options = EnumClass.toArray()
361
75
 
362
- const getLabel = (key: K): string => {
363
- return EnumClass.getLabel(key, '未知选项')
364
- }
365
-
366
- const isSelected = (current: K, target: K): boolean => {
367
- return current === target
368
- }
369
-
370
- return {
371
- options,
372
- getLabel,
373
- isSelected
76
+ static getIcon(this: EnumConstructor<number, Platform>, key: number) {
77
+ return this.get(key)!.icon
374
78
  }
375
79
  }
376
80
 
377
- // 使用示例
378
- const { options, getLabel } = useEnumSelector(UserStatus)
379
- ```
380
-
381
- ---
382
-
383
- ## 📝 类型定义
384
-
385
- ### `EnumKey`
386
-
387
- 枚举键的允许类型:
388
-
389
- ```typescript
390
- type EnumKey = string | number | boolean
81
+ console.warn(Platform.getIcon(1))
82
+ console.warn(Platform.MAC.icon)
83
+ console.warn(Platform.MAC.equalsKey(2))
391
84
  ```
392
85
 
393
- ---
394
-
395
- ### `IEnum<K>`
86
+ ## ⏰ 欢迎反馈
396
87
 
397
- 标准枚举字典接口:
88
+ 如有疑问,可以通过本仓库的 **Issues** 与我们联系,如果你有一些代码贡献,可以通过 **Pull Request** 将代码贡献,为这个项目添砖加瓦。
398
89
 
399
- ```typescript
400
- interface IEnum<K extends EnumKey = number> {
401
- key: K
402
- label?: string
403
- }
404
- ```
405
-
406
- ---
407
-
408
- ### `EnumConstructor<K, E>`
409
-
410
- 枚举类构造器类型:
411
-
412
- ```typescript
413
- type EnumConstructor<K extends EnumKey = number, E extends Enum<K> = Enum<K>> = {
414
- new(...args: any[]): E
415
- } & typeof Enum<K>
416
- ```
417
-
418
- ---
419
-
420
- ## 🛠️ 构建与开发
421
-
422
- ### 环境要求
423
-
424
- - Node.js >= 16.0.0
425
- - npm >= 7.0.0 或 yarn >= 1.22.0
426
-
427
- ### 克隆项目
428
-
429
- ```bash
430
- git clone https://github.com/AirPowerTeam/AirPower-Enum.git
431
- cd AirPower-Enum
432
- ```
433
-
434
- ### 安装依赖
435
-
436
- ```bash
437
- npm install
438
- # 或
439
- yarn install
440
- ```
441
-
442
- ### 构建项目
443
-
444
- ```bash
445
- npm run build
446
- # 或
447
- yarn build
448
- ```
449
-
450
- 构建脚本执行流程:
451
-
452
- 1. **ESLint 代码检查** - 确保代码质量
453
- 2. **TypeScript 编译** - 类型检查
454
- 3. **Vite 打包** - 生成 `dist/` 目录
455
-
456
- ### 构建输出
457
-
458
- ```
459
- dist/
460
- ├── main.js # ES Modules 格式的主文件
461
- ├── index.d.ts # 类型声明入口
462
- └── enum/
463
- ├── Enum.d.ts # Enum 类类型定义
464
- ├── IEnum.d.ts # IEnum 接口类型定义
465
- └── type.d.ts # 类型定义
466
- ```
467
-
468
- ---
469
-
470
- ## ❓ 常见问题
471
-
472
- ### Q1: 为什么不用 TypeScript 原生 `enum`?
473
-
474
- TypeScript 原生 `enum` 存在以下限制:
475
-
476
- - 不支持自定义属性和方法
477
- - 编译后可能产生额外的运行时代码
478
- - 类型推断不够灵活
479
- - 不支持泛型
480
-
481
- AirPower-Enum 通过类的方式完美解决这些问题。
482
-
483
- ---
484
-
485
- ### Q2: 如何与前端框架(Vue/React)配合使用?
486
-
487
- ```typescript
488
- // Vue 3 示例
489
- <template>
490
- <el-select
491
- v - model = "status" >
492
- <el-option
493
- v -
494
- for= "item in UserStatus.toArray()"
495
- :
496
- key = "item.key"
497
- :
498
- label = "item.label"
499
- :
500
- value = "item.key"
501
- / >
502
- </el-select>
503
- < /template>
504
-
505
- < script
506
- setup
507
- lang = "ts" >
508
- import {ref} from 'vue'
509
- import {UserStatus} from './enums/UserStatus'
510
-
511
- const status = ref(UserStatus.NORMAL.key)
512
- < /script>
513
- ```
514
-
515
- ```typescript
516
- // React 示例
517
- function StatusSelect() {
518
- const [status, setStatus] = useState(UserStatus.NORMAL.key)
519
-
520
- return (
521
- <select value = {status}
522
- onChange = {e
523
- =>
524
- setStatus(Number(e.target.value))
525
- }>
526
- {
527
- UserStatus.toArray().map(item => (
528
- <option key = {item.key}
529
- value = {item.key} >
530
- {item.label}
531
- < /option>
532
- ))
533
- }
534
- </select>
535
- )
536
- }
537
- ```
538
-
539
- ---
540
-
541
- ### Q3: 如何处理枚举的序列化/反序列化?
542
-
543
- ```typescript
544
- // 序列化
545
- const json = JSON.stringify(UserStatus.NORMAL)
546
- // 输出:{"key":0,"label":"正常"}
547
-
548
- // 反序列化
549
- const data = { key: 0, label: '正常' }
550
- const status = UserStatus.get(data.key)
551
- ```
552
-
553
- ---
554
-
555
- ### Q4: 支持常量枚举(const enum)吗?
556
-
557
- 本库采用运行时类实现,不支持 `const enum`。但这也带来了更好的调试体验和运行时灵活性。
558
-
559
- ---
560
-
561
- ## 🤝 贡献指南
562
-
563
- 我们欢迎各种形式的贡献!
564
-
565
- ### 提交 Issue
566
-
567
- 遇到问题或有新需求时,欢迎通过 [GitHub Issues](https://github.com/AirPowerTeam/AirPower-Enum/issues) 反馈。
568
-
569
- ### 提交 PR
570
-
571
- 1. Fork 本仓库
572
- 2. 创建特性分支 (`git checkout -b feature/AmazingFeature`)
573
- 3. 提交更改 (`git commit -m 'Add some AmazingFeature'`)
574
- 4. 推送到分支 (`git push origin feature/AmazingFeature`)
575
- 5. 创建 Pull Request
576
-
577
- ### 开发规范
578
-
579
- - 遵循项目的 ESLint 规则
580
- - 使用 TypeScript 严格模式
581
- - 为新增功能添加类型定义
582
- - 保持代码风格一致
583
-
584
- ### 版本发布自动化流程
585
-
586
- - 使用 `eslint --fix` 修复项目中可能出现的问题
587
- - 更新 `package.json` 中的版本号
588
- - 使用 `yarn build` 构建项目
589
- - 使用 `npm publish` 发布包
590
- - 使用 `git add/commit/push` 将本地所有变更的文件推送到 Github
591
-
592
- ### 联系方式
593
-
594
- - QQ 群:555156313
595
- - Email:admin@hamm.cn
596
-
597
- ---
598
-
599
- ## 📄 许可证
600
-
601
- 本项目采用 [MIT 许可证](LICENSE) 开源。
602
- ---
603
-
604
- ## 👥 团队信息
605
-
606
- - **作者**:Hamm
607
- - **邮箱**:admin@hamm.cn
608
- - **GitHub**:[@HammCn](https://github.com/HammCn)
609
- - **团队**:[AirPower Team](https://github.com/AirPowerTeam)
610
-
611
- ---
612
-
613
- <p align="center">
614
- 如果这个项目对你有帮助,欢迎给一个 ⭐️ Star 支持!
615
- </p>
90
+ 如果有更多的需求和建议,欢迎通过本仓库的 `Issues` 提出,也欢迎加入 QQ群 555156313 与我们及时反馈。
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@airpower/enum",
3
3
  "type": "module",
4
- "version": "2.5.1",
4
+ "version": "2.7.0",
5
5
  "description": "AirPower-Enum 是一个基于 TypeScript 的封装的类似 Java 枚举类的枚举字典处理工具。",
6
6
  "author": {
7
7
  "name": "Hamm",
@@ -34,7 +34,7 @@
34
34
  "dist"
35
35
  ],
36
36
  "scripts": {
37
- "build": "eslint src && tsc && vite build"
37
+ "build": "eslint && tsc && vite build"
38
38
  },
39
39
  "dependencies": {},
40
40
  "devDependencies": {