@haluo/util 2.0.29 → 2.0.31

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.
Files changed (59) hide show
  1. package/README.md +350 -350
  2. package/dist/index.d.ts +39 -39
  3. package/dist/index.js +43 -43
  4. package/dist/modules/cookie/index.d.ts +27 -27
  5. package/dist/modules/cookie/index.js +49 -49
  6. package/dist/modules/date/index.d.ts +52 -52
  7. package/dist/modules/date/index.js +185 -185
  8. package/dist/modules/dom/index.d.ts +28 -28
  9. package/dist/modules/dom/index.js +55 -55
  10. package/dist/modules/filter/index.d.ts +24 -24
  11. package/dist/modules/filter/index.js +38 -38
  12. package/dist/modules/format/index.d.ts +15 -15
  13. package/dist/modules/format/index.js +16 -16
  14. package/dist/modules/match/index.d.ts +12 -12
  15. package/dist/modules/match/index.js +27 -27
  16. package/dist/modules/monitor/index.d.ts +3 -3
  17. package/dist/modules/monitor/index.js +10 -10
  18. package/dist/modules/monitor/lib/jsError.d.ts +1 -1
  19. package/dist/modules/monitor/lib/jsError.js +57 -57
  20. package/dist/modules/monitor/lib/timing.d.ts +1 -1
  21. package/dist/modules/monitor/lib/timing.js +65 -65
  22. package/dist/modules/monitor/lib/xhr.d.ts +1 -1
  23. package/dist/modules/monitor/lib/xhr.js +41 -41
  24. package/dist/modules/monitor/utils/onload.d.ts +1 -1
  25. package/dist/modules/monitor/utils/onload.js +8 -8
  26. package/dist/modules/monitor/utils/tracker.d.ts +7 -7
  27. package/dist/modules/monitor/utils/tracker.js +49 -49
  28. package/dist/modules/number/index.d.ts +47 -47
  29. package/dist/modules/number/index.js +114 -114
  30. package/dist/modules/open-app/index.d.ts +92 -84
  31. package/dist/modules/open-app/index.js +260 -244
  32. package/dist/modules/sentry/index.d.ts +15 -15
  33. package/dist/modules/sentry/index.js +73 -73
  34. package/dist/modules/tools/index.d.ts +166 -166
  35. package/dist/modules/tools/index.js +382 -382
  36. package/dist/modules/upload/aliOss.d.ts +291 -291
  37. package/dist/modules/upload/aliOss.js +629 -629
  38. package/dist/modules/upload/index.d.ts +38 -38
  39. package/dist/modules/upload/index.js +44 -44
  40. package/dist/tsconfig.tsbuildinfo +1 -1
  41. package/dist/types/index.d.ts +3 -3
  42. package/dist/types/index.js +1 -1
  43. package/dist/types/modules/cookie/index.d.ts +27 -27
  44. package/dist/types/modules/date/index.d.ts +52 -52
  45. package/dist/types/modules/dom/index.d.ts +28 -28
  46. package/dist/types/modules/filter/index.d.ts +24 -24
  47. package/dist/types/modules/format/index.d.ts +15 -15
  48. package/dist/types/modules/match/index.d.ts +12 -12
  49. package/dist/types/modules/monitor/index.d.ts +3 -3
  50. package/dist/types/modules/monitor/lib/jsError.d.ts +1 -1
  51. package/dist/types/modules/monitor/lib/timing.d.ts +1 -1
  52. package/dist/types/modules/monitor/lib/xhr.d.ts +1 -1
  53. package/dist/types/modules/monitor/utils/onload.d.ts +1 -1
  54. package/dist/types/modules/monitor/utils/tracker.d.ts +7 -7
  55. package/dist/types/modules/number/index.d.ts +41 -41
  56. package/dist/types/modules/sentry/index.d.ts +15 -15
  57. package/dist/types/modules/tools/index.d.ts +166 -166
  58. package/dist/types/types/index.d.ts +3 -3
  59. package/package.json +88 -88
