create-enum-es 1.0.3 → 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 +317 -66
- 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 +158 -101
- package/package.json +7 -4
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
|
+
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,247 +1 @@
|
|
|
1
|
-
(function (
|
|
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(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});
|