@wxhccc/es-util 1.7.0 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +267 -60
- package/dist/array-tree-switch.d.ts +18 -2
- package/dist/date-time.d.ts +14 -0
- package/dist/event-target-emitter.d.ts +7 -7
- package/dist/event-target-emitter.js +38 -17
- package/dist/index.d.ts +2 -0
- package/dist/index.esm.js +342 -231
- package/dist/index.js +345 -233
- package/dist/index.min.js +2 -2
- package/dist/object-array.d.ts +2 -2
- package/dist/object-array.js +25 -17
- package/dist/optional.d.ts +6 -9
- package/dist/optional.js +136 -0
- package/dist/page-communicate.js +10 -14
- 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/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 +17 -16
package/README.md
CHANGED
|
@@ -1,26 +1,31 @@
|
|
|
1
1
|
## Es Utils library
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
is a useful methods library, contain some functions you might need in a project which based on ES
|
|
4
4
|
|
|
5
|
+
> 2.0 has broken update, see module for detail
|
|
5
6
|
|
|
6
7
|
# Installation
|
|
7
8
|
|
|
8
9
|
#### npm
|
|
9
10
|
```shell
|
|
10
|
-
$ npm install @wxhccc/es-util
|
|
11
|
+
$ npm install @wxhccc/es-util
|
|
11
12
|
```
|
|
13
|
+
#### yarn
|
|
14
|
+
```shell
|
|
15
|
+
$ yarn add @wxhccc/es-util
|
|
16
|
+
```
|
|
17
|
+
|
|
12
18
|
#### in browser
|
|
13
19
|
|
|
14
20
|
`<script src="https://cdn.jsdelivr.net/npm/@wxhccc/es-util/lib/index.min.js"></script>`
|
|
15
21
|
|
|
16
22
|
# Usage
|
|
17
|
-
|
|
18
|
-
#### in webpack or node
|
|
23
|
+
[中文文档](./README-zh.md)
|
|
19
24
|
|
|
20
25
|
```javascript
|
|
21
|
-
import * as EsUtil from 'es-util'
|
|
22
|
-
// or import { xxx } from 'es-util'
|
|
23
|
-
// or const EsUtil = require('es-util')
|
|
26
|
+
import * as EsUtil from '@wxhccc/es-util'
|
|
27
|
+
// or import { xxx } from '@wxhccc/es-util'
|
|
28
|
+
// or const EsUtil = require('@wxhccc/es-util')
|
|
24
29
|
|
|
25
30
|
// example, array2tree
|
|
26
31
|
const array = [
|
|
@@ -59,18 +64,19 @@ console.log(EsUtil.array2tree)
|
|
|
59
64
|
|
|
60
65
|
## array-tree-switch module
|
|
61
66
|
|
|
62
|
-
### `array2tree(array, options)`
|
|
67
|
+
### `array2tree(array, options)`
|
|
63
68
|
|
|
64
69
|
transform an array to tree structure
|
|
65
70
|
|
|
66
71
|
**parameters:**
|
|
67
|
-
- **array**
|
|
68
|
-
- **options** {
|
|
69
|
-
- **
|
|
70
|
-
- **
|
|
71
|
-
- **
|
|
72
|
-
- **
|
|
73
|
-
- **
|
|
72
|
+
- **array** {object[]} (The array need to transform).
|
|
73
|
+
- **options** {object} The options.
|
|
74
|
+
- **primaryKey** {string} the primary key of array item, default `'id'`
|
|
75
|
+
- **parentKey** {string} the parent key of array item, default `'pid'`
|
|
76
|
+
- **childrenKey** {string} the childrenKey key of tree structure node, default `'children'`
|
|
77
|
+
- **createRoot** {boolean | (nodes: TreeNode[]) => any} whether to create a root node, default `false`, will return an array, if `true` ,will return an object. you can pass an function as well, the transformed array will pass to function
|
|
78
|
+
- **parentRefKey** {boolean| string} whether to create a reference of current node's parent, default `false`, will do nothing. if you set it to string or `'true'(mean '_parent')` ,will use it as the object key which point to the parent node of current node.
|
|
79
|
+
|
|
74
80
|
>warning: set the `parentRefKey` will make the return tree object/array cycling, so you can't use `JSON.stringify` to stringify it
|
|
75
81
|
|
|
76
82
|
**returns**: array or object with tree structure.
|
|
@@ -78,7 +84,7 @@ transform an array to tree structure
|
|
|
78
84
|
Example
|
|
79
85
|
|
|
80
86
|
```javascript
|
|
81
|
-
import { array2tree } from 'es-util'
|
|
87
|
+
import { array2tree } from '@wxhccc/es-util'
|
|
82
88
|
|
|
83
89
|
const array = [
|
|
84
90
|
{ id: 1, parentId: 0, name: 'language' },
|
|
@@ -162,12 +168,12 @@ const tree = array2tree(array, {
|
|
|
162
168
|
transform tree structure to an array
|
|
163
169
|
|
|
164
170
|
**parameters:**
|
|
165
|
-
- **tree** {
|
|
166
|
-
- **options** {
|
|
167
|
-
- **
|
|
168
|
-
- **
|
|
169
|
-
- **
|
|
170
|
-
- **
|
|
171
|
+
- **tree** {TreeNode[] | object} The tree structure need to transform.
|
|
172
|
+
- **options** {object} The options.
|
|
173
|
+
- **primaryKey** {string} the primary key of node item, default `'id'`
|
|
174
|
+
- **parentKey** {string} the parent key of node item, default `'pid'`
|
|
175
|
+
- **childrenKey** {string} the childrenKey key of node item, default `'children'`
|
|
176
|
+
- **hasParentKey** {boolean} whether tree node has parent property, default `true`, if `false` ,will add an parent key property
|
|
171
177
|
|
|
172
178
|
|
|
173
179
|
**returns**: items array
|
|
@@ -175,7 +181,7 @@ transform tree structure to an array
|
|
|
175
181
|
Example
|
|
176
182
|
|
|
177
183
|
```javascript
|
|
178
|
-
import { tree2array } from 'es-util'
|
|
184
|
+
import { tree2array } from '@wxhccc/es-util'
|
|
179
185
|
|
|
180
186
|
const tree = {
|
|
181
187
|
id: 0,
|
|
@@ -219,7 +225,7 @@ console.log(tree)
|
|
|
219
225
|
Check whether the Chinese id number provided is valid
|
|
220
226
|
|
|
221
227
|
**parameters:**
|
|
222
|
-
- **idCard** {
|
|
228
|
+
- **idCard** {string} The IDcard number.
|
|
223
229
|
|
|
224
230
|
|
|
225
231
|
**returns**: boolean
|
|
@@ -227,7 +233,7 @@ Check whether the Chinese id number provided is valid
|
|
|
227
233
|
Example
|
|
228
234
|
|
|
229
235
|
```javascript
|
|
230
|
-
import { ChinaIdCardValid } from 'es-util'
|
|
236
|
+
import { ChinaIdCardValid } from '@wxhccc/es-util'
|
|
231
237
|
|
|
232
238
|
|
|
233
239
|
console.log(ChinaIdCardValid('[valid IDcard]')
|
|
@@ -243,8 +249,8 @@ false
|
|
|
243
249
|
Check whether the formulaString provided is valid
|
|
244
250
|
|
|
245
251
|
**parameters:**
|
|
246
|
-
- **formulaString** {
|
|
247
|
-
- **variables** {
|
|
252
|
+
- **formulaString** {string} The formula string.
|
|
253
|
+
- **variables** {string[]} The variables can appear in formula.
|
|
248
254
|
|
|
249
255
|
|
|
250
256
|
**returns**: boolean
|
|
@@ -252,7 +258,7 @@ Check whether the formulaString provided is valid
|
|
|
252
258
|
Example
|
|
253
259
|
|
|
254
260
|
```javascript
|
|
255
|
-
import { formulaValidate } from 'es-util'
|
|
261
|
+
import { formulaValidate } from '@wxhccc/es-util'
|
|
256
262
|
|
|
257
263
|
|
|
258
264
|
console.log(formulaValidate('A+B*(D-C/E)')
|
|
@@ -272,17 +278,17 @@ false
|
|
|
272
278
|
create an object from an object array.
|
|
273
279
|
|
|
274
280
|
**parameters:**
|
|
275
|
-
- **objectArray** {
|
|
276
|
-
- **keyProp** {
|
|
281
|
+
- **objectArray** {object[]} The source object array.
|
|
282
|
+
- **keyProp** {string | (item: object, index: number) => string} The property of array item be used for key, or the function to create object key, default `"key"`.
|
|
277
283
|
> if array item not contain key or function not return a string/number,the item will ignore.
|
|
278
|
-
- **valueProp** {
|
|
284
|
+
- **valueProp** {string | (item: object, index: number) => string} The property of array item be used for value, or the function to create object value, default `"value"`.
|
|
279
285
|
|
|
280
286
|
**returns**: object
|
|
281
287
|
|
|
282
288
|
Example
|
|
283
289
|
|
|
284
290
|
```javascript
|
|
285
|
-
import { mapToObject } from 'es-util'
|
|
291
|
+
import { mapToObject } from '@wxhccc/es-util'
|
|
286
292
|
|
|
287
293
|
const array = [
|
|
288
294
|
{ key: 'a', value: 'b', name: 'afsfsdfe' },
|
|
@@ -304,19 +310,16 @@ console.log(mapToObject(array, item => (item.key + item.value), 'name'))
|
|
|
304
310
|
checkout an array from an object by gived keys, you can merge new data to object item
|
|
305
311
|
|
|
306
312
|
**parameters:**
|
|
307
|
-
- **object** {
|
|
308
|
-
- **keys** {
|
|
309
|
-
- **mergeFn** {
|
|
310
|
-
- **empty** {Object} empty object
|
|
311
|
-
- **objectValue** {Object} the value of object[keyitem]
|
|
312
|
-
- **keysValue** {Object} the value of keys[keyitem]
|
|
313
|
+
- **object** {object} The source object.
|
|
314
|
+
- **keys** {string[] | Record<string, any>} The properties array of `object`. or an object contains keys which you want to pick from `object` and values you want to merge to those picked values. if keys not provided, will return `Object.values(object)`
|
|
315
|
+
- **mergeFn** {(objItem: object, newItem: any) => object} this method use `Object.assign` to merge values where two value are object default, you can provide custom function to merge value.
|
|
313
316
|
|
|
314
317
|
**returns**: array
|
|
315
318
|
|
|
316
319
|
Example
|
|
317
320
|
|
|
318
321
|
```javascript
|
|
319
|
-
import { checkoutBy } from 'es-util'
|
|
322
|
+
import { checkoutBy } from '@wxhccc/es-util'
|
|
320
323
|
|
|
321
324
|
const configs = {
|
|
322
325
|
a: { key: 1, name: 'afsfsdfe' },
|
|
@@ -344,15 +347,15 @@ console.log(checkoutBy(configs, { d: 123, b: { name: { b: 'aaa' } } }, merge)
|
|
|
344
347
|
pick and rename object's keys
|
|
345
348
|
|
|
346
349
|
**parameters:**
|
|
347
|
-
- **object** {
|
|
348
|
-
- **keysMap** {
|
|
350
|
+
- **object** {object} The source object.
|
|
351
|
+
- **keysMap** {Record<string, string>} The `oldKey-newKey` object
|
|
349
352
|
|
|
350
353
|
**returns**: array
|
|
351
354
|
|
|
352
355
|
Example
|
|
353
356
|
|
|
354
357
|
```javascript
|
|
355
|
-
import { pickRenameKeys } from 'es-util'
|
|
358
|
+
import { pickRenameKeys } from '@wxhccc/es-util'
|
|
356
359
|
|
|
357
360
|
const configs = {
|
|
358
361
|
a: { name: 'afsfsdfe' },
|
|
@@ -373,12 +376,12 @@ console.log(pickRenameKeys(configs, { 'a': 'a1', 'c': 'c3', 'd': 'd' })
|
|
|
373
376
|
transform byte size to a string in the specified format
|
|
374
377
|
|
|
375
378
|
**parameters:**
|
|
376
|
-
- **byteNum** {
|
|
377
|
-
- **options** {
|
|
378
|
-
- **
|
|
379
|
-
- **
|
|
380
|
-
- **
|
|
381
|
-
- **
|
|
379
|
+
- **byteNum** {number} The size number need to transform.
|
|
380
|
+
- **options** {object} The options.
|
|
381
|
+
- **standard** {string} the standard used to transform, default `'jedec'`, suport `'metric'`, `'iec'`. [Metric, IEC and JEDEC units](https://en.wikipedia.org/wiki/Gigabyte)
|
|
382
|
+
- **unitLvl** {string} the unit lvl to transform byte size, default `'auto'`. suport `'B','K','M','G','T','P','E','Z','Y'`
|
|
383
|
+
- **precision** {number} the precision of value, default `1`
|
|
384
|
+
- **detail** {boolean} whether to return an object of detai info, default `false`.
|
|
382
385
|
|
|
383
386
|
|
|
384
387
|
**returns**: string or object
|
|
@@ -386,7 +389,7 @@ transform byte size to a string in the specified format
|
|
|
386
389
|
Example
|
|
387
390
|
|
|
388
391
|
```javascript
|
|
389
|
-
import { byteStringify } from 'es-util'
|
|
392
|
+
import { byteStringify } from '@wxhccc/es-util'
|
|
390
393
|
|
|
391
394
|
byteStringify(1234)
|
|
392
395
|
/*log '1.2 KB'*/
|
|
@@ -414,46 +417,178 @@ byteStringify(1234, { detail: true, standard: 'metric', precision: 3 })
|
|
|
414
417
|
|
|
415
418
|
wrap promise with then and catch to return `[null, data]` or `[Error, undefined]`, useful async & await
|
|
416
419
|
|
|
420
|
+
Example
|
|
421
|
+
|
|
422
|
+
```javascript
|
|
423
|
+
import { awaitWrapper } from '@wxhccc/es-util'
|
|
424
|
+
|
|
425
|
+
const [err, data] = await awaitWrapper(Promise.resolve(1))
|
|
426
|
+
/*log [null, 1] */
|
|
427
|
+
const [err, data] = await awaitWrapper(Promise.reject())
|
|
428
|
+
/*log [err, undefined] */
|
|
429
|
+
|
|
430
|
+
```
|
|
431
|
+
|
|
417
432
|
### `wp(promise, [wrap])`
|
|
418
433
|
|
|
434
|
+
> this module has been refactor in v2.0,no longer support vue/react instance detection because there is no need in hooks.
|
|
435
|
+
|
|
419
436
|
wrap promise or a function return promise with lock method to control UI or forbiden multi task at same time
|
|
420
437
|
|
|
421
438
|
**parameters:**
|
|
422
|
-
- **promise** {Promise|() => Promise} promise object or a function return promise object. when use function and lock, it can prevent second call before previous promise settled
|
|
423
|
-
- **
|
|
439
|
+
- **promise** {Promise | () => Promise} promise object or a function return promise object. when use function and lock, it can prevent second call before previous promise settled
|
|
440
|
+
- **options** {WrapOptions}
|
|
424
441
|
- **wrap** {boolean} whether use `awaitWrapper` to wrap then promise.
|
|
425
|
-
- **lock** {
|
|
426
|
-
string:
|
|
442
|
+
- **lock** {(bool) => void | [ref, lockKey]}
|
|
443
|
+
~~string: v2.0 not support component instance bind, so can't use string~~
|
|
427
444
|
|
|
428
445
|
function: useful in ReactHook component, pass setXXX to method
|
|
429
446
|
|
|
430
447
|
syncRefHandle: an array such as `[object, keyOfObject]` useful when need to lock promise when value can't be update sync, such as React
|
|
431
448
|
- **syncRefHandle** {[ref, lockKey]} when you need function and syncRefHandle at sametime
|
|
432
|
-
- **manualUnlock** {boolean} whether not unlock automatic
|
|
433
449
|
|
|
434
450
|
**returns**: a extends promise object with `__lockValue` getter and `unlock` method
|
|
435
451
|
|
|
436
|
-
> can be use in react and vue2/3 instance when bind this
|
|
452
|
+
> ~~can be use in react and vue2/3 instance when bind this~~
|
|
453
|
+
> 2.0 will not bind this
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
#### use in react hooks
|
|
457
|
+
|
|
458
|
+
```javascript
|
|
459
|
+
import { wp } from '@wxhccc/es-util'
|
|
460
|
+
|
|
461
|
+
const [loading, setLoading] = useState(false)
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* eg.1
|
|
465
|
+
* will run setLoading(true) and run setLoading(false) after promise resettled
|
|
466
|
+
**/
|
|
467
|
+
const result = await wp(Promise.resolve(1), { lock: setLoading })
|
|
468
|
+
/**
|
|
469
|
+
* eg.2
|
|
470
|
+
*/
|
|
471
|
+
const [er, data] = await wp(Promise.resolve(1), { wrap: true, lock: setLoading })
|
|
472
|
+
/*log [null, 1] */
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* eg.3
|
|
476
|
+
* will prevent second run when use syncRefHandle
|
|
477
|
+
*/
|
|
478
|
+
const loadingRef = useRef(false)
|
|
479
|
+
|
|
480
|
+
const asyncMethod = () => Promise.resolve(1)
|
|
481
|
+
|
|
482
|
+
const runTask = async () => {
|
|
483
|
+
const [err, data] = await wp(asyncMethod, { wrap: true, lock: setLoading, syncRefHandle: [loadingRef, 'current'] })
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
runTask()
|
|
487
|
+
runTask()
|
|
488
|
+
/*log [null, 1] */
|
|
489
|
+
/*log [null, undefined], second method call will resolve undefined immediate, but not run asyncMethod */
|
|
490
|
+
|
|
491
|
+
```
|
|
437
492
|
|
|
438
|
-
|
|
493
|
+
#### use in vue setup
|
|
439
494
|
|
|
440
|
-
|
|
495
|
+
```javascript
|
|
496
|
+
import { wp } from '@wxhccc/es-util'
|
|
497
|
+
|
|
498
|
+
const loading = ref(false)
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* eg.1
|
|
502
|
+
* will run setLoading(true) and run setLoading(false) after promise resettled
|
|
503
|
+
**/
|
|
504
|
+
const setLoading = (bool) => loading.value = bool
|
|
505
|
+
const result = await wp(Promise.resolve(1), { lock: setLoading })
|
|
506
|
+
/**
|
|
507
|
+
* eg.2
|
|
508
|
+
* lock can be syncRefHandle if ref is update sync, it's more simpler for vue
|
|
509
|
+
*/
|
|
510
|
+
const asyncMethod = () => Promise.resolve(1)
|
|
511
|
+
|
|
512
|
+
const runTask = async () => {
|
|
513
|
+
const [err, data] = await wp(asyncMethod, { wrap: true, lock: [loading, 'value'] })
|
|
514
|
+
}
|
|
441
515
|
|
|
516
|
+
runTask()
|
|
517
|
+
runTask()
|
|
518
|
+
/*log [null, 1] */
|
|
519
|
+
/*log [null, undefined], second method call will resolve undefined immediate, but not run asyncMethod */
|
|
520
|
+
|
|
521
|
+
```
|
|
442
522
|
|
|
443
523
|
## event-target-emitter module
|
|
444
524
|
|
|
445
525
|
> v1.7.0 add
|
|
446
526
|
|
|
527
|
+
this is a simple event emitter, you can find other emitter package is you need more function
|
|
528
|
+
|
|
447
529
|
### `eventTargetEmitter(options)`
|
|
448
530
|
|
|
449
|
-
return an emitter instance with `on`, `off` and some other methods to handle with EventTarget
|
|
531
|
+
return an emitter instance with `on`, `off`, `emit` and some other methods to handle with EventTarget
|
|
450
532
|
|
|
451
533
|
**parameters:**
|
|
452
534
|
- **options** {ConfigOptions}
|
|
453
535
|
- **name** {string} the name of current emitter. can used to limit message received from
|
|
454
|
-
- **customHanlderCreator** {(watchers) =>
|
|
536
|
+
- **customHanlderCreator** {(watchers) => function} custome handler creator, you can handle message distribute logic as you want.
|
|
455
537
|
|
|
456
|
-
|
|
538
|
+
```javascript
|
|
539
|
+
import { eventTargetEmitter } from '@wxhccc/es-util'
|
|
540
|
+
|
|
541
|
+
/** eg.1 */
|
|
542
|
+
const emitter = eventTargetEmitter()
|
|
543
|
+
|
|
544
|
+
const runTaskA = () => {
|
|
545
|
+
console.log('a')
|
|
546
|
+
}
|
|
547
|
+
const runTaskB = () => {
|
|
548
|
+
console.log('b')
|
|
549
|
+
}
|
|
550
|
+
emitter.on('to-run-a', runTaskA)
|
|
551
|
+
emitter.on('to-run-b', runTaskA)
|
|
552
|
+
|
|
553
|
+
...
|
|
554
|
+
websocket.on('message', (task) => {
|
|
555
|
+
if (task === 'a') {
|
|
556
|
+
emitter.emit('to-run-a')
|
|
557
|
+
} else if (task === 'b') {
|
|
558
|
+
emitter.emit('to-run-b')
|
|
559
|
+
}
|
|
560
|
+
})
|
|
561
|
+
|
|
562
|
+
/** eg.2 */
|
|
563
|
+
// page-a in tab 1
|
|
564
|
+
const emitter = eventTargetEmitter({ name: 'page-a' })
|
|
565
|
+
|
|
566
|
+
const runTask = () => {
|
|
567
|
+
// this method will call when received message from page-b, but not call when received message from page-c
|
|
568
|
+
console.log('do someting')
|
|
569
|
+
}
|
|
570
|
+
emitter.on('to-run-task', runTask, { limitFrom: 'page-b' })
|
|
571
|
+
|
|
572
|
+
const onStorageMessage = (payload) => {
|
|
573
|
+
const payload = JSON.parse(localStorage.setItem('page-communicate'))
|
|
574
|
+
emitter.emit(payload)
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
window.addEventListener('storage', onStorageMessage)
|
|
578
|
+
|
|
579
|
+
// page-b in tab 2
|
|
580
|
+
const { createPayload } = eventTargetEmitter({ name: 'page-b' })
|
|
581
|
+
|
|
582
|
+
const payload = createPayload('to-run-task', { a: 1 })
|
|
583
|
+
/**
|
|
584
|
+
* payload: { method: 'to-run-task', data: { a: 1 }, originEmitterName: 'page-b' }
|
|
585
|
+
*/
|
|
586
|
+
localStorage.setItem('page-communicate', payload)
|
|
587
|
+
|
|
588
|
+
//
|
|
589
|
+
```
|
|
590
|
+
|
|
591
|
+
## page-communicate module
|
|
457
592
|
|
|
458
593
|
> v1.7.0 add
|
|
459
594
|
|
|
@@ -461,5 +596,77 @@ return an emitter instance with `on`, `off` and some other methods to handle wit
|
|
|
461
596
|
|
|
462
597
|
return an instance which can used to communicate between same-origin pages, powered by `eventTargetEmitter`
|
|
463
598
|
|
|
599
|
+
this module is full realize of `eventTargetEmitter` eg.2, it use `BroadcastChannel` if it supported, otherwise will use `window.localStorage
|
|
600
|
+
|
|
601
|
+
```javascript
|
|
602
|
+
import { pageCommunicate } from '@wxhccc/es-util'
|
|
603
|
+
|
|
604
|
+
const pc = pageCommunicate()
|
|
605
|
+
|
|
606
|
+
pc.on('update-use-info', (newUserInfo) => {
|
|
607
|
+
// update global state
|
|
608
|
+
})
|
|
609
|
+
|
|
610
|
+
// when one page change login account
|
|
611
|
+
pc.send('update-use-info', newUserInfo)
|
|
612
|
+
```
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
## raf-timer module
|
|
616
|
+
|
|
617
|
+
> v2.0.0 add
|
|
618
|
+
|
|
619
|
+
### `createRAFTimer(options)`
|
|
620
|
+
|
|
621
|
+
return an requestAnimationFrame timer instance. used to instead of window.setTimout and window.setInterval.
|
|
622
|
+
|
|
623
|
+
```javascript
|
|
624
|
+
import { createRAFTimer } from '@wxhccc/es-util'
|
|
625
|
+
|
|
626
|
+
const timer = createRAFTimer()
|
|
627
|
+
// start timer
|
|
628
|
+
timer.start()
|
|
629
|
+
|
|
630
|
+
/** mock window.setTimeout, run once */
|
|
631
|
+
timer.addTask(() => {
|
|
632
|
+
console.log('run after 1 second')
|
|
633
|
+
}, 1000, 1)
|
|
634
|
+
/** run 10 times */
|
|
635
|
+
timer.addTask(() => {
|
|
636
|
+
console.log('run every 1 second, max 10 times')
|
|
637
|
+
}, 1000, 10)
|
|
638
|
+
|
|
639
|
+
const fn = () => {}
|
|
640
|
+
/** mock window.setInterval */
|
|
641
|
+
timer.addTask(fn, 1000)
|
|
642
|
+
/** stop task */
|
|
643
|
+
timer.removeTask(fn)
|
|
644
|
+
|
|
645
|
+
// you can stop timer when no task by pass options
|
|
646
|
+
const timer = createRAFTimer({
|
|
647
|
+
autoStopWhenNoTask: true,
|
|
648
|
+
autoStartWhenAddTask: true
|
|
649
|
+
})
|
|
650
|
+
|
|
651
|
+
```
|
|
652
|
+
|
|
653
|
+
## date-time module
|
|
654
|
+
|
|
655
|
+
> v2.0.0 add
|
|
656
|
+
|
|
657
|
+
### `secondsToDuration(number, maxUnit)`
|
|
658
|
+
|
|
659
|
+
```javascript
|
|
660
|
+
import { secondsToDuration } from '@wxhccc/es-util'
|
|
661
|
+
|
|
662
|
+
const detail = secondsToDuration(12345678)
|
|
663
|
+
/*log {h: 3429, m: 21, s: 18} */
|
|
664
|
+
|
|
665
|
+
const detail = secondsToDuration(12345678, 'd')
|
|
666
|
+
/*log { d: 142, h: 21, m: 21, s: 18 } */
|
|
667
|
+
```
|
|
668
|
+
|
|
669
|
+
parse seconds to duration detail object
|
|
670
|
+
|
|
464
671
|
## License
|
|
465
672
|
MIT
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AnyFunction } from './types';
|
|
1
2
|
export interface Node {
|
|
2
3
|
[key: string]: any;
|
|
3
4
|
}
|
|
@@ -30,7 +31,17 @@ declare type Tree = TreeNode[] | {
|
|
|
30
31
|
* @param {boolean | Function} createNode whether create root node, or function which can return root node
|
|
31
32
|
* @param {boolean | string} parentRefKey parent reference key of node
|
|
32
33
|
*/
|
|
33
|
-
export declare function array2tree(array: Node[], options?: TreeOptions
|
|
34
|
+
export declare function array2tree(array: Node[], options?: TreeOptions & {
|
|
35
|
+
createNode?: false;
|
|
36
|
+
}): TreeNode[];
|
|
37
|
+
export declare function array2tree(array: Node[], options: TreeOptions & {
|
|
38
|
+
createNode: true;
|
|
39
|
+
}): {
|
|
40
|
+
[children: string]: TreeNode[];
|
|
41
|
+
};
|
|
42
|
+
export declare function array2tree<T = any>(array: Node[], options: TreeOptions & {
|
|
43
|
+
createNode: AnyFunction<T>;
|
|
44
|
+
}): T;
|
|
34
45
|
declare type Nodes = Node[];
|
|
35
46
|
declare type NodeMap = {
|
|
36
47
|
[id: string]: Node;
|
|
@@ -45,5 +56,10 @@ declare type NodeMap = {
|
|
|
45
56
|
* @param {boolean} hasParentKey whether create parent id in node
|
|
46
57
|
* @param {boolean} returnObject whether return object or array
|
|
47
58
|
*/
|
|
48
|
-
export declare function tree2array(tree: Tree, options?: NodeOptions
|
|
59
|
+
export declare function tree2array(tree: Tree, options?: NodeOptions & {
|
|
60
|
+
returnObject?: false;
|
|
61
|
+
}): Nodes;
|
|
62
|
+
export declare function tree2array(tree: Tree, options: NodeOptions & {
|
|
63
|
+
returnObject: true;
|
|
64
|
+
}): NodeMap;
|
|
49
65
|
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare type TimeUnitShort = 'd' | 'h' | 'm' | 's';
|
|
2
|
+
export interface DurationDetail {
|
|
3
|
+
d?: number;
|
|
4
|
+
h?: number;
|
|
5
|
+
m?: number;
|
|
6
|
+
s: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* 将秒数转换为时间段对象
|
|
10
|
+
* @param number 秒数
|
|
11
|
+
* @param maxUnit 最大的单位,默认是小时
|
|
12
|
+
* @returns DurationDetail
|
|
13
|
+
*/
|
|
14
|
+
export declare const secondsToDuration: (number: number, maxUnit?: Exclude<TimeUnitShort, 's'>) => DurationDetail;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AnyFunction } from
|
|
1
|
+
import { AnyFunction } from './types';
|
|
2
2
|
export interface EventPayload<T = any> {
|
|
3
3
|
/** method name to be listenered */
|
|
4
4
|
method: string;
|
|
@@ -22,7 +22,7 @@ export interface BindOptions {
|
|
|
22
22
|
/** 事件所在命名空间 */
|
|
23
23
|
namespace?: string;
|
|
24
24
|
}
|
|
25
|
-
export declare const NOMETHOD: symbol;
|
|
25
|
+
export declare const NOMETHOD: unique symbol;
|
|
26
26
|
/**
|
|
27
27
|
* 创建emitter形式的实例,可以通过实例封装事件对象,比如websocket,得到一个更便捷的方法,具体可参考page-communicate.js文件内容
|
|
28
28
|
* @param options 配置项
|
|
@@ -31,15 +31,15 @@ export declare const NOMETHOD: symbol;
|
|
|
31
31
|
export declare function eventTargetEmitter(options?: ConfigOptions): {
|
|
32
32
|
name: string | undefined;
|
|
33
33
|
rootEventListener: <T = any>(payload: EventPayload<T>) => void;
|
|
34
|
-
on: <T_1>(method: string | symbol, listener: (data: T_1) => any, options?: BindOptions
|
|
35
|
-
off: (method: string | symbol, callback?: AnyFunction
|
|
34
|
+
on: <T_1>(method: string | symbol, listener: (data: T_1) => any, options?: BindOptions) => void;
|
|
35
|
+
off: (method: string | symbol, callback?: AnyFunction, options?: BindOptions) => void;
|
|
36
36
|
emit: (methodOrPayload: string | EventPayload<any>, data?: any, target?: string | undefined) => void;
|
|
37
|
-
createPayload: (methodOrPayload: string | EventPayload, data?: any, target?: string
|
|
37
|
+
createPayload: (methodOrPayload: string | EventPayload, data?: any, target?: string) => EventPayload<any>;
|
|
38
38
|
removeAllListeners: () => void;
|
|
39
39
|
getMethodNames: {
|
|
40
|
-
(all?: false
|
|
40
|
+
(all?: false): string[];
|
|
41
41
|
(all: true): string[][];
|
|
42
42
|
};
|
|
43
|
-
getListeners: (method: string | symbol, options?: BindOptions
|
|
43
|
+
getListeners: (method: string | symbol, options?: BindOptions) => AnyFunction<any>[];
|
|
44
44
|
};
|
|
45
45
|
export default eventTargetEmitter;
|