create-enum-es 1.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 ADDED
@@ -0,0 +1,82 @@
1
+ ```
2
+ import { createEnum } from '../dist/emun-create.es.js';
3
+
4
+ const enum1 = createEnum({
5
+ On: [1, '开启'],
6
+ Off: [0, '关闭'],
7
+ })
8
+
9
+ console.log(enum1.getVal('On')) // 1
10
+ console.log(enum1.getValList('On', 'Off')) // [1, 0]
11
+ console.log(enum1.getValList('On')) // [1]
12
+ console.log(enum1.getValList()) // [1, 0]
13
+
14
+
15
+ console.log(enum1.getValMap('On', 'Off')) // {On: 1, Off: 0}
16
+ console.log(enum1.getValMap('Off')) // {Off: 0}
17
+ console.log(enum1.getValMap()) // {On: 1, Off: 0}
18
+
19
+
20
+ console.log(enum1.getName('On')) // 开启
21
+ console.log(enum1.getNameByValue(enum1.Off)) // 关闭
22
+ console.log(enum1.getName(enum1.On, { arguType: 'value' })) // 开启
23
+
24
+
25
+ console.log(enum1.getOptions())
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())
62
+ // [
63
+ // {
64
+ // "value": 1,
65
+ // "label": "开启"
66
+ // },
67
+ // {
68
+ // "value": 0,
69
+ // "label": "关闭"
70
+ // }
71
+ // ]
72
+ console.log(enum1.getOptionsByValues(enum1.On, { labelKey: 'name', valueKey: 'key' }))
73
+ // [
74
+ // {
75
+ // "key": 1,
76
+ // "name": "开启"
77
+ // }
78
+ // ]
79
+
80
+ console.log(enum1.check(enum1.On, 'On')) // true
81
+ ```
82
+
@@ -0,0 +1,241 @@
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;
@@ -0,0 +1,239 @@
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 };
@@ -0,0 +1,247 @@
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
+ }));
@@ -0,0 +1,97 @@
1
+ import { IEnum, IEMap, TEUnion, IVMap, IFeildConf, IArgLastConfig, TEValue } from "./typeing";
2
+ /**
3
+ * 枚举类
4
+ * @param {Object} enumMap 枚举对象
5
+ * 枚举格式:
6
+ * {
7
+ * 枚举key1: [枚举值1,枚举描述1],
8
+ * 枚举key2: [枚举值2,枚举描述2]
9
+ * }
10
+ */
11
+ declare class Enum<T extends IEMap> {
12
+ #private;
13
+ private __enumMap__;
14
+ private __enumValueMap__;
15
+ private __enumNameMap__;
16
+ private __valueNameMap__;
17
+ /**
18
+ * @param {Object} enumMap 枚举map
19
+ */
20
+ constructor(enumMap: T);
21
+ /**
22
+ * 获取枚举值
23
+ * @param {String} key 枚举KEY
24
+ * @return {Number} 枚举值
25
+ */
26
+ getVal(key: TEUnion<T>): TEValue<T>;
27
+ /**
28
+ * 获取多个枚举值
29
+ * @param {Array} param 多个枚举KEY
30
+ * @return {Array} {[枚举值]}
31
+ */
32
+ getValList(...args: TEUnion<T>[]): TEValue<T>[];
33
+ /**
34
+ * 获取多个枚举值Map
35
+ * @param {Array} param 多个枚举KEY,如果不传递则返回所有
36
+ * @return {Object} {[枚举key]:枚举值}
37
+ */
38
+ getValMap<V extends TEUnion<T>>(...args: V[]): IVMap<V, T>;
39
+ /**
40
+ * 获取枚举名称
41
+ * @param {String} keyOrVal 枚举KEY或枚举值
42
+ * @param {String} arguType 枚举KEY的类型,key|value
43
+ * @return {String} 枚举名称
44
+ */
45
+ getName<U extends {
46
+ arguType?: "key";
47
+ }>(keyOrVal: TEUnion<T>, _config?: U): string;
48
+ getName<U extends {
49
+ arguType: "value";
50
+ }>(keyOrVal: TEValue<T>, _config?: U): string;
51
+ /**
52
+ * 通过枚举值获取枚举名称
53
+ * @param {String|Nunber} val
54
+ * @return {String|Null}
55
+ */
56
+ getNameByValue(val: TEValue<T>): string;
57
+ /**
58
+ * 通过枚举KEY或枚举值获取列表选项
59
+ * @param {Array} param 多个枚举KEY或枚举值,如果不传递则返回所有。最后一位可以为选项的配置项
60
+ * @returns {Array} 选项名称、选项值的列表
61
+ */
62
+ getOptions<U extends TEUnion<T>[]>(..._args: U): {
63
+ label: string;
64
+ value: TEValue<T>;
65
+ }[];
66
+ getOptions<U extends [...TEUnion<T>[], {
67
+ arguType?: "key";
68
+ } & IFeildConf]>(..._args: U): IArgLastConfig<U, T>[];
69
+ getOptions<U extends [...TEValue<T>[], {
70
+ arguType: "value";
71
+ } & IFeildConf]>(..._args: U): IArgLastConfig<U, T>[];
72
+ /**
73
+ * 通过枚举值获取列表选项
74
+ * @param {Array} param args 多个枚举值,如果不传递则返回所有。最后一位可以为选项的配置项
75
+ * @returns {Array} 选项名称、选项值的列表
76
+ */
77
+ getOptionsByValues<U extends [...TEValue<T>[], IFeildConf]>(..._args: U): IArgLastConfig<U, T>[];
78
+ getOptionsByValues<U extends [...TEValue<T>[]]>(..._args: U): {
79
+ label: string;
80
+ value: TEValue<T>;
81
+ }[];
82
+ /**
83
+ * 检测字段类型
84
+ * @param {Number} typeVal 类型
85
+ * @param {String} typeKey 类型key
86
+ * @return {Boolean}
87
+ */
88
+ check(typeVal: any, typeKey: TEUnion<T>): boolean;
89
+ }
90
+ /**
91
+ * 创建枚举
92
+ * @param {Object} enumMap
93
+ * @param {String} description
94
+ * @returns
95
+ */
96
+ declare const createEnum: <T extends IEMap>(enumMap: T) => IEnum<T, Enum<T>>;
97
+ export { createEnum };
@@ -0,0 +1,30 @@
1
+ export type ITypeData = "undefined" | "null" | "boolean" | "number" | "string" | "symbol" | "function" | "array" | "object" | "date" | "regexp" | "error" | "map" | "set" | "weakmap" | "weakset" | "arraybuffer" | "dataview" | "promise" | "int8array" | "uint8array" | "uint8clampedarray" | "int16array" | "uint16array" | "int32array" | "uint32array" | "float32array" | "float64array" | "bigint64array" | "biguint64array";
2
+ export type IEMap = Readonly<Record<any, readonly [any, any]>>;
3
+ export type TEUnion<T extends IEMap> = keyof T;
4
+ export type TEValue<T extends IEMap> = T[TEUnion<T>][0];
5
+ export type IVMap<U extends string | number | symbol, T extends IEMap> = {
6
+ [K in U]: T[K] extends readonly [infer V, infer F] ? V : any;
7
+ };
8
+ export type IEnum<T extends IEMap, Enum> = {
9
+ readonly [K in keyof T]: T[K] extends readonly [infer V, infer F] ? V : any;
10
+ } & Enum;
11
+ export interface IArguConf {
12
+ arguType?: "key" | "value";
13
+ }
14
+ export interface IFeildConf {
15
+ labelKey?: string;
16
+ valueKey?: string;
17
+ }
18
+ export type IConf = IArguConf & IFeildConf;
19
+ export type IArgLastConfig<U, T extends IEMap> = U extends [...infer Rest, infer Last] ? Last extends IConf ? (Last["labelKey"] extends string ? {
20
+ [K in Last["labelKey"]]: TEValue<T>;
21
+ } : {
22
+ label: string;
23
+ }) & (Last["valueKey"] extends string ? {
24
+ [K in Last["valueKey"]]: any;
25
+ } : {
26
+ value: TEValue<T>;
27
+ }) : {
28
+ label: string;
29
+ value: TEValue<T>;
30
+ } : never;
@@ -0,0 +1,20 @@
1
+ import { ITypeData } from "../typeing";
2
+ /**
3
+ * 数据类型
4
+ * @param {*} data
5
+ * @param {String} type
6
+ * @returns {Boolean}
7
+ */
8
+ export declare function isType<T>(data: T, type: ITypeData): boolean;
9
+ /**
10
+ * 数据是否为空
11
+ * @param {*} data
12
+ * @returns
13
+ */
14
+ export declare function isEmpty<T>(data: T): boolean;
15
+ /**
16
+ * 深度拷贝
17
+ * @param {Object|Array} obj
18
+ * @return {Object|Array}
19
+ */
20
+ export declare function deepClone(obj: any): {};
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "create-enum-es",
3
+ "version": "1.0.0",
4
+ "description": "处理前端枚举的工具方法,支持ts类型检测",
5
+ "main": "dist/create-enum.es.js",
6
+ "private": false,
7
+ "files": [
8
+ "dist/",
9
+ "package.json",
10
+ "README.md"
11
+ ],
12
+ "keywords": [
13
+ "enum",
14
+ "枚举",
15
+ "TS"
16
+ ],
17
+ "type": "module",
18
+ "scripts": {
19
+ "test": "echo \"Error: no test specified\" && exit 1",
20
+ "build": "rollup -c",
21
+ "dev": "rollup -cw"
22
+ },
23
+ "author": "Xiou.Wang",
24
+ "license": "ISC",
25
+ "devDependencies": {
26
+ "@rollup/plugin-typescript": "^11.1.6",
27
+ "rollup": "^4.18.1",
28
+ "typescript": "^5.5.3"
29
+ }
30
+ }