@wxhccc/es-util 1.4.1 → 1.5.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 +442 -442
- package/dist/index.d.ts +1 -0
- package/dist/index.esm.js +109 -5
- package/dist/index.js +113 -4
- package/dist/index.min.js +2 -2
- package/dist/object-array.js +1 -1
- package/dist/optional.d.ts +45 -0
- package/dist/promise.d.ts +7 -11
- package/dist/promise.js +4 -5
- package/dist/validate.js +1 -1
- package/dist/value-string-switch.js +1 -1
- package/package.json +67 -67
- package/types/index.d.ts +1 -0
- package/types/optional.d.ts +45 -0
- package/types/promise.d.ts +4 -1
- package/dist/object-array.es.js +0 -46
- package/dist/promise.es.js +0 -152
- package/dist/validate.es.js +0 -71
- package/dist/value-string-switch.es.js +0 -66
package/README.md
CHANGED
|
@@ -1,443 +1,443 @@
|
|
|
1
|
-
## Es Utils library
|
|
2
|
-
|
|
3
|
-
**es-util** is a useful methods library, contain some functions we might need in a project which based on ES
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
# Installation
|
|
7
|
-
|
|
8
|
-
#### npm
|
|
9
|
-
```shell
|
|
10
|
-
$ npm install @wxhccc/es-util --save
|
|
11
|
-
```
|
|
12
|
-
#### in browser
|
|
13
|
-
|
|
14
|
-
`<script src="https://cdn.jsdelivr.net/npm/@wxhccc/es-util/lib/index.min.js"></script>`
|
|
15
|
-
|
|
16
|
-
# Usage
|
|
17
|
-
|
|
18
|
-
#### in webpack or node
|
|
19
|
-
|
|
20
|
-
```javascript
|
|
21
|
-
import * as EsUtil from 'es-util'
|
|
22
|
-
// or import { xxx } from 'es-util'
|
|
23
|
-
// or const EsUtil = require('es-util')
|
|
24
|
-
|
|
25
|
-
// example, array2tree
|
|
26
|
-
const array = [
|
|
27
|
-
{ id: 1, pid: 0, name: 'language' },
|
|
28
|
-
{ id: 2, pid: 1, name: 'english' },
|
|
29
|
-
{ id: 3, pid: 1, name: 'chinese' }
|
|
30
|
-
]
|
|
31
|
-
console.log(EsUtil.array2tree)
|
|
32
|
-
/* log
|
|
33
|
-
[
|
|
34
|
-
{
|
|
35
|
-
id: 1,
|
|
36
|
-
pid: 0,
|
|
37
|
-
name: 'language',
|
|
38
|
-
children: [
|
|
39
|
-
{
|
|
40
|
-
id: 2,
|
|
41
|
-
pid: 1,
|
|
42
|
-
name: 'english',
|
|
43
|
-
children: []
|
|
44
|
-
},
|
|
45
|
-
{
|
|
46
|
-
id: 3,
|
|
47
|
-
pid: 1,
|
|
48
|
-
name: 'chinese',
|
|
49
|
-
children: []
|
|
50
|
-
}
|
|
51
|
-
]
|
|
52
|
-
}
|
|
53
|
-
]
|
|
54
|
-
*/
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
# API
|
|
59
|
-
|
|
60
|
-
## array-tree-switch module
|
|
61
|
-
|
|
62
|
-
### `array2tree(array, options)`
|
|
63
|
-
|
|
64
|
-
transform an array to tree structure
|
|
65
|
-
|
|
66
|
-
**parameters:**
|
|
67
|
-
- **array** {Array} The array need to transform.
|
|
68
|
-
- **options** {Object} The options.
|
|
69
|
-
- **options.primaryKey** {String} the primary key of array item, default `'id'`
|
|
70
|
-
- **options.parentKey** {String} the parent key of array item, default `'pid'`
|
|
71
|
-
- **options.childrenKey** {String} the childrenKey key of tree structure node, default `'children'`
|
|
72
|
-
- **options.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
|
|
73
|
-
- **options.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.
|
|
74
|
-
>warning: set the `parentRefKey` will make the return tree object/array cycling, so you can't use `JSON.stringify` to stringify it
|
|
75
|
-
|
|
76
|
-
**returns**: array or object with tree structure.
|
|
77
|
-
|
|
78
|
-
Example
|
|
79
|
-
|
|
80
|
-
```javascript
|
|
81
|
-
import { array2tree } from 'es-util'
|
|
82
|
-
|
|
83
|
-
const array = [
|
|
84
|
-
{ id: 1, parentId: 0, name: 'language' },
|
|
85
|
-
{ id: 2, parentId: 1, name: 'english' },
|
|
86
|
-
{ id: 3, parentId: 1, name: 'chinese' }
|
|
87
|
-
]
|
|
88
|
-
|
|
89
|
-
const tree = array2tree(array, {
|
|
90
|
-
parentKey: 'parentId',
|
|
91
|
-
childrenKey: 'nodes',
|
|
92
|
-
createRoot: true
|
|
93
|
-
})
|
|
94
|
-
/* log
|
|
95
|
-
{
|
|
96
|
-
nodes: [
|
|
97
|
-
{
|
|
98
|
-
id: 1,
|
|
99
|
-
pid: 0,
|
|
100
|
-
name: 'language',
|
|
101
|
-
nodes: [
|
|
102
|
-
{
|
|
103
|
-
id: 2,
|
|
104
|
-
pid: 1,
|
|
105
|
-
name: 'english',
|
|
106
|
-
nodes: []
|
|
107
|
-
},
|
|
108
|
-
{
|
|
109
|
-
id: 3,
|
|
110
|
-
pid: 1,
|
|
111
|
-
name: 'chinese',
|
|
112
|
-
nodes: []
|
|
113
|
-
}
|
|
114
|
-
]
|
|
115
|
-
}
|
|
116
|
-
]
|
|
117
|
-
}
|
|
118
|
-
*/
|
|
119
|
-
const tree = array2tree(array, {
|
|
120
|
-
parentRefKey: true
|
|
121
|
-
})
|
|
122
|
-
/* log
|
|
123
|
-
[
|
|
124
|
-
{
|
|
125
|
-
id: 1,
|
|
126
|
-
pid: 0,
|
|
127
|
-
name: 'language',
|
|
128
|
-
_parent: null,
|
|
129
|
-
children: [
|
|
130
|
-
{
|
|
131
|
-
id: 2,
|
|
132
|
-
pid: 1,
|
|
133
|
-
name: 'english',
|
|
134
|
-
children: [],
|
|
135
|
-
_parent: {
|
|
136
|
-
id: 1,
|
|
137
|
-
pid: 0
|
|
138
|
-
...,
|
|
139
|
-
_parent: ...
|
|
140
|
-
}
|
|
141
|
-
},
|
|
142
|
-
{
|
|
143
|
-
id: 3,
|
|
144
|
-
pid: 1,
|
|
145
|
-
name: 'chinese',
|
|
146
|
-
children: [],
|
|
147
|
-
_parent: {
|
|
148
|
-
id: 1,
|
|
149
|
-
pid: 0
|
|
150
|
-
...,
|
|
151
|
-
_parent: ...
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
]
|
|
155
|
-
}
|
|
156
|
-
]
|
|
157
|
-
*/
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
### `tree2array(tree, options)`
|
|
161
|
-
|
|
162
|
-
transform tree structure to an array
|
|
163
|
-
|
|
164
|
-
**parameters:**
|
|
165
|
-
- **tree** {Array/Object} The tree structure need to transform.
|
|
166
|
-
- **options** {Object} The options.
|
|
167
|
-
- **options.primaryKey** {String} the primary key of node item, default `'id'`
|
|
168
|
-
- **options.parentKey** {String} the parent key of node item, default `'pid'`
|
|
169
|
-
- **options.childrenKey** {String} the childrenKey key of node item, default `'children'`
|
|
170
|
-
- **options.hasParentKey** {Boolean} whether tree node has parent property, default `'true'`, if `'false'` ,will add an parent key property
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
**returns**: items array
|
|
174
|
-
|
|
175
|
-
Example
|
|
176
|
-
|
|
177
|
-
```javascript
|
|
178
|
-
import { tree2array } from 'es-util'
|
|
179
|
-
|
|
180
|
-
const tree = {
|
|
181
|
-
id: 0,
|
|
182
|
-
nodes: [
|
|
183
|
-
{
|
|
184
|
-
id: 1,
|
|
185
|
-
name: 'language',
|
|
186
|
-
children: [
|
|
187
|
-
{
|
|
188
|
-
id: 2,
|
|
189
|
-
name: 'english',
|
|
190
|
-
children: []
|
|
191
|
-
},
|
|
192
|
-
{
|
|
193
|
-
id: 3,
|
|
194
|
-
name: 'chinese',
|
|
195
|
-
children: []
|
|
196
|
-
}
|
|
197
|
-
]
|
|
198
|
-
}
|
|
199
|
-
]
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
const tree = array2tree(array, {
|
|
203
|
-
hasParentKey: false
|
|
204
|
-
})
|
|
205
|
-
console.log(tree)
|
|
206
|
-
/* log
|
|
207
|
-
[
|
|
208
|
-
{ id: 1, pid: 0, name: 'language' },
|
|
209
|
-
{ id: 2, pid: 1, name: 'english' },
|
|
210
|
-
{ id: 3, pid: 1, name: 'chinese' }
|
|
211
|
-
]
|
|
212
|
-
*/
|
|
213
|
-
```
|
|
214
|
-
|
|
215
|
-
## validate module
|
|
216
|
-
|
|
217
|
-
### `ChinaIdCardValid(idCard)`
|
|
218
|
-
|
|
219
|
-
Check whether the Chinese id number provided is valid
|
|
220
|
-
|
|
221
|
-
**parameters:**
|
|
222
|
-
- **idCard** {String} The IDcard number.
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
**returns**: boolean
|
|
226
|
-
|
|
227
|
-
Example
|
|
228
|
-
|
|
229
|
-
```javascript
|
|
230
|
-
import { ChinaIdCardValid } from 'es-util'
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
console.log(ChinaIdCardValid('[valid IDcard]')
|
|
234
|
-
console.log(ChinaIdCardValid('111111111111111111')
|
|
235
|
-
/* log
|
|
236
|
-
true
|
|
237
|
-
false
|
|
238
|
-
*/
|
|
239
|
-
```
|
|
240
|
-
|
|
241
|
-
### `formulaValidate(formulaString, variables)`
|
|
242
|
-
|
|
243
|
-
Check whether the formulaString provided is valid
|
|
244
|
-
|
|
245
|
-
**parameters:**
|
|
246
|
-
- **formulaString** {String} The formula string.
|
|
247
|
-
- **variables** {Array} The variables can appear in formula.
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
**returns**: boolean
|
|
251
|
-
|
|
252
|
-
Example
|
|
253
|
-
|
|
254
|
-
```javascript
|
|
255
|
-
import { formulaValidate } from 'es-util'
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
console.log(formulaValidate('A+B*(D-C/E)')
|
|
259
|
-
console.log(formulaValidate('A+*B/E')
|
|
260
|
-
console.log(formulaValidate('A+*B/E', ['A', 'B', 'C'])
|
|
261
|
-
/* log
|
|
262
|
-
true
|
|
263
|
-
false
|
|
264
|
-
false
|
|
265
|
-
*/
|
|
266
|
-
```
|
|
267
|
-
|
|
268
|
-
## object-array module
|
|
269
|
-
|
|
270
|
-
### `mapToObject(objectArray, keyProp, valueProp)`
|
|
271
|
-
|
|
272
|
-
create an object from an object array.
|
|
273
|
-
|
|
274
|
-
**parameters:**
|
|
275
|
-
- **objectArray** {Array} The source object array.
|
|
276
|
-
- **keyProp** {String/Function} The property of array item be used for key, or the function to create object key, default `"key"`.
|
|
277
|
-
> if array item not contain key or function not return a string/number,the item will ignore.
|
|
278
|
-
- **valueProp** {String/Function} The property of array item be used for value, or the function to create object value, default `"value"`.
|
|
279
|
-
|
|
280
|
-
**returns**: object
|
|
281
|
-
|
|
282
|
-
Example
|
|
283
|
-
|
|
284
|
-
```javascript
|
|
285
|
-
import { mapToObject } from 'es-util'
|
|
286
|
-
|
|
287
|
-
const array = [
|
|
288
|
-
{ key: 'a', value: 'b', name: 'afsfsdfe' },
|
|
289
|
-
{ key: 'a1', value: 'b1', name: 'afssdfsfe' },
|
|
290
|
-
{ key: 'a2', value: 'b2', name: 'afsfgege' }
|
|
291
|
-
]
|
|
292
|
-
console.log(mapToObject(array)
|
|
293
|
-
/* log
|
|
294
|
-
{ a: 'b', a1: 'b1', a2: 'b2' }
|
|
295
|
-
*/
|
|
296
|
-
console.log(mapToObject(array, item => (item.key + item.value), 'name'))
|
|
297
|
-
/* log
|
|
298
|
-
{ ab: 'afsfsdfe', a1b1: 'afssdfsfe', a2b2: 'afsfgege' }
|
|
299
|
-
*/
|
|
300
|
-
```
|
|
301
|
-
|
|
302
|
-
### `checkoutBy(object, keys, mergeFn)`
|
|
303
|
-
|
|
304
|
-
checkout an array from an object by gived keys, you can merge new data to object item
|
|
305
|
-
|
|
306
|
-
**parameters:**
|
|
307
|
-
- **object** {Object} The source object.
|
|
308
|
-
- **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)`
|
|
309
|
-
- **mergeFn** {Function} this method use `Object.assign` to merge values where two value are object, you can provide custom function to merge value.
|
|
310
|
-
- **empty** {Object} empty object
|
|
311
|
-
- **objectValue** {Object} the value of object[keyitem]
|
|
312
|
-
- **keysValue** {Object} the value of keys[keyitem]
|
|
313
|
-
|
|
314
|
-
**returns**: array
|
|
315
|
-
|
|
316
|
-
Example
|
|
317
|
-
|
|
318
|
-
```javascript
|
|
319
|
-
import { checkoutBy } from 'es-util'
|
|
320
|
-
|
|
321
|
-
const configs = {
|
|
322
|
-
a: { key: 1, name: 'afsfsdfe' },
|
|
323
|
-
b: { key: 2, name: { a: 'afssdfsfe' } },
|
|
324
|
-
c: { key: 3, name: 'afsfgege' },
|
|
325
|
-
d: { key: 4 }
|
|
326
|
-
}
|
|
327
|
-
console.log(checkoutBy(configs, ['a', 'c'])
|
|
328
|
-
/* log
|
|
329
|
-
[{ key: 1, name: 'afsfsdfe' }, { key: 3, name: 'afsfgege' }]
|
|
330
|
-
*/
|
|
331
|
-
console.log(checkoutBy(configs, { a: null, b: { name: { b: 'aaa' } } })
|
|
332
|
-
/* log
|
|
333
|
-
[{ key: 1, name: 'afsfsdfe' }, { key: 2, name: { b: 'aaa' } }]
|
|
334
|
-
*/
|
|
335
|
-
const { merge } = required('lodash')
|
|
336
|
-
console.log(checkoutBy(configs, { d: 123, b: { name: { b: 'aaa' } } }, merge)
|
|
337
|
-
/* log
|
|
338
|
-
[123, { key: 2, name: { a: 'afssdfsfe', b: 'aaa' } }]
|
|
339
|
-
*/
|
|
340
|
-
```
|
|
341
|
-
|
|
342
|
-
### `pickRenameKeys(object, keysMap)`
|
|
343
|
-
|
|
344
|
-
pick and rename object's keys
|
|
345
|
-
|
|
346
|
-
**parameters:**
|
|
347
|
-
- **object** {Object} The source object.
|
|
348
|
-
- **keysMap** {Object} The `oldKey-newKey` object
|
|
349
|
-
|
|
350
|
-
**returns**: array
|
|
351
|
-
|
|
352
|
-
Example
|
|
353
|
-
|
|
354
|
-
```javascript
|
|
355
|
-
import { pickRenameKeys } from 'es-util'
|
|
356
|
-
|
|
357
|
-
const configs = {
|
|
358
|
-
a: { name: 'afsfsdfe' },
|
|
359
|
-
b: 3,
|
|
360
|
-
c: [123],
|
|
361
|
-
d: 'aaa'
|
|
362
|
-
}
|
|
363
|
-
console.log(pickRenameKeys(configs, { 'a': 'a1', 'c': 'c3', 'd': 'd' })
|
|
364
|
-
/* log
|
|
365
|
-
{ a1: { name: 'afsfsdfe' }, c3: [123], d: 'aaa' }
|
|
366
|
-
*/
|
|
367
|
-
```
|
|
368
|
-
|
|
369
|
-
## value-string-switch module
|
|
370
|
-
|
|
371
|
-
### `byteStringify(byteNum, options)`
|
|
372
|
-
|
|
373
|
-
transform byte size to a string in the specified format
|
|
374
|
-
|
|
375
|
-
**parameters:**
|
|
376
|
-
- **byteNum** {Number} The size number need to transform.
|
|
377
|
-
- **options** {Object} The options.
|
|
378
|
-
- **options.standard** {String} the standard used to transform, default `'jedec'`, suport `'metric'`, `'iec'`. [Metric, IEC and JEDEC units](https://en.wikipedia.org/wiki/Gigabyte)
|
|
379
|
-
- **options.unitLvl** {String} the unit lvl to transform byte size, default `'auto'`. suport `'B','K','M','G','T','P','E','Z','Y'`
|
|
380
|
-
- **options.precision** {Number} the precision of value, default `1`
|
|
381
|
-
- **options.detail** {Boolean} whether to return an object of detai info, default `false`.
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
**returns**: string or object
|
|
385
|
-
|
|
386
|
-
Example
|
|
387
|
-
|
|
388
|
-
```javascript
|
|
389
|
-
import { byteStringify } from 'es-util'
|
|
390
|
-
|
|
391
|
-
byteStringify(1234)
|
|
392
|
-
/*log '1.2 KB'*/
|
|
393
|
-
byteStringify(-1234, { precision: 2 })
|
|
394
|
-
/*log '-1.21 KB'*/
|
|
395
|
-
byteStringify(1234, { unitLvl: 'M', precision: 3 })
|
|
396
|
-
/*log '0.001 MB'*/
|
|
397
|
-
byteStringify(1234, { detail: true, standard: 'metric', precision: 3 })
|
|
398
|
-
/*log { value: '1.234', unit: 'kB' } */
|
|
399
|
-
|
|
400
|
-
```
|
|
401
|
-
|
|
402
|
-
> these methods remove from v1.2.0 you can use lodash instead
|
|
403
|
-
### ~~`camelize(string)`~~
|
|
404
|
-
|
|
405
|
-
### ~~`hyphenate(string)`~~
|
|
406
|
-
|
|
407
|
-
### ~~`camel2snake(string)`~~
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
## promise module
|
|
412
|
-
|
|
413
|
-
### `awaitWrapper(promise)`
|
|
414
|
-
|
|
415
|
-
wrap promise with then and catch to return `[null, data]` or `[Error, undefined]`, useful async & await
|
|
416
|
-
|
|
417
|
-
### `wp(promise, [wrap])`
|
|
418
|
-
|
|
419
|
-
wrap promise or a function return promise with lock method to control UI or forbiden multi task at same time
|
|
420
|
-
|
|
421
|
-
**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
|
-
- **wrapOrOptions** {boolean|WrapOptions} shortcut of `wrap` option or full options.
|
|
424
|
-
- **wrap** {boolean} whether use `awaitWrapper` to wrap then promise.
|
|
425
|
-
- **lock** {string|(bool) => void|[ref, lockKey]}
|
|
426
|
-
string: property(support nested) used to lock. useful in vue instance and React Class component
|
|
427
|
-
|
|
428
|
-
function: useful in ReactHook component, pass setXXX to method
|
|
429
|
-
|
|
430
|
-
syncRefHandle: an array such as `[object, keyOfObject]` useful when need to lock promise when value can't be update sync, such as React
|
|
431
|
-
- **syncRefHandle** {[ref, lockKey]} when you need function and syncRefHandle at sametime
|
|
432
|
-
- **manualUnlock** {boolean} whether not unlock automatic
|
|
433
|
-
|
|
434
|
-
**returns**: a extends promise object with `__lockValue` getter and `unlock` method
|
|
435
|
-
|
|
436
|
-
> can be use in react and vue2/3 instance when bind this
|
|
437
|
-
|
|
438
|
-
### `wpVuePlugin`
|
|
439
|
-
|
|
440
|
-
an plugin object for vue
|
|
441
|
-
|
|
442
|
-
## License
|
|
1
|
+
## Es Utils library
|
|
2
|
+
|
|
3
|
+
**es-util** is a useful methods library, contain some functions we might need in a project which based on ES
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# Installation
|
|
7
|
+
|
|
8
|
+
#### npm
|
|
9
|
+
```shell
|
|
10
|
+
$ npm install @wxhccc/es-util --save
|
|
11
|
+
```
|
|
12
|
+
#### in browser
|
|
13
|
+
|
|
14
|
+
`<script src="https://cdn.jsdelivr.net/npm/@wxhccc/es-util/lib/index.min.js"></script>`
|
|
15
|
+
|
|
16
|
+
# Usage
|
|
17
|
+
|
|
18
|
+
#### in webpack or node
|
|
19
|
+
|
|
20
|
+
```javascript
|
|
21
|
+
import * as EsUtil from 'es-util'
|
|
22
|
+
// or import { xxx } from 'es-util'
|
|
23
|
+
// or const EsUtil = require('es-util')
|
|
24
|
+
|
|
25
|
+
// example, array2tree
|
|
26
|
+
const array = [
|
|
27
|
+
{ id: 1, pid: 0, name: 'language' },
|
|
28
|
+
{ id: 2, pid: 1, name: 'english' },
|
|
29
|
+
{ id: 3, pid: 1, name: 'chinese' }
|
|
30
|
+
]
|
|
31
|
+
console.log(EsUtil.array2tree)
|
|
32
|
+
/* log
|
|
33
|
+
[
|
|
34
|
+
{
|
|
35
|
+
id: 1,
|
|
36
|
+
pid: 0,
|
|
37
|
+
name: 'language',
|
|
38
|
+
children: [
|
|
39
|
+
{
|
|
40
|
+
id: 2,
|
|
41
|
+
pid: 1,
|
|
42
|
+
name: 'english',
|
|
43
|
+
children: []
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
id: 3,
|
|
47
|
+
pid: 1,
|
|
48
|
+
name: 'chinese',
|
|
49
|
+
children: []
|
|
50
|
+
}
|
|
51
|
+
]
|
|
52
|
+
}
|
|
53
|
+
]
|
|
54
|
+
*/
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
# API
|
|
59
|
+
|
|
60
|
+
## array-tree-switch module
|
|
61
|
+
|
|
62
|
+
### `array2tree(array, options)`
|
|
63
|
+
|
|
64
|
+
transform an array to tree structure
|
|
65
|
+
|
|
66
|
+
**parameters:**
|
|
67
|
+
- **array** {Array} The array need to transform.
|
|
68
|
+
- **options** {Object} The options.
|
|
69
|
+
- **options.primaryKey** {String} the primary key of array item, default `'id'`
|
|
70
|
+
- **options.parentKey** {String} the parent key of array item, default `'pid'`
|
|
71
|
+
- **options.childrenKey** {String} the childrenKey key of tree structure node, default `'children'`
|
|
72
|
+
- **options.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
|
|
73
|
+
- **options.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.
|
|
74
|
+
>warning: set the `parentRefKey` will make the return tree object/array cycling, so you can't use `JSON.stringify` to stringify it
|
|
75
|
+
|
|
76
|
+
**returns**: array or object with tree structure.
|
|
77
|
+
|
|
78
|
+
Example
|
|
79
|
+
|
|
80
|
+
```javascript
|
|
81
|
+
import { array2tree } from 'es-util'
|
|
82
|
+
|
|
83
|
+
const array = [
|
|
84
|
+
{ id: 1, parentId: 0, name: 'language' },
|
|
85
|
+
{ id: 2, parentId: 1, name: 'english' },
|
|
86
|
+
{ id: 3, parentId: 1, name: 'chinese' }
|
|
87
|
+
]
|
|
88
|
+
|
|
89
|
+
const tree = array2tree(array, {
|
|
90
|
+
parentKey: 'parentId',
|
|
91
|
+
childrenKey: 'nodes',
|
|
92
|
+
createRoot: true
|
|
93
|
+
})
|
|
94
|
+
/* log
|
|
95
|
+
{
|
|
96
|
+
nodes: [
|
|
97
|
+
{
|
|
98
|
+
id: 1,
|
|
99
|
+
pid: 0,
|
|
100
|
+
name: 'language',
|
|
101
|
+
nodes: [
|
|
102
|
+
{
|
|
103
|
+
id: 2,
|
|
104
|
+
pid: 1,
|
|
105
|
+
name: 'english',
|
|
106
|
+
nodes: []
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
id: 3,
|
|
110
|
+
pid: 1,
|
|
111
|
+
name: 'chinese',
|
|
112
|
+
nodes: []
|
|
113
|
+
}
|
|
114
|
+
]
|
|
115
|
+
}
|
|
116
|
+
]
|
|
117
|
+
}
|
|
118
|
+
*/
|
|
119
|
+
const tree = array2tree(array, {
|
|
120
|
+
parentRefKey: true
|
|
121
|
+
})
|
|
122
|
+
/* log
|
|
123
|
+
[
|
|
124
|
+
{
|
|
125
|
+
id: 1,
|
|
126
|
+
pid: 0,
|
|
127
|
+
name: 'language',
|
|
128
|
+
_parent: null,
|
|
129
|
+
children: [
|
|
130
|
+
{
|
|
131
|
+
id: 2,
|
|
132
|
+
pid: 1,
|
|
133
|
+
name: 'english',
|
|
134
|
+
children: [],
|
|
135
|
+
_parent: {
|
|
136
|
+
id: 1,
|
|
137
|
+
pid: 0
|
|
138
|
+
...,
|
|
139
|
+
_parent: ...
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
id: 3,
|
|
144
|
+
pid: 1,
|
|
145
|
+
name: 'chinese',
|
|
146
|
+
children: [],
|
|
147
|
+
_parent: {
|
|
148
|
+
id: 1,
|
|
149
|
+
pid: 0
|
|
150
|
+
...,
|
|
151
|
+
_parent: ...
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
]
|
|
155
|
+
}
|
|
156
|
+
]
|
|
157
|
+
*/
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### `tree2array(tree, options)`
|
|
161
|
+
|
|
162
|
+
transform tree structure to an array
|
|
163
|
+
|
|
164
|
+
**parameters:**
|
|
165
|
+
- **tree** {Array/Object} The tree structure need to transform.
|
|
166
|
+
- **options** {Object} The options.
|
|
167
|
+
- **options.primaryKey** {String} the primary key of node item, default `'id'`
|
|
168
|
+
- **options.parentKey** {String} the parent key of node item, default `'pid'`
|
|
169
|
+
- **options.childrenKey** {String} the childrenKey key of node item, default `'children'`
|
|
170
|
+
- **options.hasParentKey** {Boolean} whether tree node has parent property, default `'true'`, if `'false'` ,will add an parent key property
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
**returns**: items array
|
|
174
|
+
|
|
175
|
+
Example
|
|
176
|
+
|
|
177
|
+
```javascript
|
|
178
|
+
import { tree2array } from 'es-util'
|
|
179
|
+
|
|
180
|
+
const tree = {
|
|
181
|
+
id: 0,
|
|
182
|
+
nodes: [
|
|
183
|
+
{
|
|
184
|
+
id: 1,
|
|
185
|
+
name: 'language',
|
|
186
|
+
children: [
|
|
187
|
+
{
|
|
188
|
+
id: 2,
|
|
189
|
+
name: 'english',
|
|
190
|
+
children: []
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
id: 3,
|
|
194
|
+
name: 'chinese',
|
|
195
|
+
children: []
|
|
196
|
+
}
|
|
197
|
+
]
|
|
198
|
+
}
|
|
199
|
+
]
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const tree = array2tree(array, {
|
|
203
|
+
hasParentKey: false
|
|
204
|
+
})
|
|
205
|
+
console.log(tree)
|
|
206
|
+
/* log
|
|
207
|
+
[
|
|
208
|
+
{ id: 1, pid: 0, name: 'language' },
|
|
209
|
+
{ id: 2, pid: 1, name: 'english' },
|
|
210
|
+
{ id: 3, pid: 1, name: 'chinese' }
|
|
211
|
+
]
|
|
212
|
+
*/
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## validate module
|
|
216
|
+
|
|
217
|
+
### `ChinaIdCardValid(idCard)`
|
|
218
|
+
|
|
219
|
+
Check whether the Chinese id number provided is valid
|
|
220
|
+
|
|
221
|
+
**parameters:**
|
|
222
|
+
- **idCard** {String} The IDcard number.
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
**returns**: boolean
|
|
226
|
+
|
|
227
|
+
Example
|
|
228
|
+
|
|
229
|
+
```javascript
|
|
230
|
+
import { ChinaIdCardValid } from 'es-util'
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
console.log(ChinaIdCardValid('[valid IDcard]')
|
|
234
|
+
console.log(ChinaIdCardValid('111111111111111111')
|
|
235
|
+
/* log
|
|
236
|
+
true
|
|
237
|
+
false
|
|
238
|
+
*/
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
### `formulaValidate(formulaString, variables)`
|
|
242
|
+
|
|
243
|
+
Check whether the formulaString provided is valid
|
|
244
|
+
|
|
245
|
+
**parameters:**
|
|
246
|
+
- **formulaString** {String} The formula string.
|
|
247
|
+
- **variables** {Array} The variables can appear in formula.
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
**returns**: boolean
|
|
251
|
+
|
|
252
|
+
Example
|
|
253
|
+
|
|
254
|
+
```javascript
|
|
255
|
+
import { formulaValidate } from 'es-util'
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
console.log(formulaValidate('A+B*(D-C/E)')
|
|
259
|
+
console.log(formulaValidate('A+*B/E')
|
|
260
|
+
console.log(formulaValidate('A+*B/E', ['A', 'B', 'C'])
|
|
261
|
+
/* log
|
|
262
|
+
true
|
|
263
|
+
false
|
|
264
|
+
false
|
|
265
|
+
*/
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
## object-array module
|
|
269
|
+
|
|
270
|
+
### `mapToObject(objectArray, keyProp, valueProp)`
|
|
271
|
+
|
|
272
|
+
create an object from an object array.
|
|
273
|
+
|
|
274
|
+
**parameters:**
|
|
275
|
+
- **objectArray** {Array} The source object array.
|
|
276
|
+
- **keyProp** {String/Function} The property of array item be used for key, or the function to create object key, default `"key"`.
|
|
277
|
+
> if array item not contain key or function not return a string/number,the item will ignore.
|
|
278
|
+
- **valueProp** {String/Function} The property of array item be used for value, or the function to create object value, default `"value"`.
|
|
279
|
+
|
|
280
|
+
**returns**: object
|
|
281
|
+
|
|
282
|
+
Example
|
|
283
|
+
|
|
284
|
+
```javascript
|
|
285
|
+
import { mapToObject } from 'es-util'
|
|
286
|
+
|
|
287
|
+
const array = [
|
|
288
|
+
{ key: 'a', value: 'b', name: 'afsfsdfe' },
|
|
289
|
+
{ key: 'a1', value: 'b1', name: 'afssdfsfe' },
|
|
290
|
+
{ key: 'a2', value: 'b2', name: 'afsfgege' }
|
|
291
|
+
]
|
|
292
|
+
console.log(mapToObject(array)
|
|
293
|
+
/* log
|
|
294
|
+
{ a: 'b', a1: 'b1', a2: 'b2' }
|
|
295
|
+
*/
|
|
296
|
+
console.log(mapToObject(array, item => (item.key + item.value), 'name'))
|
|
297
|
+
/* log
|
|
298
|
+
{ ab: 'afsfsdfe', a1b1: 'afssdfsfe', a2b2: 'afsfgege' }
|
|
299
|
+
*/
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
### `checkoutBy(object, keys, mergeFn)`
|
|
303
|
+
|
|
304
|
+
checkout an array from an object by gived keys, you can merge new data to object item
|
|
305
|
+
|
|
306
|
+
**parameters:**
|
|
307
|
+
- **object** {Object} The source object.
|
|
308
|
+
- **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)`
|
|
309
|
+
- **mergeFn** {Function} this method use `Object.assign` to merge values where two value are object, you can provide custom function to merge value.
|
|
310
|
+
- **empty** {Object} empty object
|
|
311
|
+
- **objectValue** {Object} the value of object[keyitem]
|
|
312
|
+
- **keysValue** {Object} the value of keys[keyitem]
|
|
313
|
+
|
|
314
|
+
**returns**: array
|
|
315
|
+
|
|
316
|
+
Example
|
|
317
|
+
|
|
318
|
+
```javascript
|
|
319
|
+
import { checkoutBy } from 'es-util'
|
|
320
|
+
|
|
321
|
+
const configs = {
|
|
322
|
+
a: { key: 1, name: 'afsfsdfe' },
|
|
323
|
+
b: { key: 2, name: { a: 'afssdfsfe' } },
|
|
324
|
+
c: { key: 3, name: 'afsfgege' },
|
|
325
|
+
d: { key: 4 }
|
|
326
|
+
}
|
|
327
|
+
console.log(checkoutBy(configs, ['a', 'c'])
|
|
328
|
+
/* log
|
|
329
|
+
[{ key: 1, name: 'afsfsdfe' }, { key: 3, name: 'afsfgege' }]
|
|
330
|
+
*/
|
|
331
|
+
console.log(checkoutBy(configs, { a: null, b: { name: { b: 'aaa' } } })
|
|
332
|
+
/* log
|
|
333
|
+
[{ key: 1, name: 'afsfsdfe' }, { key: 2, name: { b: 'aaa' } }]
|
|
334
|
+
*/
|
|
335
|
+
const { merge } = required('lodash')
|
|
336
|
+
console.log(checkoutBy(configs, { d: 123, b: { name: { b: 'aaa' } } }, merge)
|
|
337
|
+
/* log
|
|
338
|
+
[123, { key: 2, name: { a: 'afssdfsfe', b: 'aaa' } }]
|
|
339
|
+
*/
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
### `pickRenameKeys(object, keysMap)`
|
|
343
|
+
|
|
344
|
+
pick and rename object's keys
|
|
345
|
+
|
|
346
|
+
**parameters:**
|
|
347
|
+
- **object** {Object} The source object.
|
|
348
|
+
- **keysMap** {Object} The `oldKey-newKey` object
|
|
349
|
+
|
|
350
|
+
**returns**: array
|
|
351
|
+
|
|
352
|
+
Example
|
|
353
|
+
|
|
354
|
+
```javascript
|
|
355
|
+
import { pickRenameKeys } from 'es-util'
|
|
356
|
+
|
|
357
|
+
const configs = {
|
|
358
|
+
a: { name: 'afsfsdfe' },
|
|
359
|
+
b: 3,
|
|
360
|
+
c: [123],
|
|
361
|
+
d: 'aaa'
|
|
362
|
+
}
|
|
363
|
+
console.log(pickRenameKeys(configs, { 'a': 'a1', 'c': 'c3', 'd': 'd' })
|
|
364
|
+
/* log
|
|
365
|
+
{ a1: { name: 'afsfsdfe' }, c3: [123], d: 'aaa' }
|
|
366
|
+
*/
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
## value-string-switch module
|
|
370
|
+
|
|
371
|
+
### `byteStringify(byteNum, options)`
|
|
372
|
+
|
|
373
|
+
transform byte size to a string in the specified format
|
|
374
|
+
|
|
375
|
+
**parameters:**
|
|
376
|
+
- **byteNum** {Number} The size number need to transform.
|
|
377
|
+
- **options** {Object} The options.
|
|
378
|
+
- **options.standard** {String} the standard used to transform, default `'jedec'`, suport `'metric'`, `'iec'`. [Metric, IEC and JEDEC units](https://en.wikipedia.org/wiki/Gigabyte)
|
|
379
|
+
- **options.unitLvl** {String} the unit lvl to transform byte size, default `'auto'`. suport `'B','K','M','G','T','P','E','Z','Y'`
|
|
380
|
+
- **options.precision** {Number} the precision of value, default `1`
|
|
381
|
+
- **options.detail** {Boolean} whether to return an object of detai info, default `false`.
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
**returns**: string or object
|
|
385
|
+
|
|
386
|
+
Example
|
|
387
|
+
|
|
388
|
+
```javascript
|
|
389
|
+
import { byteStringify } from 'es-util'
|
|
390
|
+
|
|
391
|
+
byteStringify(1234)
|
|
392
|
+
/*log '1.2 KB'*/
|
|
393
|
+
byteStringify(-1234, { precision: 2 })
|
|
394
|
+
/*log '-1.21 KB'*/
|
|
395
|
+
byteStringify(1234, { unitLvl: 'M', precision: 3 })
|
|
396
|
+
/*log '0.001 MB'*/
|
|
397
|
+
byteStringify(1234, { detail: true, standard: 'metric', precision: 3 })
|
|
398
|
+
/*log { value: '1.234', unit: 'kB' } */
|
|
399
|
+
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
> these methods remove from v1.2.0 you can use lodash instead
|
|
403
|
+
### ~~`camelize(string)`~~
|
|
404
|
+
|
|
405
|
+
### ~~`hyphenate(string)`~~
|
|
406
|
+
|
|
407
|
+
### ~~`camel2snake(string)`~~
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
## promise module
|
|
412
|
+
|
|
413
|
+
### `awaitWrapper(promise)`
|
|
414
|
+
|
|
415
|
+
wrap promise with then and catch to return `[null, data]` or `[Error, undefined]`, useful async & await
|
|
416
|
+
|
|
417
|
+
### `wp(promise, [wrap])`
|
|
418
|
+
|
|
419
|
+
wrap promise or a function return promise with lock method to control UI or forbiden multi task at same time
|
|
420
|
+
|
|
421
|
+
**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
|
+
- **wrapOrOptions** {boolean|WrapOptions} shortcut of `wrap` option or full options.
|
|
424
|
+
- **wrap** {boolean} whether use `awaitWrapper` to wrap then promise.
|
|
425
|
+
- **lock** {string|(bool) => void|[ref, lockKey]}
|
|
426
|
+
string: property(support nested) used to lock. useful in vue instance and React Class component
|
|
427
|
+
|
|
428
|
+
function: useful in ReactHook component, pass setXXX to method
|
|
429
|
+
|
|
430
|
+
syncRefHandle: an array such as `[object, keyOfObject]` useful when need to lock promise when value can't be update sync, such as React
|
|
431
|
+
- **syncRefHandle** {[ref, lockKey]} when you need function and syncRefHandle at sametime
|
|
432
|
+
- **manualUnlock** {boolean} whether not unlock automatic
|
|
433
|
+
|
|
434
|
+
**returns**: a extends promise object with `__lockValue` getter and `unlock` method
|
|
435
|
+
|
|
436
|
+
> can be use in react and vue2/3 instance when bind this
|
|
437
|
+
|
|
438
|
+
### `wpVuePlugin`
|
|
439
|
+
|
|
440
|
+
an plugin object for vue
|
|
441
|
+
|
|
442
|
+
## License
|
|
443
443
|
MIT
|