create-enum-es 1.0.3 → 2.0.1
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 +56 -68
- package/dist/create-enum.cjs.js +1 -241
- package/dist/create-enum.es.js +1 -239
- package/dist/create-enum.umd.js +1 -247
- package/dist/index.d.ts +23 -88
- package/package.json +7 -2
package/README.md
CHANGED
|
@@ -1,82 +1,70 @@
|
|
|
1
|
-
```
|
|
2
|
-
import
|
|
1
|
+
```ts
|
|
2
|
+
import createEnum from "create-enum-es";
|
|
3
3
|
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
})
|
|
4
|
+
const base = createEnum({
|
|
5
|
+
AA: [11, "AA-"],
|
|
6
|
+
BB: [22, "BB-"],
|
|
7
|
+
} as const);
|
|
8
8
|
|
|
9
|
-
console.log(
|
|
10
|
-
|
|
11
|
-
console.log(enum1.getValList('On')) // [1]
|
|
12
|
-
console.log(enum1.getValList()) // [1, 0]
|
|
9
|
+
console.log("base.AA :>> ", base.AA);
|
|
10
|
+
// 11
|
|
13
11
|
|
|
12
|
+
console.log("base.BB :>> ", base.BB);
|
|
13
|
+
// 22
|
|
14
14
|
|
|
15
|
-
console.log(
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
console.log("base.options() :>> ", base.options());
|
|
16
|
+
// [
|
|
17
|
+
// {
|
|
18
|
+
// "value": 11,
|
|
19
|
+
// "label": "AA-"
|
|
20
|
+
// },
|
|
21
|
+
// {
|
|
22
|
+
// "value": 22,
|
|
23
|
+
// "label": "BB-"
|
|
24
|
+
// }
|
|
25
|
+
// ]
|
|
18
26
|
|
|
27
|
+
const enumInstance = createEnum({
|
|
28
|
+
A: [1, "A-", { a: "axx" }],
|
|
29
|
+
B: [2, "B-", { b: "bxx" }],
|
|
30
|
+
} as const);
|
|
19
31
|
|
|
20
|
-
|
|
21
|
-
console.log(
|
|
22
|
-
|
|
32
|
+
const value = enumInstance.value("A");
|
|
33
|
+
console.log("value :>> ", value);
|
|
34
|
+
// 1
|
|
23
35
|
|
|
36
|
+
const values = enumInstance.values();
|
|
37
|
+
console.log("values :>> ", values);
|
|
38
|
+
// [1, 2]
|
|
24
39
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
// {
|
|
28
|
-
// "value": 1,
|
|
29
|
-
// "label": "开启"
|
|
30
|
-
// },
|
|
31
|
-
// {
|
|
32
|
-
// "value": 0,
|
|
33
|
-
// "label": "关闭"
|
|
34
|
-
// }
|
|
35
|
-
// ]
|
|
36
|
-
console.log(enum1.getOptions({ labelKey: 'name', valueKey: 'key' }))
|
|
37
|
-
// [
|
|
38
|
-
// {
|
|
39
|
-
// "key": 1,
|
|
40
|
-
// "name": "开启"
|
|
41
|
-
// },
|
|
42
|
-
// {
|
|
43
|
-
// "key": 0,
|
|
44
|
-
// "name": "关闭"
|
|
45
|
-
// }
|
|
46
|
-
// ]
|
|
47
|
-
console.log(enum1.getOptions('On'))
|
|
48
|
-
// [
|
|
49
|
-
// {
|
|
50
|
-
// "value": 1,
|
|
51
|
-
// "label": "开启"
|
|
52
|
-
// }
|
|
53
|
-
// ]
|
|
54
|
-
console.log(enum1.getOptions(enum1.On, { arguType: 'value' }))
|
|
55
|
-
// [
|
|
56
|
-
// {
|
|
57
|
-
// "value": 1,
|
|
58
|
-
// "label": "开启"
|
|
59
|
-
// }
|
|
60
|
-
// ]
|
|
61
|
-
console.log(enum1.getOptionsByValues())
|
|
40
|
+
const options = enumInstance.options();
|
|
41
|
+
console.log("options :>> ", options);
|
|
62
42
|
// [
|
|
63
|
-
//
|
|
64
|
-
//
|
|
65
|
-
//
|
|
66
|
-
//
|
|
67
|
-
//
|
|
68
|
-
// "value": 0,
|
|
69
|
-
// "label": "关闭"
|
|
43
|
+
// {
|
|
44
|
+
// "value": 1,
|
|
45
|
+
// "label": "A-",
|
|
46
|
+
// "extra": {
|
|
47
|
+
// "a": "axx"
|
|
70
48
|
// }
|
|
71
|
-
//
|
|
72
|
-
|
|
73
|
-
//
|
|
74
|
-
//
|
|
75
|
-
//
|
|
76
|
-
//
|
|
49
|
+
// },
|
|
50
|
+
// {
|
|
51
|
+
// "value": 2,
|
|
52
|
+
// "label": "B-",
|
|
53
|
+
// "extra": {
|
|
54
|
+
// "b": "bxx"
|
|
77
55
|
// }
|
|
56
|
+
// }
|
|
78
57
|
// ]
|
|
79
58
|
|
|
80
|
-
|
|
81
|
-
|
|
59
|
+
const label = enumInstance.label("A");
|
|
60
|
+
console.log("label :>> ", label);
|
|
61
|
+
// 'A-'
|
|
82
62
|
|
|
63
|
+
const extra = enumInstance.extra("A");
|
|
64
|
+
console.log("extra :>> ", extra);
|
|
65
|
+
// {"a": "axx"}
|
|
66
|
+
|
|
67
|
+
const check = enumInstance.check(1, "A");
|
|
68
|
+
console.log("check :>> ", check);
|
|
69
|
+
// true
|
|
70
|
+
```
|
package/dist/create-enum.cjs.js
CHANGED
|
@@ -1,241 +1 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 数据类型
|
|
5
|
-
* @param {*} data
|
|
6
|
-
* @param {String} type
|
|
7
|
-
* @returns {Boolean}
|
|
8
|
-
*/
|
|
9
|
-
function isType(data, type) {
|
|
10
|
-
const dataType = Object.prototype.toString.call(data).slice(8, -1).toLowerCase();
|
|
11
|
-
return type === dataType;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* 数据是否为空
|
|
15
|
-
* @param {*} data
|
|
16
|
-
* @returns
|
|
17
|
-
*/
|
|
18
|
-
function isEmpty(data) {
|
|
19
|
-
if (isType(data, "array") || isType(data, "string")) {
|
|
20
|
-
return data.length === 0;
|
|
21
|
-
}
|
|
22
|
-
if (data instanceof Map || data instanceof Set) {
|
|
23
|
-
return data.size === 0;
|
|
24
|
-
}
|
|
25
|
-
if (isType(data, "object")) {
|
|
26
|
-
return Object.keys(data).length === 0;
|
|
27
|
-
}
|
|
28
|
-
return Boolean(data);
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* 深度拷贝
|
|
32
|
-
* @param {Object|Array} obj
|
|
33
|
-
* @return {Object|Array}
|
|
34
|
-
*/
|
|
35
|
-
function deepClone(obj) {
|
|
36
|
-
let objClone = Array.isArray(obj) ? [] : {};
|
|
37
|
-
if (obj && typeof obj === "object") {
|
|
38
|
-
for (let key in obj) {
|
|
39
|
-
if (obj.hasOwnProperty(key)) {
|
|
40
|
-
//判断ojb子元素是否为对象,如果是,递归复制
|
|
41
|
-
if (obj[key] && typeof obj[key] === "object") {
|
|
42
|
-
objClone[key] = deepClone(obj[key]);
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
//如果不是,简单复制
|
|
46
|
-
objClone[key] = obj[key];
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
return objClone;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* 获取配置参数
|
|
56
|
-
* @param {*} args
|
|
57
|
-
* @returns
|
|
58
|
-
*/
|
|
59
|
-
function getConfigParams(args) {
|
|
60
|
-
let config = { labelKey: "label", valueKey: "value", arguType: "key" }; // 选项参数配置
|
|
61
|
-
const lastArgu = args[args.length - 1];
|
|
62
|
-
if (isType(lastArgu, "object")) {
|
|
63
|
-
config = { ...config, ...lastArgu };
|
|
64
|
-
args = args.slice(0, args.length - 1);
|
|
65
|
-
}
|
|
66
|
-
return [config, args];
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* 判断枚举key列表是否有效
|
|
70
|
-
* @param {*} args
|
|
71
|
-
* @returns {Boolean}
|
|
72
|
-
*/
|
|
73
|
-
function judgEnumKeys(keys) {
|
|
74
|
-
return !isEmpty(keys) && keys.every((key) => typeof key === "string");
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* 枚举类
|
|
78
|
-
* @param {Object} enumMap 枚举对象
|
|
79
|
-
* 枚举格式:
|
|
80
|
-
* {
|
|
81
|
-
* 枚举key1: [枚举值1,枚举描述1],
|
|
82
|
-
* 枚举key2: [枚举值2,枚举描述2]
|
|
83
|
-
* }
|
|
84
|
-
*/
|
|
85
|
-
class Enum {
|
|
86
|
-
__enumMap__;
|
|
87
|
-
__enumValueMap__;
|
|
88
|
-
__enumNameMap__;
|
|
89
|
-
__valueNameMap__;
|
|
90
|
-
/**
|
|
91
|
-
* @param {Object} enumMap 枚举map
|
|
92
|
-
*/
|
|
93
|
-
constructor(enumMap) {
|
|
94
|
-
if (!isType(enumMap, "object")) {
|
|
95
|
-
throw new TypeError("初始化参数值必须是一个object!");
|
|
96
|
-
}
|
|
97
|
-
this.#init(deepClone(enumMap));
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* 初始化
|
|
101
|
-
* @param {Object} enumMap 枚举对象
|
|
102
|
-
* @private
|
|
103
|
-
*/
|
|
104
|
-
#init(enumMap) {
|
|
105
|
-
this.#setEnumMap(enumMap); //处理映射关系
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* 设置枚举间的映射
|
|
109
|
-
* @param {Object} enumMap
|
|
110
|
-
* @private
|
|
111
|
-
*/
|
|
112
|
-
#setEnumMap(enumMap) {
|
|
113
|
-
const enumValueMap = {};
|
|
114
|
-
const enumNameMap = {};
|
|
115
|
-
const valueNameMap = {};
|
|
116
|
-
Object.keys(enumMap).forEach((key) => {
|
|
117
|
-
const item = enumMap[key];
|
|
118
|
-
if (!Array.isArray(item)) {
|
|
119
|
-
throw new TypeError("初始化参数对象字段的值必是一个array!");
|
|
120
|
-
}
|
|
121
|
-
enumValueMap[key] = item[0];
|
|
122
|
-
enumNameMap[key] = item[1];
|
|
123
|
-
valueNameMap[item[0]] = item[1];
|
|
124
|
-
});
|
|
125
|
-
this.__enumMap__ = Object.freeze(enumMap);
|
|
126
|
-
this.__enumValueMap__ = Object.freeze(enumValueMap);
|
|
127
|
-
this.__enumNameMap__ = Object.freeze(enumNameMap);
|
|
128
|
-
this.__valueNameMap__ = Object.freeze(valueNameMap);
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* 获取枚举值
|
|
132
|
-
* @param {String} key 枚举KEY
|
|
133
|
-
* @return {Number} 枚举值
|
|
134
|
-
*/
|
|
135
|
-
getVal(key) {
|
|
136
|
-
return this.__enumValueMap__[key];
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* 获取多个枚举值
|
|
140
|
-
* @param {Array} param 多个枚举KEY
|
|
141
|
-
* @return {Array} {[枚举值]}
|
|
142
|
-
*/
|
|
143
|
-
getValList(...args) {
|
|
144
|
-
let keys = Object.keys(this.__enumMap__); // 不传递返回所有
|
|
145
|
-
if (judgEnumKeys(args)) {
|
|
146
|
-
keys = Array.from(args);
|
|
147
|
-
}
|
|
148
|
-
return keys.map((key) => this.getVal(key));
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* 获取多个枚举值Map
|
|
152
|
-
* @param {Array} param 多个枚举KEY,如果不传递则返回所有
|
|
153
|
-
* @return {Object} {[枚举key]:枚举值}
|
|
154
|
-
*/
|
|
155
|
-
getValMap(...args) {
|
|
156
|
-
let keys = Object.keys(this.__enumMap__); // 不传递返回所有
|
|
157
|
-
if (judgEnumKeys(args)) {
|
|
158
|
-
keys = Array.from(args);
|
|
159
|
-
}
|
|
160
|
-
return keys.reduce((wrap, key) => {
|
|
161
|
-
wrap[key] = this.getVal(key);
|
|
162
|
-
return wrap;
|
|
163
|
-
}, {});
|
|
164
|
-
}
|
|
165
|
-
getName(keyOrVal, _config) {
|
|
166
|
-
const [config] = getConfigParams([_config]);
|
|
167
|
-
if (config.arguType === "key") {
|
|
168
|
-
return this.__enumNameMap__[keyOrVal];
|
|
169
|
-
}
|
|
170
|
-
else if (config.arguType === "value") {
|
|
171
|
-
return this.getNameByValue(keyOrVal);
|
|
172
|
-
}
|
|
173
|
-
else {
|
|
174
|
-
throw new TypeError("参数arguType的值类型不为 key|value !");
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* 通过枚举值获取枚举名称
|
|
179
|
-
* @param {String|Nunber} val
|
|
180
|
-
* @return {String|Null}
|
|
181
|
-
*/
|
|
182
|
-
getNameByValue(val) {
|
|
183
|
-
return this.__valueNameMap__[val];
|
|
184
|
-
}
|
|
185
|
-
getOptions(..._args) {
|
|
186
|
-
const [config, args] = getConfigParams(_args);
|
|
187
|
-
if (config.arguType === "key") {
|
|
188
|
-
let keys = Object.keys(this.__enumMap__); // 不传递返回所有
|
|
189
|
-
if (judgEnumKeys(args)) {
|
|
190
|
-
keys = Array.from(args);
|
|
191
|
-
}
|
|
192
|
-
return keys.map((key) => {
|
|
193
|
-
const value = this.getVal(key);
|
|
194
|
-
const name = this.getName(key);
|
|
195
|
-
return { [config.valueKey]: value, [config.labelKey]: name };
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
else if (config.arguType === "value") {
|
|
199
|
-
return this.getOptionsByValues(..._args);
|
|
200
|
-
}
|
|
201
|
-
else {
|
|
202
|
-
throw new TypeError("参数arguType的值类型不为 key | value !");
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
getOptionsByValues(..._args) {
|
|
206
|
-
const [config, args] = getConfigParams(_args);
|
|
207
|
-
let values = Object.values(this.__enumValueMap__); // 不传递返回所有
|
|
208
|
-
if (!isEmpty(args)) {
|
|
209
|
-
values = Array.from(args);
|
|
210
|
-
}
|
|
211
|
-
return values.map((value) => {
|
|
212
|
-
const name = this.getNameByValue(value);
|
|
213
|
-
return { [config.valueKey]: value, [config.labelKey]: name };
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* 检测字段类型
|
|
218
|
-
* @param {Number} typeVal 类型
|
|
219
|
-
* @param {String} typeKey 类型key
|
|
220
|
-
* @return {Boolean}
|
|
221
|
-
*/
|
|
222
|
-
check(typeVal, typeKey) {
|
|
223
|
-
return this.getVal(typeKey) === typeVal;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
/**
|
|
227
|
-
* 创建枚举
|
|
228
|
-
* @param {Object} enumMap
|
|
229
|
-
* @param {String} description
|
|
230
|
-
* @returns
|
|
231
|
-
*/
|
|
232
|
-
const createEnum = (enumMap) => {
|
|
233
|
-
const enumInstance = new Enum(enumMap); // 返回实例
|
|
234
|
-
const e = Object.create(enumInstance);
|
|
235
|
-
for (const key in enumMap) {
|
|
236
|
-
e[key] = enumMap[key]?.[0];
|
|
237
|
-
}
|
|
238
|
-
return Object.freeze(e);
|
|
239
|
-
};
|
|
240
|
-
|
|
241
|
-
exports.createEnum = createEnum;
|
|
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)};
|
package/dist/create-enum.es.js
CHANGED
|
@@ -1,239 +1 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 数据类型
|
|
3
|
-
* @param {*} data
|
|
4
|
-
* @param {String} type
|
|
5
|
-
* @returns {Boolean}
|
|
6
|
-
*/
|
|
7
|
-
function isType(data, type) {
|
|
8
|
-
const dataType = Object.prototype.toString.call(data).slice(8, -1).toLowerCase();
|
|
9
|
-
return type === dataType;
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* 数据是否为空
|
|
13
|
-
* @param {*} data
|
|
14
|
-
* @returns
|
|
15
|
-
*/
|
|
16
|
-
function isEmpty(data) {
|
|
17
|
-
if (isType(data, "array") || isType(data, "string")) {
|
|
18
|
-
return data.length === 0;
|
|
19
|
-
}
|
|
20
|
-
if (data instanceof Map || data instanceof Set) {
|
|
21
|
-
return data.size === 0;
|
|
22
|
-
}
|
|
23
|
-
if (isType(data, "object")) {
|
|
24
|
-
return Object.keys(data).length === 0;
|
|
25
|
-
}
|
|
26
|
-
return Boolean(data);
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* 深度拷贝
|
|
30
|
-
* @param {Object|Array} obj
|
|
31
|
-
* @return {Object|Array}
|
|
32
|
-
*/
|
|
33
|
-
function deepClone(obj) {
|
|
34
|
-
let objClone = Array.isArray(obj) ? [] : {};
|
|
35
|
-
if (obj && typeof obj === "object") {
|
|
36
|
-
for (let key in obj) {
|
|
37
|
-
if (obj.hasOwnProperty(key)) {
|
|
38
|
-
//判断ojb子元素是否为对象,如果是,递归复制
|
|
39
|
-
if (obj[key] && typeof obj[key] === "object") {
|
|
40
|
-
objClone[key] = deepClone(obj[key]);
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
//如果不是,简单复制
|
|
44
|
-
objClone[key] = obj[key];
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return objClone;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* 获取配置参数
|
|
54
|
-
* @param {*} args
|
|
55
|
-
* @returns
|
|
56
|
-
*/
|
|
57
|
-
function getConfigParams(args) {
|
|
58
|
-
let config = { labelKey: "label", valueKey: "value", arguType: "key" }; // 选项参数配置
|
|
59
|
-
const lastArgu = args[args.length - 1];
|
|
60
|
-
if (isType(lastArgu, "object")) {
|
|
61
|
-
config = { ...config, ...lastArgu };
|
|
62
|
-
args = args.slice(0, args.length - 1);
|
|
63
|
-
}
|
|
64
|
-
return [config, args];
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* 判断枚举key列表是否有效
|
|
68
|
-
* @param {*} args
|
|
69
|
-
* @returns {Boolean}
|
|
70
|
-
*/
|
|
71
|
-
function judgEnumKeys(keys) {
|
|
72
|
-
return !isEmpty(keys) && keys.every((key) => typeof key === "string");
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* 枚举类
|
|
76
|
-
* @param {Object} enumMap 枚举对象
|
|
77
|
-
* 枚举格式:
|
|
78
|
-
* {
|
|
79
|
-
* 枚举key1: [枚举值1,枚举描述1],
|
|
80
|
-
* 枚举key2: [枚举值2,枚举描述2]
|
|
81
|
-
* }
|
|
82
|
-
*/
|
|
83
|
-
class Enum {
|
|
84
|
-
__enumMap__;
|
|
85
|
-
__enumValueMap__;
|
|
86
|
-
__enumNameMap__;
|
|
87
|
-
__valueNameMap__;
|
|
88
|
-
/**
|
|
89
|
-
* @param {Object} enumMap 枚举map
|
|
90
|
-
*/
|
|
91
|
-
constructor(enumMap) {
|
|
92
|
-
if (!isType(enumMap, "object")) {
|
|
93
|
-
throw new TypeError("初始化参数值必须是一个object!");
|
|
94
|
-
}
|
|
95
|
-
this.#init(deepClone(enumMap));
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* 初始化
|
|
99
|
-
* @param {Object} enumMap 枚举对象
|
|
100
|
-
* @private
|
|
101
|
-
*/
|
|
102
|
-
#init(enumMap) {
|
|
103
|
-
this.#setEnumMap(enumMap); //处理映射关系
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* 设置枚举间的映射
|
|
107
|
-
* @param {Object} enumMap
|
|
108
|
-
* @private
|
|
109
|
-
*/
|
|
110
|
-
#setEnumMap(enumMap) {
|
|
111
|
-
const enumValueMap = {};
|
|
112
|
-
const enumNameMap = {};
|
|
113
|
-
const valueNameMap = {};
|
|
114
|
-
Object.keys(enumMap).forEach((key) => {
|
|
115
|
-
const item = enumMap[key];
|
|
116
|
-
if (!Array.isArray(item)) {
|
|
117
|
-
throw new TypeError("初始化参数对象字段的值必是一个array!");
|
|
118
|
-
}
|
|
119
|
-
enumValueMap[key] = item[0];
|
|
120
|
-
enumNameMap[key] = item[1];
|
|
121
|
-
valueNameMap[item[0]] = item[1];
|
|
122
|
-
});
|
|
123
|
-
this.__enumMap__ = Object.freeze(enumMap);
|
|
124
|
-
this.__enumValueMap__ = Object.freeze(enumValueMap);
|
|
125
|
-
this.__enumNameMap__ = Object.freeze(enumNameMap);
|
|
126
|
-
this.__valueNameMap__ = Object.freeze(valueNameMap);
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* 获取枚举值
|
|
130
|
-
* @param {String} key 枚举KEY
|
|
131
|
-
* @return {Number} 枚举值
|
|
132
|
-
*/
|
|
133
|
-
getVal(key) {
|
|
134
|
-
return this.__enumValueMap__[key];
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* 获取多个枚举值
|
|
138
|
-
* @param {Array} param 多个枚举KEY
|
|
139
|
-
* @return {Array} {[枚举值]}
|
|
140
|
-
*/
|
|
141
|
-
getValList(...args) {
|
|
142
|
-
let keys = Object.keys(this.__enumMap__); // 不传递返回所有
|
|
143
|
-
if (judgEnumKeys(args)) {
|
|
144
|
-
keys = Array.from(args);
|
|
145
|
-
}
|
|
146
|
-
return keys.map((key) => this.getVal(key));
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* 获取多个枚举值Map
|
|
150
|
-
* @param {Array} param 多个枚举KEY,如果不传递则返回所有
|
|
151
|
-
* @return {Object} {[枚举key]:枚举值}
|
|
152
|
-
*/
|
|
153
|
-
getValMap(...args) {
|
|
154
|
-
let keys = Object.keys(this.__enumMap__); // 不传递返回所有
|
|
155
|
-
if (judgEnumKeys(args)) {
|
|
156
|
-
keys = Array.from(args);
|
|
157
|
-
}
|
|
158
|
-
return keys.reduce((wrap, key) => {
|
|
159
|
-
wrap[key] = this.getVal(key);
|
|
160
|
-
return wrap;
|
|
161
|
-
}, {});
|
|
162
|
-
}
|
|
163
|
-
getName(keyOrVal, _config) {
|
|
164
|
-
const [config] = getConfigParams([_config]);
|
|
165
|
-
if (config.arguType === "key") {
|
|
166
|
-
return this.__enumNameMap__[keyOrVal];
|
|
167
|
-
}
|
|
168
|
-
else if (config.arguType === "value") {
|
|
169
|
-
return this.getNameByValue(keyOrVal);
|
|
170
|
-
}
|
|
171
|
-
else {
|
|
172
|
-
throw new TypeError("参数arguType的值类型不为 key|value !");
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* 通过枚举值获取枚举名称
|
|
177
|
-
* @param {String|Nunber} val
|
|
178
|
-
* @return {String|Null}
|
|
179
|
-
*/
|
|
180
|
-
getNameByValue(val) {
|
|
181
|
-
return this.__valueNameMap__[val];
|
|
182
|
-
}
|
|
183
|
-
getOptions(..._args) {
|
|
184
|
-
const [config, args] = getConfigParams(_args);
|
|
185
|
-
if (config.arguType === "key") {
|
|
186
|
-
let keys = Object.keys(this.__enumMap__); // 不传递返回所有
|
|
187
|
-
if (judgEnumKeys(args)) {
|
|
188
|
-
keys = Array.from(args);
|
|
189
|
-
}
|
|
190
|
-
return keys.map((key) => {
|
|
191
|
-
const value = this.getVal(key);
|
|
192
|
-
const name = this.getName(key);
|
|
193
|
-
return { [config.valueKey]: value, [config.labelKey]: name };
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
else if (config.arguType === "value") {
|
|
197
|
-
return this.getOptionsByValues(..._args);
|
|
198
|
-
}
|
|
199
|
-
else {
|
|
200
|
-
throw new TypeError("参数arguType的值类型不为 key | value !");
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
getOptionsByValues(..._args) {
|
|
204
|
-
const [config, args] = getConfigParams(_args);
|
|
205
|
-
let values = Object.values(this.__enumValueMap__); // 不传递返回所有
|
|
206
|
-
if (!isEmpty(args)) {
|
|
207
|
-
values = Array.from(args);
|
|
208
|
-
}
|
|
209
|
-
return values.map((value) => {
|
|
210
|
-
const name = this.getNameByValue(value);
|
|
211
|
-
return { [config.valueKey]: value, [config.labelKey]: name };
|
|
212
|
-
});
|
|
213
|
-
}
|
|
214
|
-
/**
|
|
215
|
-
* 检测字段类型
|
|
216
|
-
* @param {Number} typeVal 类型
|
|
217
|
-
* @param {String} typeKey 类型key
|
|
218
|
-
* @return {Boolean}
|
|
219
|
-
*/
|
|
220
|
-
check(typeVal, typeKey) {
|
|
221
|
-
return this.getVal(typeKey) === typeVal;
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* 创建枚举
|
|
226
|
-
* @param {Object} enumMap
|
|
227
|
-
* @param {String} description
|
|
228
|
-
* @returns
|
|
229
|
-
*/
|
|
230
|
-
const createEnum = (enumMap) => {
|
|
231
|
-
const enumInstance = new Enum(enumMap); // 返回实例
|
|
232
|
-
const e = Object.create(enumInstance);
|
|
233
|
-
for (const key in enumMap) {
|
|
234
|
-
e[key] = enumMap[key]?.[0];
|
|
235
|
-
}
|
|
236
|
-
return Object.freeze(e);
|
|
237
|
-
};
|
|
238
|
-
|
|
239
|
-
export { createEnum };
|
|
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};
|
package/dist/create-enum.umd.js
CHANGED
|
@@ -1,247 +1 @@
|
|
|
1
|
-
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.$Enum = {}));
|
|
5
|
-
})(this, (function (exports) { 'use strict';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 数据类型
|
|
9
|
-
* @param {*} data
|
|
10
|
-
* @param {String} type
|
|
11
|
-
* @returns {Boolean}
|
|
12
|
-
*/
|
|
13
|
-
function isType(data, type) {
|
|
14
|
-
const dataType = Object.prototype.toString.call(data).slice(8, -1).toLowerCase();
|
|
15
|
-
return type === dataType;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* 数据是否为空
|
|
19
|
-
* @param {*} data
|
|
20
|
-
* @returns
|
|
21
|
-
*/
|
|
22
|
-
function isEmpty(data) {
|
|
23
|
-
if (isType(data, "array") || isType(data, "string")) {
|
|
24
|
-
return data.length === 0;
|
|
25
|
-
}
|
|
26
|
-
if (data instanceof Map || data instanceof Set) {
|
|
27
|
-
return data.size === 0;
|
|
28
|
-
}
|
|
29
|
-
if (isType(data, "object")) {
|
|
30
|
-
return Object.keys(data).length === 0;
|
|
31
|
-
}
|
|
32
|
-
return Boolean(data);
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* 深度拷贝
|
|
36
|
-
* @param {Object|Array} obj
|
|
37
|
-
* @return {Object|Array}
|
|
38
|
-
*/
|
|
39
|
-
function deepClone(obj) {
|
|
40
|
-
let objClone = Array.isArray(obj) ? [] : {};
|
|
41
|
-
if (obj && typeof obj === "object") {
|
|
42
|
-
for (let key in obj) {
|
|
43
|
-
if (obj.hasOwnProperty(key)) {
|
|
44
|
-
//判断ojb子元素是否为对象,如果是,递归复制
|
|
45
|
-
if (obj[key] && typeof obj[key] === "object") {
|
|
46
|
-
objClone[key] = deepClone(obj[key]);
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
//如果不是,简单复制
|
|
50
|
-
objClone[key] = obj[key];
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return objClone;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* 获取配置参数
|
|
60
|
-
* @param {*} args
|
|
61
|
-
* @returns
|
|
62
|
-
*/
|
|
63
|
-
function getConfigParams(args) {
|
|
64
|
-
let config = { labelKey: "label", valueKey: "value", arguType: "key" }; // 选项参数配置
|
|
65
|
-
const lastArgu = args[args.length - 1];
|
|
66
|
-
if (isType(lastArgu, "object")) {
|
|
67
|
-
config = { ...config, ...lastArgu };
|
|
68
|
-
args = args.slice(0, args.length - 1);
|
|
69
|
-
}
|
|
70
|
-
return [config, args];
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* 判断枚举key列表是否有效
|
|
74
|
-
* @param {*} args
|
|
75
|
-
* @returns {Boolean}
|
|
76
|
-
*/
|
|
77
|
-
function judgEnumKeys(keys) {
|
|
78
|
-
return !isEmpty(keys) && keys.every((key) => typeof key === "string");
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* 枚举类
|
|
82
|
-
* @param {Object} enumMap 枚举对象
|
|
83
|
-
* 枚举格式:
|
|
84
|
-
* {
|
|
85
|
-
* 枚举key1: [枚举值1,枚举描述1],
|
|
86
|
-
* 枚举key2: [枚举值2,枚举描述2]
|
|
87
|
-
* }
|
|
88
|
-
*/
|
|
89
|
-
class Enum {
|
|
90
|
-
__enumMap__;
|
|
91
|
-
__enumValueMap__;
|
|
92
|
-
__enumNameMap__;
|
|
93
|
-
__valueNameMap__;
|
|
94
|
-
/**
|
|
95
|
-
* @param {Object} enumMap 枚举map
|
|
96
|
-
*/
|
|
97
|
-
constructor(enumMap) {
|
|
98
|
-
if (!isType(enumMap, "object")) {
|
|
99
|
-
throw new TypeError("初始化参数值必须是一个object!");
|
|
100
|
-
}
|
|
101
|
-
this.#init(deepClone(enumMap));
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* 初始化
|
|
105
|
-
* @param {Object} enumMap 枚举对象
|
|
106
|
-
* @private
|
|
107
|
-
*/
|
|
108
|
-
#init(enumMap) {
|
|
109
|
-
this.#setEnumMap(enumMap); //处理映射关系
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* 设置枚举间的映射
|
|
113
|
-
* @param {Object} enumMap
|
|
114
|
-
* @private
|
|
115
|
-
*/
|
|
116
|
-
#setEnumMap(enumMap) {
|
|
117
|
-
const enumValueMap = {};
|
|
118
|
-
const enumNameMap = {};
|
|
119
|
-
const valueNameMap = {};
|
|
120
|
-
Object.keys(enumMap).forEach((key) => {
|
|
121
|
-
const item = enumMap[key];
|
|
122
|
-
if (!Array.isArray(item)) {
|
|
123
|
-
throw new TypeError("初始化参数对象字段的值必是一个array!");
|
|
124
|
-
}
|
|
125
|
-
enumValueMap[key] = item[0];
|
|
126
|
-
enumNameMap[key] = item[1];
|
|
127
|
-
valueNameMap[item[0]] = item[1];
|
|
128
|
-
});
|
|
129
|
-
this.__enumMap__ = Object.freeze(enumMap);
|
|
130
|
-
this.__enumValueMap__ = Object.freeze(enumValueMap);
|
|
131
|
-
this.__enumNameMap__ = Object.freeze(enumNameMap);
|
|
132
|
-
this.__valueNameMap__ = Object.freeze(valueNameMap);
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* 获取枚举值
|
|
136
|
-
* @param {String} key 枚举KEY
|
|
137
|
-
* @return {Number} 枚举值
|
|
138
|
-
*/
|
|
139
|
-
getVal(key) {
|
|
140
|
-
return this.__enumValueMap__[key];
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* 获取多个枚举值
|
|
144
|
-
* @param {Array} param 多个枚举KEY
|
|
145
|
-
* @return {Array} {[枚举值]}
|
|
146
|
-
*/
|
|
147
|
-
getValList(...args) {
|
|
148
|
-
let keys = Object.keys(this.__enumMap__); // 不传递返回所有
|
|
149
|
-
if (judgEnumKeys(args)) {
|
|
150
|
-
keys = Array.from(args);
|
|
151
|
-
}
|
|
152
|
-
return keys.map((key) => this.getVal(key));
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* 获取多个枚举值Map
|
|
156
|
-
* @param {Array} param 多个枚举KEY,如果不传递则返回所有
|
|
157
|
-
* @return {Object} {[枚举key]:枚举值}
|
|
158
|
-
*/
|
|
159
|
-
getValMap(...args) {
|
|
160
|
-
let keys = Object.keys(this.__enumMap__); // 不传递返回所有
|
|
161
|
-
if (judgEnumKeys(args)) {
|
|
162
|
-
keys = Array.from(args);
|
|
163
|
-
}
|
|
164
|
-
return keys.reduce((wrap, key) => {
|
|
165
|
-
wrap[key] = this.getVal(key);
|
|
166
|
-
return wrap;
|
|
167
|
-
}, {});
|
|
168
|
-
}
|
|
169
|
-
getName(keyOrVal, _config) {
|
|
170
|
-
const [config] = getConfigParams([_config]);
|
|
171
|
-
if (config.arguType === "key") {
|
|
172
|
-
return this.__enumNameMap__[keyOrVal];
|
|
173
|
-
}
|
|
174
|
-
else if (config.arguType === "value") {
|
|
175
|
-
return this.getNameByValue(keyOrVal);
|
|
176
|
-
}
|
|
177
|
-
else {
|
|
178
|
-
throw new TypeError("参数arguType的值类型不为 key|value !");
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* 通过枚举值获取枚举名称
|
|
183
|
-
* @param {String|Nunber} val
|
|
184
|
-
* @return {String|Null}
|
|
185
|
-
*/
|
|
186
|
-
getNameByValue(val) {
|
|
187
|
-
return this.__valueNameMap__[val];
|
|
188
|
-
}
|
|
189
|
-
getOptions(..._args) {
|
|
190
|
-
const [config, args] = getConfigParams(_args);
|
|
191
|
-
if (config.arguType === "key") {
|
|
192
|
-
let keys = Object.keys(this.__enumMap__); // 不传递返回所有
|
|
193
|
-
if (judgEnumKeys(args)) {
|
|
194
|
-
keys = Array.from(args);
|
|
195
|
-
}
|
|
196
|
-
return keys.map((key) => {
|
|
197
|
-
const value = this.getVal(key);
|
|
198
|
-
const name = this.getName(key);
|
|
199
|
-
return { [config.valueKey]: value, [config.labelKey]: name };
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
else if (config.arguType === "value") {
|
|
203
|
-
return this.getOptionsByValues(..._args);
|
|
204
|
-
}
|
|
205
|
-
else {
|
|
206
|
-
throw new TypeError("参数arguType的值类型不为 key | value !");
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
getOptionsByValues(..._args) {
|
|
210
|
-
const [config, args] = getConfigParams(_args);
|
|
211
|
-
let values = Object.values(this.__enumValueMap__); // 不传递返回所有
|
|
212
|
-
if (!isEmpty(args)) {
|
|
213
|
-
values = Array.from(args);
|
|
214
|
-
}
|
|
215
|
-
return values.map((value) => {
|
|
216
|
-
const name = this.getNameByValue(value);
|
|
217
|
-
return { [config.valueKey]: value, [config.labelKey]: name };
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
/**
|
|
221
|
-
* 检测字段类型
|
|
222
|
-
* @param {Number} typeVal 类型
|
|
223
|
-
* @param {String} typeKey 类型key
|
|
224
|
-
* @return {Boolean}
|
|
225
|
-
*/
|
|
226
|
-
check(typeVal, typeKey) {
|
|
227
|
-
return this.getVal(typeKey) === typeVal;
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* 创建枚举
|
|
232
|
-
* @param {Object} enumMap
|
|
233
|
-
* @param {String} description
|
|
234
|
-
* @returns
|
|
235
|
-
*/
|
|
236
|
-
const createEnum = (enumMap) => {
|
|
237
|
-
const enumInstance = new Enum(enumMap); // 返回实例
|
|
238
|
-
const e = Object.create(enumInstance);
|
|
239
|
-
for (const key in enumMap) {
|
|
240
|
-
e[key] = enumMap[key]?.[0];
|
|
241
|
-
}
|
|
242
|
-
return Object.freeze(e);
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
exports.createEnum = createEnum;
|
|
246
|
-
|
|
247
|
-
}));
|
|
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)}});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,48 +1,14 @@
|
|
|
1
|
-
type IEMap = Readonly<Record<any, readonly [any, any]>>;
|
|
1
|
+
type IEMap = Readonly<Record<any, readonly [any, any] | readonly [any, any, any]>>;
|
|
2
2
|
type TEUnion<T extends IEMap> = keyof T;
|
|
3
3
|
type TEValue<T extends IEMap> = T[TEUnion<T>][0];
|
|
4
|
-
type
|
|
5
|
-
[K in
|
|
6
|
-
}
|
|
7
|
-
type IEnum<T extends IEMap, Enum> = {
|
|
8
|
-
readonly [K in keyof T]: T[K] extends readonly [infer V, infer F] ? V : any;
|
|
9
|
-
} & Enum;
|
|
10
|
-
interface IArguConf {
|
|
11
|
-
arguType?: "key" | "value";
|
|
12
|
-
}
|
|
13
|
-
interface IFeildConf {
|
|
14
|
-
labelKey?: string;
|
|
15
|
-
valueKey?: string;
|
|
16
|
-
}
|
|
17
|
-
type IConf = IArguConf & IFeildConf;
|
|
18
|
-
type IArgLastConfig<U, T extends IEMap> = U extends [...infer Rest, infer Last] ? Last extends IConf ? (Last["labelKey"] extends string ? {
|
|
19
|
-
[K in Last["labelKey"]]: TEValue<T>;
|
|
20
|
-
} : {
|
|
21
|
-
label: string;
|
|
22
|
-
}) & (Last["valueKey"] extends string ? {
|
|
23
|
-
[K in Last["valueKey"]]: any;
|
|
24
|
-
} : {
|
|
25
|
-
value: TEValue<T>;
|
|
26
|
-
}) : {
|
|
27
|
-
label: string;
|
|
28
|
-
value: TEValue<T>;
|
|
29
|
-
} : never;
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* 枚举类
|
|
33
|
-
* @param {Object} enumMap 枚举对象
|
|
34
|
-
* 枚举格式:
|
|
35
|
-
* {
|
|
36
|
-
* 枚举key1: [枚举值1,枚举描述1],
|
|
37
|
-
* 枚举key2: [枚举值2,枚举描述2]
|
|
38
|
-
* }
|
|
39
|
-
*/
|
|
4
|
+
type IEnum<T extends IEMap> = {
|
|
5
|
+
readonly [K in keyof T]: T[K] extends readonly [infer V, any] ? V : any;
|
|
6
|
+
} & Enum<T>;
|
|
40
7
|
declare class Enum<T extends IEMap> {
|
|
41
8
|
#private;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
private __valueNameMap__;
|
|
9
|
+
protected __enumMap__: T;
|
|
10
|
+
protected __enumLabelMap__: any;
|
|
11
|
+
protected __enumExtraMap__: any;
|
|
46
12
|
/**
|
|
47
13
|
* @param {Object} enumMap 枚举map
|
|
48
14
|
*/
|
|
@@ -52,62 +18,37 @@ declare class Enum<T extends IEMap> {
|
|
|
52
18
|
* @param {String} key 枚举KEY
|
|
53
19
|
* @return {Number} 枚举值
|
|
54
20
|
*/
|
|
55
|
-
|
|
21
|
+
value(key: TEUnion<T>): TEValue<T>;
|
|
56
22
|
/**
|
|
57
23
|
* 获取多个枚举值
|
|
58
24
|
* @param {Array} param 多个枚举KEY
|
|
59
25
|
* @return {Array} {[枚举值]}
|
|
60
26
|
*/
|
|
61
|
-
|
|
27
|
+
values(...args: TEUnion<T>[]): any | TEValue<T>[];
|
|
62
28
|
/**
|
|
63
29
|
* 获取多个枚举值Map
|
|
64
30
|
* @param {Array} param 多个枚举KEY,如果不传递则返回所有
|
|
65
31
|
* @return {Object} {[枚举key]:枚举值}
|
|
66
32
|
*/
|
|
67
|
-
|
|
33
|
+
options<V extends TEUnion<T>>(...args: V[]): {
|
|
34
|
+
value: TEValue<T>;
|
|
35
|
+
label: string;
|
|
36
|
+
extra: any;
|
|
37
|
+
}[];
|
|
68
38
|
/**
|
|
69
39
|
* 获取枚举名称
|
|
70
40
|
* @param {String} keyOrVal 枚举KEY或枚举值
|
|
71
|
-
* @param {String} arguType 枚举KEY的类型,key|value
|
|
72
41
|
* @return {String} 枚举名称
|
|
73
42
|
*/
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}>(keyOrVal: TEUnion<T>, _config?: U): string;
|
|
77
|
-
getName<U extends {
|
|
78
|
-
arguType: "value";
|
|
79
|
-
}>(keyOrVal: TEValue<T>, _config?: U): string;
|
|
80
|
-
/**
|
|
81
|
-
* 通过枚举值获取枚举名称
|
|
82
|
-
* @param {String|Nunber} val
|
|
83
|
-
* @return {String|Null}
|
|
84
|
-
*/
|
|
85
|
-
getNameByValue(val: TEValue<T>): string;
|
|
86
|
-
/**
|
|
87
|
-
* 通过枚举KEY或枚举值获取列表选项
|
|
88
|
-
* @param {Array} param 多个枚举KEY或枚举值,如果不传递则返回所有。最后一位可以为选项的配置项
|
|
89
|
-
* @returns {Array} 选项名称、选项值的列表
|
|
90
|
-
*/
|
|
91
|
-
getOptions<U extends TEUnion<T>[]>(..._args: U): {
|
|
92
|
-
label: string;
|
|
93
|
-
value: TEValue<T>;
|
|
94
|
-
}[];
|
|
95
|
-
getOptions<U extends [...TEUnion<T>[], {
|
|
96
|
-
arguType?: "key";
|
|
97
|
-
} & IFeildConf]>(..._args: U): IArgLastConfig<U, T>[];
|
|
98
|
-
getOptions<U extends [...TEValue<T>[], {
|
|
99
|
-
arguType: "value";
|
|
100
|
-
} & IFeildConf]>(..._args: U): IArgLastConfig<U, T>[];
|
|
43
|
+
label(keyOrVal: TEUnion<T> | TEValue<T>): string;
|
|
44
|
+
label(keyOrVal: any): any;
|
|
101
45
|
/**
|
|
102
|
-
*
|
|
103
|
-
* @param
|
|
104
|
-
* @
|
|
46
|
+
* 获取枚举关联参数
|
|
47
|
+
* @param {String} keyOrVal 枚举KEY或枚举值
|
|
48
|
+
* @return {String} 枚举名称
|
|
105
49
|
*/
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
label: string;
|
|
109
|
-
value: TEValue<T>;
|
|
110
|
-
}[];
|
|
50
|
+
extra(keyOrVal: TEUnion<T> | TEValue<T>): any;
|
|
51
|
+
extra(keyOrVal: any): any;
|
|
111
52
|
/**
|
|
112
53
|
* 检测字段类型
|
|
113
54
|
* @param {Number} typeVal 类型
|
|
@@ -116,12 +57,6 @@ declare class Enum<T extends IEMap> {
|
|
|
116
57
|
*/
|
|
117
58
|
check(typeVal: any, typeKey: TEUnion<T>): boolean;
|
|
118
59
|
}
|
|
119
|
-
|
|
120
|
-
* 创建枚举
|
|
121
|
-
* @param {Object} enumMap
|
|
122
|
-
* @param {String} description
|
|
123
|
-
* @returns
|
|
124
|
-
*/
|
|
125
|
-
declare const createEnum: <T extends IEMap>(enumMap: T) => IEnum<T, Enum<T>>;
|
|
60
|
+
declare const createEnum: <T extends IEMap>(enumMap: T) => Readonly<IEnum<T>>;
|
|
126
61
|
|
|
127
|
-
export { createEnum };
|
|
62
|
+
export { createEnum as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-enum-es",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "处理前端枚举的工具方法,支持ts类型检测",
|
|
5
5
|
"main": "dist/create-enum.es.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -31,9 +31,14 @@
|
|
|
31
31
|
"author": "Xiou.Wang",
|
|
32
32
|
"license": "ISC",
|
|
33
33
|
"devDependencies": {
|
|
34
|
+
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
35
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
34
36
|
"@rollup/plugin-typescript": "^11.1.6",
|
|
35
|
-
"rollup": "^4.
|
|
37
|
+
"rollup": "^4.45.1",
|
|
36
38
|
"rollup-plugin-dts": "^6.1.1",
|
|
37
39
|
"typescript": "^5.5.3"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"lodash-es": "^4.17.21"
|
|
38
43
|
}
|
|
39
44
|
}
|