@wxhccc/es-util 1.5.2 → 2.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/LICENSE +20 -20
- package/README.md +268 -42
- package/dist/array-tree-switch.d.ts +18 -2
- package/dist/date-time.d.ts +14 -0
- package/dist/event-target-emitter.d.ts +45 -0
- package/dist/event-target-emitter.js +147 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.esm.js +541 -213
- package/dist/index.js +548 -215
- package/dist/index.min.js +3 -3
- package/dist/object-array.d.ts +7 -9
- package/dist/object-array.js +35 -17
- package/dist/optional.d.ts +6 -9
- package/dist/optional.js +136 -0
- package/dist/page-communicate.d.ts +27 -0
- package/dist/page-communicate.js +66 -0
- package/dist/promise.d.ts +19 -24
- package/dist/promise.js +25 -133
- package/dist/raf-timer.d.ts +36 -0
- package/dist/raf-timer.js +103 -0
- package/dist/types.d.ts +4 -0
- package/dist/utils.d.ts +10 -0
- package/dist/utils.js +30 -0
- package/dist/validate.js +55 -7
- package/dist/value-string-switch/byte-string.d.ts +0 -1
- package/dist/value-string-switch/index.d.ts +2 -0
- package/dist/value-string-switch/mask-data.d.ts +21 -0
- package/dist/value-string-switch/uri-b64.d.ts +8 -0
- package/dist/value-string-switch.js +72 -9
- package/package.json +21 -20
- package/types/array-tree-switch.d.ts +0 -49
- package/types/index.d.ts +0 -6
- package/types/object-array.d.ts +0 -33
- package/types/optional.d.ts +0 -50
- package/types/promise.d.ts +0 -44
- package/types/validate.d.ts +0 -2
- package/types/value-string-switch/byte-string.d.ts +0 -12
- package/types/value-string-switch/index.d.ts +0 -1
package/dist/index.js
CHANGED
|
@@ -1,43 +1,51 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @wxhccc/es-util
|
|
3
|
-
* (c)
|
|
2
|
+
* @wxhccc/es-util v2.0.0
|
|
3
|
+
* (c) 2022 wxhccc
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
6
6
|
'use strict';
|
|
7
7
|
|
|
8
8
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
9
9
|
|
|
10
|
-
const { hasOwnProperty
|
|
10
|
+
const { hasOwnProperty, toString } = Object.prototype;
|
|
11
|
+
const objForEch = (obj, callbackfn) => {
|
|
12
|
+
Object.keys(obj).forEach((key, index) => callbackfn(key, obj[key], index));
|
|
13
|
+
};
|
|
14
|
+
const isFn = (fn) => typeof fn === 'function';
|
|
15
|
+
const isArr = Array.isArray;
|
|
16
|
+
const objToString = (obj) => toString.call(obj);
|
|
17
|
+
const objType = (val) => {
|
|
18
|
+
const typeKeys = objToString(val).match(/^\[object (.*)\]$/);
|
|
19
|
+
return typeKeys ? typeKeys[1] : '';
|
|
20
|
+
};
|
|
21
|
+
const isObj = (obj) => objType(obj) === 'Object';
|
|
22
|
+
const hasOwnProp = (obj, v) => hasOwnProperty.call(obj, v);
|
|
23
|
+
|
|
11
24
|
function createNode(children, value = []) {
|
|
12
25
|
return { [children]: value };
|
|
13
26
|
}
|
|
14
|
-
/**
|
|
15
|
-
* translate array to tree
|
|
16
|
-
* @param {Array} array the node array need to be translate
|
|
17
|
-
* @param {Object} options the options
|
|
18
|
-
* @param {string} primaryKey the primary key of node
|
|
19
|
-
* @param {string} parentKey the parent's key of node
|
|
20
|
-
* @param {string} childrenKey the child's key of node
|
|
21
|
-
* @param {boolean | Function} createNode whether create root node, or function which can return root node
|
|
22
|
-
* @param {boolean | string} parentRefKey parent reference key of node
|
|
23
|
-
*/
|
|
24
27
|
function array2tree(array, options = {}) {
|
|
25
|
-
const { primaryKey: id, parentKey: pid, childrenKey: children, createRoot, parentRefKey } =
|
|
28
|
+
const { primaryKey: id, parentKey: pid, childrenKey: children, createRoot, parentRefKey } = {
|
|
26
29
|
primaryKey: 'id',
|
|
27
30
|
parentKey: 'pid',
|
|
28
31
|
childrenKey: 'children',
|
|
29
32
|
createRoot: false,
|
|
30
|
-
parentRefKey: false
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
33
|
+
parentRefKey: false,
|
|
34
|
+
...options
|
|
35
|
+
};
|
|
36
|
+
const pRefKey = parentRefKey &&
|
|
37
|
+
(typeof parentRefKey === 'string' ? parentRefKey : '_parent');
|
|
38
|
+
const nodeMap = {};
|
|
39
|
+
const treeNodes = [];
|
|
40
|
+
isArr(array) &&
|
|
41
|
+
array.forEach((item) => {
|
|
42
|
+
!hasOwnProp(nodeMap, item[id]) &&
|
|
43
|
+
(nodeMap[item[id]] = Object.assign({}, item, createNode(children)));
|
|
44
|
+
hasOwnProp(nodeMap, item[pid]) &&
|
|
45
|
+
nodeMap[item[pid]][children].push(Object.assign(nodeMap[item[id]], pRefKey ? { [pRefKey]: nodeMap[item[pid]] } : {}));
|
|
46
|
+
!item[pid] && treeNodes.push(nodeMap[item[id]]);
|
|
47
|
+
});
|
|
48
|
+
if (isFn(createRoot)) {
|
|
41
49
|
return createRoot(treeNodes);
|
|
42
50
|
}
|
|
43
51
|
else if (createRoot) {
|
|
@@ -46,22 +54,12 @@ function array2tree(array, options = {}) {
|
|
|
46
54
|
return treeNodes;
|
|
47
55
|
}
|
|
48
56
|
function nodeChildFilter(node, childrenKey) {
|
|
49
|
-
|
|
50
|
-
for (
|
|
57
|
+
const newNode = {};
|
|
58
|
+
for (const i in node) {
|
|
51
59
|
i !== childrenKey && (newNode[i] = node[i]);
|
|
52
60
|
}
|
|
53
61
|
return newNode;
|
|
54
62
|
}
|
|
55
|
-
/**
|
|
56
|
-
* translate tree to array
|
|
57
|
-
* @param {Array/Object} tree the node tree need to be translate
|
|
58
|
-
* @param {Object} options the options
|
|
59
|
-
* @param {string} primaryKey the primary key of tree node
|
|
60
|
-
* @param {string} parentKey the parent's key of tree node
|
|
61
|
-
* @param {string} childrenKey the child's key of tree node
|
|
62
|
-
* @param {boolean} hasParentKey whether create parent id in node
|
|
63
|
-
* @param {boolean} returnObject whether return object or array
|
|
64
|
-
*/
|
|
65
63
|
function tree2array(tree, options = {}) {
|
|
66
64
|
const { primaryKey: id, parentKey: pid, childrenKey: children, hasParentKey, returnObject } = Object.assign({
|
|
67
65
|
hasParentKey: true,
|
|
@@ -70,20 +68,22 @@ function tree2array(tree, options = {}) {
|
|
|
70
68
|
childrenKey: 'children',
|
|
71
69
|
returnObject: false
|
|
72
70
|
}, options);
|
|
73
|
-
|
|
74
|
-
if (!
|
|
71
|
+
const nodes = returnObject ? {} : [];
|
|
72
|
+
if (!isArr(tree) && typeof tree !== 'object')
|
|
75
73
|
return nodes;
|
|
76
74
|
function getNode(node, parentId, root = false) {
|
|
77
|
-
|
|
75
|
+
const baseNode = hasParentKey ? {} : { [pid]: parentId };
|
|
78
76
|
if (!root) {
|
|
79
77
|
const newNode = Object.assign(baseNode, nodeChildFilter(node, children));
|
|
80
|
-
returnObject
|
|
78
|
+
returnObject
|
|
79
|
+
? (nodes[node[id]] = newNode)
|
|
80
|
+
: nodes.push(newNode);
|
|
81
81
|
}
|
|
82
|
-
if (
|
|
82
|
+
if (isArr(node[children]) && node[children].length) {
|
|
83
83
|
node[children].forEach((childNode) => getNode(childNode, node[id]));
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
|
-
const rootTree =
|
|
86
|
+
const rootTree = isArr(tree) ? createNode(children, tree) : tree;
|
|
87
87
|
getNode(rootTree, null, true);
|
|
88
88
|
return nodes;
|
|
89
89
|
}
|
|
@@ -93,7 +93,43 @@ const idCardReg = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{
|
|
|
93
93
|
/* 身份证合法性验证 */
|
|
94
94
|
function ChinaIdCardValid(idNo) {
|
|
95
95
|
const idNumber = idNo.toString().toUpperCase();
|
|
96
|
-
const provCodes = [
|
|
96
|
+
const provCodes = [
|
|
97
|
+
'11',
|
|
98
|
+
'12',
|
|
99
|
+
'13',
|
|
100
|
+
'14',
|
|
101
|
+
'15',
|
|
102
|
+
'21',
|
|
103
|
+
'22',
|
|
104
|
+
'23',
|
|
105
|
+
'31',
|
|
106
|
+
'32',
|
|
107
|
+
'33',
|
|
108
|
+
'34',
|
|
109
|
+
'35',
|
|
110
|
+
'36',
|
|
111
|
+
'37',
|
|
112
|
+
'41',
|
|
113
|
+
'42',
|
|
114
|
+
'43',
|
|
115
|
+
'44',
|
|
116
|
+
'45',
|
|
117
|
+
'46',
|
|
118
|
+
'50',
|
|
119
|
+
'51',
|
|
120
|
+
'52',
|
|
121
|
+
'53',
|
|
122
|
+
'54',
|
|
123
|
+
'61',
|
|
124
|
+
'62',
|
|
125
|
+
'63',
|
|
126
|
+
'64',
|
|
127
|
+
'65',
|
|
128
|
+
'71',
|
|
129
|
+
'81',
|
|
130
|
+
'82',
|
|
131
|
+
'91'
|
|
132
|
+
];
|
|
97
133
|
const lastCodeCheck = () => {
|
|
98
134
|
if (idNumber.length === 18) {
|
|
99
135
|
const chars = idNumber.split('');
|
|
@@ -115,7 +151,9 @@ function ChinaIdCardValid(idNo) {
|
|
|
115
151
|
}
|
|
116
152
|
return true;
|
|
117
153
|
};
|
|
118
|
-
return idCardReg.test(idNumber) &&
|
|
154
|
+
return (idCardReg.test(idNumber) &&
|
|
155
|
+
provCodes.includes(idNumber.substr(0, 2)) &&
|
|
156
|
+
lastCodeCheck());
|
|
119
157
|
}
|
|
120
158
|
// 公式验证函数
|
|
121
159
|
function formulaValidate(string, variables) {
|
|
@@ -127,7 +165,8 @@ function formulaValidate(string, variables) {
|
|
|
127
165
|
const bns = /[^+\-*/]\(/; // 错误情况,(前面不是运算符
|
|
128
166
|
const blns = /\)[^+\-*/]/; // 错误情况,)后面不是运算符
|
|
129
167
|
const bmatch = () => {
|
|
130
|
-
|
|
168
|
+
// 错误情况,括号不配对
|
|
169
|
+
const stack = [];
|
|
131
170
|
for (let i = 0, item; i < string.length; i++) {
|
|
132
171
|
item = string.charAt(i);
|
|
133
172
|
if (item === '(') {
|
|
@@ -148,32 +187,54 @@ function formulaValidate(string, variables) {
|
|
|
148
187
|
const varInList = (vars) => {
|
|
149
188
|
const tmpStr = string.replace(/[()+\-*/]{1,}/g, '`');
|
|
150
189
|
const array = tmpStr.split('`');
|
|
151
|
-
return array.every(item =>
|
|
190
|
+
return array.every((item) => !/[A-Z]/i.test(item) || vars.includes(item));
|
|
152
191
|
};
|
|
153
|
-
return !sc.test(string) &&
|
|
192
|
+
return (!sc.test(string) &&
|
|
193
|
+
!eb.test(string) &&
|
|
194
|
+
!sl.test(string) &&
|
|
195
|
+
!bs.test(string) &&
|
|
196
|
+
!bns.test(string) &&
|
|
197
|
+
!blns.test(string) &&
|
|
198
|
+
bmatch() &&
|
|
199
|
+
(!isArr(variables) || varInList(variables)));
|
|
154
200
|
}
|
|
155
201
|
|
|
156
202
|
function mapToObject(objectArray, keyProp = 'key', valueProp = 'value') {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
&&
|
|
203
|
+
const result = {};
|
|
204
|
+
isArr(objectArray) &&
|
|
205
|
+
keyProp &&
|
|
206
|
+
valueProp &&
|
|
207
|
+
[keyProp, valueProp].every((val) => ['string', 'function'].includes(typeof val)) &&
|
|
208
|
+
objectArray.forEach((item, index) => {
|
|
161
209
|
const key = typeof keyProp === 'string' ? item[keyProp] : keyProp(item, index);
|
|
162
210
|
const value = typeof valueProp === 'string' ? item[valueProp] : valueProp(item, index);
|
|
163
211
|
['string', 'number'].includes(typeof key) && (result[key] = value);
|
|
164
212
|
});
|
|
165
213
|
return result;
|
|
166
214
|
}
|
|
215
|
+
/**
|
|
216
|
+
* checkout values form object as an array
|
|
217
|
+
* 从对象中挑选指定的值并以数组形式返回,必要时可使用自定义属性覆盖选中对象
|
|
218
|
+
* @param {Object} object
|
|
219
|
+
* @param {string[] | Object} keys
|
|
220
|
+
* @param {string} valueProp
|
|
221
|
+
*/
|
|
167
222
|
function checkoutBy(object, keys, mergeFn) {
|
|
168
|
-
if (!(object
|
|
223
|
+
if (!isObj(object)) {
|
|
169
224
|
return [];
|
|
170
|
-
if (Array.isArray(keys)) {
|
|
171
|
-
return keys.map(item => (object[item]));
|
|
172
225
|
}
|
|
173
|
-
|
|
174
|
-
return
|
|
175
|
-
|
|
176
|
-
|
|
226
|
+
if (isArr(keys)) {
|
|
227
|
+
return keys.map((key) => object[key]);
|
|
228
|
+
}
|
|
229
|
+
else if (keys && isObj(keys)) {
|
|
230
|
+
return Object.keys(keys).map((key) => {
|
|
231
|
+
const value = keys[key];
|
|
232
|
+
return isObj(object[key]) && isObj(value)
|
|
233
|
+
? mergeFn
|
|
234
|
+
? mergeFn({}, object[key], value)
|
|
235
|
+
: Object.assign({}, object[key], value)
|
|
236
|
+
: value || object[key];
|
|
237
|
+
});
|
|
177
238
|
}
|
|
178
239
|
return Object.values(object);
|
|
179
240
|
}
|
|
@@ -184,11 +245,12 @@ function checkoutBy(object, keys, mergeFn) {
|
|
|
184
245
|
* @param {Object} keysMap
|
|
185
246
|
*/
|
|
186
247
|
function pickRenameKeys(object, keysMap) {
|
|
187
|
-
if (!object || !(object
|
|
248
|
+
if (!object || !isObj(object) || !keysMap || !isObj(keysMap)) {
|
|
188
249
|
return;
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
250
|
+
}
|
|
251
|
+
const result = {};
|
|
252
|
+
for (const i in object) {
|
|
253
|
+
i in keysMap && (result[keysMap[i]] = object[i]);
|
|
192
254
|
}
|
|
193
255
|
return result;
|
|
194
256
|
}
|
|
@@ -206,7 +268,11 @@ const configCreater = (standard, unit) => {
|
|
|
206
268
|
Z: { expMax: 7, units: { metric: 'ZB', iec: 'ZiB', jedec: 'ZB' } },
|
|
207
269
|
Y: { expMax: 8, units: { metric: 'YB', iec: 'YiB', jedec: 'YB' } }
|
|
208
270
|
};
|
|
209
|
-
const standBase = {
|
|
271
|
+
const standBase = {
|
|
272
|
+
metric: 1000,
|
|
273
|
+
iec: 1024,
|
|
274
|
+
jedec: 1024
|
|
275
|
+
};
|
|
210
276
|
return {
|
|
211
277
|
base: standBase[standard] || standBase['jedec'],
|
|
212
278
|
unitMap: unitMap[unit] || Object.values(unitMap)
|
|
@@ -230,14 +296,14 @@ function byteStringify(byteNum, options) {
|
|
|
230
296
|
const getValue = (value) => Number.isInteger(value) ? value : value.toFixed(precision);
|
|
231
297
|
let resUnit = '';
|
|
232
298
|
let resValue = 0;
|
|
233
|
-
if (!
|
|
299
|
+
if (!isArr(unitMap)) {
|
|
234
300
|
const maxValue = Math.pow(base, unitMap.expMax);
|
|
235
301
|
resUnit = unitMap.units[standard];
|
|
236
302
|
const value = byteNumber / maxValue;
|
|
237
303
|
resValue = getValue(value * negative);
|
|
238
304
|
}
|
|
239
305
|
else {
|
|
240
|
-
unitMap.some(item => {
|
|
306
|
+
unitMap.some((item) => {
|
|
241
307
|
const maxValue = Math.pow(base, item.expMax);
|
|
242
308
|
const value = byteNumber / maxValue;
|
|
243
309
|
if (value > base)
|
|
@@ -248,16 +314,66 @@ function byteStringify(byteNum, options) {
|
|
|
248
314
|
});
|
|
249
315
|
}
|
|
250
316
|
return detail ? { value: resValue, unit: resUnit } : `${resValue} ${resUnit}`;
|
|
251
|
-
}
|
|
252
|
-
// translate string to byte num
|
|
253
|
-
function byteParse(byteStr, options = {}) {
|
|
254
317
|
}
|
|
255
318
|
|
|
256
|
-
const
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
return typeKeys ? typeKeys[1] : '';
|
|
319
|
+
const modeIndexs = {
|
|
320
|
+
telphone: { startCharIndex: -8, endCharIndex: -5 },
|
|
321
|
+
idcard: { startCharIndex: -12, endCharIndex: -5 }
|
|
260
322
|
};
|
|
323
|
+
/**
|
|
324
|
+
* 对给定的字符串进行脱敏处理
|
|
325
|
+
* @param data 需要处理的字符串
|
|
326
|
+
* @param options 配置参数对象
|
|
327
|
+
* @returns
|
|
328
|
+
*/
|
|
329
|
+
function maskData(data, options) {
|
|
330
|
+
if (typeof data !== 'string' || !data) {
|
|
331
|
+
return '';
|
|
332
|
+
}
|
|
333
|
+
const modeOpts = options && options.mode && modeIndexs[options.mode]
|
|
334
|
+
? modeIndexs[options.mode]
|
|
335
|
+
: {};
|
|
336
|
+
const { maskWith, startCharIndex: start = 0, endCharIndex: end, startFrom, endUntil } = { maskWith: '*', ...modeOpts, ...options };
|
|
337
|
+
let handleData = data;
|
|
338
|
+
let beforeChars = '';
|
|
339
|
+
let afterChars = '';
|
|
340
|
+
const startFromIndex = startFrom ? data.indexOf(startFrom) : -1;
|
|
341
|
+
if (startFromIndex > -1) {
|
|
342
|
+
const index = startFromIndex + startFrom.length;
|
|
343
|
+
handleData = handleData.slice(index);
|
|
344
|
+
beforeChars = data.slice(0, index);
|
|
345
|
+
}
|
|
346
|
+
const endUtilIndex = endUntil ? data.lastIndexOf(endUntil) : -1;
|
|
347
|
+
if (endUtilIndex > -1) {
|
|
348
|
+
handleData = handleData.slice(0, endUtilIndex);
|
|
349
|
+
afterChars = data.slice(endUtilIndex);
|
|
350
|
+
}
|
|
351
|
+
const chars = handleData.split('');
|
|
352
|
+
const newChars = [beforeChars];
|
|
353
|
+
const charLen = chars.length;
|
|
354
|
+
const indexs = [
|
|
355
|
+
start >= 0 ? start : charLen + start,
|
|
356
|
+
end !== undefined ? (end >= 0 ? end : charLen + end) : charLen - 1
|
|
357
|
+
];
|
|
358
|
+
let [startIdx, endIdx] = indexs;
|
|
359
|
+
endIdx = endIdx > 0 && endIdx < charLen ? endIdx : charLen - 1;
|
|
360
|
+
startIdx = startIdx >= 0 && startIdx <= endIdx ? startIdx : 0;
|
|
361
|
+
chars.forEach((char, idx) => {
|
|
362
|
+
newChars.push(idx >= startIdx && idx <= endIdx ? maskWith : char);
|
|
363
|
+
});
|
|
364
|
+
newChars.push(afterChars);
|
|
365
|
+
return newChars.join('');
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* 将字符串编码为base64, 采用encodeURIComponent+window.btoa,主要为了解决atob/btoa处理特殊字符会报错的问题,性能不如buffer方案,适用于简单场景
|
|
370
|
+
* @param str 待转换的字符串
|
|
371
|
+
* @returns
|
|
372
|
+
*/
|
|
373
|
+
const b64UriEncode = (str) => window.btoa(encodeURIComponent(str));
|
|
374
|
+
/** b64UriEncode对应的解码函数 */
|
|
375
|
+
const b64UriDecode = (str) => decodeURIComponent(window.atob(str));
|
|
376
|
+
|
|
261
377
|
/**
|
|
262
378
|
* wrap promise and handle reject or err by return an array like [error, undefined]
|
|
263
379
|
* @param promise promise
|
|
@@ -272,158 +388,55 @@ async function awaitWrapper(promise) {
|
|
|
272
388
|
return [err, undefined];
|
|
273
389
|
}
|
|
274
390
|
}
|
|
275
|
-
function
|
|
276
|
-
|
|
277
|
-
return 'unknown';
|
|
278
|
-
if (context._isVue || (context.$ && context.$.vnode)) {
|
|
279
|
-
return 'vue';
|
|
280
|
-
}
|
|
281
|
-
else if ('setState' in context) {
|
|
282
|
-
return 'react';
|
|
283
|
-
}
|
|
284
|
-
return 'unknown';
|
|
285
|
-
}
|
|
286
|
-
const emptyPromise = () => {
|
|
287
|
-
const ep = Promise.resolve(undefined);
|
|
288
|
-
return Object.assign(ep, {
|
|
289
|
-
then(...args) {
|
|
290
|
-
const [onfulfilled] = args;
|
|
291
|
-
// if then has be called by Promise.all, let ite pass
|
|
292
|
-
if (args.length === 2 && typeof onfulfilled === 'function') {
|
|
293
|
-
return onfulfilled(undefined);
|
|
294
|
-
}
|
|
295
|
-
return ep;
|
|
296
|
-
},
|
|
297
|
-
catch: (onrejected) => ep,
|
|
298
|
-
finally: () => ep
|
|
299
|
-
});
|
|
300
|
-
};
|
|
301
|
-
const lockCtx = {};
|
|
302
|
-
function wrapPromise(promise, wrapOrOptions) {
|
|
303
|
-
const contextType = checkContext(this);
|
|
304
|
-
const isReactiveIns = contextType !== 'unknown';
|
|
305
|
-
const context = isReactiveIns ? this : lockCtx;
|
|
306
|
-
const stateKey = contextType === 'react' ? 'state' : '';
|
|
307
|
-
const contextState = stateKey ? context[stateKey] : context;
|
|
308
|
-
const { wrap, lock, syncRefHandle, manualUnlock } = typeof wrapOrOptions === 'boolean' ? { wrap: wrapOrOptions } : { ...wrapOrOptions };
|
|
391
|
+
function wrapPromise(promise, options) {
|
|
392
|
+
const { wrap, lock, syncRefHandle } = { ...options };
|
|
309
393
|
let lockSwitchHook = undefined;
|
|
310
394
|
let lockRefHandle = undefined;
|
|
311
|
-
let lockKey = [];
|
|
312
395
|
let needLock = false;
|
|
313
|
-
let
|
|
314
|
-
const has = (val, key) => !!val && hasOwnProperty.call(val, key);
|
|
315
|
-
const isObj = (obj) => objType(obj) === 'Object';
|
|
316
|
-
const getValue = (obj, path) => {
|
|
317
|
-
let result = false;
|
|
318
|
-
if (obj && isObj(obj) && Array.isArray(path) && path.length) {
|
|
319
|
-
let curObj = obj;
|
|
320
|
-
for (let i = 0; i < path.length; i++) {
|
|
321
|
-
const key = path[i];
|
|
322
|
-
if (typeof curObj !== 'object' || !has(curObj, key)) {
|
|
323
|
-
break;
|
|
324
|
-
}
|
|
325
|
-
curObj = curObj[key];
|
|
326
|
-
i === path.length - 1 &&
|
|
327
|
-
(result = (typeof curObj === 'boolean') ? curObj : false);
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
return result;
|
|
331
|
-
};
|
|
332
|
-
const setValue = (obj, path, value) => {
|
|
333
|
-
// if vue2 and path[0] not defined, do nothing
|
|
334
|
-
if (contextType === 'vue' && context.$set && !has(obj, path[0]))
|
|
335
|
-
return;
|
|
336
|
-
const { $set = (o, key, val) => { o[key] = val; } } = context;
|
|
337
|
-
const isStateRect = contextType === 'react';
|
|
338
|
-
const originObj = isStateRect ? { ...obj } : obj;
|
|
339
|
-
let curObj = originObj;
|
|
340
|
-
let canSet = false;
|
|
341
|
-
for (let i = 0; i < path.length; i++) {
|
|
342
|
-
const key = path[i];
|
|
343
|
-
const keyExist = has(curObj, key);
|
|
344
|
-
if (i === path.length - 1) {
|
|
345
|
-
const isBool = typeof curObj[key] === 'boolean';
|
|
346
|
-
canSet = !keyExist || isBool;
|
|
347
|
-
canSet && $set(curObj, key, value);
|
|
348
|
-
}
|
|
349
|
-
else {
|
|
350
|
-
!keyExist && $set(curObj, key, {});
|
|
351
|
-
if (!isObj(curObj[key]))
|
|
352
|
-
break;
|
|
353
|
-
isStateRect && (curObj[key] = { ...curObj[key] });
|
|
354
|
-
curObj = curObj[key];
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
// trigger setState when run in react class component
|
|
358
|
-
isStateRect && canSet && context.setState({ [path[0]]: originObj[path[0]] });
|
|
359
|
-
};
|
|
396
|
+
let isLocking = false;
|
|
360
397
|
const stateLock = (bool) => {
|
|
361
|
-
if (
|
|
362
|
-
// if not reactive instance, save lock key as string, no need to structure nested object
|
|
363
|
-
isReactiveIns ? setValue(contextState, lockKey, bool) : (lockCtx[lock] = bool);
|
|
364
|
-
return;
|
|
365
|
-
}
|
|
366
|
-
if (lockRefHandle)
|
|
398
|
+
if (lockRefHandle) {
|
|
367
399
|
lockRefHandle[0][lockRefHandle[1]] = bool;
|
|
368
|
-
|
|
400
|
+
}
|
|
401
|
+
if (lockSwitchHook) {
|
|
369
402
|
lockSwitchHook(bool);
|
|
403
|
+
}
|
|
370
404
|
};
|
|
371
405
|
const checkLock = () => {
|
|
372
|
-
// use refHandle if
|
|
373
|
-
|
|
374
|
-
return lockRefHandle[0][lockRefHandle[1]];
|
|
375
|
-
if (!isReactiveIns && typeof lock === 'string')
|
|
376
|
-
return contextState[lock];
|
|
377
|
-
return getValue(contextState, lockKey);
|
|
406
|
+
// use refHandle if state not update sync
|
|
407
|
+
return lockRefHandle ? lockRefHandle[0][lockRefHandle[1]] : false;
|
|
378
408
|
};
|
|
379
409
|
if (lock) {
|
|
380
|
-
const isRefHandle = (val) =>
|
|
381
|
-
if (
|
|
382
|
-
lockKey = lock.split('.');
|
|
383
|
-
}
|
|
384
|
-
else if (isRefHandle(lock)) {
|
|
410
|
+
const isRefHandle = (val) => isArr(val) && val.length === 2;
|
|
411
|
+
if (isRefHandle(lock)) {
|
|
385
412
|
lockRefHandle = lock;
|
|
386
413
|
}
|
|
387
|
-
else if (
|
|
414
|
+
else if (isFn(lock)) {
|
|
388
415
|
lockSwitchHook = lock;
|
|
389
416
|
if (isRefHandle(syncRefHandle)) {
|
|
390
417
|
lockRefHandle = syncRefHandle;
|
|
391
418
|
}
|
|
392
419
|
}
|
|
393
|
-
needLock =
|
|
394
|
-
needLock && (
|
|
420
|
+
needLock = !!(lockSwitchHook || lockRefHandle);
|
|
421
|
+
needLock && (isLocking = checkLock());
|
|
395
422
|
}
|
|
396
423
|
let corePromsie;
|
|
397
424
|
// if promise is a function, here need to call it in promise.then to use locking check to prevent next call
|
|
398
|
-
if (
|
|
399
|
-
|
|
425
|
+
if (isFn(promise)) {
|
|
426
|
+
const truePromise = isLocking ? Promise.resolve(undefined) : promise();
|
|
427
|
+
corePromsie = wrap ? awaitWrapper(truePromise) : truePromise;
|
|
400
428
|
}
|
|
401
429
|
else {
|
|
402
430
|
corePromsie = wrap ? awaitWrapper(promise) : promise;
|
|
403
431
|
}
|
|
404
|
-
|
|
405
|
-
Object.defineProperties(corePromsie, {
|
|
406
|
-
'__lockValue': { get: checkLock },
|
|
407
|
-
_checkLockKey: { value: (key) => lockCtx[key] },
|
|
408
|
-
unlock: { value: unlock }
|
|
409
|
-
});
|
|
410
|
-
if (needLock && !ignoreLock) {
|
|
432
|
+
if (corePromsie instanceof Promise && needLock && !isLocking) {
|
|
411
433
|
stateLock(true);
|
|
412
|
-
|
|
434
|
+
corePromsie.finally(() => stateLock(false));
|
|
413
435
|
}
|
|
414
436
|
return corePromsie;
|
|
415
437
|
}
|
|
416
|
-
|
|
417
|
-
const
|
|
418
|
-
install(appOrVue, key = '$wp') {
|
|
419
|
-
if (appOrVue.config && 'globalProperties' in appOrVue.config) {
|
|
420
|
-
appOrVue.config.globalProperties[key] = wrapPromise;
|
|
421
|
-
}
|
|
422
|
-
else {
|
|
423
|
-
appOrVue.prototype[key] = wrapPromise;
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
};
|
|
438
|
+
/** short for method `wrapPromise` */
|
|
439
|
+
const wp = wrapPromise;
|
|
427
440
|
|
|
428
441
|
function safeJsonParse(json = '', faileValue) {
|
|
429
442
|
try {
|
|
@@ -445,17 +458,23 @@ const isNullable = (value) => value === '' || value === undefined || value === n
|
|
|
445
458
|
*/
|
|
446
459
|
const filterNullable = (obj, deep) => {
|
|
447
460
|
const result = {};
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
461
|
+
objForEch(obj, (key, value) => {
|
|
462
|
+
if (!isNullable(value)) {
|
|
463
|
+
if (!deep) {
|
|
464
|
+
result[key] = value;
|
|
465
|
+
}
|
|
466
|
+
else if (isArr(value)) {
|
|
467
|
+
result[key] = value.map((item) => isObj(item) ? filterNullable(item) : item);
|
|
468
|
+
}
|
|
469
|
+
else {
|
|
470
|
+
result[key] = isObj(value) ? filterNullable(value) : value;
|
|
471
|
+
}
|
|
452
472
|
}
|
|
453
473
|
});
|
|
454
474
|
return result;
|
|
455
475
|
};
|
|
456
|
-
const { toString } = Object.prototype;
|
|
457
476
|
/**
|
|
458
|
-
* 将对象中属性值为null的转换为undefined
|
|
477
|
+
* 将对象中属性值为null的转换为undefined,会返回一个浅拷贝的新对象
|
|
459
478
|
* @param data 对象数据
|
|
460
479
|
* @returns
|
|
461
480
|
*/
|
|
@@ -463,13 +482,15 @@ const switchNullToUndefined = (data) => {
|
|
|
463
482
|
if (data === null) {
|
|
464
483
|
return undefined;
|
|
465
484
|
}
|
|
466
|
-
if (
|
|
485
|
+
if (isArr(data)) {
|
|
467
486
|
return data.map(switchNullToUndefined);
|
|
468
487
|
}
|
|
469
|
-
else if (
|
|
470
|
-
|
|
471
|
-
|
|
488
|
+
else if (isObj(data)) {
|
|
489
|
+
const result = {};
|
|
490
|
+
objForEch(data, (key, value) => {
|
|
491
|
+
result[key] = switchNullToUndefined(value);
|
|
472
492
|
});
|
|
493
|
+
return result;
|
|
473
494
|
}
|
|
474
495
|
return data;
|
|
475
496
|
};
|
|
@@ -483,7 +504,7 @@ const formParamsSwitch = (formData, maps, options) => {
|
|
|
483
504
|
const dateKeys = Object.keys(maps);
|
|
484
505
|
const dateData = {};
|
|
485
506
|
const restData = {};
|
|
486
|
-
|
|
507
|
+
objForEch(formData, (key) => {
|
|
487
508
|
if (dateKeys.includes(key)) {
|
|
488
509
|
dateData[key] = formData[key];
|
|
489
510
|
}
|
|
@@ -496,7 +517,10 @@ const formParamsSwitch = (formData, maps, options) => {
|
|
|
496
517
|
if (accessKeys.length === 0) {
|
|
497
518
|
return formData;
|
|
498
519
|
}
|
|
499
|
-
const { filterNullable: filterNullableValue, dateFormat } = {
|
|
520
|
+
const { filterNullable: filterNullableValue, dateFormat } = {
|
|
521
|
+
filterNullable: true,
|
|
522
|
+
...options
|
|
523
|
+
};
|
|
500
524
|
const handleData = accessKeys.reduce((acc, key) => {
|
|
501
525
|
const values = dateData[key];
|
|
502
526
|
const mapValue = maps[key];
|
|
@@ -505,7 +529,7 @@ const formParamsSwitch = (formData, maps, options) => {
|
|
|
505
529
|
}
|
|
506
530
|
// 数组合并操作/取最后一位操作
|
|
507
531
|
if (mapValue === 'join' || mapValue === 'pop') {
|
|
508
|
-
if (
|
|
532
|
+
if (isArr(values) && values.length) {
|
|
509
533
|
acc[key] = mapValue === 'join' ? values.join(',') : values.slice(-1)[0];
|
|
510
534
|
}
|
|
511
535
|
}
|
|
@@ -516,8 +540,10 @@ const formParamsSwitch = (formData, maps, options) => {
|
|
|
516
540
|
const [startKey, endKey, format] = mapValue;
|
|
517
541
|
const [startValue, endValue] = values;
|
|
518
542
|
Object.assign(acc, {
|
|
519
|
-
[startKey]: dateFormat
|
|
520
|
-
|
|
543
|
+
[startKey]: isFn(dateFormat)
|
|
544
|
+
? dateFormat(startValue, format)
|
|
545
|
+
: startValue,
|
|
546
|
+
[endKey]: isFn(dateFormat) ? dateFormat(endValue, format) : endValue
|
|
521
547
|
});
|
|
522
548
|
}
|
|
523
549
|
return acc;
|
|
@@ -531,22 +557,329 @@ const formParamsSwitch = (formData, maps, options) => {
|
|
|
531
557
|
/** 创建一个绑定了默认时间格式转换函数的formParamsSwitch函数 */
|
|
532
558
|
const formParamsSwitchCreator = (dateFormat) => (formData, maps, options) => formParamsSwitch(formData, maps, { dateFormat, ...options });
|
|
533
559
|
|
|
560
|
+
const isStr = (val) => typeof val === 'string';
|
|
561
|
+
const NOMETHOD = Symbol('NOMETHOD');
|
|
562
|
+
/**
|
|
563
|
+
* 创建emitter形式的实例,可以通过实例封装事件对象,比如websocket,得到一个更便捷的方法,具体可参考page-communicate.js文件内容
|
|
564
|
+
* @param options 配置项
|
|
565
|
+
* @returns
|
|
566
|
+
*/
|
|
567
|
+
function eventTargetEmitter(options) {
|
|
568
|
+
const watchers = new Map([[NOMETHOD, new Set()]]);
|
|
569
|
+
const { name: currentName, customHanlderCreator } = options || {};
|
|
570
|
+
const customHanlder = isFn(customHanlderCreator)
|
|
571
|
+
? customHanlderCreator(watchers)
|
|
572
|
+
: null;
|
|
573
|
+
const splitRegExp = /\|-[a-z]+-\|/;
|
|
574
|
+
const listenerSafeRun = (listener, ...args) => {
|
|
575
|
+
try {
|
|
576
|
+
listener(...args);
|
|
577
|
+
}
|
|
578
|
+
catch (e) {
|
|
579
|
+
console.error(e);
|
|
580
|
+
}
|
|
581
|
+
};
|
|
582
|
+
const defaultHanlder = (payload) => {
|
|
583
|
+
const { method, data, originEmitterName: from, targetEmitterName: to } = payload || [];
|
|
584
|
+
if (method && typeof method === 'string') {
|
|
585
|
+
const matchListeners = [];
|
|
586
|
+
watchers.forEach((value, key) => {
|
|
587
|
+
if (typeof key === 'string' && key.startsWith(method)) {
|
|
588
|
+
const [, eFrom] = key.split(splitRegExp);
|
|
589
|
+
if (!eFrom || !from || eFrom === from) {
|
|
590
|
+
matchListeners.push(...value);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
});
|
|
594
|
+
if (matchListeners.length &&
|
|
595
|
+
(!currentName || !to || currentName === to)) {
|
|
596
|
+
matchListeners.forEach((listener) => listenerSafeRun(listener, data));
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
const noMethodListeners = watchers.get(NOMETHOD);
|
|
600
|
+
if (noMethodListeners) {
|
|
601
|
+
noMethodListeners.forEach((listener) => listenerSafeRun(listener, data, payload));
|
|
602
|
+
}
|
|
603
|
+
};
|
|
604
|
+
const rootEventListener = isFn(customHanlder) ? customHanlder : defaultHanlder;
|
|
605
|
+
const getFullMethodName = (method, options) => {
|
|
606
|
+
const { limitFrom = '', namespace = '' } = options || {};
|
|
607
|
+
return (limitFrom || namespace) && typeof method === 'string'
|
|
608
|
+
? `${method}|-from-|${limitFrom}|-ns-|${namespace}`
|
|
609
|
+
: method;
|
|
610
|
+
};
|
|
611
|
+
const on = (method, listener, options) => {
|
|
612
|
+
if (typeof method === 'symbol' && method !== NOMETHOD) {
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
const trueMethod = getFullMethodName(method, options);
|
|
616
|
+
if (!watchers.has(trueMethod)) {
|
|
617
|
+
watchers.set(trueMethod, new Set());
|
|
618
|
+
}
|
|
619
|
+
const listeners = watchers.get(trueMethod);
|
|
620
|
+
listeners?.add(listener);
|
|
621
|
+
};
|
|
622
|
+
const off = (method, callback, options) => {
|
|
623
|
+
if (typeof method === 'symbol' && method !== NOMETHOD) {
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
626
|
+
const { namespace } = options || {};
|
|
627
|
+
if (namespace && !method) {
|
|
628
|
+
watchers.forEach((_value, key) => {
|
|
629
|
+
if (typeof key === 'string' && key.endsWith(namespace)) {
|
|
630
|
+
watchers.delete(key);
|
|
631
|
+
}
|
|
632
|
+
});
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
const trueMethod = getFullMethodName(method, options);
|
|
636
|
+
if (!callback) {
|
|
637
|
+
watchers.delete(trueMethod);
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
640
|
+
watchers.get(trueMethod)?.delete(callback);
|
|
641
|
+
};
|
|
642
|
+
const removeAllListeners = () => {
|
|
643
|
+
watchers.clear();
|
|
644
|
+
};
|
|
645
|
+
const createPayload = (methodOrPayload, data, target) => {
|
|
646
|
+
return isStr(methodOrPayload)
|
|
647
|
+
? {
|
|
648
|
+
method: methodOrPayload,
|
|
649
|
+
data,
|
|
650
|
+
...(target && isStr(target) ? { targetEmitterName: target } : {}),
|
|
651
|
+
...(currentName && isStr(currentName)
|
|
652
|
+
? { originEmitterName: currentName }
|
|
653
|
+
: {})
|
|
654
|
+
}
|
|
655
|
+
: methodOrPayload;
|
|
656
|
+
};
|
|
657
|
+
const emit = (...args) => {
|
|
658
|
+
const payload = createPayload(...args);
|
|
659
|
+
rootEventListener(payload);
|
|
660
|
+
};
|
|
661
|
+
/** 获取当前所有监听方法的方法名,默认仅返回函数名,设置all为true可以返回具体来源限制和命名空间字段 */
|
|
662
|
+
const getMethodNames = (all) => {
|
|
663
|
+
if (!all) {
|
|
664
|
+
const result = [];
|
|
665
|
+
watchers.forEach((_v, key) => isStr(key) && result.push(key.split(splitRegExp)));
|
|
666
|
+
return result;
|
|
667
|
+
}
|
|
668
|
+
const result = [];
|
|
669
|
+
watchers.forEach((_v, key) => isStr(key) && result.push(key));
|
|
670
|
+
return result;
|
|
671
|
+
};
|
|
672
|
+
const getListeners = (method, options) => {
|
|
673
|
+
if (typeof method === 'symbol') {
|
|
674
|
+
return method === NOMETHOD ? Array.from(watchers.get(NOMETHOD) || []) : [];
|
|
675
|
+
}
|
|
676
|
+
const trueMethod = getFullMethodName(method, options);
|
|
677
|
+
const listeners = watchers.get(trueMethod);
|
|
678
|
+
return listeners ? Array.from(listeners) : [];
|
|
679
|
+
};
|
|
680
|
+
return {
|
|
681
|
+
name: currentName,
|
|
682
|
+
rootEventListener,
|
|
683
|
+
on,
|
|
684
|
+
off,
|
|
685
|
+
emit,
|
|
686
|
+
createPayload,
|
|
687
|
+
removeAllListeners,
|
|
688
|
+
getMethodNames,
|
|
689
|
+
getListeners
|
|
690
|
+
};
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
/**
|
|
694
|
+
* 创建同源页面之间通信的封装实例,默认使用BroadcastChannel实现,在不支持的浏览器环境中会使用localstorage实现
|
|
695
|
+
* @param options 配置对象
|
|
696
|
+
* @returns
|
|
697
|
+
*/
|
|
698
|
+
function pageCommunicate(options) {
|
|
699
|
+
const { messageHanlder, currentPage } = options || {};
|
|
700
|
+
const { emit, on, off, createPayload, removeAllListeners } = eventTargetEmitter({
|
|
701
|
+
name: currentPage,
|
|
702
|
+
customHanlderCreator: messageHanlder
|
|
703
|
+
});
|
|
704
|
+
const onMessage = (event) => emit(event.data);
|
|
705
|
+
let bc;
|
|
706
|
+
const ls = window.localStorage;
|
|
707
|
+
const channelName = 'PAGE_COMMUNICATE_CHANNEL';
|
|
708
|
+
if (typeof BroadcastChannel !== undefined) {
|
|
709
|
+
bc = new BroadcastChannel(channelName);
|
|
710
|
+
bc.addEventListener('message', onMessage);
|
|
711
|
+
}
|
|
712
|
+
else {
|
|
713
|
+
const onStorageMessage = () => {
|
|
714
|
+
const payload = safeJsonParse(ls.getItem(channelName) || '');
|
|
715
|
+
if (payload) {
|
|
716
|
+
emit(payload);
|
|
717
|
+
}
|
|
718
|
+
};
|
|
719
|
+
window.addEventListener('storage', onStorageMessage);
|
|
720
|
+
}
|
|
721
|
+
const send = (...args) => {
|
|
722
|
+
const payload = createPayload(...args);
|
|
723
|
+
if (bc) {
|
|
724
|
+
bc.postMessage(payload);
|
|
725
|
+
}
|
|
726
|
+
else {
|
|
727
|
+
const payloadWitchTs = JSON.stringify({ ...payload, ts: +new Date() });
|
|
728
|
+
ls.setItem(channelName, payloadWitchTs);
|
|
729
|
+
}
|
|
730
|
+
};
|
|
731
|
+
const destory = () => {
|
|
732
|
+
removeAllListeners();
|
|
733
|
+
if (bc) {
|
|
734
|
+
bc.close();
|
|
735
|
+
}
|
|
736
|
+
else {
|
|
737
|
+
ls.removeItem(channelName);
|
|
738
|
+
}
|
|
739
|
+
};
|
|
740
|
+
return { on, off, send, destory };
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
/**
|
|
744
|
+
* 创建一个计时器
|
|
745
|
+
* @returns
|
|
746
|
+
*/
|
|
747
|
+
const createRAFTimer = (options) => {
|
|
748
|
+
const { autoStopWhenNoTask, autoStartWhenAddTask } = { ...options };
|
|
749
|
+
// 计时器句柄
|
|
750
|
+
let ticker = 0;
|
|
751
|
+
// 是否暂停状态
|
|
752
|
+
let paused = true;
|
|
753
|
+
// 计时器实际运行时间,不包括暂停区间
|
|
754
|
+
let timeStamp = 0;
|
|
755
|
+
// 上一次时序记录的时间戳
|
|
756
|
+
let lastTimeStamp = 0;
|
|
757
|
+
let tasks = [];
|
|
758
|
+
let taskNeedClear = false;
|
|
759
|
+
const timerHandler = (time) => {
|
|
760
|
+
const delta = time - (lastTimeStamp || time);
|
|
761
|
+
lastTimeStamp = time;
|
|
762
|
+
timeStamp += delta;
|
|
763
|
+
if (taskNeedClear) {
|
|
764
|
+
tasks = tasks.filter((item) => !item.needClear);
|
|
765
|
+
taskNeedClear = false;
|
|
766
|
+
}
|
|
767
|
+
// will auto stop when autoStopWhenNoTask is true and no task
|
|
768
|
+
if (autoStopWhenNoTask && !tasks.length) {
|
|
769
|
+
paused = true;
|
|
770
|
+
return;
|
|
771
|
+
}
|
|
772
|
+
tasks.forEach((item) => {
|
|
773
|
+
const { callback, maxTime, runTime, elapsed, interval, needClear } = item;
|
|
774
|
+
if (needClear) {
|
|
775
|
+
return;
|
|
776
|
+
}
|
|
777
|
+
if (maxTime && runTime >= maxTime) {
|
|
778
|
+
item.needClear = true;
|
|
779
|
+
taskNeedClear = true;
|
|
780
|
+
return;
|
|
781
|
+
}
|
|
782
|
+
if (elapsed >= interval) {
|
|
783
|
+
try {
|
|
784
|
+
callback(timeStamp);
|
|
785
|
+
}
|
|
786
|
+
catch (e) { }
|
|
787
|
+
item.elapsed = 0;
|
|
788
|
+
item.runTime += 1;
|
|
789
|
+
}
|
|
790
|
+
else {
|
|
791
|
+
item.elapsed += delta;
|
|
792
|
+
}
|
|
793
|
+
});
|
|
794
|
+
ticker = window.requestAnimationFrame(timerHandler);
|
|
795
|
+
};
|
|
796
|
+
/** start timer(开始计时器) */
|
|
797
|
+
const start = () => {
|
|
798
|
+
ticker = window.requestAnimationFrame(timerHandler);
|
|
799
|
+
paused = false;
|
|
800
|
+
};
|
|
801
|
+
/** stop timer(停止计时器) */
|
|
802
|
+
const stop = () => {
|
|
803
|
+
window.cancelAnimationFrame(ticker);
|
|
804
|
+
paused = true;
|
|
805
|
+
};
|
|
806
|
+
/** start/stop toggle (开启和停止切换) */
|
|
807
|
+
const pauseToggle = () => {
|
|
808
|
+
paused ? start() : stop();
|
|
809
|
+
};
|
|
810
|
+
const isPaused = () => paused;
|
|
811
|
+
/**
|
|
812
|
+
* add new task (添加定时任务,注意:无引用关系函数无法被remove,如需提前移除,需要确保函数可被寻到)
|
|
813
|
+
* @param callback task callback (任务回调函数)
|
|
814
|
+
* @param interval callback trigger interval, unit: ms (回调触发间隔,单位:ms)
|
|
815
|
+
* @param maxTime max time callback trigger (回调函数的最大触发次数)
|
|
816
|
+
*/
|
|
817
|
+
const addTask = (callback, interval, maxTime) => {
|
|
818
|
+
if (!callback) {
|
|
819
|
+
return;
|
|
820
|
+
}
|
|
821
|
+
if (autoStartWhenAddTask && paused) {
|
|
822
|
+
start();
|
|
823
|
+
}
|
|
824
|
+
tasks.push({ callback, elapsed: 0, interval, maxTime, runTime: 0 });
|
|
825
|
+
};
|
|
826
|
+
/**
|
|
827
|
+
* remove task (移除定时任务)
|
|
828
|
+
* @param callback task callback (任务回调函数)
|
|
829
|
+
*/
|
|
830
|
+
const removeTask = (callback) => {
|
|
831
|
+
tasks = tasks.filter((item) => item.callback !== callback);
|
|
832
|
+
};
|
|
833
|
+
return { start, stop, pauseToggle, addTask, removeTask, isPaused };
|
|
834
|
+
};
|
|
835
|
+
|
|
836
|
+
/**
|
|
837
|
+
* 将秒数转换为时间段对象
|
|
838
|
+
* @param number 秒数
|
|
839
|
+
* @param maxUnit 最大的单位,默认是小时
|
|
840
|
+
* @returns DurationDetail
|
|
841
|
+
*/
|
|
842
|
+
const secondsToDuration = (number, maxUnit = 'h') => {
|
|
843
|
+
const secNum = Number.isInteger(number) ? number : Math.round(number);
|
|
844
|
+
const result = { s: secNum % 60 };
|
|
845
|
+
switch (maxUnit) {
|
|
846
|
+
case 'm':
|
|
847
|
+
result.m = Math.floor(secNum / 60);
|
|
848
|
+
break;
|
|
849
|
+
case 'h':
|
|
850
|
+
result.m = Math.floor((secNum % 3600) / 60);
|
|
851
|
+
result.h = Math.floor(secNum / 3600);
|
|
852
|
+
break;
|
|
853
|
+
case 'd':
|
|
854
|
+
result.m = Math.floor((secNum % 3600) / 60);
|
|
855
|
+
result.h = Math.floor((secNum % (3600 * 24)) / 3600);
|
|
856
|
+
result.d = Math.floor(secNum / 3600 / 24);
|
|
857
|
+
break;
|
|
858
|
+
}
|
|
859
|
+
return result;
|
|
860
|
+
};
|
|
861
|
+
|
|
534
862
|
exports.ChinaIdCardValid = ChinaIdCardValid;
|
|
863
|
+
exports.NOMETHOD = NOMETHOD;
|
|
535
864
|
exports.array2tree = array2tree;
|
|
536
865
|
exports.awaitWrapper = awaitWrapper;
|
|
537
|
-
exports.
|
|
866
|
+
exports.b64UriDecode = b64UriDecode;
|
|
867
|
+
exports.b64UriEncode = b64UriEncode;
|
|
538
868
|
exports.byteStringify = byteStringify;
|
|
539
869
|
exports.checkoutBy = checkoutBy;
|
|
870
|
+
exports.createRAFTimer = createRAFTimer;
|
|
871
|
+
exports.eventTargetEmitter = eventTargetEmitter;
|
|
540
872
|
exports.filterNullable = filterNullable;
|
|
541
873
|
exports.formParamsSwitch = formParamsSwitch;
|
|
542
874
|
exports.formParamsSwitchCreator = formParamsSwitchCreator;
|
|
543
875
|
exports.formulaValidate = formulaValidate;
|
|
544
876
|
exports.isNullable = isNullable;
|
|
545
877
|
exports.mapToObject = mapToObject;
|
|
546
|
-
exports.
|
|
878
|
+
exports.maskData = maskData;
|
|
879
|
+
exports.pageCommunicate = pageCommunicate;
|
|
547
880
|
exports.pickRenameKeys = pickRenameKeys;
|
|
548
881
|
exports.safeJsonParse = safeJsonParse;
|
|
882
|
+
exports.secondsToDuration = secondsToDuration;
|
|
549
883
|
exports.switchNullToUndefined = switchNullToUndefined;
|
|
550
884
|
exports.tree2array = tree2array;
|
|
551
885
|
exports.wp = wp;
|
|
552
|
-
exports.wpVuePlugin = wpVuePlugin;
|