@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 +19 -8
- package/dist/enum/IEnum.d.ts +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -26,27 +26,31 @@ cnpm install @airpower/enum
|
|
|
26
26
|
## 📖 如何使用
|
|
27
27
|
|
|
28
28
|
```ts
|
|
29
|
-
import {
|
|
29
|
+
import type { EnumConstructor } from './enum'
|
|
30
|
+
import { Enum } from './enum'
|
|
30
31
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
static
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
static
|
|
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
|
## ⏰ 欢迎反馈
|
package/dist/enum/IEnum.d.ts
CHANGED