@wxhccc/es-util 2.0.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/README.md +36 -33
- package/dist/event-target-emitter.js +1 -1
- package/dist/index.esm.js +1 -2
- package/dist/index.js +1 -2
- package/dist/index.min.js +1 -1
- package/dist/object-array.js +1 -1
- package/dist/optional.js +1 -1
- package/dist/page-communicate.js +1 -1
- package/dist/promise.d.ts +3 -1
- package/dist/promise.js +1 -1
- package/dist/raf-timer.js +1 -1
- package/dist/utils.js +1 -1
- package/dist/validate.js +1 -1
- package/dist/value-string-switch/byte-string.d.ts +9 -3
- package/dist/value-string-switch.js +1 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,6 +20,7 @@ $ yarn add @wxhccc/es-util
|
|
|
20
20
|
`<script src="https://cdn.jsdelivr.net/npm/@wxhccc/es-util/lib/index.min.js"></script>`
|
|
21
21
|
|
|
22
22
|
# Usage
|
|
23
|
+
[中文文档](./README-zh.md)
|
|
23
24
|
|
|
24
25
|
```javascript
|
|
25
26
|
import * as EsUtil from '@wxhccc/es-util'
|
|
@@ -68,13 +69,13 @@ console.log(EsUtil.array2tree)
|
|
|
68
69
|
transform an array to tree structure
|
|
69
70
|
|
|
70
71
|
**parameters:**
|
|
71
|
-
- **array** {
|
|
72
|
-
- **options** {
|
|
73
|
-
- **primaryKey** {
|
|
74
|
-
- **parentKey** {
|
|
75
|
-
- **childrenKey** {
|
|
76
|
-
- **createRoot** {
|
|
77
|
-
- **parentRefKey** {
|
|
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.
|
|
78
79
|
|
|
79
80
|
>warning: set the `parentRefKey` will make the return tree object/array cycling, so you can't use `JSON.stringify` to stringify it
|
|
80
81
|
|
|
@@ -167,12 +168,12 @@ const tree = array2tree(array, {
|
|
|
167
168
|
transform tree structure to an array
|
|
168
169
|
|
|
169
170
|
**parameters:**
|
|
170
|
-
- **tree** {
|
|
171
|
-
- **options** {
|
|
172
|
-
- **primaryKey** {
|
|
173
|
-
- **parentKey** {
|
|
174
|
-
- **childrenKey** {
|
|
175
|
-
- **hasParentKey** {
|
|
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
|
|
176
177
|
|
|
177
178
|
|
|
178
179
|
**returns**: items array
|
|
@@ -224,7 +225,7 @@ console.log(tree)
|
|
|
224
225
|
Check whether the Chinese id number provided is valid
|
|
225
226
|
|
|
226
227
|
**parameters:**
|
|
227
|
-
- **idCard** {
|
|
228
|
+
- **idCard** {string} The IDcard number.
|
|
228
229
|
|
|
229
230
|
|
|
230
231
|
**returns**: boolean
|
|
@@ -248,8 +249,8 @@ false
|
|
|
248
249
|
Check whether the formulaString provided is valid
|
|
249
250
|
|
|
250
251
|
**parameters:**
|
|
251
|
-
- **formulaString** {
|
|
252
|
-
- **variables** {
|
|
252
|
+
- **formulaString** {string} The formula string.
|
|
253
|
+
- **variables** {string[]} The variables can appear in formula.
|
|
253
254
|
|
|
254
255
|
|
|
255
256
|
**returns**: boolean
|
|
@@ -277,10 +278,10 @@ false
|
|
|
277
278
|
create an object from an object array.
|
|
278
279
|
|
|
279
280
|
**parameters:**
|
|
280
|
-
- **objectArray** {
|
|
281
|
-
- **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"`.
|
|
282
283
|
> if array item not contain key or function not return a string/number,the item will ignore.
|
|
283
|
-
- **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"`.
|
|
284
285
|
|
|
285
286
|
**returns**: object
|
|
286
287
|
|
|
@@ -309,9 +310,9 @@ console.log(mapToObject(array, item => (item.key + item.value), 'name'))
|
|
|
309
310
|
checkout an array from an object by gived keys, you can merge new data to object item
|
|
310
311
|
|
|
311
312
|
**parameters:**
|
|
312
|
-
- **object** {
|
|
313
|
-
- **keys** {
|
|
314
|
-
- **mergeFn** {
|
|
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.
|
|
315
316
|
|
|
316
317
|
**returns**: array
|
|
317
318
|
|
|
@@ -346,8 +347,8 @@ console.log(checkoutBy(configs, { d: 123, b: { name: { b: 'aaa' } } }, merge)
|
|
|
346
347
|
pick and rename object's keys
|
|
347
348
|
|
|
348
349
|
**parameters:**
|
|
349
|
-
- **object** {
|
|
350
|
-
- **keysMap** {
|
|
350
|
+
- **object** {object} The source object.
|
|
351
|
+
- **keysMap** {Record<string, string>} The `oldKey-newKey` object
|
|
351
352
|
|
|
352
353
|
**returns**: array
|
|
353
354
|
|
|
@@ -375,12 +376,12 @@ console.log(pickRenameKeys(configs, { 'a': 'a1', 'c': 'c3', 'd': 'd' })
|
|
|
375
376
|
transform byte size to a string in the specified format
|
|
376
377
|
|
|
377
378
|
**parameters:**
|
|
378
|
-
- **byteNum** {
|
|
379
|
-
- **options** {
|
|
380
|
-
- **standard** {
|
|
381
|
-
- **unitLvl** {
|
|
382
|
-
- **precision** {
|
|
383
|
-
- **detail** {
|
|
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`.
|
|
384
385
|
|
|
385
386
|
|
|
386
387
|
**returns**: string or object
|
|
@@ -435,10 +436,12 @@ const [err, data] = await awaitWrapper(Promise.reject())
|
|
|
435
436
|
wrap promise or a function return promise with lock method to control UI or forbiden multi task at same time
|
|
436
437
|
|
|
437
438
|
**parameters:**
|
|
438
|
-
- **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
|
|
439
|
-
- **options** {
|
|
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}
|
|
440
441
|
- **wrap** {boolean} whether use `awaitWrapper` to wrap then promise.
|
|
441
442
|
- **lock** {(bool) => void | [ref, lockKey]}
|
|
443
|
+
~~string: v2.0 not support component instance bind, so can't use string~~
|
|
444
|
+
|
|
442
445
|
function: useful in ReactHook component, pass setXXX to method
|
|
443
446
|
|
|
444
447
|
syncRefHandle: an array such as `[object, keyOfObject]` useful when need to lock promise when value can't be update sync, such as React
|
|
@@ -530,7 +533,7 @@ return an emitter instance with `on`, `off`, `emit` and some other methods to ha
|
|
|
530
533
|
**parameters:**
|
|
531
534
|
- **options** {ConfigOptions}
|
|
532
535
|
- **name** {string} the name of current emitter. can used to limit message received from
|
|
533
|
-
- **customHanlderCreator** {(watchers) =>
|
|
536
|
+
- **customHanlderCreator** {(watchers) => function} custome handler creator, you can handle message distribute logic as you want.
|
|
534
537
|
|
|
535
538
|
```javascript
|
|
536
539
|
import { eventTargetEmitter } from '@wxhccc/es-util'
|
package/dist/index.esm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @wxhccc/es-util v2.0.
|
|
2
|
+
* @wxhccc/es-util v2.0.1
|
|
3
3
|
* (c) 2022 wxhccc
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
@@ -274,7 +274,6 @@ const configCreater = (standard, unit) => {
|
|
|
274
274
|
unitMap: unitMap[unit] || Object.values(unitMap)
|
|
275
275
|
};
|
|
276
276
|
};
|
|
277
|
-
// translate byte num to string
|
|
278
277
|
function byteStringify(byteNum, options) {
|
|
279
278
|
const opts = Object.assign({
|
|
280
279
|
standard: 'jedec',
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @wxhccc/es-util v2.0.
|
|
2
|
+
* @wxhccc/es-util v2.0.1
|
|
3
3
|
* (c) 2022 wxhccc
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
@@ -278,7 +278,6 @@ const configCreater = (standard, unit) => {
|
|
|
278
278
|
unitMap: unitMap[unit] || Object.values(unitMap)
|
|
279
279
|
};
|
|
280
280
|
};
|
|
281
|
-
// translate byte num to string
|
|
282
281
|
function byteStringify(byteNum, options) {
|
|
283
282
|
const opts = Object.assign({
|
|
284
283
|
standard: 'jedec',
|
package/dist/index.min.js
CHANGED
package/dist/object-array.js
CHANGED
package/dist/optional.js
CHANGED
package/dist/page-communicate.js
CHANGED
package/dist/promise.d.ts
CHANGED
|
@@ -5,7 +5,9 @@
|
|
|
5
5
|
*/
|
|
6
6
|
export declare function awaitWrapper<T, K = Error>(promise: Promise<T>): Promise<[null, T] | [K, undefined]>;
|
|
7
7
|
/** 防止多次promise函数运行控制器,对于响应式系统,可以同时用作BoolSwitch */
|
|
8
|
-
export declare type LockRefHandle
|
|
8
|
+
export declare type LockRefHandle<T extends {
|
|
9
|
+
[key: string]: boolean;
|
|
10
|
+
} = Record<string, boolean>> = [T, string];
|
|
9
11
|
/** loading等状态的切换函数 */
|
|
10
12
|
export declare type BoolSwitch = (val: boolean) => unknown;
|
|
11
13
|
/**
|
package/dist/promise.js
CHANGED
package/dist/raf-timer.js
CHANGED
package/dist/utils.js
CHANGED
package/dist/validate.js
CHANGED
|
@@ -4,8 +4,14 @@ interface Options {
|
|
|
4
4
|
precision?: number;
|
|
5
5
|
detail?: boolean;
|
|
6
6
|
}
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
declare type StrOrNum = number | string;
|
|
8
|
+
export declare function byteStringify(byteNum: StrOrNum, options?: Options & {
|
|
9
|
+
detail?: false;
|
|
10
|
+
}): string;
|
|
11
|
+
export declare function byteStringify(byteNum: StrOrNum, options: Options & {
|
|
12
|
+
detail: true;
|
|
13
|
+
}): {
|
|
14
|
+
value: StrOrNum;
|
|
9
15
|
unit: string;
|
|
10
|
-
}
|
|
16
|
+
};
|
|
11
17
|
export {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @wxhccc/es-util v2.0.
|
|
2
|
+
* @wxhccc/es-util v2.0.1
|
|
3
3
|
* (c) 2022 wxhccc
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
@@ -32,7 +32,6 @@ const configCreater = (standard, unit) => {
|
|
|
32
32
|
unitMap: unitMap[unit] || Object.values(unitMap)
|
|
33
33
|
};
|
|
34
34
|
};
|
|
35
|
-
// translate byte num to string
|
|
36
35
|
function byteStringify(byteNum, options) {
|
|
37
36
|
const opts = Object.assign({
|
|
38
37
|
standard: 'jedec',
|