@airpower/enum 0.0.7 → 0.0.9

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
@@ -26,27 +26,31 @@ cnpm install @airpower/enum
26
26
  ## 📖 如何使用
27
27
 
28
28
  ```ts
29
- import { Enum } from '@/airpower/enum'
29
+ import type { EnumConstructor } from './enum'
30
+ import { Enum } from './enum'
30
31
 
31
- class UserGender extends Enum<string> {
32
- static MALE = new UserGender('MALE', '男')
33
- static FEMALE = new UserGender('FEMALE', '')
32
+ // 普通数字枚举
33
+ class UserStatus extends Enum {
34
+ static readonly NORMAL = new UserStatus(0, '正常')
35
+ static readonly DISABLED = new UserStatus(1, '禁用')
34
36
  }
35
37
 
36
- class UserStatus extends Enum {
37
- static NORMAL = new UserStatus(0, '正常')
38
- static DISABLED = new UserStatus(1, '禁用')
38
+ // 字符串枚举(支持数字、字符串、布尔值)
39
+ class UserGender extends Enum<string> {
40
+ static readonly MALE = new UserGender('MALE', '')
41
+ static readonly FEMALE = new UserGender('FEMALE', '女')
39
42
  }
40
43
 
41
44
  // 扩展自定义属性
42
-
43
45
  class Platform extends Enum<number> {
44
46
  static readonly MAC = new Platform(1, 'mac', 'apple.png')
45
47
  static readonly WINDOWS = new Platform(2, 'windows', 'windows.png')
46
48
  static readonly ANDROID = new Platform(3, 'android', 'android.png')
47
49
 
50
+ // 自定义属性
48
51
  icon!: string
49
52
 
53
+ // 1. 通过构造初始化(此时可以设置icon为readonly)
50
54
  constructor(key: number, label?: string, icon?: string) {
51
55
  super(key, label)
52
56
  if (icon) {
@@ -54,6 +58,12 @@ class Platform extends Enum<number> {
54
58
  }
55
59
  }
56
60
 
61
+ // 2. 通过 set 方法初始化
62
+ setIcon(icon: string) {
63
+ this.icon = icon
64
+ return this
65
+ }
66
+
57
67
  static getIcon(this: EnumConstructor<number, Platform>, key: number) {
58
68
  return this.get(key)!.icon
59
69
  }
@@ -61,6 +71,7 @@ class Platform extends Enum<number> {
61
71
 
62
72
  console.warn(Platform.getIcon(1))
63
73
  console.warn(Platform.MAC.icon)
74
+ console.warn(Platform.MAC.equalsKey(2))
64
75
  ```
65
76
 
66
77
  ## ⏰ 欢迎反馈
@@ -8,9 +8,9 @@ export interface IEnum<K extends EnumKey = number> {
8
8
  /**
9
9
  * ### 字典的值
10
10
  */
11
- readonly key: K;
11
+ key: K;
12
12
  /**
13
13
  * ### 字典的显示标题
14
14
  */
15
- readonly label?: string;
15
+ label?: string;
16
16
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@airpower/enum",
3
3
  "type": "module",
4
- "version": "0.0.7",
4
+ "version": "0.0.9",
5
5
  "description": "AirPower-Enum 是一个基于 TypeScript 的封装的类似 Java 枚举类的枚举字典处理工具。",
6
6
  "author": {
7
7
  "name": "Hamm",