@hairy/utils 1.49.0 → 1.50.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.
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2025-PRESENT Hairyf <https://github.com/antfu>
3
+ Copyright (c) 2025-PRESENT Hairyf <https://github.com/hairyf>
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,36 +1,688 @@
1
1
  # @hairy/utils
2
2
 
3
- [![npm version][npm-version-src]][npm-version-href]
4
- [![npm downloads][npm-downloads-src]][npm-downloads-href]
5
- [![bundle][bundle-src]][bundle-href]
6
- [![JSDocs][jsdocs-src]][jsdocs-href]
7
- [![License][license-src]][license-href]
3
+ <!-- automd:badges name="@hairy/utils" github="hairyf/hairylib" license bundlephobia -->
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@hairy/utils)](https://npmjs.com/package/@hairy/utils)
6
+ [![npm downloads](https://img.shields.io/npm/dm/@hairy/utils)](https://npm.chart.dev/@hairy/utils)
7
+ [![bundle size](https://img.shields.io/bundlephobia/minzip/@hairy/utils)](https://bundlephobia.com/package/@hairy/utils)
8
+ [![license](https://img.shields.io/github/license/hairyf/hairylib)](https://github.com/hairyf/hairylib/blob/main/LICENSE)
9
+
10
+ <!-- /automd -->
11
+
12
+ Core utility functions shared across all Hairylib packages.
8
13
 
9
14
  ## Install
10
15
 
16
+ <!-- automd:pm-install name="@hairy/utils" -->
17
+
18
+ ```sh
19
+ # ✨ Auto-detect
20
+ npx nypm install @hairy/utils
21
+
22
+ # npm
23
+ npm install @hairy/utils
24
+
25
+ # yarn
26
+ yarn add @hairy/utils
27
+
28
+ # pnpm
29
+ pnpm add @hairy/utils
30
+
31
+ # bun
32
+ bun install @hairy/utils
33
+
34
+ # deno
35
+ deno install npm:@hairy/utils
36
+ ```
37
+
38
+ <!-- /automd -->
39
+
40
+ ## API
41
+
42
+ <!-- automd:jsdocs src="./src/index.ts" headingLevel="2" -->
43
+
44
+ ### `arange()`
45
+
46
+ ### `average(array, options?)`
47
+
48
+ ### `BIG_INTS`
49
+
50
+ #### `b`
51
+
52
+ ##### `d`
53
+
54
+ - **Type**: `number`
55
+ - **Default**: `10`
56
+
57
+ ##### `n`
58
+
59
+ - **Type**: `string`
60
+ - **Default**: `"b"`
61
+
62
+ ##### `v`
63
+
64
+ - **Type**: `number`
65
+ - **Default**: `1000000000`
66
+
67
+ #### `k`
68
+
69
+ ##### `d`
70
+
71
+ - **Type**: `number`
72
+ - **Default**: `4`
73
+
74
+ ##### `n`
75
+
76
+ - **Type**: `string`
77
+ - **Default**: `"k"`
78
+
79
+ ##### `v`
80
+
81
+ - **Type**: `number`
82
+ - **Default**: `1000`
83
+
84
+ #### `m`
85
+
86
+ ##### `d`
87
+
88
+ - **Type**: `number`
89
+ - **Default**: `7`
90
+
91
+ ##### `n`
92
+
93
+ - **Type**: `string`
94
+ - **Default**: `"m"`
95
+
96
+ ##### `v`
97
+
98
+ - **Type**: `number`
99
+ - **Default**: `1000000`
100
+
101
+ #### `t`
102
+
103
+ ##### `d`
104
+
105
+ - **Type**: `number`
106
+ - **Default**: `13`
107
+
108
+ ##### `n`
109
+
110
+ - **Type**: `string`
111
+ - **Default**: `"t"`
112
+
113
+ ##### `v`
114
+
115
+ - **Type**: `number`
116
+ - **Default**: `1000000000000`
117
+
118
+ ### `Bignumber()`
119
+
120
+ ### `bignumber(n, base?)`
121
+
122
+ ### `call(fn)`
123
+
124
+ ### `camelCase()`
125
+
126
+ ### `capitalCase()`
127
+
128
+ ### `chunk()`
129
+
130
+ ### `clone()`
131
+
132
+ ### `cloneDeep()`
133
+
134
+ ### `cloneDeepWith()`
135
+
136
+ ### `cloneWith()`
137
+
138
+ ### `compose()`
139
+
140
+ ### `concat()`
141
+
142
+ ### `constantCase()`
143
+
144
+ ### `cover(value, mode, symbol)`
145
+
146
+ Intercept front and back characters, hide middle characters
147
+
148
+ ### `debounce()`
149
+
150
+ ### `decimal(value, n)`
151
+
152
+ retain n decimal places
153
+
154
+ ### `DEFAULT_BIGNUM_CONFIG`
155
+
156
+ #### `DECIMAL_PLACES`
157
+
158
+ - **Type**: `number`
159
+ - **Default**: `6`
160
+
161
+ #### `ROUNDING_MODE`
162
+
163
+ - **Type**: `number`
164
+ - **Default**: `0`
165
+
166
+ ### `Deferred()`
167
+
168
+ ### `delay(ms)`
169
+
170
+ Delay for a given number of milliseconds.
171
+
172
+ **Example:**
173
+
174
+ ```ts
175
+ delay(1000).then(() => { console.log('1 second') })
176
+ ```
177
+
178
+ ### `dialsPhone(phoneNumber)`
179
+
180
+ ### `divide(array, options?)`
181
+
182
+ ### `dotCase()`
183
+
184
+ ### `downloadBlobFile(data, name)`
185
+
186
+ Generate Blob | string file and download it
187
+
188
+ ### `downloadNetworkFile(url, name?)`
189
+
190
+ Download network files
191
+
192
+ ### `downloadUrlFile(url, name?)`
193
+
194
+ Download network files
195
+
196
+ ### `ensurePrefix(prefix, str)`
197
+
198
+ Ensure prefix of a string
199
+
200
+ ### `ensureSuffix(suffix, str)`
201
+
202
+ Ensure suffix of a string
203
+
204
+ ### `find()`
205
+
206
+ ### `formatNumeric(value, options?)`
207
+
208
+ format number thousand separator and unit
209
+
210
+ ### `formdataToObject(formData)`
211
+
212
+ formData to object
213
+
214
+ ### `get()`
215
+
216
+ ### `ghost(strictMessage?)`
217
+
218
+ ### `groupBy()`
219
+
220
+ ### `gt(a, b)`
221
+
222
+ ### `gte(a, b)`
223
+
224
+ ### `integer(value)`
225
+
226
+ format as a positive integer
227
+
228
+ ### `isAndroid()`
229
+
230
+ ### `isArguments()`
231
+
232
+ ### `isArray()`
233
+
234
+ ### `isArrayBuffer()`
235
+
236
+ ### `isArrayLike()`
237
+
238
+ ### `isArrayLikeObject()`
239
+
240
+ ### `isBoolean()`
241
+
242
+ ### `isBrowser()`
243
+
244
+ ### `isBuffer()`
245
+
246
+ ### `isChrome()`
247
+
248
+ ### `isDate()`
249
+
250
+ ### `isEdge()`
251
+
252
+ ### `isElement()`
253
+
254
+ ### `isEmpty()`
255
+
256
+ ### `isEqual()`
257
+
258
+ ### `isEqualWith()`
259
+
260
+ ### `isError()`
261
+
262
+ ### `isFF()`
263
+
264
+ ### `isFormData(value)`
265
+
266
+ ### `isFunction()`
267
+
268
+ ### `isIE()`
269
+
270
+ ### `isIE11()`
271
+
272
+ ### `isIE9()`
273
+
274
+ ### `isInteger()`
275
+
276
+ ### `isIOS()`
277
+
278
+ ### `isMap()`
279
+
280
+ ### `isMatch()`
281
+
282
+ ### `isMatchWith()`
283
+
284
+ ### `isMobile()`
285
+
286
+ ### `isNaN()`
287
+
288
+ ### `isNative()`
289
+
290
+ ### `isNull()`
291
+
292
+ ### `isNumber()`
293
+
294
+ ### `isObject()`
295
+
296
+ ### `isObjectLike()`
297
+
298
+ ### `isPhantomJS()`
299
+
300
+ ### `isPlainObject()`
301
+
302
+ ### `isRegexp()`
303
+
304
+ ### `isSet()`
305
+
306
+ ### `isString()`
307
+
308
+ ### `isSymbol()`
309
+
310
+ ### `isTruthy(value)`
311
+
312
+ ### `isUndefined()`
313
+
314
+ ### `isWeakMap()`
315
+
316
+ ### `isWeakSet()`
317
+
318
+ ### `isWeex()`
319
+
320
+ ### `isWindow(value)`
321
+
322
+ ### `join()`
323
+
324
+ ### `kebabCase()`
325
+
326
+ ### `keyBy()`
327
+
328
+ ### `keys()`
329
+
330
+ ### `loop()`
331
+
332
+ ### `lt(a, b)`
333
+
334
+ ### `lte(a, b)`
335
+
336
+ ### `max()`
337
+
338
+ ### `maxBy()`
339
+
340
+ ### `merge()`
341
+
342
+ ### `mergeWith()`
343
+
344
+ ### `min()`
345
+
346
+ ### `minBy()`
347
+
348
+ ### `multiply(array, options?)`
349
+
350
+ ### `noCase()`
351
+
352
+ ### `nonnanable(value)`
353
+
354
+ Check if a value is not NaN.
355
+
356
+ ### `noop()`
357
+
358
+ ### `numberify(value)`
359
+
360
+ Convert a value to a number.
361
+
362
+ ### `numberish(value)`
363
+
364
+ Convert a value to a numberish value.
365
+
366
+ ### `objectToFormdata(object)`
367
+
368
+ Object to formData
369
+
370
+ ### `off(obj)`
371
+
372
+ ### `omit()`
373
+
374
+ ### `omitBy()`
375
+
376
+ ### `on(obj)`
377
+
378
+ ### `once()`
379
+
380
+ ### `openFilePicker(option)`
381
+
382
+ eslint-disable prefer-promise-reject-errors
383
+
384
+ Select multiple files
385
+
386
+ ### `openImagePicker(options)`
387
+
388
+ Select multiple images
389
+
390
+ ### `parseNumeric(num, delimiters)`
391
+
392
+ ### `pascalCase()`
393
+
394
+ ### `pascalSnakeCase()`
395
+
396
+ ### `pathCase()`
397
+
398
+ ### `percentage(total, count, options?)`
399
+
400
+ calculate percentage
401
+
402
+ ### `pick()`
403
+
404
+ ### `pickBy()`
405
+
406
+ ### `pipe()`
407
+
408
+ ### `plus(array, options?)`
409
+
410
+ ### `proxy(initObject?, initExtend?, options?: { strictMessage? })`
411
+
412
+ eslint-disable ts/no-empty-object-type
413
+
414
+ Creates a proxy object that updates the original object when the proxy is updated.
415
+
416
+ **Example:**
417
+
418
+ ```ts
419
+ const obj = proxy({ name: 'John' })
420
+ console.log(obj.name) // John
421
+
422
+ obj.proxy.update({ name: 'Jane' })
423
+ console.log(obj.name) // Jane
424
+
425
+ const obj2 = proxy()
426
+
427
+ obj2.any // Error: Proxy not updated. Call object.proxy.update() to update the proxy.
428
+
429
+ obj2.proxy.source // undefined
430
+ obj2.update({ name: 'John' })
431
+
432
+ // get the original object
433
+ obj2.proxy.source // { name: 'John' }
434
+ ```
435
+
436
+ ### `randomItem(array)`
437
+
438
+ Get a random item from an array.
439
+
440
+ **Example:**
441
+
442
+ ```ts
443
+ randomItem(['a', 'b', 'c']) // 'a' | 'b' | 'c'
444
+ ```
445
+
446
+ ### `randomNumber(min, max)`
447
+
448
+ Get a random number between a minimum and maximum value.
449
+
450
+ **Example:**
451
+
452
+ ```ts
453
+ randomNumber(0, 100) // 0-100
454
+ ```
455
+
456
+ ### `randomString(size, chars)`
457
+
458
+ Get a random string of a given size.
459
+
460
+ **Example:**
461
+
462
+ ```ts
463
+ randomString() // 10 characters long
464
+ randomString(20) // 20 characters long
465
+ randomString(20, 'abcdefghijklmnopqrstuvwxyz') // 20 characters long
466
+ ```
467
+
468
+ ### `range()`
469
+
470
+ ### `readFileReader(formType, file)`
471
+
472
+ Read File file
473
+
474
+ ### `redirectTo(url, target)`
475
+
476
+ ### `riposte()`
477
+
478
+ Select a value based on a condition.
479
+
480
+ **Example:**
481
+
482
+ ```ts
483
+ select(
484
+ [condition1, value],
485
+ [condition2, value2],
486
+ // default value
487
+ [true, value3],
488
+ ...
489
+ ) // value
490
+ ```
491
+
492
+ ### `select()`
493
+
494
+ Select a value based on a condition.
495
+
496
+ **Example:**
497
+
498
+ ```ts
499
+ select(
500
+ [condition1, value],
501
+ [condition2, value2],
502
+ // default value
503
+ [true, value3],
504
+ ...
505
+ ) // value
506
+ ```
507
+
508
+ ### `selectImages(options)`
509
+
510
+ Select multiple images
511
+
512
+ ### `sentenceCase()`
513
+
514
+ ### `set()`
515
+
516
+ ### `shortenId(value, startWith, endWith)`
517
+
518
+ Shortens an identifier string by showing only the beginning and end portions, with ellipsis in the middle. Suitable for various types of identifiers like IPFS CID, transaction hashes, EVM addresses, user IDs, etc.
519
+
520
+ ### `showOpenFilePicker(option)`
521
+
522
+ eslint-disable prefer-promise-reject-errors
523
+
524
+ Select multiple files
525
+
526
+ ### `showOpenImagePicker(options)`
527
+
528
+ Select multiple images
529
+
530
+ ### `size(dimension, unit?)`
531
+
532
+ ### `slash(str)`
533
+
534
+ Replace backslash to slash
535
+
536
+ ### `snakeCase()`
537
+
538
+ ### `stringify(value)`
539
+
540
+ Convert a value to a string.
541
+
542
+ ### `template(str)`
543
+
544
+ ### `to(promise, error?)`
545
+
546
+ Convert a promise to a tuple of [error, data].
547
+
548
+ **Example:**
549
+
550
+ ```ts
551
+ to(Promise.resolve('data')) // Promise<[null, 'data']>
552
+ to(Promise.reject(new Error('error'))) // Promise<[Error, undefined]>
553
+ ```
554
+
555
+ ### `toArray(value?, required)`
556
+
557
+ Convert a value to an array.
558
+
559
+ **Example:**
560
+
561
+ ```ts
562
+ toArray(arrorOrItemOrUndefined) // item[] | undefined
563
+ toArray(arrayOrItemOrUndefined, true) // item[]
564
+ ```
565
+
566
+ ### `trainCase()`
567
+
568
+ ### `truncate()`
569
+
570
+ ### `tryParseJson(text)`
571
+
572
+ ### `unindent(str)`
573
+
574
+ Remove common leading whitespace from a template string. Will also remove empty lines at the beginning and end.
575
+
576
+ **Example:**
577
+
578
+ ```ts
579
+ const str = unindent`
580
+ if (a) {
581
+ b()
582
+ }
583
+ `
11
584
  ```
12
- ni @hairy/utils
585
+
586
+ ### `uniq()`
587
+
588
+ ### `uniqBy()`
589
+
590
+ ### `uniqWith()`
591
+
592
+ ### `unit(value, unit)`
593
+
594
+ ### `unwrap(value)`
595
+
596
+ Unwrap a value or a function that returns a value.
597
+
598
+ **Example:**
599
+
600
+ ```ts
601
+ unwrap({ name: 'John' }) // { name: 'John' }
602
+ unwrap(() => { return { name: 'John' } }) // { name: 'John' }
13
603
  ```
14
604
 
15
- ## CDN
605
+ ### `values()`
606
+
607
+ ### `whenever(value, callback)`
608
+
609
+ Call a callback if a value is not null or undefined.
16
610
 
17
- ```html
18
- <script src="https://unpkg.com/@hairy/utils"></script>
611
+ **Example:**
612
+
613
+ ```ts
614
+ whenever(value, (value) => { return 'value' }) // value
19
615
  ```
20
616
 
617
+ ### `zerofill(value, n, type)`
618
+
619
+ leading zeros
620
+
621
+ ### `zeroRemove(value, convert)`
622
+
623
+ <!-- /automd -->
624
+
625
+ ## Source
626
+
627
+ <!-- automd:file src="./src/index.ts" code lang="ts" -->
628
+
629
+ ```ts [index.ts]
630
+ export * from './browser'
631
+ export * from './is'
632
+ export * from './module'
633
+ export * from './number'
634
+ export * from './string'
635
+ export * from './typings'
636
+ export * from './util'
637
+ ```
638
+
639
+ <!-- /automd -->
640
+
641
+ ## Contributors
642
+
643
+ <!-- automd:contributors github="hairyf/hairylib" author="Hairyf" license="MIT" -->
644
+
645
+ Published under the [MIT](https://github.com/hairyf/hairylib/blob/main/LICENSE) license.
646
+ Made by [@Hairyf](https://github.com/Hairyf) and [community](https://github.com/hairyf/hairylib/graphs/contributors) 💛
647
+ <br><br>
648
+ <a href="https://github.com/hairyf/hairylib/graphs/contributors">
649
+ <img src="https://contrib.rocks/image?repo=hairyf/hairylib" />
650
+ </a>
651
+
652
+ <!-- /automd -->
653
+
21
654
  ## License
22
655
 
23
- [MIT](./LICENSE) License © [Hairyf](https://github.com/hairyf)
656
+ <!-- automd:fetch url="gh:hairyf/hairylib/main/LICENSE" -->
657
+
658
+ MIT License
659
+
660
+ Copyright (c) 2025-PRESENT Hairyf <https://github.com/hairyf>
661
+
662
+ Permission is hereby granted, free of charge, to any person obtaining a copy
663
+ of this software and associated documentation files (the "Software"), to deal
664
+ in the Software without restriction, including without limitation the rights
665
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
666
+ copies of the Software, and to permit persons to whom the Software is
667
+ furnished to do so, subject to the following conditions:
668
+
669
+ The above copyright notice and this permission notice shall be included in all
670
+ copies or substantial portions of the Software.
671
+
672
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
673
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
674
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
675
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
676
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
677
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
678
+ SOFTWARE.
679
+
680
+ <!-- /automd -->
681
+
682
+ <!-- automd:with-automd -->
683
+
684
+ ---
24
685
 
25
- <!-- Badges -->
686
+ _🤖 auto updated with [automd](https://automd.unjs.io)_
26
687
 
27
- [npm-version-src]: https://img.shields.io/npm/v/@hairy/utils?style=flat&colorA=080f12&colorB=1fa669
28
- [npm-version-href]: https://npmjs.com/package/@hairy/utils
29
- [npm-downloads-src]: https://img.shields.io/npm/dm/@hairy/utils?style=flat&colorA=080f12&colorB=1fa669
30
- [npm-downloads-href]: https://npmjs.com/package/@hairy/utils
31
- [bundle-src]: https://img.shields.io/bundlephobia/minzip/@hairy/utils?style=flat&colorA=080f12&colorB=1fa669&label=minzip
32
- [bundle-href]: https://bundlephobia.com/result?p=@hairy/utils
33
- [license-src]: https://img.shields.io/github/license/hairyf/hairylib.svg?style=flat&colorA=080f12&colorB=1fa669
34
- [license-href]: https://github.com/hairyf/hairylib/blob/main/LICENSE
35
- [jsdocs-src]: https://img.shields.io/badge/jsdocs-reference-080f12?style=flat&colorA=080f12&colorB=1fa669
36
- [jsdocs-href]: https://www.jsdocs.io/package/@hairy/utils
688
+ <!-- /automd -->