@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 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** {Array} (The array need to transform).
72
- - **options** {Object} The options.
73
- - **primaryKey** {String} the primary key of array item, default `'id'`
74
- - **parentKey** {String} the parent key of array item, default `'pid'`
75
- - **childrenKey** {String} the childrenKey key of tree structure node, default `'children'`
76
- - **createRoot** {Boolean/Function} 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
77
- - **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.
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** {Array/Object} The tree structure need to transform.
171
- - **options** {Object} The options.
172
- - **primaryKey** {String} the primary key of node item, default `'id'`
173
- - **parentKey** {String} the parent key of node item, default `'pid'`
174
- - **childrenKey** {String} the childrenKey key of node item, default `'children'`
175
- - **hasParentKey** {Boolean} whether tree node has parent property, default `'true'`, if `'false'` ,will add an parent key property
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** {String} The IDcard number.
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** {String} The formula string.
252
- - **variables** {Array} The variables can appear in formula.
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** {Array} The source object array.
281
- - **keyProp** {String/Function} The property of array item be used for key, or the function to create object key, default `"key"`.
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** {String/Function} The property of array item be used for value, or the function to create object value, default `"value"`.
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** {Object} The source object.
313
- - **keys** {Array/Object} 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)`
314
- - **mergeFn** {Function} this method use `Object.assign` to merge values where two value are object default, you can provide custom function to merge value.
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** {Object} The source object.
350
- - **keysMap** {Object} The `oldKey-newKey` object
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** {Number} The size number need to transform.
379
- - **options** {Object} The options.
380
- - **standard** {String} the standard used to transform, default `'jedec'`, suport `'metric'`, `'iec'`. [Metric, IEC and JEDEC units](https://en.wikipedia.org/wiki/Gigabyte)
381
- - **unitLvl** {String} the unit lvl to transform byte size, default `'auto'`. suport `'B','K','M','G','T','P','E','Z','Y'`
382
- - **precision** {Number} the precision of value, default `1`
383
- - **detail** {Boolean} whether to return an object of detai info, default `false`.
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** {boolean|WrapOptions} shortcut of `wrap` option or full 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) => Function} custome handler creator, you can handle message distribute logic as you want.
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'
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @wxhccc/es-util v2.0.0
2
+ * @wxhccc/es-util v2.0.1
3
3
  * (c) 2022 wxhccc
4
4
  * @license MIT
5
5
  */
package/dist/index.esm.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @wxhccc/es-util v2.0.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.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
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @wxhccc/es-util v2.0.0
2
+ * @wxhccc/es-util v2.0.1
3
3
  * (c) 2022 wxhccc
4
4
  * @license MIT
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @wxhccc/es-util v2.0.0
2
+ * @wxhccc/es-util v2.0.1
3
3
  * (c) 2022 wxhccc
4
4
  * @license MIT
5
5
  */
package/dist/optional.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @wxhccc/es-util v2.0.0
2
+ * @wxhccc/es-util v2.0.1
3
3
  * (c) 2022 wxhccc
4
4
  * @license MIT
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @wxhccc/es-util v2.0.0
2
+ * @wxhccc/es-util v2.0.1
3
3
  * (c) 2022 wxhccc
4
4
  * @license MIT
5
5
  */
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 = [Record<string, boolean>, string];
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
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @wxhccc/es-util v2.0.0
2
+ * @wxhccc/es-util v2.0.1
3
3
  * (c) 2022 wxhccc
4
4
  * @license MIT
5
5
  */
package/dist/raf-timer.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @wxhccc/es-util v2.0.0
2
+ * @wxhccc/es-util v2.0.1
3
3
  * (c) 2022 wxhccc
4
4
  * @license MIT
5
5
  */
package/dist/utils.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @wxhccc/es-util v2.0.0
2
+ * @wxhccc/es-util v2.0.1
3
3
  * (c) 2022 wxhccc
4
4
  * @license MIT
5
5
  */
package/dist/validate.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @wxhccc/es-util v2.0.0
2
+ * @wxhccc/es-util v2.0.1
3
3
  * (c) 2022 wxhccc
4
4
  * @license MIT
5
5
  */
@@ -4,8 +4,14 @@ interface Options {
4
4
  precision?: number;
5
5
  detail?: boolean;
6
6
  }
7
- export declare function byteStringify(byteNum: number | string, options?: Options): string | {
8
- value: string | number;
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
- } | undefined;
16
+ };
11
17
  export {};
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @wxhccc/es-util v2.0.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',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxhccc/es-util",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "A library that contains some useful methods",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",