@@ -1,382 +1,382 @@
1
- /**
2
- * @file: tools 常用的工具函数
3
- * @Author: wanghui
4
- */
5
- 'use strict';
6
- let previous = 0;
7
- let timeout = null;
8
- import cloneDeep from 'lodash/cloneDeep';
9
- import copy from 'clipboard-copy';
10
- class ToolsClass {
11
- constructor() {
12
- this.__loaded__ = {}; // 已加载的资源
13
- /**
14
- * 阻止事件传递
15
- * @param {Event} e
16
- * @return {Void}
17
- */
18
- this.__setDefault__ = (e) => {
19
- e && e.preventDefault();
20
- };
21
- }
22
- /**
23
- * 深拷贝 Object|Array
24
- * 深拷贝,支持 普通对象、数组,但是未解决Function、Date、RegExp,且1M以上数据性能不好
25
- * @param {String} data
26
- * @return {Object}
27
- */
28
- deepCopy(data) {
29
- return JSON.parse(JSON.stringify(data));
30
- }
31
- /**
32
- * 深拷贝 Object|Array
33
- * 支持 普通对象、数组和函数的深复制,但是未解决循环引用、Date、RegExp
34
- * @param {Object|Array} data
35
- * @return {Object}
36
- */
37
- deepCopy2(obj) {
38
- const _obj = Array.isArray(obj) ? [] : {};
39
- for (const i in obj) {
40
- _obj[i] = typeof obj[i] === 'object' ? this.deepCopy2(obj[i]) : obj[i];
41
- }
42
- return _obj;
43
- }
44
- /**
45
- * 深拷贝 Object|Array
46
- * 注意:需要 yarn add lodash
47
- * @param {Object|Array} data
48
- * @return {Object}
49
- */
50
- deepCopy3(obj) {
51
- return cloneDeep(obj);
52
- }
53
- /**
54
- * 防抖
55
- * demo:debounce(func, 300)(args)
56
- * @param {Function} func 执行函数
57
- * @param {Number} wait 节流时间,毫秒
58
- * @return {Function} delay
59
- */
60
- debounce(func, wait) {
61
- const delay = function () {
62
- const args = arguments;
63
- if (timeout)
64
- clearTimeout(timeout);
65
- timeout = setTimeout(() => {
66
- func.apply(delay, args);
67
- }, wait);
68
- };
69
- return delay;
70
- }
71
- /**
72
- * 节流
73
- * demo:throttle(func, 300)(args)
74
- * @param {Object|Array} func 执行函数
75
- * @param {Number} wait 节流时间,毫秒
76
- * @return {Function} delay
77
- */
78
- throttle(func, wait) {
79
- const delay = function () {
80
- const now = Date.now();
81
- if (now - previous > wait) {
82
- func.apply(delay, arguments);
83
- previous = now;
84
- }
85
- };
86
- return delay;
87
- }
88
- /**
89
- * 获取路径中文件名称
90
- * @param {String} url
91
- * @return {String}
92
- */
93
- getUrlName(url) {
94
- return (url && url.split('?')[0].split('/').reverse()[0]);
95
- }
96
- /**
97
- * 动态加载脚本
98
- * tip:按需加载时用(多次调用,js不会重复加载)
99
- * @param {String} url
100
- * @return {Promise}
101
- */
102
- loadJs(url) {
103
- if (!(window && window.document)) {
104
- return new Error('仅支持浏览器');
105
- }
106
- const name = this.getUrlName(url);
107
- const id = 'js_' + name;
108
- return new Promise((resolve, reject) => {
109
- if (this.__loaded__[id]) {
110
- return resolve();
111
- }
112
- const script = document.createElement('script');
113
- script.type = 'text/javascript';
114
- script.async = true;
115
- script.src = url;
116
- script.id = id;
117
- script.onload = () => {
118
- this.__loaded__[id] = true;
119
- resolve();
120
- };
121
- script.onerror = (e) => {
122
- reject(e);
123
- };
124
- document.body.appendChild(script);
125
- });
126
- }
127
- /**
128
- * 动态加载样式
129
- * @param {String} url
130
- * @return {Promise}
131
- */
132
- loadCss(url) {
133
- if (!(window && window.document)) {
134
- return new Error('仅支持浏览器');
135
- }
136
- const name = this.getUrlName(url);
137
- const id = 'css_' + name;
138
- return new Promise((resolve, reject) => {
139
- if (this.__loaded__[id]) {
140
- return resolve();
141
- }
142
- const link = document.createElement('link');
143
- link.type = 'text/css';
144
- link.rel = 'stylesheet';
145
- link.href = url;
146
- link.id = id;
147
- link.onload = () => {
148
- this.__loaded__[id] = true;
149
- resolve();
150
- };
151
- link.onerror = (e) => {
152
- reject(e);
153
- };
154
- document.head.appendChild(link);
155
- });
156
- }
157
- /**
158
- * 蒙层显示后,html禁止滚动操作
159
- * @param {String} className
160
- * @return {Void}
161
- */
162
- stopScroll = (className) => {
163
- if (!(window && window.document)) {
164
- return new Error('仅支持浏览器');
165
- }
166
- const html = document.documentElement;
167
- html.style.overflow = 'hidden';
168
- html.style.height = '100%';
169
- const body = document.body;
170
- body.style.overflow = 'hidden';
171
- body.style.height = '100%';
172
- if (className) {
173
- const dom = document.querySelector(`.${className}`);
174
- dom && dom.addEventListener('touchmove', this.__setDefault__);
175
- }
176
- };
177
- /**
178
- * 蒙层隐藏后,html开启滚动操作
179
- * @param {String} className
180
- * @return {Void}
181
- */
182
- startScroll = (className) => {
183
- if (!(window && window.document)) {
184
- return new Error('仅支持浏览器');
185
- }
186
- const html = document.documentElement;
187
- html.style.overflow = 'visible';
188
- html.style.height = 'auto';
189
- const body = document.body;
190
- body.style.overflow = 'visible';
191
- body.style.height = 'auto';
192
- if (className) {
193
- const dom = document.querySelector(`.${className}`);
194
- dom && dom.removeEventListener('touchmove', this.__setDefault__);
195
- }
196
- };
197
- /**
198
- * 字符串复制到剪贴板
199
- * 注意:需要 yarn add clipboard-copy
200
- * @param {String} str
201
- * @return {String}
202
- */
203
- clipboard(str) {
204
- if (!(window && window.document)) {
205
- return new Error('仅支持浏览器');
206
- }
207
- return copy(str);
208
- }
209
- /**
210
- * 首字母大写
211
- * @param {String} str
212
- * @return {String}
213
- */
214
- firstUpperCase(str) {
215
- return str.charAt(0).toUpperCase() + str.toString().slice(1);
216
- }
217
- /**
218
- * 截取数组或字符串,示例:slice('1234', 3)
219
- * @param {Array|String} target 数组或字符串
220
- * @param {Number} length 截取长度,从0开始
221
- * @return {any}
222
- */
223
- slice(target = '', length = 0) {
224
- return target.slice(0, length);
225
- }
226
- /**
227
- * 获取guid
228
- * @return {String}
229
- */
230
- guid() {
231
- function S4() {
232
- return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
233
- }
234
- return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
235
- }
236
- /**
237
- * 获取文本字节数(含中文)
238
- * @param {String} str
239
- * @return {String}
240
- */
241
- getBytesOfText(str = '') {
242
- return str.replace(/[^\u0000-\u00ff]/g, "aa").length;
243
- }
244
- /**
245
- * object 对象转 array 数组
246
- * demo:
247
- * objectToArray({a:1,b:2}) 输出:["a=1", "b=2"]
248
- * @param {Object} obj
249
- * @returns {Array} arr
250
- */
251
- objectToArray = (obj) => {
252
- var arr = [];
253
- if (typeof obj === 'object') {
254
- for (var key in obj) {
255
- if (obj.hasOwnProperty(key)) {
256
- arr.push([key, obj[key]].join('='));
257
- }
258
- }
259
- }
260
- return arr;
261
- };
262
- /**
263
- * convertEnum
264
- * 枚举键值互换
265
- * @param {Object} obj
266
- * convertKeyValueEnum({a: 1, b: 2}) // {1: "a", 2: "b"}
267
- * @returns {Object} result
268
- */
269
- convertKeyValueEnum = (obj) => {
270
- const result = {};
271
- if (typeof obj === 'object') {
272
- for (const key in obj) {
273
- if (obj.hasOwnProperty(key)) {
274
- result[obj[key]] = key;
275
- }
276
- }
277
- }
278
- return result;
279
- };
280
- /**
281
- * 数组去重
282
- * @param {Array} arr
283
- * @returns {Array}
284
- */
285
- uniqueArr(arr) {
286
- return Array.from(new Set(arr));
287
- }
288
- /**
289
- * 数组元素交换位置
290
- * index1和index2分别是两个数组的索引值,即是两个要交换元素位置的索引值,如1,5就是数组中下标为1和5的两个元素交换位置
291
- * @param {Array<string | number>} array 数组
292
- * @param {number} index1 添加项目的位置
293
- * @param {number} index2 删除项目的位置
294
- * swapArray([1, 2, 3, 4], 2, 3) // [1, 2, 4, 3]
295
- * @returns {Array<string | number>} array
296
- */
297
- swapArray(array, index1, index2) {
298
- [array[index1], array[index2]] = [array[index2], array[index1]];
299
- return array;
300
- }
301
- /**
302
- * 过滤表情符号
303
- * @param {String} str
304
- * @return {String}
305
- */
306
- filterEmoji(str) {
307
- return str.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/mg, '');
308
- }
309
- /**
310
- * 是否包含表情
311
- * @param {String} str
312
- * @return {boolean}
313
- */
314
- containsEmoji(str) {
315
- const reg = /\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/mg;
316
- return reg.test(str);
317
- }
318
- /**
319
- * 是否包含表汉字
320
- * @param {String} str
321
- * @return {boolean}
322
- */
323
- containsHanZi(str) {
324
- const reg = /[\u4e00-\u9fa5]/mg;
325
- return reg.test(str);
326
- }
327
- isEmpty(val) {
328
- // null or undefined
329
- if (val == null)
330
- return true;
331
- if (typeof val === 'boolean')
332
- return false;
333
- if (typeof val === 'number')
334
- return !val;
335
- if (val instanceof Error)
336
- return val.message === '';
337
- switch (Object.prototype.toString.call(val)) {
338
- // String or Array
339
- case '[object String]':
340
- case '[object Array]':
341
- return !val.length;
342
- // Map or Set or File
343
- case '[object File]':
344
- case '[object Map]':
345
- case '[object Set]': {
346
- return !val.size;
347
- }
348
- // Plain Object
349
- case '[object Object]': {
350
- return !Object.keys(val).length;
351
- }
352
- }
353
- return false;
354
- }
355
- ;
356
- isDefined = (val) => {
357
- return val !== undefined && val !== null;
358
- };
359
- /**
360
- * 字段脱敏处理
361
- * @param {String} field 未脱敏字段
362
- * @param {Int} before 开头未脱敏字符数
363
- * @param {Int} after 结尾未脱敏字符数
364
- * @return {String} 已脱敏字段
365
- */
366
- sensitiveField(field, before = 3, after = 4) {
367
- if (!field) {
368
- return '';
369
- }
370
- field = String(field);
371
- let sensitiveLen = field.length - before - after;
372
- if (sensitiveLen < 0) {
373
- sensitiveLen = 0;
374
- }
375
- // 匹配中文、英文、数字
376
- const regItem = '[\u4e00-\u9fa5a-zA-Z0-9]';
377
- const regExp = `(${regItem}{${before}})${regItem}*(${regItem}{${after}})`;
378
- const reg = new RegExp(regExp);
379
- return field.replace(reg, `$1${"*".repeat(sensitiveLen)}$2`);
380
- }
381
- }
382
- export default new ToolsClass();
1
+ /**
2
+ * @file: tools 常用的工具函数
3
+ * @Author: wanghui
4
+ */
5
+ 'use strict';
6
+ let previous = 0;
7
+ let timeout = null;
8
+ import cloneDeep from 'lodash/cloneDeep';
9
+ import copy from 'clipboard-copy';
10
+ class ToolsClass {
11
+ constructor() {
12
+ this.__loaded__ = {}; // 已加载的资源
13
+ /**
14
+ * 阻止事件传递
15
+ * @param {Event} e
16
+ * @return {Void}
17
+ */
18
+ this.__setDefault__ = (e) => {
19
+ e && e.preventDefault();
20
+ };
21
+ }
22
+ /**
23
+ * 深拷贝 Object|Array
24
+ * 深拷贝,支持 普通对象、数组,但是未解决Function、Date、RegExp,且1M以上数据性能不好
25
+ * @param {String} data
26
+ * @return {Object}
27
+ */
28
+ deepCopy(data) {
29
+ return JSON.parse(JSON.stringify(data));
30
+ }
31
+ /**
32
+ * 深拷贝 Object|Array
33
+ * 支持 普通对象、数组和函数的深复制,但是未解决循环引用、Date、RegExp
34
+ * @param {Object|Array} data
35
+ * @return {Object}
36
+ */
37
+ deepCopy2(obj) {
38
+ const _obj = Array.isArray(obj) ? [] : {};
39
+ for (const i in obj) {
40
+ _obj[i] = typeof obj[i] === 'object' ? this.deepCopy2(obj[i]) : obj[i];
41
+ }
42
+ return _obj;
43
+ }
44
+ /**
45
+ * 深拷贝 Object|Array
46
+ * 注意:需要 yarn add lodash
47
+ * @param {Object|Array} data
48
+ * @return {Object}
49
+ */
50
+ deepCopy3(obj) {
51
+ return cloneDeep(obj);
52
+ }
53
+ /**
54
+ * 防抖
55
+ * demo:debounce(func, 300)(args)
56
+ * @param {Function} func 执行函数
57
+ * @param {Number} wait 节流时间,毫秒
58
+ * @return {Function} delay
59
+ */
60
+ debounce(func, wait) {
61
+ const delay = function () {
62
+ const args = arguments;
63
+ if (timeout)
64
+ clearTimeout(timeout);
65
+ timeout = setTimeout(() => {
66
+ func.apply(delay, args);
67
+ }, wait);
68
+ };
69
+ return delay;
70
+ }
71
+ /**
72
+ * 节流
73
+ * demo:throttle(func, 300)(args)
74
+ * @param {Object|Array} func 执行函数
75
+ * @param {Number} wait 节流时间,毫秒
76
+ * @return {Function} delay
77
+ */
78
+ throttle(func, wait) {
79
+ const delay = function () {
80
+ const now = Date.now();
81
+ if (now - previous > wait) {
82
+ func.apply(delay, arguments);
83
+ previous = now;
84
+ }
85
+ };
86
+ return delay;
87
+ }
88
+ /**
89
+ * 获取路径中文件名称
90
+ * @param {String} url
91
+ * @return {String}
92
+ */
93
+ getUrlName(url) {
94
+ return (url && url.split('?')[0].split('/').reverse()[0]);
95
+ }
96
+ /**
97
+ * 动态加载脚本
98
+ * tip:按需加载时用(多次调用,js不会重复加载)
99
+ * @param {String} url
100
+ * @return {Promise}
101
+ */
102
+ loadJs(url) {
103
+ if (!(window && window.document)) {
104
+ return new Error('仅支持浏览器');
105
+ }
106
+ const name = this.getUrlName(url);
107
+ const id = 'js_' + name;
108
+ return new Promise((resolve, reject) => {
109
+ if (this.__loaded__[id]) {
110
+ return resolve();
111
+ }
112
+ const script = document.createElement('script');
113
+ script.type = 'text/javascript';
114
+ script.async = true;
115
+ script.src = url;
116
+ script.id = id;
117
+ script.onload = () => {
118
+ this.__loaded__[id] = true;
119
+ resolve();
120
+ };
121
+ script.onerror = (e) => {
122
+ reject(e);
123
+ };
124
+ document.body.appendChild(script);
125
+ });
126
+ }
127
+ /**
128
+ * 动态加载样式
129
+ * @param {String} url
130
+ * @return {Promise}
131
+ */
132
+ loadCss(url) {
133
+ if (!(window && window.document)) {
134
+ return new Error('仅支持浏览器');
135
+ }
136
+ const name = this.getUrlName(url);
137
+ const id = 'css_' + name;
138
+ return new Promise((resolve, reject) => {
139
+ if (this.__loaded__[id]) {
140
+ return resolve();
141
+ }
142
+ const link = document.createElement('link');
143
+ link.type = 'text/css';
144
+ link.rel = 'stylesheet';
145
+ link.href = url;
146
+ link.id = id;
147
+ link.onload = () => {
148
+ this.__loaded__[id] = true;
149
+ resolve();
150
+ };
151
+ link.onerror = (e) => {
152
+ reject(e);
153
+ };
154
+ document.head.appendChild(link);
155
+ });
156
+ }
157
+ /**
158
+ * 蒙层显示后,html禁止滚动操作
159
+ * @param {String} className
160
+ * @return {Void}
161
+ */
162
+ stopScroll = (className) => {
163
+ if (!(window && window.document)) {
164
+ return new Error('仅支持浏览器');
165
+ }
166
+ const html = document.documentElement;
167
+ html.style.overflow = 'hidden';
168
+ html.style.height = '100%';
169
+ const body = document.body;
170
+ body.style.overflow = 'hidden';
171
+ body.style.height = '100%';
172
+ if (className) {
173
+ const dom = document.querySelector(`.${className}`);
174
+ dom && dom.addEventListener('touchmove', this.__setDefault__);
175
+ }
176
+ };
177
+ /**
178
+ * 蒙层隐藏后,html开启滚动操作
179
+ * @param {String} className
180
+ * @return {Void}
181
+ */
182
+ startScroll = (className) => {
183
+ if (!(window && window.document)) {
184
+ return new Error('仅支持浏览器');
185
+ }
186
+ const html = document.documentElement;
187
+ html.style.overflow = 'visible';
188
+ html.style.height = 'auto';
189
+ const body = document.body;
190
+ body.style.overflow = 'visible';
191
+ body.style.height = 'auto';
192
+ if (className) {
193
+ const dom = document.querySelector(`.${className}`);
194
+ dom && dom.removeEventListener('touchmove', this.__setDefault__);
195
+ }
196
+ };
197
+ /**
198
+ * 字符串复制到剪贴板
199
+ * 注意:需要 yarn add clipboard-copy
200
+ * @param {String} str
201
+ * @return {String}
202
+ */
203
+ clipboard(str) {
204
+ if (!(window && window.document)) {
205
+ return new Error('仅支持浏览器');
206
+ }
207
+ return copy(str);
208
+ }
209
+ /**
210
+ * 首字母大写
211
+ * @param {String} str
212
+ * @return {String}
213
+ */
214
+ firstUpperCase(str) {
215
+ return str.charAt(0).toUpperCase() + str.toString().slice(1);
216
+ }
217
+ /**
218
+ * 截取数组或字符串,示例:slice('1234', 3)
219
+ * @param {Array|String} target 数组或字符串
220
+ * @param {Number} length 截取长度,从0开始
221
+ * @return {any}
222
+ */
223
+ slice(target = '', length = 0) {
224
+ return target.slice(0, length);
225
+ }
226
+ /**
227
+ * 获取guid
228
+ * @return {String}
229
+ */
230
+ guid() {
231
+ function S4() {
232
+ return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
233
+ }
234
+ return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
235
+ }
236
+ /**
237
+ * 获取文本字节数(含中文)
238
+ * @param {String} str
239
+ * @return {String}
240
+ */
241
+ getBytesOfText(str = '') {
242
+ return str.replace(/[^\u0000-\u00ff]/g, "aa").length;
243
+ }
244
+ /**
245
+ * object 对象转 array 数组
246
+ * demo:
247
+ * objectToArray({a:1,b:2}) 输出:["a=1", "b=2"]
248
+ * @param {Object} obj
249
+ * @returns {Array} arr
250
+ */
251
+ objectToArray = (obj) => {
252
+ var arr = [];
253
+ if (typeof obj === 'object') {
254
+ for (var key in obj) {
255
+ if (obj.hasOwnProperty(key)) {
256
+ arr.push([key, obj[key]].join('='));
257
+ }
258
+ }
259
+ }
260
+ return arr;
261
+ };
262
+ /**
263
+ * convertEnum
264
+ * 枚举键值互换
265
+ * @param {Object} obj
266
+ * convertKeyValueEnum({a: 1, b: 2}) // {1: "a", 2: "b"}
267
+ * @returns {Object} result
268
+ */
269
+ convertKeyValueEnum = (obj) => {
270
+ const result = {};
271
+ if (typeof obj === 'object') {
272
+ for (const key in obj) {
273
+ if (obj.hasOwnProperty(key)) {
274
+ result[obj[key]] = key;
275
+ }
276
+ }
277
+ }
278
+ return result;
279
+ };
280
+ /**
281
+ * 数组去重
282
+ * @param {Array} arr
283
+ * @returns {Array}
284
+ */
285
+ uniqueArr(arr) {
286
+ return Array.from(new Set(arr));
287
+ }
288
+ /**
289
+ * 数组元素交换位置
290
+ * index1和index2分别是两个数组的索引值,即是两个要交换元素位置的索引值,如1,5就是数组中下标为1和5的两个元素交换位置
291
+ * @param {Array<string | number>} array 数组
292
+ * @param {number} index1 添加项目的位置
293
+ * @param {number} index2 删除项目的位置
294
+ * swapArray([1, 2, 3, 4], 2, 3) // [1, 2, 4, 3]
295
+ * @returns {Array<string | number>} array
296
+ */
297
+ swapArray(array, index1, index2) {
298
+ [array[index1], array[index2]] = [array[index2], array[index1]];
299
+ return array;
300
+ }
301
+ /**
302
+ * 过滤表情符号
303
+ * @param {String} str
304
+ * @return {String}
305
+ */
306
+ filterEmoji(str) {
307
+ return str.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/mg, '');
308
+ }
309
+ /**
310
+ * 是否包含表情
311
+ * @param {String} str
312
+ * @return {boolean}
313
+ */
314
+ containsEmoji(str) {
315
+ const reg = /\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/mg;
316
+ return reg.test(str);
317
+ }
318
+ /**
319
+ * 是否包含表汉字
320
+ * @param {String} str
321
+ * @return {boolean}
322
+ */
323
+ containsHanZi(str) {
324
+ const reg = /[\u4e00-\u9fa5]/mg;
325
+ return reg.test(str);
326
+ }
327
+ isEmpty(val) {
328
+ // null or undefined
329
+ if (val == null)
330
+ return true;
331
+ if (typeof val === 'boolean')
332
+ return false;
333
+ if (typeof val === 'number')
334
+ return !val;
335
+ if (val instanceof Error)
336
+ return val.message === '';
337
+ switch (Object.prototype.toString.call(val)) {
338
+ // String or Array
339
+ case '[object String]':
340
+ case '[object Array]':
341
+ return !val.length;
342
+ // Map or Set or File
343
+ case '[object File]':
344
+ case '[object Map]':
345
+ case '[object Set]': {
346
+ return !val.size;
347
+ }
348
+ // Plain Object
349
+ case '[object Object]': {
350
+ return !Object.keys(val).length;
351
+ }
352
+ }
353
+ return false;
354
+ }
355
+ ;
356
+ isDefined = (val) => {
357
+ return val !== undefined && val !== null;
358
+ };
359
+ /**
360
+ * 字段脱敏处理
361
+ * @param {String} field 未脱敏字段
362
+ * @param {Int} before 开头未脱敏字符数
363
+ * @param {Int} after 结尾未脱敏字符数
364
+ * @return {String} 已脱敏字段
365
+ */
366
+ sensitiveField(field, before = 3, after = 4) {
367
+ if (!field) {
368
+ return '';
369
+ }
370
+ field = String(field);
371
+ let sensitiveLen = field.length - before - after;
372
+ if (sensitiveLen < 0) {
373
+ sensitiveLen = 0;
374
+ }
375
+ // 匹配中文、英文、数字
376
+ const regItem = '[\u4e00-\u9fa5a-zA-Z0-9]';
377
+ const regExp = `(${regItem}{${before}})${regItem}*(${regItem}{${after}})`;
378
+ const reg = new RegExp(regExp);
379
+ return field.replace(reg, `$1${"*".repeat(sensitiveLen)}$2`);
380
+ }
381
+ }
382
+ export default new ToolsClass();