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