create-enum-es 2.0.1 → 3.0.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.
- package/README.md +314 -51
- package/dist/create-enum.cjs.js +1 -1
- package/dist/create-enum.es.js +1 -1
- package/dist/create-enum.umd.js +1 -1
- package/dist/index.d.ts +161 -39
- package/package.json +4 -6
package/README.md
CHANGED
|
@@ -1,70 +1,333 @@
|
|
|
1
|
+
# create-enum-es
|
|
2
|
+
|
|
3
|
+
> 零依赖、类型安全的前端枚举工具,为 TypeScript 项目提供完整的枚举管理方案。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install create-enum-es
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 快速开始
|
|
12
|
+
|
|
1
13
|
```ts
|
|
2
14
|
import createEnum from "create-enum-es";
|
|
3
15
|
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
16
|
+
const Status = createEnum({
|
|
17
|
+
ACTIVE: [1, "激活", { color: "green" }],
|
|
18
|
+
INACTIVE: [0, "未激活", { color: "gray" }],
|
|
19
|
+
PENDING: [2, "待审核", { color: "orange" }],
|
|
7
20
|
} as const);
|
|
8
21
|
|
|
9
|
-
|
|
10
|
-
//
|
|
22
|
+
Status.ACTIVE; // 1(直接属性访问)
|
|
23
|
+
Status.label("ACTIVE"); // '激活'
|
|
24
|
+
Status.label(1); // '激活'(通过 value 查 label)
|
|
25
|
+
Status.extra("ACTIVE"); // { color: 'green' }
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
> **重要:** 定义时需要在对象末尾加上 `as const` 以获得完整的类型推导。
|
|
29
|
+
|
|
30
|
+
## API
|
|
31
|
+
|
|
32
|
+
### `createEnum(enumMap)`
|
|
33
|
+
|
|
34
|
+
创建一个冻结的枚举实例。参数格式:
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
{
|
|
38
|
+
KEY: [value, label, extra?] // extra 可选
|
|
39
|
+
} as const
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
### 属性访问
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
Status.ACTIVE // 1
|
|
48
|
+
Status.INACTIVE // 0
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
直接通过 Key 获取 Value,享受完整的类型提示。
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
### `value(key)`
|
|
56
|
+
|
|
57
|
+
通过 Key 获取 Value。
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
Status.value("ACTIVE"); // 1
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
### `values(...keys?)`
|
|
66
|
+
|
|
67
|
+
获取多个枚举值。不传参返回所有值。
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
Status.values(); // [1, 0, 2]
|
|
71
|
+
Status.values("ACTIVE", "PENDING"); // [1, 2]
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
### `keys()`
|
|
77
|
+
|
|
78
|
+
获取所有枚举 Key。
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
Status.keys(); // ['ACTIVE', 'INACTIVE', 'PENDING']
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
### `label(keyOrValue)`
|
|
87
|
+
|
|
88
|
+
通过 Key 或 Value 获取 Label,支持双向查找。
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
Status.label("ACTIVE"); // '激活'
|
|
92
|
+
Status.label(1); // '激活'
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
### `extra(keyOrValue)`
|
|
98
|
+
|
|
99
|
+
通过 Key 或 Value 获取附加数据。
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
Status.extra("ACTIVE"); // { color: 'green' }
|
|
103
|
+
Status.extra(1); // { color: 'green' }
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
### `keyOf(value)`
|
|
109
|
+
|
|
110
|
+
通过 Value 反查 Key。
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
Status.keyOf(1); // 'ACTIVE'
|
|
114
|
+
Status.keyOf(0); // 'INACTIVE'
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
### `check(val, key)`
|
|
120
|
+
|
|
121
|
+
检测一个值是否等于指定 Key 对应的枚举值。
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
Status.check(1, "ACTIVE"); // true
|
|
125
|
+
Status.check(1, "INACTIVE"); // false
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
### `has(val)`
|
|
131
|
+
|
|
132
|
+
判断一个值是否属于该枚举(类型守卫)。
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
Status.has(1); // true
|
|
136
|
+
Status.has(99); // false
|
|
137
|
+
|
|
138
|
+
// 类型守卫:
|
|
139
|
+
const val: unknown = 1;
|
|
140
|
+
if (Status.has(val)) {
|
|
141
|
+
// val 在此分支中被收窄为 TEValue<typeof Status>
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
### `size`
|
|
148
|
+
|
|
149
|
+
获取枚举项数量。
|
|
11
150
|
|
|
12
|
-
|
|
13
|
-
//
|
|
151
|
+
```ts
|
|
152
|
+
Status.size; // 3
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
### `options(...keys?)`
|
|
158
|
+
|
|
159
|
+
获取枚举选项列表,适配 Select/Radio/Checkbox 等 UI 组件。不传参返回所有。
|
|
14
160
|
|
|
15
|
-
|
|
161
|
+
```ts
|
|
162
|
+
Status.options();
|
|
16
163
|
// [
|
|
17
|
-
// {
|
|
18
|
-
//
|
|
19
|
-
//
|
|
20
|
-
// },
|
|
21
|
-
// {
|
|
22
|
-
// "value": 22,
|
|
23
|
-
// "label": "BB-"
|
|
24
|
-
// }
|
|
164
|
+
// { value: 1, label: '激活', extra: { color: 'green' } },
|
|
165
|
+
// { value: 0, label: '未激活', extra: { color: 'gray' } },
|
|
166
|
+
// { value: 2, label: '待审核', extra: { color: 'orange' } },
|
|
25
167
|
// ]
|
|
26
168
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
169
|
+
Status.options("ACTIVE", "PENDING");
|
|
170
|
+
// 只返回指定项
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
---
|
|
174
|
+
|
|
175
|
+
### `pick(...keys)`
|
|
176
|
+
|
|
177
|
+
选取指定 Key,生成新的枚举实例。
|
|
178
|
+
|
|
179
|
+
```ts
|
|
180
|
+
const ActiveOnly = Status.pick("ACTIVE", "PENDING");
|
|
181
|
+
ActiveOnly.ACTIVE; // 1
|
|
182
|
+
ActiveOnly.PENDING; // 2
|
|
183
|
+
// ActiveOnly.INACTIVE ← 类型错误,不存在
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
---
|
|
187
|
+
|
|
188
|
+
### `omit(...keys)`
|
|
189
|
+
|
|
190
|
+
排除指定 Key,生成新的枚举实例。
|
|
191
|
+
|
|
192
|
+
```ts
|
|
193
|
+
const WithoutPending = Status.omit("PENDING");
|
|
194
|
+
WithoutPending.ACTIVE; // 1
|
|
195
|
+
WithoutPending.INACTIVE; // 0
|
|
196
|
+
// WithoutPending.PENDING ← 类型错误,不存在
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
---
|
|
31
200
|
|
|
32
|
-
|
|
33
|
-
console.log("value :>> ", value);
|
|
34
|
-
// 1
|
|
201
|
+
### `filter(predicate)`
|
|
35
202
|
|
|
36
|
-
|
|
37
|
-
console.log("values :>> ", values);
|
|
38
|
-
// [1, 2]
|
|
203
|
+
按条件过滤枚举项,返回满足条件的 options 数组。
|
|
39
204
|
|
|
40
|
-
|
|
41
|
-
|
|
205
|
+
```ts
|
|
206
|
+
// 过滤掉 INACTIVE
|
|
207
|
+
Status.filter((value) => value !== 0);
|
|
42
208
|
// [
|
|
43
|
-
// {
|
|
44
|
-
//
|
|
45
|
-
// "label": "A-",
|
|
46
|
-
// "extra": {
|
|
47
|
-
// "a": "axx"
|
|
48
|
-
// }
|
|
49
|
-
// },
|
|
50
|
-
// {
|
|
51
|
-
// "value": 2,
|
|
52
|
-
// "label": "B-",
|
|
53
|
-
// "extra": {
|
|
54
|
-
// "b": "bxx"
|
|
55
|
-
// }
|
|
56
|
-
// }
|
|
209
|
+
// { value: 1, label: '激活', extra: { color: 'green' } },
|
|
210
|
+
// { value: 2, label: '待审核', extra: { color: 'orange' } },
|
|
57
211
|
// ]
|
|
58
212
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
213
|
+
// 根据 extra 过滤
|
|
214
|
+
Status.filter((_v, _l, _k, extra) => extra?.color === "green");
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
回调参数:`(value, label, key, extra) => boolean`
|
|
218
|
+
|
|
219
|
+
---
|
|
220
|
+
|
|
221
|
+
### `forEach(callback)`
|
|
222
|
+
|
|
223
|
+
遍历所有枚举项。
|
|
224
|
+
|
|
225
|
+
```ts
|
|
226
|
+
Status.forEach((value, label, key, extra) => {
|
|
227
|
+
console.log(`${key}: ${value} - ${label}`, extra);
|
|
228
|
+
});
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
---
|
|
232
|
+
|
|
233
|
+
### `for...of` 迭代
|
|
234
|
+
|
|
235
|
+
枚举实例实现了 `Symbol.iterator`,支持 `for...of` 和展开运算符。
|
|
236
|
+
|
|
237
|
+
```ts
|
|
238
|
+
for (const { key, value, label, extra } of Status) {
|
|
239
|
+
console.log(key, value, label, extra);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const items = [...Status]; // 展开为数组
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
---
|
|
246
|
+
|
|
247
|
+
### `toMap()`
|
|
248
|
+
|
|
249
|
+
转为 `Map<value, label>`。
|
|
250
|
+
|
|
251
|
+
```ts
|
|
252
|
+
Status.toMap();
|
|
253
|
+
// Map { 1 => '激活', 0 => '未激活', 2 => '待审核' }
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
---
|
|
257
|
+
|
|
258
|
+
### `toRecord()`
|
|
259
|
+
|
|
260
|
+
转为 `{ [value]: label }` 对象。
|
|
261
|
+
|
|
262
|
+
```ts
|
|
263
|
+
Status.toRecord();
|
|
264
|
+
// { 1: '激活', 0: '未激活', 2: '待审核' }
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
---
|
|
268
|
+
|
|
269
|
+
## TypeScript 类型支持
|
|
270
|
+
|
|
271
|
+
所有 API 均提供完整的类型推导:
|
|
272
|
+
|
|
273
|
+
```ts
|
|
274
|
+
const Status = createEnum({
|
|
275
|
+
ACTIVE: [1, "激活", { color: "green" }],
|
|
276
|
+
INACTIVE: [0, "未激活", { color: "gray" }],
|
|
277
|
+
} as const);
|
|
278
|
+
|
|
279
|
+
Status.ACTIVE; // 类型为 1(字面量)
|
|
280
|
+
Status.value("ACTIVE"); // 类型为 number
|
|
281
|
+
Status.label("ACTIVE"); // 类型为 string
|
|
282
|
+
Status.extra("ACTIVE"); // 类型为 { readonly color: "green" }
|
|
283
|
+
|
|
284
|
+
// pick/omit 返回类型正确收窄
|
|
285
|
+
const picked = Status.pick("ACTIVE");
|
|
286
|
+
picked.ACTIVE; // ✅
|
|
287
|
+
picked.INACTIVE; // ❌ 类型错误
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
## 典型场景
|
|
291
|
+
|
|
292
|
+
### 配合 Select 组件
|
|
293
|
+
|
|
294
|
+
```tsx
|
|
295
|
+
const Status = createEnum({ ... } as const);
|
|
296
|
+
|
|
297
|
+
// React / Vue
|
|
298
|
+
<Select options={Status.options()} />
|
|
299
|
+
|
|
300
|
+
// 只展示部分选项
|
|
301
|
+
<Select options={Status.options("ACTIVE", "PENDING")} />
|
|
62
302
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
303
|
+
// 动态过滤
|
|
304
|
+
<Select options={Status.filter((v) => v !== 0)} />
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
### 表格列渲染
|
|
66
308
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
309
|
+
```tsx
|
|
310
|
+
const columns = [
|
|
311
|
+
{
|
|
312
|
+
title: "状态",
|
|
313
|
+
render: (val) => Status.label(val),
|
|
314
|
+
},
|
|
315
|
+
];
|
|
70
316
|
```
|
|
317
|
+
|
|
318
|
+
### 条件判断
|
|
319
|
+
|
|
320
|
+
```ts
|
|
321
|
+
if (Status.check(row.status, "ACTIVE")) {
|
|
322
|
+
// ...
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// 或者
|
|
326
|
+
if (row.status === Status.ACTIVE) {
|
|
327
|
+
// ...
|
|
328
|
+
}
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
## License
|
|
332
|
+
|
|
333
|
+
ISC
|
package/dist/create-enum.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";function t(t,e){return t===e||t!=t&&e!=e}function e(e,r){for(var n=e.length;n--;)if(t(e[n][0],r))return n;return-1}var r=Array.prototype.splice;function n(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}n.prototype.clear=function(){this.__data__=[],this.size=0},n.prototype.delete=function(t){var n=this.__data__,o=e(n,t);return!(o<0)&&(o==n.length-1?n.pop():r.call(n,o,1),--this.size,!0)},n.prototype.get=function(t){var r=this.__data__,n=e(r,t);return n<0?void 0:r[n][1]},n.prototype.has=function(t){return e(this.__data__,t)>-1},n.prototype.set=function(t,r){var n=this.__data__,o=e(n,t);return o<0?(++this.size,n.push([t,r])):n[o][1]=r,this};var o="object"==typeof global&&global&&global.Object===Object&&global,a="object"==typeof self&&self&&self.Object===Object&&self,c=o||a||Function("return this")(),u=c.Symbol,i=Object.prototype,s=i.hasOwnProperty,p=i.toString,f=u?u.toStringTag:void 0;var l=Object.prototype.toString;var b=u?u.toStringTag:void 0;function y(t){return null==t?void 0===t?"[object Undefined]":"[object Null]":b&&b in Object(t)?function(t){var e=s.call(t,f),r=t[f];try{t[f]=void 0;var n=!0}catch(t){}var o=p.call(t);return n&&(e?t[f]=r:delete t[f]),o}(t):function(t){return l.call(t)}(t)}function _(t){var e=typeof t;return null!=t&&("object"==e||"function"==e)}function j(t){if(!_(t))return!1;var e=y(t);return"[object Function]"==e||"[object GeneratorFunction]"==e||"[object AsyncFunction]"==e||"[object Proxy]"==e}var h,v=c["__core-js_shared__"],d=(h=/[^.]+$/.exec(v&&v.keys&&v.keys.IE_PROTO||""))?"Symbol(src)_1."+h:"";var g=Function.prototype.toString;function O(t){if(null!=t){try{return g.call(t)}catch(t){}try{return t+""}catch(t){}}return""}var m=/^\[object .+?Constructor\]$/,w=Function.prototype,A=Object.prototype,x=w.toString,z=A.hasOwnProperty,M=RegExp("^"+x.call(z).replace(/[\\^$.*+?()[\]{}|]/g,"\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$");function S(t){return!(!_(t)||(e=t,d&&d in e))&&(j(t)?M:m).test(O(t));var e}function E(t,e){var r=function(t,e){return null==t?void 0:t[e]}(t,e);return S(r)?r:void 0}var P=E(c,"Map"),F=E(Object,"create");var U=Object.prototype.hasOwnProperty;var I=Object.prototype.hasOwnProperty;function T(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}function k(t,e){var r,n,o=t.__data__;return("string"==(n=typeof(r=e))||"number"==n||"symbol"==n||"boolean"==n?"__proto__"!==r:null===r)?o["string"==typeof e?"string":"hash"]:o.map}function B(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}T.prototype.clear=function(){this.__data__=F?F(null):{},this.size=0},T.prototype.delete=function(t){var e=this.has(t)&&delete this.__data__[t];return this.size-=e?1:0,e},T.prototype.get=function(t){var e=this.__data__;if(F){var r=e[t];return"__lodash_hash_undefined__"===r?void 0:r}return U.call(e,t)?e[t]:void 0},T.prototype.has=function(t){var e=this.__data__;return F?void 0!==e[t]:I.call(e,t)},T.prototype.set=function(t,e){var r=this.__data__;return this.size+=this.has(t)?0:1,r[t]=F&&void 0===e?"__lodash_hash_undefined__":e,this},B.prototype.clear=function(){this.size=0,this.__data__={hash:new T,map:new(P||n),string:new T}},B.prototype.delete=function(t){var e=k(this,t).delete(t);return this.size-=e?1:0,e},B.prototype.get=function(t){return k(this,t).get(t)},B.prototype.has=function(t){return k(this,t).has(t)},B.prototype.set=function(t,e){var r=k(this,t),n=r.size;return r.set(t,e),this.size+=r.size==n?0:1,this};function D(t){var e=this.__data__=new n(t);this.size=e.size}D.prototype.clear=function(){this.__data__=new n,this.size=0},D.prototype.delete=function(t){var e=this.__data__,r=e.delete(t);return this.size=e.size,r},D.prototype.get=function(t){return this.__data__.get(t)},D.prototype.has=function(t){return this.__data__.has(t)},D.prototype.set=function(t,e){var r=this.__data__;if(r instanceof n){var o=r.__data__;if(!P||o.length<199)return o.push([t,e]),this.size=++r.size,this;r=this.__data__=new B(o)}return r.set(t,e),this.size=r.size,this};var $=function(){try{var t=E(Object,"defineProperty");return t({},"",{}),t}catch(t){}}();var L=Object.prototype.hasOwnProperty;function R(e,r,n){var o=e[r];L.call(e,r)&&t(o,n)&&(void 0!==n||r in e)||function(t,e,r){"__proto__"==e&&$?$(t,e,{configurable:!0,enumerable:!0,value:r,writable:!0}):t[e]=r}(e,r,n)}function V(t){return null!=t&&"object"==typeof t}function C(t){return V(t)&&"[object Arguments]"==y(t)}var N=Object.prototype,W=N.hasOwnProperty,q=N.propertyIsEnumerable,G=C(function(){return arguments}())?C:function(t){return V(t)&&W.call(t,"callee")&&!q.call(t,"callee")},H=Array.isArray;var J="object"==typeof exports&&exports&&!exports.nodeType&&exports,K=J&&"object"==typeof module&&module&&!module.nodeType&&module,Q=K&&K.exports===J?c.Buffer:void 0,X=(Q?Q.isBuffer:void 0)||function(){return!1},Y=/^(?:0|[1-9]\d*)$/;function Z(t,e){var r=typeof t;return!!(e=null==e?9007199254740991:e)&&("number"==r||"symbol"!=r&&Y.test(t))&&t>-1&&t%1==0&&t<e}function tt(t){return"number"==typeof t&&t>-1&&t%1==0&&t<=9007199254740991}var et={};function rt(t){return function(e){return t(e)}}et["[object Float32Array]"]=et["[object Float64Array]"]=et["[object Int8Array]"]=et["[object Int16Array]"]=et["[object Int32Array]"]=et["[object Uint8Array]"]=et["[object Uint8ClampedArray]"]=et["[object Uint16Array]"]=et["[object Uint32Array]"]=!0,et["[object Arguments]"]=et["[object Array]"]=et["[object ArrayBuffer]"]=et["[object Boolean]"]=et["[object DataView]"]=et["[object Date]"]=et["[object Error]"]=et["[object Function]"]=et["[object Map]"]=et["[object Number]"]=et["[object Object]"]=et["[object RegExp]"]=et["[object Set]"]=et["[object String]"]=et["[object WeakMap]"]=!1;var nt="object"==typeof exports&&exports&&!exports.nodeType&&exports,ot=nt&&"object"==typeof module&&module&&!module.nodeType&&module,at=ot&&ot.exports===nt&&o.process,ct=function(){try{var t=ot&&ot.require&&ot.require("util").types;return t||at&&at.binding&&at.binding("util")}catch(t){}}(),ut=ct&&ct.isTypedArray,it=ut?rt(ut):function(t){return V(t)&&tt(t.length)&&!!et[y(t)]},st=Object.prototype.hasOwnProperty;function pt(t,e){var r=H(t),n=!r&&G(t),o=!r&&!n&&X(t),a=!r&&!n&&!o&&it(t),c=r||n||o||a,u=c?function(t,e){for(var r=-1,n=Array(t);++r<t;)n[r]=e(r);return n}(t.length,String):[],i=u.length;for(var s in t)!st.call(t,s)||c&&("length"==s||o&&("offset"==s||"parent"==s)||a&&("buffer"==s||"byteLength"==s||"byteOffset"==s)||Z(s,i))||u.push(s);return u}var ft=Object.prototype;function lt(t){var e=t&&t.constructor;return t===("function"==typeof e&&e.prototype||ft)}function bt(t,e){return function(r){return t(e(r))}}var yt=bt(Object.keys,Object),_t=Object.prototype.hasOwnProperty;function jt(t){if(!lt(t))return yt(t);var e=[];for(var r in Object(t))_t.call(t,r)&&"constructor"!=r&&e.push(r);return e}function ht(t){return null!=t&&tt(t.length)&&!j(t)}function vt(t){return ht(t)?pt(t):jt(t)}var dt="object"==typeof exports&&exports&&!exports.nodeType&&exports,gt=dt&&"object"==typeof module&&module&&!module.nodeType&&module,Ot=gt&>.exports===dt?c.Buffer:void 0;Ot&&Ot.allocUnsafe;var mt=Object.prototype.propertyIsEnumerable,wt=Object.getOwnPropertySymbols,At=wt?function(t){return null==t?[]:(t=Object(t),function(t,e){for(var r=-1,n=null==t?0:t.length,o=0,a=[];++r<n;){var c=t[r];e(c,r,t)&&(a[o++]=c)}return a}(wt(t),function(e){return mt.call(t,e)}))}:function(){return[]};var xt=bt(Object.getPrototypeOf,Object);function zt(t){return function(t,e,r){var n=e(t);return H(t)?n:function(t,e){for(var r=-1,n=e.length,o=t.length;++r<n;)t[o+r]=e[r];return t}(n,r(t))}(t,vt,At)}var Mt=E(c,"DataView"),St=E(c,"Promise"),Et=E(c,"Set"),Pt=E(c,"WeakMap"),Ft="[object Map]",Ut="[object Promise]",It="[object Set]",Tt="[object WeakMap]",kt="[object DataView]",Bt=O(Mt),Dt=O(P),$t=O(St),Lt=O(Et),Rt=O(Pt),Vt=y;(Mt&&Vt(new Mt(new ArrayBuffer(1)))!=kt||P&&Vt(new P)!=Ft||St&&Vt(St.resolve())!=Ut||Et&&Vt(new Et)!=It||Pt&&Vt(new Pt)!=Tt)&&(Vt=function(t){var e=y(t),r="[object Object]"==e?t.constructor:void 0,n=r?O(r):"";if(n)switch(n){case Bt:return kt;case Dt:return Ft;case $t:return Ut;case Lt:return It;case Rt:return Tt}return e});var Ct=Object.prototype.hasOwnProperty;var Nt=c.Uint8Array;function Wt(t){var e=new t.constructor(t.byteLength);return new Nt(e).set(new Nt(t)),e}var qt=/\w*$/;var Gt=u?u.prototype:void 0,Ht=Gt?Gt.valueOf:void 0;function Jt(t,e,r){var n,o,a,c,u,i=t.constructor;switch(e){case"[object ArrayBuffer]":return Wt(t);case"[object Boolean]":case"[object Date]":return new i(+t);case"[object DataView]":return u=Wt((c=t).buffer),new c.constructor(u,c.byteOffset,c.byteLength);case"[object Float32Array]":case"[object Float64Array]":case"[object Int8Array]":case"[object Int16Array]":case"[object Int32Array]":case"[object Uint8Array]":case"[object Uint8ClampedArray]":case"[object Uint16Array]":case"[object Uint32Array]":return function(t){var e=Wt(t.buffer);return new t.constructor(e,t.byteOffset,t.length)}(t);case"[object Map]":case"[object Set]":return new i;case"[object Number]":case"[object String]":return new i(t);case"[object RegExp]":return(a=new(o=t).constructor(o.source,qt.exec(o))).lastIndex=o.lastIndex,a;case"[object Symbol]":return n=t,Ht?Object(Ht.call(n)):{}}}var Kt=Object.create,Qt=function(){function t(){}return function(e){if(!_(e))return{};if(Kt)return Kt(e);t.prototype=e;var r=new t;return t.prototype=void 0,r}}();var Xt=ct&&ct.isMap,Yt=Xt?rt(Xt):function(t){return V(t)&&"[object Map]"==Vt(t)};var Zt=ct&&ct.isSet,te=Zt?rt(Zt):function(t){return V(t)&&"[object Set]"==Vt(t)},ee="[object Arguments]",re="[object Function]",ne="[object Object]",oe={};function ae(t,e,r,n,o,a){var c;if(void 0!==c)return c;if(!_(t))return t;var u=H(t);if(u)c=function(t){var e=t.length,r=new t.constructor(e);return e&&"string"==typeof t[0]&&Ct.call(t,"index")&&(r.index=t.index,r.input=t.input),r}(t);else{var i=Vt(t),s=i==re||"[object GeneratorFunction]"==i;if(X(t))return t.slice();if(i==ne||i==ee||s&&!o)c=s?{}:function(t){return"function"!=typeof t.constructor||lt(t)?{}:Qt(xt(t))}(t);else{if(!oe[i])return o?t:{};c=Jt(t,i)}}a||(a=new D);var p=a.get(t);if(p)return p;a.set(t,c),te(t)?t.forEach(function(n){c.add(ae(n,e,r,n,t,a))}):Yt(t)&&t.forEach(function(n,o){c.set(o,ae(n,e,r,o,t,a))});var f=u?void 0:zt(t);return function(t,e){for(var r=-1,n=null==t?0:t.length;++r<n&&!1!==e(t[r],r,t););}(f||t,function(n,o){f&&(n=t[o=n]),R(c,o,ae(n,e,r,o,t,a))}),c}oe[ee]=oe["[object Array]"]=oe["[object ArrayBuffer]"]=oe["[object DataView]"]=oe["[object Boolean]"]=oe["[object Date]"]=oe["[object Float32Array]"]=oe["[object Float64Array]"]=oe["[object Int8Array]"]=oe["[object Int16Array]"]=oe["[object Int32Array]"]=oe["[object Map]"]=oe["[object Number]"]=oe[ne]=oe["[object RegExp]"]=oe["[object Set]"]=oe["[object String]"]=oe["[object Symbol]"]=oe["[object Uint8Array]"]=oe["[object Uint8ClampedArray]"]=oe["[object Uint16Array]"]=oe["[object Uint32Array]"]=!0,oe["[object Error]"]=oe[re]=oe["[object WeakMap]"]=!1;var ce=Object.prototype.hasOwnProperty;var ue=Function.prototype,ie=Object.prototype,se=ue.toString,pe=ie.hasOwnProperty,fe=se.call(Object);function le(t){return!function(t){if(null==t)return!0;if(ht(t)&&(H(t)||"string"==typeof t||"function"==typeof t.splice||X(t)||it(t)||G(t)))return!t.length;var e=Vt(t);if("[object Map]"==e||"[object Set]"==e)return!t.size;if(lt(t))return!jt(t).length;for(var r in t)if(ce.call(t,r))return!1;return!0}(t)&&t.every(t=>"string"==typeof t)}class be{__enumMap__;__enumLabelMap__;__enumExtraMap__;constructor(t){if(!function(t){if(!V(t)||"[object Object]"!=y(t))return!1;var e=xt(t);if(null===e)return!0;var r=pe.call(e,"constructor")&&e.constructor;return"function"==typeof r&&r instanceof r&&se.call(r)==fe}(t))throw new TypeError("初始化参数值必须是一个object!");this.#t(ae(t,5))}#t(t){const e={},r={};Object.keys(t).forEach(n=>{const o=t[n];if(!H(o))throw new TypeError("初始化参数对象字段的值必是一个array!");this[n]=o[0],e[n]=o[1],e[o[0]]=o[1],r[n]=o[2],r[o[0]]=o[2]}),this.__enumMap__=Object.freeze(t),this.__enumLabelMap__=Object.freeze(e),this.__enumExtraMap__=Object.freeze(r)}value(t){return this[t]}values(...t){let e=Object.keys(this.__enumMap__);return le(t)&&(e=Array.from(t)),e.map(t=>this.value(t))}options(...t){let e=Object.keys(this.__enumMap__);return le(t)&&(e=Array.from(t)),e.map(t=>({value:this.value(t),label:this.label(t),extra:this.extra(t)}))}label(t){return this.__enumLabelMap__[t]}extra(t){return this.__enumExtraMap__[t]}check(t,e){return this.value(e)===t}}module.exports=t=>{const e=new be(t);return Object.freeze(e)};
|
|
1
|
+
"use strict";class e{__enumMap__;__enumLabelMap__;__enumExtraMap__;__valueKeyMap__;constructor(e){if(null===e||"object"!=typeof e||Array.isArray(e))throw new TypeError("初始化参数值必须是一个 object!");const t={},_={},r={},n=Object.keys(e);for(let a=0;a<n.length;a++){const s=n[a],u=e[s];if(!Array.isArray(u))throw new TypeError(`初始化参数对象字段 "${s}" 的值必须是一个 array!`);this[s]=u[0],t[s]=u[1],t[u[0]]=u[1],_[s]=u[2],_[u[0]]=u[2],r[u[0]]=s}this.__enumMap__=Object.freeze(e),this.__enumLabelMap__=Object.freeze(t),this.__enumExtraMap__=Object.freeze(_),this.__valueKeyMap__=Object.freeze(r)}keys(){return Object.keys(this.__enumMap__)}value(e){return this[e]}values(...e){return(e.length>0?e:this.keys()).map(e=>this.value(e))}options(...e){return(e.length>0?e:this.keys()).map(e=>({value:this.value(e),label:this.label(e),extra:this.extra(e)}))}label(e){return this.__enumLabelMap__[e]}extra(e){return this.__enumExtraMap__[e]}keyOf(e){return this.__valueKeyMap__[e]}check(e,t){return this.value(t)===e}has(e){return this.__valueKeyMap__.hasOwnProperty(e)}get size(){return Object.keys(this.__enumMap__).length}omit(...e){const _={},r=new Set(e),n=Object.keys(this.__enumMap__);for(let e=0;e<n.length;e++){const t=n[e];r.has(t)||(_[t]=this.__enumMap__[t])}return t(_)}pick(...e){const _={};for(let t=0;t<e.length;t++){const r=e[t];this.__enumMap__.hasOwnProperty(r)&&(_[r]=this.__enumMap__[r])}return t(_)}forEach(e){const t=Object.keys(this.__enumMap__);for(let _=0;_<t.length;_++){const r=t[_];e(this.value(r),this.__enumLabelMap__[r],r,this.__enumExtraMap__[r])}}toMap(){const e=new Map,t=Object.keys(this.__enumMap__);for(let _=0;_<t.length;_++){const r=t[_],n=this.__enumMap__[r];e.set(n[0],n[1])}return e}toRecord(){const e={},t=Object.keys(this.__enumMap__);for(let _=0;_<t.length;_++){const r=t[_],n=this.__enumMap__[r];e[n[0]]=n[1]}return e}filter(e){const t=[],_=Object.keys(this.__enumMap__);for(let r=0;r<_.length;r++){const n=_[r],a=this.value(n),s=this.__enumLabelMap__[n],u=this.__enumExtraMap__[n];e(a,s,n,u)&&t.push({value:a,label:s,extra:u})}return t}[Symbol.iterator](){const e=Object.keys(this.__enumMap__);let t=0;const _=this;return{next(){if(t<e.length){const r=e[t++];return{done:!1,value:{key:r,value:_.value(r),label:_.__enumLabelMap__[r],extra:_.__enumExtraMap__[r]}}}return{done:!0,value:void 0}}}}}const t=t=>{const _=new e(t);return Object.freeze(_)};module.exports=t;
|
package/dist/create-enum.es.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function t(t,e){return t===e||t!=t&&e!=e}function e(e,r){for(var n=e.length;n--;)if(t(e[n][0],r))return n;return-1}var r=Array.prototype.splice;function n(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}n.prototype.clear=function(){this.__data__=[],this.size=0},n.prototype.delete=function(t){var n=this.__data__,o=e(n,t);return!(o<0)&&(o==n.length-1?n.pop():r.call(n,o,1),--this.size,!0)},n.prototype.get=function(t){var r=this.__data__,n=e(r,t);return n<0?void 0:r[n][1]},n.prototype.has=function(t){return e(this.__data__,t)>-1},n.prototype.set=function(t,r){var n=this.__data__,o=e(n,t);return o<0?(++this.size,n.push([t,r])):n[o][1]=r,this};var o="object"==typeof global&&global&&global.Object===Object&&global,a="object"==typeof self&&self&&self.Object===Object&&self,c=o||a||Function("return this")(),u=c.Symbol,i=Object.prototype,s=i.hasOwnProperty,p=i.toString,f=u?u.toStringTag:void 0;var l=Object.prototype.toString;var b=u?u.toStringTag:void 0;function y(t){return null==t?void 0===t?"[object Undefined]":"[object Null]":b&&b in Object(t)?function(t){var e=s.call(t,f),r=t[f];try{t[f]=void 0;var n=!0}catch(t){}var o=p.call(t);return n&&(e?t[f]=r:delete t[f]),o}(t):function(t){return l.call(t)}(t)}function _(t){var e=typeof t;return null!=t&&("object"==e||"function"==e)}function j(t){if(!_(t))return!1;var e=y(t);return"[object Function]"==e||"[object GeneratorFunction]"==e||"[object AsyncFunction]"==e||"[object Proxy]"==e}var h,v=c["__core-js_shared__"],d=(h=/[^.]+$/.exec(v&&v.keys&&v.keys.IE_PROTO||""))?"Symbol(src)_1."+h:"";var g=Function.prototype.toString;function O(t){if(null!=t){try{return g.call(t)}catch(t){}try{return t+""}catch(t){}}return""}var m=/^\[object .+?Constructor\]$/,w=Function.prototype,A=Object.prototype,x=w.toString,z=A.hasOwnProperty,M=RegExp("^"+x.call(z).replace(/[\\^$.*+?()[\]{}|]/g,"\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$");function S(t){return!(!_(t)||(e=t,d&&d in e))&&(j(t)?M:m).test(O(t));var e}function E(t,e){var r=function(t,e){return null==t?void 0:t[e]}(t,e);return S(r)?r:void 0}var P=E(c,"Map"),F=E(Object,"create");var U=Object.prototype.hasOwnProperty;var I=Object.prototype.hasOwnProperty;function T(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}function k(t,e){var r,n,o=t.__data__;return("string"==(n=typeof(r=e))||"number"==n||"symbol"==n||"boolean"==n?"__proto__"!==r:null===r)?o["string"==typeof e?"string":"hash"]:o.map}function B(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}T.prototype.clear=function(){this.__data__=F?F(null):{},this.size=0},T.prototype.delete=function(t){var e=this.has(t)&&delete this.__data__[t];return this.size-=e?1:0,e},T.prototype.get=function(t){var e=this.__data__;if(F){var r=e[t];return"__lodash_hash_undefined__"===r?void 0:r}return U.call(e,t)?e[t]:void 0},T.prototype.has=function(t){var e=this.__data__;return F?void 0!==e[t]:I.call(e,t)},T.prototype.set=function(t,e){var r=this.__data__;return this.size+=this.has(t)?0:1,r[t]=F&&void 0===e?"__lodash_hash_undefined__":e,this},B.prototype.clear=function(){this.size=0,this.__data__={hash:new T,map:new(P||n),string:new T}},B.prototype.delete=function(t){var e=k(this,t).delete(t);return this.size-=e?1:0,e},B.prototype.get=function(t){return k(this,t).get(t)},B.prototype.has=function(t){return k(this,t).has(t)},B.prototype.set=function(t,e){var r=k(this,t),n=r.size;return r.set(t,e),this.size+=r.size==n?0:1,this};function D(t){var e=this.__data__=new n(t);this.size=e.size}D.prototype.clear=function(){this.__data__=new n,this.size=0},D.prototype.delete=function(t){var e=this.__data__,r=e.delete(t);return this.size=e.size,r},D.prototype.get=function(t){return this.__data__.get(t)},D.prototype.has=function(t){return this.__data__.has(t)},D.prototype.set=function(t,e){var r=this.__data__;if(r instanceof n){var o=r.__data__;if(!P||o.length<199)return o.push([t,e]),this.size=++r.size,this;r=this.__data__=new B(o)}return r.set(t,e),this.size=r.size,this};var $=function(){try{var t=E(Object,"defineProperty");return t({},"",{}),t}catch(t){}}();var L=Object.prototype.hasOwnProperty;function R(e,r,n){var o=e[r];L.call(e,r)&&t(o,n)&&(void 0!==n||r in e)||function(t,e,r){"__proto__"==e&&$?$(t,e,{configurable:!0,enumerable:!0,value:r,writable:!0}):t[e]=r}(e,r,n)}function V(t){return null!=t&&"object"==typeof t}function C(t){return V(t)&&"[object Arguments]"==y(t)}var N=Object.prototype,W=N.hasOwnProperty,q=N.propertyIsEnumerable,G=C(function(){return arguments}())?C:function(t){return V(t)&&W.call(t,"callee")&&!q.call(t,"callee")},H=Array.isArray;var J="object"==typeof exports&&exports&&!exports.nodeType&&exports,K=J&&"object"==typeof module&&module&&!module.nodeType&&module,Q=K&&K.exports===J?c.Buffer:void 0,X=(Q?Q.isBuffer:void 0)||function(){return!1},Y=/^(?:0|[1-9]\d*)$/;function Z(t,e){var r=typeof t;return!!(e=null==e?9007199254740991:e)&&("number"==r||"symbol"!=r&&Y.test(t))&&t>-1&&t%1==0&&t<e}function tt(t){return"number"==typeof t&&t>-1&&t%1==0&&t<=9007199254740991}var et={};function rt(t){return function(e){return t(e)}}et["[object Float32Array]"]=et["[object Float64Array]"]=et["[object Int8Array]"]=et["[object Int16Array]"]=et["[object Int32Array]"]=et["[object Uint8Array]"]=et["[object Uint8ClampedArray]"]=et["[object Uint16Array]"]=et["[object Uint32Array]"]=!0,et["[object Arguments]"]=et["[object Array]"]=et["[object ArrayBuffer]"]=et["[object Boolean]"]=et["[object DataView]"]=et["[object Date]"]=et["[object Error]"]=et["[object Function]"]=et["[object Map]"]=et["[object Number]"]=et["[object Object]"]=et["[object RegExp]"]=et["[object Set]"]=et["[object String]"]=et["[object WeakMap]"]=!1;var nt="object"==typeof exports&&exports&&!exports.nodeType&&exports,ot=nt&&"object"==typeof module&&module&&!module.nodeType&&module,at=ot&&ot.exports===nt&&o.process,ct=function(){try{var t=ot&&ot.require&&ot.require("util").types;return t||at&&at.binding&&at.binding("util")}catch(t){}}(),ut=ct&&ct.isTypedArray,it=ut?rt(ut):function(t){return V(t)&&tt(t.length)&&!!et[y(t)]},st=Object.prototype.hasOwnProperty;function pt(t,e){var r=H(t),n=!r&&G(t),o=!r&&!n&&X(t),a=!r&&!n&&!o&&it(t),c=r||n||o||a,u=c?function(t,e){for(var r=-1,n=Array(t);++r<t;)n[r]=e(r);return n}(t.length,String):[],i=u.length;for(var s in t)!st.call(t,s)||c&&("length"==s||o&&("offset"==s||"parent"==s)||a&&("buffer"==s||"byteLength"==s||"byteOffset"==s)||Z(s,i))||u.push(s);return u}var ft=Object.prototype;function lt(t){var e=t&&t.constructor;return t===("function"==typeof e&&e.prototype||ft)}function bt(t,e){return function(r){return t(e(r))}}var yt=bt(Object.keys,Object),_t=Object.prototype.hasOwnProperty;function jt(t){if(!lt(t))return yt(t);var e=[];for(var r in Object(t))_t.call(t,r)&&"constructor"!=r&&e.push(r);return e}function ht(t){return null!=t&&tt(t.length)&&!j(t)}function vt(t){return ht(t)?pt(t):jt(t)}var dt="object"==typeof exports&&exports&&!exports.nodeType&&exports,gt=dt&&"object"==typeof module&&module&&!module.nodeType&&module,Ot=gt&>.exports===dt?c.Buffer:void 0;Ot&&Ot.allocUnsafe;var mt=Object.prototype.propertyIsEnumerable,wt=Object.getOwnPropertySymbols,At=wt?function(t){return null==t?[]:(t=Object(t),function(t,e){for(var r=-1,n=null==t?0:t.length,o=0,a=[];++r<n;){var c=t[r];e(c,r,t)&&(a[o++]=c)}return a}(wt(t),function(e){return mt.call(t,e)}))}:function(){return[]};var xt=bt(Object.getPrototypeOf,Object);function zt(t){return function(t,e,r){var n=e(t);return H(t)?n:function(t,e){for(var r=-1,n=e.length,o=t.length;++r<n;)t[o+r]=e[r];return t}(n,r(t))}(t,vt,At)}var Mt=E(c,"DataView"),St=E(c,"Promise"),Et=E(c,"Set"),Pt=E(c,"WeakMap"),Ft="[object Map]",Ut="[object Promise]",It="[object Set]",Tt="[object WeakMap]",kt="[object DataView]",Bt=O(Mt),Dt=O(P),$t=O(St),Lt=O(Et),Rt=O(Pt),Vt=y;(Mt&&Vt(new Mt(new ArrayBuffer(1)))!=kt||P&&Vt(new P)!=Ft||St&&Vt(St.resolve())!=Ut||Et&&Vt(new Et)!=It||Pt&&Vt(new Pt)!=Tt)&&(Vt=function(t){var e=y(t),r="[object Object]"==e?t.constructor:void 0,n=r?O(r):"";if(n)switch(n){case Bt:return kt;case Dt:return Ft;case $t:return Ut;case Lt:return It;case Rt:return Tt}return e});var Ct=Object.prototype.hasOwnProperty;var Nt=c.Uint8Array;function Wt(t){var e=new t.constructor(t.byteLength);return new Nt(e).set(new Nt(t)),e}var qt=/\w*$/;var Gt=u?u.prototype:void 0,Ht=Gt?Gt.valueOf:void 0;function Jt(t,e,r){var n,o,a,c,u,i=t.constructor;switch(e){case"[object ArrayBuffer]":return Wt(t);case"[object Boolean]":case"[object Date]":return new i(+t);case"[object DataView]":return u=Wt((c=t).buffer),new c.constructor(u,c.byteOffset,c.byteLength);case"[object Float32Array]":case"[object Float64Array]":case"[object Int8Array]":case"[object Int16Array]":case"[object Int32Array]":case"[object Uint8Array]":case"[object Uint8ClampedArray]":case"[object Uint16Array]":case"[object Uint32Array]":return function(t){var e=Wt(t.buffer);return new t.constructor(e,t.byteOffset,t.length)}(t);case"[object Map]":case"[object Set]":return new i;case"[object Number]":case"[object String]":return new i(t);case"[object RegExp]":return(a=new(o=t).constructor(o.source,qt.exec(o))).lastIndex=o.lastIndex,a;case"[object Symbol]":return n=t,Ht?Object(Ht.call(n)):{}}}var Kt=Object.create,Qt=function(){function t(){}return function(e){if(!_(e))return{};if(Kt)return Kt(e);t.prototype=e;var r=new t;return t.prototype=void 0,r}}();var Xt=ct&&ct.isMap,Yt=Xt?rt(Xt):function(t){return V(t)&&"[object Map]"==Vt(t)};var Zt=ct&&ct.isSet,te=Zt?rt(Zt):function(t){return V(t)&&"[object Set]"==Vt(t)},ee="[object Arguments]",re="[object Function]",ne="[object Object]",oe={};function ae(t,e,r,n,o,a){var c;if(void 0!==c)return c;if(!_(t))return t;var u=H(t);if(u)c=function(t){var e=t.length,r=new t.constructor(e);return e&&"string"==typeof t[0]&&Ct.call(t,"index")&&(r.index=t.index,r.input=t.input),r}(t);else{var i=Vt(t),s=i==re||"[object GeneratorFunction]"==i;if(X(t))return t.slice();if(i==ne||i==ee||s&&!o)c=s?{}:function(t){return"function"!=typeof t.constructor||lt(t)?{}:Qt(xt(t))}(t);else{if(!oe[i])return o?t:{};c=Jt(t,i)}}a||(a=new D);var p=a.get(t);if(p)return p;a.set(t,c),te(t)?t.forEach(function(n){c.add(ae(n,e,r,n,t,a))}):Yt(t)&&t.forEach(function(n,o){c.set(o,ae(n,e,r,o,t,a))});var f=u?void 0:zt(t);return function(t,e){for(var r=-1,n=null==t?0:t.length;++r<n&&!1!==e(t[r],r,t););}(f||t,function(n,o){f&&(n=t[o=n]),R(c,o,ae(n,e,r,o,t,a))}),c}oe[ee]=oe["[object Array]"]=oe["[object ArrayBuffer]"]=oe["[object DataView]"]=oe["[object Boolean]"]=oe["[object Date]"]=oe["[object Float32Array]"]=oe["[object Float64Array]"]=oe["[object Int8Array]"]=oe["[object Int16Array]"]=oe["[object Int32Array]"]=oe["[object Map]"]=oe["[object Number]"]=oe[ne]=oe["[object RegExp]"]=oe["[object Set]"]=oe["[object String]"]=oe["[object Symbol]"]=oe["[object Uint8Array]"]=oe["[object Uint8ClampedArray]"]=oe["[object Uint16Array]"]=oe["[object Uint32Array]"]=!0,oe["[object Error]"]=oe[re]=oe["[object WeakMap]"]=!1;var ce=Object.prototype.hasOwnProperty;var ue=Function.prototype,ie=Object.prototype,se=ue.toString,pe=ie.hasOwnProperty,fe=se.call(Object);function le(t){return!function(t){if(null==t)return!0;if(ht(t)&&(H(t)||"string"==typeof t||"function"==typeof t.splice||X(t)||it(t)||G(t)))return!t.length;var e=Vt(t);if("[object Map]"==e||"[object Set]"==e)return!t.size;if(lt(t))return!jt(t).length;for(var r in t)if(ce.call(t,r))return!1;return!0}(t)&&t.every(t=>"string"==typeof t)}class be{__enumMap__;__enumLabelMap__;__enumExtraMap__;constructor(t){if(!function(t){if(!V(t)||"[object Object]"!=y(t))return!1;var e=xt(t);if(null===e)return!0;var r=pe.call(e,"constructor")&&e.constructor;return"function"==typeof r&&r instanceof r&&se.call(r)==fe}(t))throw new TypeError("初始化参数值必须是一个object!");this.#t(ae(t,5))}#t(t){const e={},r={};Object.keys(t).forEach(n=>{const o=t[n];if(!H(o))throw new TypeError("初始化参数对象字段的值必是一个array!");this[n]=o[0],e[n]=o[1],e[o[0]]=o[1],r[n]=o[2],r[o[0]]=o[2]}),this.__enumMap__=Object.freeze(t),this.__enumLabelMap__=Object.freeze(e),this.__enumExtraMap__=Object.freeze(r)}value(t){return this[t]}values(...t){let e=Object.keys(this.__enumMap__);return le(t)&&(e=Array.from(t)),e.map(t=>this.value(t))}options(...t){let e=Object.keys(this.__enumMap__);return le(t)&&(e=Array.from(t)),e.map(t=>({value:this.value(t),label:this.label(t),extra:this.extra(t)}))}label(t){return this.__enumLabelMap__[t]}extra(t){return this.__enumExtraMap__[t]}check(t,e){return this.value(e)===t}}const ye=t=>{const e=new be(t);return Object.freeze(e)};export{ye as default};
|
|
1
|
+
class e{__enumMap__;__enumLabelMap__;__enumExtraMap__;__valueKeyMap__;constructor(e){if(null===e||"object"!=typeof e||Array.isArray(e))throw new TypeError("初始化参数值必须是一个 object!");const t={},_={},r={},a=Object.keys(e);for(let n=0;n<a.length;n++){const s=a[n],u=e[s];if(!Array.isArray(u))throw new TypeError(`初始化参数对象字段 "${s}" 的值必须是一个 array!`);this[s]=u[0],t[s]=u[1],t[u[0]]=u[1],_[s]=u[2],_[u[0]]=u[2],r[u[0]]=s}this.__enumMap__=Object.freeze(e),this.__enumLabelMap__=Object.freeze(t),this.__enumExtraMap__=Object.freeze(_),this.__valueKeyMap__=Object.freeze(r)}keys(){return Object.keys(this.__enumMap__)}value(e){return this[e]}values(...e){return(e.length>0?e:this.keys()).map(e=>this.value(e))}options(...e){return(e.length>0?e:this.keys()).map(e=>({value:this.value(e),label:this.label(e),extra:this.extra(e)}))}label(e){return this.__enumLabelMap__[e]}extra(e){return this.__enumExtraMap__[e]}keyOf(e){return this.__valueKeyMap__[e]}check(e,t){return this.value(t)===e}has(e){return this.__valueKeyMap__.hasOwnProperty(e)}get size(){return Object.keys(this.__enumMap__).length}omit(...e){const _={},r=new Set(e),a=Object.keys(this.__enumMap__);for(let e=0;e<a.length;e++){const t=a[e];r.has(t)||(_[t]=this.__enumMap__[t])}return t(_)}pick(...e){const _={};for(let t=0;t<e.length;t++){const r=e[t];this.__enumMap__.hasOwnProperty(r)&&(_[r]=this.__enumMap__[r])}return t(_)}forEach(e){const t=Object.keys(this.__enumMap__);for(let _=0;_<t.length;_++){const r=t[_];e(this.value(r),this.__enumLabelMap__[r],r,this.__enumExtraMap__[r])}}toMap(){const e=new Map,t=Object.keys(this.__enumMap__);for(let _=0;_<t.length;_++){const r=t[_],a=this.__enumMap__[r];e.set(a[0],a[1])}return e}toRecord(){const e={},t=Object.keys(this.__enumMap__);for(let _=0;_<t.length;_++){const r=t[_],a=this.__enumMap__[r];e[a[0]]=a[1]}return e}filter(e){const t=[],_=Object.keys(this.__enumMap__);for(let r=0;r<_.length;r++){const a=_[r],n=this.value(a),s=this.__enumLabelMap__[a],u=this.__enumExtraMap__[a];e(n,s,a,u)&&t.push({value:n,label:s,extra:u})}return t}[Symbol.iterator](){const e=Object.keys(this.__enumMap__);let t=0;const _=this;return{next(){if(t<e.length){const r=e[t++];return{done:!1,value:{key:r,value:_.value(r),label:_.__enumLabelMap__[r],extra:_.__enumExtraMap__[r]}}}return{done:!0,value:void 0}}}}}const t=t=>{const _=new e(t);return Object.freeze(_)};export{t as default};
|
package/dist/create-enum.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).$Enum=e()}(this,function(){"use strict";function t(t,e){return t===e||t!=t&&e!=e}function e(e,r){for(var n=e.length;n--;)if(t(e[n][0],r))return n;return-1}var r=Array.prototype.splice;function n(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}n.prototype.clear=function(){this.__data__=[],this.size=0},n.prototype.delete=function(t){var n=this.__data__,o=e(n,t);return!(o<0)&&(o==n.length-1?n.pop():r.call(n,o,1),--this.size,!0)},n.prototype.get=function(t){var r=this.__data__,n=e(r,t);return n<0?void 0:r[n][1]},n.prototype.has=function(t){return e(this.__data__,t)>-1},n.prototype.set=function(t,r){var n=this.__data__,o=e(n,t);return o<0?(++this.size,n.push([t,r])):n[o][1]=r,this};var o="object"==typeof global&&global&&global.Object===Object&&global,a="object"==typeof self&&self&&self.Object===Object&&self,c=o||a||Function("return this")(),i=c.Symbol,u=Object.prototype,s=u.hasOwnProperty,f=u.toString,p=i?i.toStringTag:void 0;var l=Object.prototype.toString;var b=i?i.toStringTag:void 0;function y(t){return null==t?void 0===t?"[object Undefined]":"[object Null]":b&&b in Object(t)?function(t){var e=s.call(t,p),r=t[p];try{t[p]=void 0;var n=!0}catch(t){}var o=f.call(t);return n&&(e?t[p]=r:delete t[p]),o}(t):function(t){return l.call(t)}(t)}function _(t){var e=typeof t;return null!=t&&("object"==e||"function"==e)}function j(t){if(!_(t))return!1;var e=y(t);return"[object Function]"==e||"[object GeneratorFunction]"==e||"[object AsyncFunction]"==e||"[object Proxy]"==e}var h,v=c["__core-js_shared__"],d=(h=/[^.]+$/.exec(v&&v.keys&&v.keys.IE_PROTO||""))?"Symbol(src)_1."+h:"";var g=Function.prototype.toString;function O(t){if(null!=t){try{return g.call(t)}catch(t){}try{return t+""}catch(t){}}return""}var m=/^\[object .+?Constructor\]$/,w=Function.prototype,A=Object.prototype,x=w.toString,z=A.hasOwnProperty,M=RegExp("^"+x.call(z).replace(/[\\^$.*+?()[\]{}|]/g,"\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$");function S(t){return!(!_(t)||(e=t,d&&d in e))&&(j(t)?M:m).test(O(t));var e}function E(t,e){var r=function(t,e){return null==t?void 0:t[e]}(t,e);return S(r)?r:void 0}var P=E(c,"Map"),F=E(Object,"create");var U=Object.prototype.hasOwnProperty;var I=Object.prototype.hasOwnProperty;function T(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}function k(t,e){var r,n,o=t.__data__;return("string"==(n=typeof(r=e))||"number"==n||"symbol"==n||"boolean"==n?"__proto__"!==r:null===r)?o["string"==typeof e?"string":"hash"]:o.map}function B(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}T.prototype.clear=function(){this.__data__=F?F(null):{},this.size=0},T.prototype.delete=function(t){var e=this.has(t)&&delete this.__data__[t];return this.size-=e?1:0,e},T.prototype.get=function(t){var e=this.__data__;if(F){var r=e[t];return"__lodash_hash_undefined__"===r?void 0:r}return U.call(e,t)?e[t]:void 0},T.prototype.has=function(t){var e=this.__data__;return F?void 0!==e[t]:I.call(e,t)},T.prototype.set=function(t,e){var r=this.__data__;return this.size+=this.has(t)?0:1,r[t]=F&&void 0===e?"__lodash_hash_undefined__":e,this},B.prototype.clear=function(){this.size=0,this.__data__={hash:new T,map:new(P||n),string:new T}},B.prototype.delete=function(t){var e=k(this,t).delete(t);return this.size-=e?1:0,e},B.prototype.get=function(t){return k(this,t).get(t)},B.prototype.has=function(t){return k(this,t).has(t)},B.prototype.set=function(t,e){var r=k(this,t),n=r.size;return r.set(t,e),this.size+=r.size==n?0:1,this};function $(t){var e=this.__data__=new n(t);this.size=e.size}$.prototype.clear=function(){this.__data__=new n,this.size=0},$.prototype.delete=function(t){var e=this.__data__,r=e.delete(t);return this.size=e.size,r},$.prototype.get=function(t){return this.__data__.get(t)},$.prototype.has=function(t){return this.__data__.has(t)},$.prototype.set=function(t,e){var r=this.__data__;if(r instanceof n){var o=r.__data__;if(!P||o.length<199)return o.push([t,e]),this.size=++r.size,this;r=this.__data__=new B(o)}return r.set(t,e),this.size=r.size,this};var D=function(){try{var t=E(Object,"defineProperty");return t({},"",{}),t}catch(t){}}();var L=Object.prototype.hasOwnProperty;function R(e,r,n){var o=e[r];L.call(e,r)&&t(o,n)&&(void 0!==n||r in e)||function(t,e,r){"__proto__"==e&&D?D(t,e,{configurable:!0,enumerable:!0,value:r,writable:!0}):t[e]=r}(e,r,n)}function V(t){return null!=t&&"object"==typeof t}function C(t){return V(t)&&"[object Arguments]"==y(t)}var N=Object.prototype,W=N.hasOwnProperty,q=N.propertyIsEnumerable,G=C(function(){return arguments}())?C:function(t){return V(t)&&W.call(t,"callee")&&!q.call(t,"callee")},H=Array.isArray;var J="object"==typeof exports&&exports&&!exports.nodeType&&exports,K=J&&"object"==typeof module&&module&&!module.nodeType&&module,Q=K&&K.exports===J?c.Buffer:void 0,X=(Q?Q.isBuffer:void 0)||function(){return!1},Y=/^(?:0|[1-9]\d*)$/;function Z(t,e){var r=typeof t;return!!(e=null==e?9007199254740991:e)&&("number"==r||"symbol"!=r&&Y.test(t))&&t>-1&&t%1==0&&t<e}function tt(t){return"number"==typeof t&&t>-1&&t%1==0&&t<=9007199254740991}var et={};function rt(t){return function(e){return t(e)}}et["[object Float32Array]"]=et["[object Float64Array]"]=et["[object Int8Array]"]=et["[object Int16Array]"]=et["[object Int32Array]"]=et["[object Uint8Array]"]=et["[object Uint8ClampedArray]"]=et["[object Uint16Array]"]=et["[object Uint32Array]"]=!0,et["[object Arguments]"]=et["[object Array]"]=et["[object ArrayBuffer]"]=et["[object Boolean]"]=et["[object DataView]"]=et["[object Date]"]=et["[object Error]"]=et["[object Function]"]=et["[object Map]"]=et["[object Number]"]=et["[object Object]"]=et["[object RegExp]"]=et["[object Set]"]=et["[object String]"]=et["[object WeakMap]"]=!1;var nt="object"==typeof exports&&exports&&!exports.nodeType&&exports,ot=nt&&"object"==typeof module&&module&&!module.nodeType&&module,at=ot&&ot.exports===nt&&o.process,ct=function(){try{var t=ot&&ot.require&&ot.require("util").types;return t||at&&at.binding&&at.binding("util")}catch(t){}}(),it=ct&&ct.isTypedArray,ut=it?rt(it):function(t){return V(t)&&tt(t.length)&&!!et[y(t)]},st=Object.prototype.hasOwnProperty;function ft(t,e){var r=H(t),n=!r&&G(t),o=!r&&!n&&X(t),a=!r&&!n&&!o&&ut(t),c=r||n||o||a,i=c?function(t,e){for(var r=-1,n=Array(t);++r<t;)n[r]=e(r);return n}(t.length,String):[],u=i.length;for(var s in t)!st.call(t,s)||c&&("length"==s||o&&("offset"==s||"parent"==s)||a&&("buffer"==s||"byteLength"==s||"byteOffset"==s)||Z(s,u))||i.push(s);return i}var pt=Object.prototype;function lt(t){var e=t&&t.constructor;return t===("function"==typeof e&&e.prototype||pt)}function bt(t,e){return function(r){return t(e(r))}}var yt=bt(Object.keys,Object),_t=Object.prototype.hasOwnProperty;function jt(t){if(!lt(t))return yt(t);var e=[];for(var r in Object(t))_t.call(t,r)&&"constructor"!=r&&e.push(r);return e}function ht(t){return null!=t&&tt(t.length)&&!j(t)}function vt(t){return ht(t)?ft(t):jt(t)}var dt="object"==typeof exports&&exports&&!exports.nodeType&&exports,gt=dt&&"object"==typeof module&&module&&!module.nodeType&&module,Ot=gt&>.exports===dt?c.Buffer:void 0;Ot&&Ot.allocUnsafe;var mt=Object.prototype.propertyIsEnumerable,wt=Object.getOwnPropertySymbols,At=wt?function(t){return null==t?[]:(t=Object(t),function(t,e){for(var r=-1,n=null==t?0:t.length,o=0,a=[];++r<n;){var c=t[r];e(c,r,t)&&(a[o++]=c)}return a}(wt(t),function(e){return mt.call(t,e)}))}:function(){return[]};var xt=bt(Object.getPrototypeOf,Object);function zt(t){return function(t,e,r){var n=e(t);return H(t)?n:function(t,e){for(var r=-1,n=e.length,o=t.length;++r<n;)t[o+r]=e[r];return t}(n,r(t))}(t,vt,At)}var Mt=E(c,"DataView"),St=E(c,"Promise"),Et=E(c,"Set"),Pt=E(c,"WeakMap"),Ft="[object Map]",Ut="[object Promise]",It="[object Set]",Tt="[object WeakMap]",kt="[object DataView]",Bt=O(Mt),$t=O(P),Dt=O(St),Lt=O(Et),Rt=O(Pt),Vt=y;(Mt&&Vt(new Mt(new ArrayBuffer(1)))!=kt||P&&Vt(new P)!=Ft||St&&Vt(St.resolve())!=Ut||Et&&Vt(new Et)!=It||Pt&&Vt(new Pt)!=Tt)&&(Vt=function(t){var e=y(t),r="[object Object]"==e?t.constructor:void 0,n=r?O(r):"";if(n)switch(n){case Bt:return kt;case $t:return Ft;case Dt:return Ut;case Lt:return It;case Rt:return Tt}return e});var Ct=Object.prototype.hasOwnProperty;var Nt=c.Uint8Array;function Wt(t){var e=new t.constructor(t.byteLength);return new Nt(e).set(new Nt(t)),e}var qt=/\w*$/;var Gt=i?i.prototype:void 0,Ht=Gt?Gt.valueOf:void 0;function Jt(t,e,r){var n,o,a,c,i,u=t.constructor;switch(e){case"[object ArrayBuffer]":return Wt(t);case"[object Boolean]":case"[object Date]":return new u(+t);case"[object DataView]":return i=Wt((c=t).buffer),new c.constructor(i,c.byteOffset,c.byteLength);case"[object Float32Array]":case"[object Float64Array]":case"[object Int8Array]":case"[object Int16Array]":case"[object Int32Array]":case"[object Uint8Array]":case"[object Uint8ClampedArray]":case"[object Uint16Array]":case"[object Uint32Array]":return function(t){var e=Wt(t.buffer);return new t.constructor(e,t.byteOffset,t.length)}(t);case"[object Map]":case"[object Set]":return new u;case"[object Number]":case"[object String]":return new u(t);case"[object RegExp]":return(a=new(o=t).constructor(o.source,qt.exec(o))).lastIndex=o.lastIndex,a;case"[object Symbol]":return n=t,Ht?Object(Ht.call(n)):{}}}var Kt=Object.create,Qt=function(){function t(){}return function(e){if(!_(e))return{};if(Kt)return Kt(e);t.prototype=e;var r=new t;return t.prototype=void 0,r}}();var Xt=ct&&ct.isMap,Yt=Xt?rt(Xt):function(t){return V(t)&&"[object Map]"==Vt(t)};var Zt=ct&&ct.isSet,te=Zt?rt(Zt):function(t){return V(t)&&"[object Set]"==Vt(t)},ee="[object Arguments]",re="[object Function]",ne="[object Object]",oe={};function ae(t,e,r,n,o,a){var c;if(void 0!==c)return c;if(!_(t))return t;var i=H(t);if(i)c=function(t){var e=t.length,r=new t.constructor(e);return e&&"string"==typeof t[0]&&Ct.call(t,"index")&&(r.index=t.index,r.input=t.input),r}(t);else{var u=Vt(t),s=u==re||"[object GeneratorFunction]"==u;if(X(t))return t.slice();if(u==ne||u==ee||s&&!o)c=s?{}:function(t){return"function"!=typeof t.constructor||lt(t)?{}:Qt(xt(t))}(t);else{if(!oe[u])return o?t:{};c=Jt(t,u)}}a||(a=new $);var f=a.get(t);if(f)return f;a.set(t,c),te(t)?t.forEach(function(n){c.add(ae(n,e,r,n,t,a))}):Yt(t)&&t.forEach(function(n,o){c.set(o,ae(n,e,r,o,t,a))});var p=i?void 0:zt(t);return function(t,e){for(var r=-1,n=null==t?0:t.length;++r<n&&!1!==e(t[r],r,t););}(p||t,function(n,o){p&&(n=t[o=n]),R(c,o,ae(n,e,r,o,t,a))}),c}oe[ee]=oe["[object Array]"]=oe["[object ArrayBuffer]"]=oe["[object DataView]"]=oe["[object Boolean]"]=oe["[object Date]"]=oe["[object Float32Array]"]=oe["[object Float64Array]"]=oe["[object Int8Array]"]=oe["[object Int16Array]"]=oe["[object Int32Array]"]=oe["[object Map]"]=oe["[object Number]"]=oe[ne]=oe["[object RegExp]"]=oe["[object Set]"]=oe["[object String]"]=oe["[object Symbol]"]=oe["[object Uint8Array]"]=oe["[object Uint8ClampedArray]"]=oe["[object Uint16Array]"]=oe["[object Uint32Array]"]=!0,oe["[object Error]"]=oe[re]=oe["[object WeakMap]"]=!1;var ce=Object.prototype.hasOwnProperty;var ie=Function.prototype,ue=Object.prototype,se=ie.toString,fe=ue.hasOwnProperty,pe=se.call(Object);function le(t){return!function(t){if(null==t)return!0;if(ht(t)&&(H(t)||"string"==typeof t||"function"==typeof t.splice||X(t)||ut(t)||G(t)))return!t.length;var e=Vt(t);if("[object Map]"==e||"[object Set]"==e)return!t.size;if(lt(t))return!jt(t).length;for(var r in t)if(ce.call(t,r))return!1;return!0}(t)&&t.every(t=>"string"==typeof t)}class be{__enumMap__;__enumLabelMap__;__enumExtraMap__;constructor(t){if(!function(t){if(!V(t)||"[object Object]"!=y(t))return!1;var e=xt(t);if(null===e)return!0;var r=fe.call(e,"constructor")&&e.constructor;return"function"==typeof r&&r instanceof r&&se.call(r)==pe}(t))throw new TypeError("初始化参数值必须是一个object!");this.#t(ae(t,5))}#t(t){const e={},r={};Object.keys(t).forEach(n=>{const o=t[n];if(!H(o))throw new TypeError("初始化参数对象字段的值必是一个array!");this[n]=o[0],e[n]=o[1],e[o[0]]=o[1],r[n]=o[2],r[o[0]]=o[2]}),this.__enumMap__=Object.freeze(t),this.__enumLabelMap__=Object.freeze(e),this.__enumExtraMap__=Object.freeze(r)}value(t){return this[t]}values(...t){let e=Object.keys(this.__enumMap__);return le(t)&&(e=Array.from(t)),e.map(t=>this.value(t))}options(...t){let e=Object.keys(this.__enumMap__);return le(t)&&(e=Array.from(t)),e.map(t=>({value:this.value(t),label:this.label(t),extra:this.extra(t)}))}label(t){return this.__enumLabelMap__[t]}extra(t){return this.__enumExtraMap__[t]}check(t,e){return this.value(e)===t}}return t=>{const e=new be(t);return Object.freeze(e)}});
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).$Enum=t()}(this,function(){"use strict";class e{__enumMap__;__enumLabelMap__;__enumExtraMap__;__valueKeyMap__;constructor(e){if(null===e||"object"!=typeof e||Array.isArray(e))throw new TypeError("初始化参数值必须是一个 object!");const t={},_={},n={},r=Object.keys(e);for(let a=0;a<r.length;a++){const s=r[a],u=e[s];if(!Array.isArray(u))throw new TypeError(`初始化参数对象字段 "${s}" 的值必须是一个 array!`);this[s]=u[0],t[s]=u[1],t[u[0]]=u[1],_[s]=u[2],_[u[0]]=u[2],n[u[0]]=s}this.__enumMap__=Object.freeze(e),this.__enumLabelMap__=Object.freeze(t),this.__enumExtraMap__=Object.freeze(_),this.__valueKeyMap__=Object.freeze(n)}keys(){return Object.keys(this.__enumMap__)}value(e){return this[e]}values(...e){return(e.length>0?e:this.keys()).map(e=>this.value(e))}options(...e){return(e.length>0?e:this.keys()).map(e=>({value:this.value(e),label:this.label(e),extra:this.extra(e)}))}label(e){return this.__enumLabelMap__[e]}extra(e){return this.__enumExtraMap__[e]}keyOf(e){return this.__valueKeyMap__[e]}check(e,t){return this.value(t)===e}has(e){return this.__valueKeyMap__.hasOwnProperty(e)}get size(){return Object.keys(this.__enumMap__).length}omit(...e){const _={},n=new Set(e),r=Object.keys(this.__enumMap__);for(let e=0;e<r.length;e++){const t=r[e];n.has(t)||(_[t]=this.__enumMap__[t])}return t(_)}pick(...e){const _={};for(let t=0;t<e.length;t++){const n=e[t];this.__enumMap__.hasOwnProperty(n)&&(_[n]=this.__enumMap__[n])}return t(_)}forEach(e){const t=Object.keys(this.__enumMap__);for(let _=0;_<t.length;_++){const n=t[_];e(this.value(n),this.__enumLabelMap__[n],n,this.__enumExtraMap__[n])}}toMap(){const e=new Map,t=Object.keys(this.__enumMap__);for(let _=0;_<t.length;_++){const n=t[_],r=this.__enumMap__[n];e.set(r[0],r[1])}return e}toRecord(){const e={},t=Object.keys(this.__enumMap__);for(let _=0;_<t.length;_++){const n=t[_],r=this.__enumMap__[n];e[r[0]]=r[1]}return e}filter(e){const t=[],_=Object.keys(this.__enumMap__);for(let n=0;n<_.length;n++){const r=_[n],a=this.value(r),s=this.__enumLabelMap__[r],u=this.__enumExtraMap__[r];e(a,s,r,u)&&t.push({value:a,label:s,extra:u})}return t}[Symbol.iterator](){const e=Object.keys(this.__enumMap__);let t=0;const _=this;return{next(){if(t<e.length){const n=e[t++];return{done:!1,value:{key:n,value:_.value(n),label:_.__enumLabelMap__[n],extra:_.__enumExtraMap__[n]}}}return{done:!0,value:void 0}}}}}const t=t=>{const _=new e(t);return Object.freeze(_)};return t});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,62 +1,184 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 宽化字面量类型
|
|
3
|
+
*/
|
|
4
|
+
type WidenLiteral<T> = T extends string ? string : T extends number ? number : T extends boolean ? boolean : T;
|
|
5
|
+
/**
|
|
6
|
+
* 枚举 Map 约束类型:{ KEY: [value, label, extra?] }
|
|
7
|
+
*/
|
|
8
|
+
type IEMap = Readonly<Record<string, readonly [any, any, any?]>>;
|
|
9
|
+
/**
|
|
10
|
+
* 枚举 Key 联合类型
|
|
11
|
+
*/
|
|
2
12
|
type TEUnion<T extends IEMap> = keyof T;
|
|
3
|
-
|
|
13
|
+
/**
|
|
14
|
+
* 枚举 Value 联合类型
|
|
15
|
+
*/
|
|
16
|
+
type TEValue<T extends IEMap> = T[keyof T][0];
|
|
17
|
+
/**
|
|
18
|
+
* 根据 Key 获取 Value 类型
|
|
19
|
+
*/
|
|
20
|
+
type TValue<K, T extends IEMap> = WidenLiteral<T[K & keyof T][0]>;
|
|
21
|
+
/**
|
|
22
|
+
* 根据 Key 获取 Label 类型
|
|
23
|
+
*/
|
|
24
|
+
type TULabel<K, T extends IEMap> = WidenLiteral<T[K & keyof T][1]>;
|
|
25
|
+
/**
|
|
26
|
+
* 根据 Value 获取 Label 类型
|
|
27
|
+
*/
|
|
28
|
+
type TVLabel<V, T extends IEMap> = WidenLiteral<Extract<T[keyof T], readonly [V, any, any?]>[1]>;
|
|
29
|
+
/**
|
|
30
|
+
* 根据 Key 获取 Extra 类型
|
|
31
|
+
*/
|
|
32
|
+
type TUExtra<K, T extends IEMap> = WidenLiteral<T[K & keyof T][2]>;
|
|
33
|
+
/**
|
|
34
|
+
* 根据 Value 获取 Extra 类型
|
|
35
|
+
*/
|
|
36
|
+
type TVExtra<V, T extends IEMap> = WidenLiteral<Extract<T[keyof T], readonly [V, any, any?]>[2]>;
|
|
37
|
+
/**
|
|
38
|
+
* 根据 Value 反查 Key 类型
|
|
39
|
+
*/
|
|
40
|
+
type TKeyOfValue<V, T extends IEMap> = {
|
|
41
|
+
[K in keyof T]: T[K][0] extends V ? K : never;
|
|
42
|
+
}[keyof T];
|
|
43
|
+
/**
|
|
44
|
+
* Option 项类型
|
|
45
|
+
*/
|
|
46
|
+
type TOption<K extends keyof T, T extends IEMap> = {
|
|
47
|
+
value: TValue<K, T>;
|
|
48
|
+
label: TULabel<K, T>;
|
|
49
|
+
extra: TUExtra<K, T>;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* 排除指定 Key 后的枚举类型
|
|
53
|
+
*/
|
|
54
|
+
type TOmitEnum<T extends IEMap, K extends keyof T> = Omit<T, K> extends infer R ? R extends IEMap ? R : never : never;
|
|
55
|
+
/**
|
|
56
|
+
* 选取指定 Key 的枚举类型
|
|
57
|
+
*/
|
|
58
|
+
type TPickEnum<T extends IEMap, K extends keyof T> = Pick<T, K> extends infer R ? R extends IEMap ? R : never : never;
|
|
59
|
+
/**
|
|
60
|
+
* 最终暴露的枚举实例类型
|
|
61
|
+
*/
|
|
4
62
|
type IEnum<T extends IEMap> = {
|
|
5
|
-
readonly [K in keyof T]: T[K]
|
|
63
|
+
readonly [K in keyof T]: T[K][0];
|
|
6
64
|
} & Enum<T>;
|
|
7
65
|
declare class Enum<T extends IEMap> {
|
|
8
|
-
|
|
9
|
-
protected
|
|
10
|
-
protected
|
|
11
|
-
protected
|
|
66
|
+
protected readonly __enumMap__: T;
|
|
67
|
+
protected readonly __enumLabelMap__: Record<string | number, unknown>;
|
|
68
|
+
protected readonly __enumExtraMap__: Record<string | number, unknown>;
|
|
69
|
+
protected readonly __valueKeyMap__: Record<string | number, string>;
|
|
70
|
+
constructor(enumMap: T);
|
|
12
71
|
/**
|
|
13
|
-
*
|
|
72
|
+
* 获取所有枚举 Key 列表
|
|
14
73
|
*/
|
|
15
|
-
|
|
74
|
+
keys(): TEUnion<T>[];
|
|
16
75
|
/**
|
|
17
76
|
* 获取枚举值
|
|
18
|
-
* @param
|
|
19
|
-
* @return {Number} 枚举值
|
|
77
|
+
* @param key 枚举 Key
|
|
20
78
|
*/
|
|
21
|
-
value(key:
|
|
79
|
+
value<K extends TEUnion<T>>(key: K): TValue<K, T>;
|
|
22
80
|
/**
|
|
23
|
-
*
|
|
24
|
-
* @param
|
|
25
|
-
* @return {Array} {[枚举值]}
|
|
81
|
+
* 获取多个枚举值,不传参则返回所有
|
|
82
|
+
* @param args 枚举 Key 列表
|
|
26
83
|
*/
|
|
27
|
-
values(...args:
|
|
84
|
+
values<K extends TEUnion<T>>(...args: K[]): TValue<K, T>[];
|
|
28
85
|
/**
|
|
29
|
-
*
|
|
30
|
-
* @param
|
|
31
|
-
* @return {Object} {[枚举key]:枚举值}
|
|
86
|
+
* 获取枚举选项列表(适配 Select/Radio 等组件),不传参则返回所有
|
|
87
|
+
* @param args 枚举 Key 列表
|
|
32
88
|
*/
|
|
33
|
-
options<
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
89
|
+
options<K extends TEUnion<T>>(...args: K[]): TOption<K, T>[];
|
|
90
|
+
/**
|
|
91
|
+
* 获取枚举名称(通过 Key 或 Value)
|
|
92
|
+
* @param keyOrVal 枚举 Key 或 Value
|
|
93
|
+
*/
|
|
94
|
+
label<K extends TEUnion<T>>(key: K): TULabel<K, T>;
|
|
95
|
+
label<V extends TEValue<T>>(val: V): TVLabel<V, T>;
|
|
96
|
+
/**
|
|
97
|
+
* 获取枚举附加数据(通过 Key 或 Value)
|
|
98
|
+
* @param keyOrVal 枚举 Key 或 Value
|
|
99
|
+
*/
|
|
100
|
+
extra<K extends TEUnion<T>>(key: K): TUExtra<K, T>;
|
|
101
|
+
extra<V extends TEValue<T>>(val: V): TVExtra<V, T>;
|
|
102
|
+
/**
|
|
103
|
+
* 通过 Value 反查 Key
|
|
104
|
+
* @param val 枚举 Value
|
|
105
|
+
*/
|
|
106
|
+
keyOf<V extends TEValue<T>>(val: V): TKeyOfValue<V, T>;
|
|
107
|
+
/**
|
|
108
|
+
* 检测值是否等于指定枚举 Key 对应的值
|
|
109
|
+
* @param val 待检测的值
|
|
110
|
+
* @param key 枚举 Key
|
|
111
|
+
*/
|
|
112
|
+
check<K extends TEUnion<T>>(val: unknown, key: K): boolean;
|
|
113
|
+
/**
|
|
114
|
+
* 检测值是否属于该枚举的任意一个值
|
|
115
|
+
* @param val 待检测的值
|
|
116
|
+
*/
|
|
117
|
+
has(val: unknown): val is TEValue<T>;
|
|
118
|
+
/**
|
|
119
|
+
* 获取枚举项数量
|
|
120
|
+
*/
|
|
121
|
+
get size(): number;
|
|
122
|
+
/**
|
|
123
|
+
* 排除指定 Key,生成新的枚举实例
|
|
124
|
+
* @param args 要排除的 Key 列表
|
|
125
|
+
*/
|
|
126
|
+
omit<K extends TEUnion<T>>(...args: K[]): Readonly<IEnum<TOmitEnum<T, K>>>;
|
|
127
|
+
/**
|
|
128
|
+
* 选取指定 Key,生成新的枚举实例
|
|
129
|
+
* @param args 要保留的 Key 列表
|
|
130
|
+
*/
|
|
131
|
+
pick<K extends TEUnion<T>>(...args: K[]): Readonly<IEnum<TPickEnum<T, K & string>>>;
|
|
132
|
+
/**
|
|
133
|
+
* 遍历枚举项
|
|
134
|
+
* @param callback 回调函数 (value, label, key, extra) => void
|
|
135
|
+
*/
|
|
136
|
+
forEach(callback: (value: TEValue<T>, label: unknown, key: TEUnion<T>, extra: unknown) => void): void;
|
|
38
137
|
/**
|
|
39
|
-
*
|
|
40
|
-
* @param {String} keyOrVal 枚举KEY或枚举值
|
|
41
|
-
* @return {String} 枚举名称
|
|
138
|
+
* 将枚举转为 Map 对象 { value => label }
|
|
42
139
|
*/
|
|
43
|
-
|
|
44
|
-
label(keyOrVal: any): any;
|
|
140
|
+
toMap(): Map<TEValue<T>, unknown>;
|
|
45
141
|
/**
|
|
46
|
-
*
|
|
47
|
-
* @param {String} keyOrVal 枚举KEY或枚举值
|
|
48
|
-
* @return {String} 枚举名称
|
|
142
|
+
* 将枚举转为 Record 对象 { [value]: label }
|
|
49
143
|
*/
|
|
50
|
-
|
|
51
|
-
extra(keyOrVal: any): any;
|
|
144
|
+
toRecord(): Record<string | number, unknown>;
|
|
52
145
|
/**
|
|
53
|
-
*
|
|
54
|
-
* @param
|
|
55
|
-
* @param {String} typeKey 类型key
|
|
56
|
-
* @return {Boolean}
|
|
146
|
+
* 过滤枚举项,返回满足条件的 options
|
|
147
|
+
* @param predicate 过滤函数
|
|
57
148
|
*/
|
|
58
|
-
|
|
149
|
+
filter(predicate: (value: TEValue<T>, label: unknown, key: TEUnion<T>, extra: unknown) => boolean): TOption<TEUnion<T>, T>[];
|
|
150
|
+
/**
|
|
151
|
+
* 支持 for...of 迭代
|
|
152
|
+
*/
|
|
153
|
+
[Symbol.iterator](): Iterator<{
|
|
154
|
+
key: TEUnion<T>;
|
|
155
|
+
value: TEValue<T>;
|
|
156
|
+
label: unknown;
|
|
157
|
+
extra: unknown;
|
|
158
|
+
}>;
|
|
59
159
|
}
|
|
160
|
+
/**
|
|
161
|
+
* 创建枚举实例
|
|
162
|
+
* @param enumMap 枚举定义对象,格式:{ KEY: [value, label, extra?] as const }
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* const Status = createEnum({
|
|
166
|
+
* ACTIVE: [1, '激活', { color: 'green' }],
|
|
167
|
+
* INACTIVE: [0, '未激活', { color: 'gray' }],
|
|
168
|
+
* } as const);
|
|
169
|
+
*
|
|
170
|
+
* Status.ACTIVE // 1
|
|
171
|
+
* Status.value('ACTIVE') // 1
|
|
172
|
+
* Status.label('ACTIVE') // '激活'
|
|
173
|
+
* Status.label(1) // '激活'
|
|
174
|
+
* Status.extra('ACTIVE') // { color: 'green' }
|
|
175
|
+
* Status.keyOf(1) // 'ACTIVE'
|
|
176
|
+
* Status.has(1) // true
|
|
177
|
+
* Status.has(99) // false
|
|
178
|
+
* Status.options() // [{ value: 1, label: '激活', extra: {...} }, ...]
|
|
179
|
+
* Status.pick('ACTIVE') // 新枚举,仅包含 ACTIVE
|
|
180
|
+
* Status.omit('INACTIVE')// 新枚举,排除 INACTIVE
|
|
181
|
+
*/
|
|
60
182
|
declare const createEnum: <T extends IEMap>(enumMap: T) => Readonly<IEnum<T>>;
|
|
61
183
|
|
|
62
184
|
export { createEnum as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-enum-es",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "处理前端枚举的工具方法,支持ts类型检测",
|
|
5
5
|
"main": "dist/create-enum.es.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
],
|
|
25
25
|
"type": "module",
|
|
26
26
|
"scripts": {
|
|
27
|
-
"test": "
|
|
27
|
+
"test": "vitest --run",
|
|
28
28
|
"build": "rollup -c",
|
|
29
29
|
"dev": "rollup -cw"
|
|
30
30
|
},
|
|
@@ -36,9 +36,7 @@
|
|
|
36
36
|
"@rollup/plugin-typescript": "^11.1.6",
|
|
37
37
|
"rollup": "^4.45.1",
|
|
38
38
|
"rollup-plugin-dts": "^6.1.1",
|
|
39
|
-
"typescript": "^5.5.3"
|
|
40
|
-
|
|
41
|
-
"dependencies": {
|
|
42
|
-
"lodash-es": "^4.17.21"
|
|
39
|
+
"typescript": "^5.5.3",
|
|
40
|
+
"vitest": "^4.1.9"
|
|
43
41
|
}
|
|
44
42
|
}
|