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