lyb-js 1.6.32 → 1.6.34
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/Browser/LibJsGetRowValue.d.ts +6 -0
- package/Browser/LibJsGetRowValue.js +10 -0
- package/Misc/LibJsDiffObject.d.ts +8 -0
- package/Misc/LibJsDiffObject.js +48 -0
- package/Misc/LibJsPruneEmpty.js +4 -1
- package/README.md +989 -806
- package/package.json +1 -1
- package/Base/LibIsNull.d.ts +0 -2
- package/Base/LibIsNull.js +0 -2
package/README.md
CHANGED
|
@@ -1,806 +1,989 @@
|
|
|
1
|
-
# Lib自用JS工具方法
|
|
2
|
-
|
|
3
|
-
## 介绍
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
console.log(
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
```
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
```
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
```
|
|
272
|
-
|
|
273
|
-
###
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
```ts
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
```
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
```
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
}
|
|
395
|
-
```
|
|
396
|
-
|
|
397
|
-
###
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
```ts
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
```
|
|
416
|
-
|
|
417
|
-
###
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
```ts
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
```
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
###
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
```ts
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
```
|
|
489
|
-
|
|
490
|
-
###
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
```ts
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
const
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
console.
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
```
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
//
|
|
575
|
-
```
|
|
576
|
-
|
|
577
|
-
###
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
```ts
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
1
|
+
# Lib自用JS工具方法
|
|
2
|
+
|
|
3
|
+
## 介绍
|
|
4
|
+
|
|
5
|
+
`lyb-js` 是一个偏项目实用型的工具集合,覆盖浏览器交互、数据处理、数学计算、时间格式化、随机工具等常见场景。
|
|
6
|
+
|
|
7
|
+
文档以“快速上手”为目标:
|
|
8
|
+
|
|
9
|
+
- 先告诉你怎么安装、怎么导入
|
|
10
|
+
- 再给每个工具最常用的调用方式
|
|
11
|
+
- 示例尽量贴近实际项目,不把 README 写成冗长 API 手册
|
|
12
|
+
|
|
13
|
+
## 安装
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install lyb-js
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pnpm add lyb-js
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
yarn add lyb-js
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## 起步
|
|
28
|
+
|
|
29
|
+
### 1. 整库引入
|
|
30
|
+
|
|
31
|
+
适合快速试用,或者你已经统一通过 `LibJs` 管理工具访问。
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { LibJs } from "lyb-js";
|
|
35
|
+
|
|
36
|
+
const type = LibJs.Base.libJsGetDataType("Hello World");
|
|
37
|
+
console.log(type); // "string"
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### 2. 按需引入
|
|
41
|
+
|
|
42
|
+
更推荐。只引入当前用到的方法,路径按分类和文件名区分。
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { libJsGetDataType } from "lyb-js/Base/LibJsGetDataType";
|
|
46
|
+
import { libJsCalculateExpression } from "lyb-js/Math/LibJsCalculateExpression";
|
|
47
|
+
|
|
48
|
+
console.log(libJsGetDataType([1, 2, 3])); // "array"
|
|
49
|
+
console.log(libJsCalculateExpression("(1+2)-(3*4)/5")); // 0.6
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### 3. 项目内二次封装
|
|
53
|
+
|
|
54
|
+
如果同一批工具会在多个文件重复使用,建议在你自己的 `utils.ts` 中统一导出。
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
// utils.ts
|
|
58
|
+
export * from "lyb-js/Base/LibJsGetDataType";
|
|
59
|
+
export * from "lyb-js/Math/LibJsCalculateExpression";
|
|
60
|
+
export * from "lyb-js/Browser/LibJsGetRowValue";
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
// page.ts
|
|
65
|
+
import {
|
|
66
|
+
libJsGetDataType,
|
|
67
|
+
libJsCalculateExpression,
|
|
68
|
+
libJsGetRowValue,
|
|
69
|
+
} from "./utils";
|
|
70
|
+
|
|
71
|
+
console.log(libJsGetDataType({ a: 1 })); // "object"
|
|
72
|
+
console.log(libJsCalculateExpression("10/4", 2)); // 2.5
|
|
73
|
+
console.log(libJsGetRowValue({ user: { name: "Tom" } }, "user.name")); // "Tom"
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## 使用说明
|
|
77
|
+
|
|
78
|
+
### 环境说明
|
|
79
|
+
|
|
80
|
+
- `Browser`、`File` 中的大多数工具依赖 `window`、`document`、`navigator`,适合浏览器环境。
|
|
81
|
+
- `Data`、`Formatter`、`Math`、`Random` 中的大多数工具既可在浏览器使用,也可在 Node 环境使用。
|
|
82
|
+
- `Time` 分类依赖 `dayjs` 的少量能力,但作为库使用时你只需要正常安装 `lyb-js` 即可。
|
|
83
|
+
|
|
84
|
+
### 阅读方式
|
|
85
|
+
|
|
86
|
+
每个工具章节都遵循同一结构:
|
|
87
|
+
|
|
88
|
+
1. 作用说明
|
|
89
|
+
2. 按需导入
|
|
90
|
+
3. 1 到 2 个最常见示例
|
|
91
|
+
|
|
92
|
+
如果你只想找某个工具的导入方式,可以直接看每个章节的第一段代码。
|
|
93
|
+
|
|
94
|
+
## 目录
|
|
95
|
+
|
|
96
|
+
### Base-基础
|
|
97
|
+
|
|
98
|
+
- [LibJsGetDataType-数据类型](#libjsgetdatatype-数据类型)
|
|
99
|
+
- [LibJsIsNull-是否为空值](#libjsisnull-是否为空值)
|
|
100
|
+
- [LibJsPromiseTimeout-延时执行](#libjspromisetimeout-延时执行)
|
|
101
|
+
- [LibJsResizeWatcher-窗口监听](#libjsresizewatcher-窗口监听)
|
|
102
|
+
|
|
103
|
+
### Browser-浏览器
|
|
104
|
+
|
|
105
|
+
- [LibJsColorConsole-有色打印](#libjscolorconsole-有色打印)
|
|
106
|
+
- [LibJsCopy-复制文本到剪贴板](#libjscopy-复制文本到剪贴板)
|
|
107
|
+
- [LibJsGetRowValue-按点路径获取对象值](#libjsgetrowvalue-按点路径获取对象值)
|
|
108
|
+
- [LibJsIsMobile-判断手机](#libjsismobile-判断手机)
|
|
109
|
+
- [LibJsIsPad-判断平板](#libjsispad-判断平板)
|
|
110
|
+
- [LibJsObjToUrlParams-对象转Url参数](#libjsobjtourlparams-对象转url参数)
|
|
111
|
+
- [LibJsParseQueryString-URL参数转对象](#libjsparsequerystring-url参数转对象)
|
|
112
|
+
- [LibJsPathParams-地址栏参数](#libjspathparams-地址栏参数)
|
|
113
|
+
- [LibJsSetTitleIcon-网站标题图标](#libjssettitleicon-网站标题图标)
|
|
114
|
+
- [LibJsTagTitleTip-网站标题交互](#libjstagtitletip-网站标题交互)
|
|
115
|
+
|
|
116
|
+
### Data-数据
|
|
117
|
+
|
|
118
|
+
- [LibJsChunkArray-数组拆分](#libjschunkarray-数组拆分)
|
|
119
|
+
- [LibJsDeepJSONParse-深度解析JSON](#libjsdeepjsonparse-深度解析json)
|
|
120
|
+
- [LibJsGroupArrayByKey-分类汇总](#libjsgrouparraybykey-分类汇总)
|
|
121
|
+
- [LibJsMatchEmail-匹配E-Mail](#libjsmatchemail-匹配e-mail)
|
|
122
|
+
- [libJsPickUnique-随机选择未使用元素](#libjspickunique-随机选择未使用元素)
|
|
123
|
+
- [LibJsShuffleArray-数组乱序](#libjsshufflearray-数组乱序)
|
|
124
|
+
- [LibJsStepArray-数组偏移](#libjssteparray-数组偏移)
|
|
125
|
+
- [LibReverseArrayFromIndex-数组定位翻转](#libreversearrayfromindex-数组定位翻转)
|
|
126
|
+
|
|
127
|
+
### File-文件
|
|
128
|
+
|
|
129
|
+
- [LibJsDownloadImageLink-图片下载](#libjsdownloadimagelink-图片下载)
|
|
130
|
+
- [LibJsImageOptimizer-图片压缩](#libjsimageoptimizer-图片压缩)
|
|
131
|
+
- [LibJsSaveJson-保存文件](#libjssavejson-保存文件)
|
|
132
|
+
|
|
133
|
+
### Formatter-格式化
|
|
134
|
+
|
|
135
|
+
- [LibJsFormatterByte-字节格式化](#libjsformatterbyte-字节格式化)
|
|
136
|
+
- [LibJsMaskPhoneNumber-隐藏手机号码](#libjsmaskphonenumber-隐藏手机号码)
|
|
137
|
+
- [LibJsNumberUnit-数字单位](#libjsnumberunit-数字单位)
|
|
138
|
+
- [LibJsNumComma-数字逗号](#libjsnumcomma-数字逗号)
|
|
139
|
+
- [LibJsSecondsFormatterChinese-中文时间](#libjssecondsformatterchinese-中文时间)
|
|
140
|
+
|
|
141
|
+
### Math-数学
|
|
142
|
+
|
|
143
|
+
- [LibJsCalculateExpression-表达式字符串](#libjscalculateexpression-表达式字符串)
|
|
144
|
+
- [LibJsConvertAngle-角弧度互转](#libjsconvertangle-角弧度互转)
|
|
145
|
+
- [LibJsCoordsAngle-两点角度](#libjscoordsangle-两点角度)
|
|
146
|
+
- [LibJsCoordsDistance-两点距离](#libjscoordsdistance-两点距离)
|
|
147
|
+
- [LibJsDecimal-高精度计算](#libjsdecimal-高精度计算)
|
|
148
|
+
- [LibJsLerp-线性插值](#libjslerp-线性插值)
|
|
149
|
+
- [LibJsNormalizeInRange-范围归一化](#libjsnormalizeinrange-范围归一化)
|
|
150
|
+
|
|
151
|
+
### Misc-杂项
|
|
152
|
+
|
|
153
|
+
- [LibJsClassObservable-类属性监听器](#libjsclassobservable-类属性监听器)
|
|
154
|
+
- [LibJsEmitter-事件发射器](#libjsemitter-事件发射器)
|
|
155
|
+
- [LibJsEmitterClose-一次性关闭监听](#libjsemitterclose-一次性关闭监听)
|
|
156
|
+
- [LibJsHorizontal-游戏横版状态](#libjshorizontal-游戏横版状态)
|
|
157
|
+
- [LibJsNumberStepper-数字步进器](#libjsnumberstepper-数字步进器)
|
|
158
|
+
- [LibJsDiffObject-筛出已修改的值](#libjsdiffobject-筛出已修改的值)
|
|
159
|
+
- [LibJsPruneEmpty-对象属性去空值](#libjspruneempty-对象属性去空值)
|
|
160
|
+
- [LibJsPullUpLoad-上拉加载](#libjspullupload-上拉加载)
|
|
161
|
+
- [LibJsRegFormValidate-表单验证](#libjsregformvalidate-表单验证)
|
|
162
|
+
- [LibJsRetryRequest-请求重连](#libjsretryrequest-请求重连)
|
|
163
|
+
|
|
164
|
+
### Random-随机
|
|
165
|
+
|
|
166
|
+
- [LibJsProbabilityResult-概率触发](#libjsprobabilityresult-概率触发)
|
|
167
|
+
- [LibJsRandom-随机数](#libjsrandom-随机数)
|
|
168
|
+
- [LibJsRandomColor-随机色](#libjsrandomcolor-随机色)
|
|
169
|
+
- [LibJsUniqueRandomNumbers-随机数数组](#libjsuniquerandomnumbers-随机数数组)
|
|
170
|
+
|
|
171
|
+
### Time-时间
|
|
172
|
+
|
|
173
|
+
- [LibJsCountdown-倒计时](#libjscountdown-倒计时)
|
|
174
|
+
- [LibJsSameTimeCheck-时间比对](#libjssametimecheck-时间比对)
|
|
175
|
+
- [LibJsTimeAgo-中文时间差](#libjstimeago-中文时间差)
|
|
176
|
+
- [LibJsTimeGreeting-时间问候](#libjstimegreeting-时间问候)
|
|
177
|
+
|
|
178
|
+
## Base-基础
|
|
179
|
+
|
|
180
|
+
### LibJsGetDataType-数据类型
|
|
181
|
+
|
|
182
|
+
返回标准化后的数据类型字符串。
|
|
183
|
+
|
|
184
|
+
```ts
|
|
185
|
+
import { libJsGetDataType } from "lyb-js/Base/LibJsGetDataType";
|
|
186
|
+
|
|
187
|
+
console.log(libJsGetDataType(123)); // "number"
|
|
188
|
+
console.log(libJsGetDataType("hello")); // "string"
|
|
189
|
+
console.log(libJsGetDataType([1, 2, 3])); // "array"
|
|
190
|
+
console.log(libJsGetDataType(() => {})); // "function"
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### LibJsIsNull-是否为空值
|
|
194
|
+
|
|
195
|
+
判断值是否为 `null`、`undefined` 或空字符串 `""`。
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
import { libJsIsNull } from "lyb-js/Base/LibJsIsNull";
|
|
199
|
+
|
|
200
|
+
console.log(libJsIsNull(null)); // true
|
|
201
|
+
console.log(libJsIsNull(undefined)); // true
|
|
202
|
+
console.log(libJsIsNull("")); // true
|
|
203
|
+
console.log(libJsIsNull(0)); // false
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### LibJsPromiseTimeout-延时执行
|
|
207
|
+
|
|
208
|
+
延时执行回调;页面切到后台时会暂停计时,回到当前页后继续。
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
import { libJsPromiseTimeout } from "lyb-js/Base/LibJsPromiseTimeout";
|
|
212
|
+
|
|
213
|
+
await libJsPromiseTimeout(1000, () => {
|
|
214
|
+
console.log("1 秒后执行");
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
console.log("延时结束");
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### LibJsResizeWatcher-窗口监听
|
|
221
|
+
|
|
222
|
+
监听窗口尺寸变化,适合做画布缩放、布局适配或横竖屏比例计算。
|
|
223
|
+
|
|
224
|
+
```ts
|
|
225
|
+
import { LibJsResizeWatcher } from "lyb-js/Base/LibJsResizeWatcher";
|
|
226
|
+
|
|
227
|
+
const watcher = new LibJsResizeWatcher();
|
|
228
|
+
|
|
229
|
+
const off = watcher.on((w, h, scale) => {
|
|
230
|
+
console.log("窗口宽高", w, h);
|
|
231
|
+
console.log("缩放比例", scale);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
off();
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
```ts
|
|
238
|
+
const fixedHorizontal = new LibJsResizeWatcher("h");
|
|
239
|
+
fixedHorizontal.on((w, h, scale) => {
|
|
240
|
+
console.log(w, h, scale); // 1920 1080 ...
|
|
241
|
+
});
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## Browser-浏览器
|
|
245
|
+
|
|
246
|
+
### LibJsColorConsole-有色打印
|
|
247
|
+
|
|
248
|
+
给 `console.log` 增加标题色块和结构化信息,适合调试接口和业务流程。
|
|
249
|
+
|
|
250
|
+
```ts
|
|
251
|
+
import { libJsColorConsole } from "lyb-js/Browser/LibJsColorConsole";
|
|
252
|
+
|
|
253
|
+
libJsColorConsole("请求成功", "green", [
|
|
254
|
+
{ label: "接口", value: "/api/user" },
|
|
255
|
+
{ label: "状态码", value: 200 },
|
|
256
|
+
]);
|
|
257
|
+
|
|
258
|
+
libJsColorConsole("调试信息", "blue", { page: "home", loaded: true });
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### LibJsCopy-复制文本到剪贴板
|
|
262
|
+
|
|
263
|
+
复制字符串到剪贴板,内部兼容 `navigator.clipboard` 和降级方案。
|
|
264
|
+
|
|
265
|
+
```ts
|
|
266
|
+
import { libJsCopy } from "lyb-js/Browser/LibJsCopy";
|
|
267
|
+
|
|
268
|
+
button.addEventListener("click", () => {
|
|
269
|
+
libJsCopy("Hello World!");
|
|
270
|
+
});
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
### LibJsGetRowValue-按点路径获取对象值
|
|
274
|
+
|
|
275
|
+
按点路径读取对象中的嵌套值,路径不存在时返回 `undefined`。
|
|
276
|
+
|
|
277
|
+
```ts
|
|
278
|
+
import { libJsGetRowValue } from "lyb-js/Browser/LibJsGetRowValue";
|
|
279
|
+
|
|
280
|
+
const row = {
|
|
281
|
+
user: {
|
|
282
|
+
profile: {
|
|
283
|
+
name: "Tom",
|
|
284
|
+
},
|
|
285
|
+
},
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
console.log(libJsGetRowValue(row, "user.profile.name")); // "Tom"
|
|
289
|
+
console.log(libJsGetRowValue(row, "user.profile.age")); // undefined
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
### LibJsIsMobile-判断手机
|
|
293
|
+
|
|
294
|
+
通过 `userAgent` 判断当前设备是否为手机端。
|
|
295
|
+
|
|
296
|
+
```ts
|
|
297
|
+
import { libJsIsMobile } from "lyb-js/Browser/LibJsIsMobile";
|
|
298
|
+
|
|
299
|
+
if (libJsIsMobile()) {
|
|
300
|
+
console.log("当前是手机设备");
|
|
301
|
+
}
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
### LibJsIsPad-判断平板
|
|
305
|
+
|
|
306
|
+
结合 `userAgent`、屏幕尺寸、宽高比判断是否为平板设备。
|
|
307
|
+
|
|
308
|
+
```ts
|
|
309
|
+
import { libJsIsPad } from "lyb-js/Browser/LibJsIsPad";
|
|
310
|
+
|
|
311
|
+
console.log(libJsIsPad()); // true 或 false
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
### LibJsObjToUrlParams-对象转Url参数
|
|
315
|
+
|
|
316
|
+
把普通对象拼接成查询字符串。
|
|
317
|
+
|
|
318
|
+
```ts
|
|
319
|
+
import { libJsObjToUrlParams } from "lyb-js/Browser/LibJsObjToUrlParams";
|
|
320
|
+
|
|
321
|
+
const query = libJsObjToUrlParams({
|
|
322
|
+
name: "John",
|
|
323
|
+
age: 30,
|
|
324
|
+
active: true,
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
console.log(query); // "name=John&age=30&active=true"
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
### LibJsParseQueryString-URL参数转对象
|
|
331
|
+
|
|
332
|
+
把查询字符串转回对象。注意导入路径文件名是小写 `libJsParseQueryString`。
|
|
333
|
+
|
|
334
|
+
```ts
|
|
335
|
+
import { libJsParseQueryString } from "lyb-js/Browser/libJsParseQueryString";
|
|
336
|
+
|
|
337
|
+
console.log(libJsParseQueryString("name=lengyibai&age=18"));
|
|
338
|
+
// { name: "lengyibai", age: "18" }
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
### LibJsPathParams-地址栏参数
|
|
342
|
+
|
|
343
|
+
读取当前地址栏参数,也可以传入一段完整 URL 进行解析。
|
|
344
|
+
|
|
345
|
+
```ts
|
|
346
|
+
import { libJsPathParams } from "lyb-js/Browser/LibJsPathParams";
|
|
347
|
+
|
|
348
|
+
console.log(libJsPathParams("https://example.com?a=1&b=hello"));
|
|
349
|
+
// { a: "1", b: "hello" }
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
### LibJsSetTitleIcon-网站标题图标
|
|
353
|
+
|
|
354
|
+
动态设置网页标题和 favicon。
|
|
355
|
+
|
|
356
|
+
```ts
|
|
357
|
+
import { libJsSetTitleIcon } from "lyb-js/Browser/LibJsSetTitleIcon";
|
|
358
|
+
|
|
359
|
+
libJsSetTitleIcon("我的网站", "/favicon.ico");
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
### LibJsTagTitleTip-网站标题交互
|
|
363
|
+
|
|
364
|
+
页面隐藏和返回时切换标题,适合网页消息提醒。
|
|
365
|
+
|
|
366
|
+
```ts
|
|
367
|
+
import { libJsTagTitleTip } from "lyb-js/Browser/LibJsTagTitleTip";
|
|
368
|
+
|
|
369
|
+
libJsTagTitleTip("欢迎回来", "你有新的消息");
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
## Data-数据
|
|
373
|
+
|
|
374
|
+
### LibJsChunkArray-数组拆分
|
|
375
|
+
|
|
376
|
+
把数组按固定长度拆成多个小数组。
|
|
377
|
+
|
|
378
|
+
```ts
|
|
379
|
+
import { libJsChunkArray } from "lyb-js/Data/LibJsChunkArray";
|
|
380
|
+
|
|
381
|
+
console.log(libJsChunkArray([1, 2, 3, 4, 5, 6], 2));
|
|
382
|
+
// [[1, 2], [3, 4], [5, 6]]
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
### LibJsDeepJSONParse-深度解析JSON
|
|
386
|
+
|
|
387
|
+
递归解析对象或数组中嵌套的 JSON 字符串。
|
|
388
|
+
|
|
389
|
+
```ts
|
|
390
|
+
import { libJsDeepJSONParse } from "lyb-js/Data/LibJsDeepJSONParse";
|
|
391
|
+
|
|
392
|
+
const data = '{"user":"{\\"name\\":\\"Tom\\",\\"age\\":18}"}';
|
|
393
|
+
console.log(libJsDeepJSONParse(data));
|
|
394
|
+
// { user: { name: "Tom", age: 18 } }
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
### LibJsGroupArrayByKey-分类汇总
|
|
398
|
+
|
|
399
|
+
按对象中的某个字段分组。
|
|
400
|
+
|
|
401
|
+
```ts
|
|
402
|
+
import { libJsGroupArrayByKey } from "lyb-js/Data/LibJsGroupArrayByKey";
|
|
403
|
+
|
|
404
|
+
const list = [
|
|
405
|
+
{ type: "fruit", name: "apple" },
|
|
406
|
+
{ type: "fruit", name: "banana" },
|
|
407
|
+
{ type: "drink", name: "tea" },
|
|
408
|
+
];
|
|
409
|
+
|
|
410
|
+
console.log(libJsGroupArrayByKey(list, "type"));
|
|
411
|
+
// {
|
|
412
|
+
// fruit: [{...}, {...}],
|
|
413
|
+
// drink: [{...}]
|
|
414
|
+
// }
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
### LibJsMatchEmail-匹配E-Mail
|
|
418
|
+
|
|
419
|
+
根据当前输入自动补全邮箱后缀。
|
|
420
|
+
|
|
421
|
+
```ts
|
|
422
|
+
import { libJsMatchEmail } from "lyb-js/Data/LibJsMatchEmail";
|
|
423
|
+
|
|
424
|
+
const result = libJsMatchEmail("user@g", ["@gmail.com", "@github.com"]);
|
|
425
|
+
console.log(result); // ["user@gmail.com", "user@github.com"]
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
### libJsPickUnique-随机选择未使用元素
|
|
429
|
+
|
|
430
|
+
从候选数组中随机选一个还没有被使用过的元素。
|
|
431
|
+
|
|
432
|
+
```ts
|
|
433
|
+
import { libJsPickUnique } from "lyb-js/Data/libJsPickUnique";
|
|
434
|
+
|
|
435
|
+
const pool = ["A", "B", "C", "D"];
|
|
436
|
+
const used = ["A", "B"];
|
|
437
|
+
|
|
438
|
+
console.log(libJsPickUnique(pool, used)); // "C" 或 "D"
|
|
439
|
+
console.log(libJsPickUnique(["A"], ["A"])); // undefined
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
### LibJsShuffleArray-数组乱序
|
|
443
|
+
|
|
444
|
+
返回一个打乱顺序的新数组,不会直接修改原数组。
|
|
445
|
+
|
|
446
|
+
```ts
|
|
447
|
+
import { libJsShuffleArray } from "lyb-js/Data/LibJsShuffleArray";
|
|
448
|
+
|
|
449
|
+
console.log(libJsShuffleArray([1, 2, 3, 4, 5]));
|
|
450
|
+
// [3, 5, 1, 4, 2] 结果每次不同
|
|
451
|
+
```
|
|
452
|
+
|
|
453
|
+
### LibJsStepArray-数组偏移
|
|
454
|
+
|
|
455
|
+
让数组整体循环移动;正数向右移,负数向左移。
|
|
456
|
+
|
|
457
|
+
```ts
|
|
458
|
+
import { libJsStepArray } from "lyb-js/Data/LibJsStepArray";
|
|
459
|
+
|
|
460
|
+
console.log(libJsStepArray([1, 2, 3, 4, 5], 2));
|
|
461
|
+
// [4, 5, 1, 2, 3]
|
|
462
|
+
|
|
463
|
+
console.log(libJsStepArray([1, 2, 3, 4, 5], -1));
|
|
464
|
+
// [2, 3, 4, 5, 1]
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
### LibReverseArrayFromIndex-数组定位翻转
|
|
468
|
+
|
|
469
|
+
保留某个索引之前的内容,把该索引后面的数组反转。
|
|
470
|
+
|
|
471
|
+
```ts
|
|
472
|
+
import { libReverseArrayFromIndex } from "lyb-js/Data/LibReverseArrayFromIndex";
|
|
473
|
+
|
|
474
|
+
console.log(libReverseArrayFromIndex([1, 2, 3, 4, 5], 1));
|
|
475
|
+
// [1, 2, 5, 4, 3]
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
## File-文件
|
|
479
|
+
|
|
480
|
+
### LibJsDownloadImageLink-图片下载
|
|
481
|
+
|
|
482
|
+
把图片链接下载到本地。
|
|
483
|
+
|
|
484
|
+
```ts
|
|
485
|
+
import { libJsDownloadImageLink } from "lyb-js/File/LibJsDownloadImageLink";
|
|
486
|
+
|
|
487
|
+
libJsDownloadImageLink("https://example.com/banner.jpg", "banner.jpg");
|
|
488
|
+
```
|
|
489
|
+
|
|
490
|
+
### LibJsImageOptimizer-图片压缩
|
|
491
|
+
|
|
492
|
+
在浏览器里压缩图片,并通过回调拿到压缩后的 `File`、`FormData` 和预览地址。
|
|
493
|
+
|
|
494
|
+
```ts
|
|
495
|
+
import { libJsImageOptimizer } from "lyb-js/File/LibJsImageOptimizer";
|
|
496
|
+
|
|
497
|
+
input.addEventListener("change", () => {
|
|
498
|
+
const file = input.files?.[0];
|
|
499
|
+
if (!file) return;
|
|
500
|
+
|
|
501
|
+
libJsImageOptimizer({
|
|
502
|
+
file,
|
|
503
|
+
width: 1200,
|
|
504
|
+
ratio: 0.8,
|
|
505
|
+
maxSize: 1024,
|
|
506
|
+
success(formData, newFile, url) {
|
|
507
|
+
console.log(formData, newFile);
|
|
508
|
+
preview.src = url;
|
|
509
|
+
},
|
|
510
|
+
fail(error) {
|
|
511
|
+
console.error("压缩失败", error);
|
|
512
|
+
},
|
|
513
|
+
});
|
|
514
|
+
});
|
|
515
|
+
```
|
|
516
|
+
|
|
517
|
+
### LibJsSaveJson-保存文件
|
|
518
|
+
|
|
519
|
+
把字符串或 JSON 内容保存成浏览器下载文件。
|
|
520
|
+
|
|
521
|
+
```ts
|
|
522
|
+
import { libJsSaveJson } from "lyb-js/File/LibJsSaveJson";
|
|
523
|
+
|
|
524
|
+
const content = JSON.stringify({ name: "Tom", age: 18 }, null, 2);
|
|
525
|
+
libJsSaveJson("user.json", content);
|
|
526
|
+
```
|
|
527
|
+
|
|
528
|
+
## Formatter-格式化
|
|
529
|
+
|
|
530
|
+
### LibJsFormatterByte-字节格式化
|
|
531
|
+
|
|
532
|
+
把字节数格式化为更易读的大小信息。
|
|
533
|
+
|
|
534
|
+
```ts
|
|
535
|
+
import { libJsFormatterByte } from "lyb-js/Formatter/LibJsFormatterByte";
|
|
536
|
+
|
|
537
|
+
console.log(libJsFormatterByte(1536));
|
|
538
|
+
// ["1.50", "KB", "1.50 KB"]
|
|
539
|
+
```
|
|
540
|
+
|
|
541
|
+
### LibJsMaskPhoneNumber-隐藏手机号码
|
|
542
|
+
|
|
543
|
+
文件名是 `LibJsMaskPhoneNumber`,导出的方法名是 `libJsMaskCenterFour`。
|
|
544
|
+
|
|
545
|
+
```ts
|
|
546
|
+
import { libJsMaskCenterFour } from "lyb-js/Formatter/LibJsMaskPhoneNumber";
|
|
547
|
+
|
|
548
|
+
console.log(libJsMaskCenterFour("13800138000"));
|
|
549
|
+
// "138****8000"
|
|
550
|
+
```
|
|
551
|
+
|
|
552
|
+
### LibJsNumberUnit-数字单位
|
|
553
|
+
|
|
554
|
+
根据阈值自动追加单位,适合展示“万、亿、K、M”等短格式数字。
|
|
555
|
+
|
|
556
|
+
```ts
|
|
557
|
+
import { libJsNumberUnit } from "lyb-js/Formatter/LibJsNumberUnit";
|
|
558
|
+
|
|
559
|
+
console.log(libJsNumberUnit(12600, { 万: 10000, 亿: 100000000 }, 1));
|
|
560
|
+
// "1.2万"
|
|
561
|
+
|
|
562
|
+
console.log(libJsNumberUnit(2500000, { K: 1000, M: 1000000 }, 2));
|
|
563
|
+
// "2.50M"
|
|
564
|
+
```
|
|
565
|
+
|
|
566
|
+
### LibJsNumComma-数字逗号
|
|
567
|
+
|
|
568
|
+
给数字加千分位,并按指定小数位截断,不做四舍五入。
|
|
569
|
+
|
|
570
|
+
```ts
|
|
571
|
+
import { libJsNumComma } from "lyb-js/Formatter/LibJsNumComma";
|
|
572
|
+
|
|
573
|
+
console.log(libJsNumComma(1234567.8912, 2));
|
|
574
|
+
// "1,234,567.89"
|
|
575
|
+
```
|
|
576
|
+
|
|
577
|
+
### LibJsSecondsFormatterChinese-中文时间
|
|
578
|
+
|
|
579
|
+
把秒数格式化成中文时间描述。
|
|
580
|
+
|
|
581
|
+
```ts
|
|
582
|
+
import { libJsSecondsFormatterChinese } from "lyb-js/Formatter/LibJsSecondsFormatterChinese";
|
|
583
|
+
|
|
584
|
+
console.log(libJsSecondsFormatterChinese(90));
|
|
585
|
+
// "1分钟30秒"
|
|
586
|
+
|
|
587
|
+
console.log(libJsSecondsFormatterChinese(3661));
|
|
588
|
+
// "1小时1分钟1秒"
|
|
589
|
+
```
|
|
590
|
+
|
|
591
|
+
## Math-数学
|
|
592
|
+
|
|
593
|
+
### LibJsCalculateExpression-表达式字符串
|
|
594
|
+
|
|
595
|
+
计算只包含加减乘除和括号的表达式字符串。
|
|
596
|
+
|
|
597
|
+
```ts
|
|
598
|
+
import { libJsCalculateExpression } from "lyb-js/Math/LibJsCalculateExpression";
|
|
599
|
+
|
|
600
|
+
console.log(libJsCalculateExpression("(1+2)-(3*4)/5"));
|
|
601
|
+
// 0.6
|
|
602
|
+
|
|
603
|
+
console.log(libJsCalculateExpression("10/3", 2));
|
|
604
|
+
// 3.33
|
|
605
|
+
```
|
|
606
|
+
|
|
607
|
+
### LibJsConvertAngle-角弧度互转
|
|
608
|
+
|
|
609
|
+
在角度和弧度之间转换。
|
|
610
|
+
|
|
611
|
+
```ts
|
|
612
|
+
import { libJsConvertAngle } from "lyb-js/Math/LibJsConvertAngle";
|
|
613
|
+
|
|
614
|
+
console.log(libJsConvertAngle(180, "rad")); // 3.141592653589793
|
|
615
|
+
console.log(libJsConvertAngle(Math.PI, "deg")); // 180
|
|
616
|
+
```
|
|
617
|
+
|
|
618
|
+
### LibJsCoordsAngle-两点角度
|
|
619
|
+
|
|
620
|
+
计算两点连线角度,默认返回角度制。
|
|
621
|
+
|
|
622
|
+
```ts
|
|
623
|
+
import { libJsCoordsAngle } from "lyb-js/Math/LibJsCoordsAngle";
|
|
624
|
+
|
|
625
|
+
console.log(libJsCoordsAngle({ x: 0, y: 0 }, { x: 1, y: 0 }));
|
|
626
|
+
console.log(libJsCoordsAngle({ x: 0, y: 0 }, { x: 1, y: 1 }, "rad"));
|
|
627
|
+
```
|
|
628
|
+
|
|
629
|
+
### LibJsCoordsDistance-两点距离
|
|
630
|
+
|
|
631
|
+
计算两点之间的直线距离。
|
|
632
|
+
|
|
633
|
+
```ts
|
|
634
|
+
import { libJsCoordsDistance } from "lyb-js/Math/LibJsCoordsDistance";
|
|
635
|
+
|
|
636
|
+
console.log(libJsCoordsDistance({ x: 0, y: 0 }, { x: 3, y: 4 }));
|
|
637
|
+
// 5
|
|
638
|
+
```
|
|
639
|
+
|
|
640
|
+
### LibJsDecimal-高精度计算
|
|
641
|
+
|
|
642
|
+
使用 `decimal.js` 处理高精度加减乘除。
|
|
643
|
+
|
|
644
|
+
```ts
|
|
645
|
+
import { libJsDecimal } from "lyb-js/Math/LibJsDecimal";
|
|
646
|
+
|
|
647
|
+
console.log(libJsDecimal(0.1, 0.2, "+", 2)); // 0.3
|
|
648
|
+
console.log(libJsDecimal(1.5, 0.3, "*", 2)); // 0.45
|
|
649
|
+
```
|
|
650
|
+
|
|
651
|
+
### LibJsLerp-线性插值
|
|
652
|
+
|
|
653
|
+
文件名是 `LibJsLerp`,导出的方法名也是 `LibJsLerp`。
|
|
654
|
+
|
|
655
|
+
```ts
|
|
656
|
+
import { LibJsLerp } from "lyb-js/Math/LibJsLerp";
|
|
657
|
+
|
|
658
|
+
console.log(LibJsLerp(0, 100, 0.25)); // 25
|
|
659
|
+
console.log(LibJsLerp(10, 20, 0.5)); // 15
|
|
660
|
+
```
|
|
661
|
+
|
|
662
|
+
### LibJsNormalizeInRange-范围归一化
|
|
663
|
+
|
|
664
|
+
把一个值映射到 `0 ~ 1` 区间。
|
|
665
|
+
|
|
666
|
+
```ts
|
|
667
|
+
import { LibJsNormalizeInRange } from "lyb-js/Math/LibJsNormalizeInRange";
|
|
668
|
+
|
|
669
|
+
console.log(LibJsNormalizeInRange(0, 100, 50)); // 0.5
|
|
670
|
+
console.log(LibJsNormalizeInRange(0, 100, 120)); // 1
|
|
671
|
+
```
|
|
672
|
+
|
|
673
|
+
## Misc-杂项
|
|
674
|
+
|
|
675
|
+
### LibJsClassObservable-类属性监听器
|
|
676
|
+
|
|
677
|
+
给普通对象状态加上监听、按键更新、批量订阅等能力。
|
|
678
|
+
|
|
679
|
+
```ts
|
|
680
|
+
import { LibJsClassObservable } from "lyb-js/Misc/LibJsClassObservable";
|
|
681
|
+
|
|
682
|
+
const store = new LibJsClassObservable({
|
|
683
|
+
count: 0,
|
|
684
|
+
visible: false,
|
|
685
|
+
});
|
|
686
|
+
|
|
687
|
+
const off = store.onValue("count", (value) => {
|
|
688
|
+
console.log("count changed:", value);
|
|
689
|
+
});
|
|
690
|
+
|
|
691
|
+
store.setValue("count", 1);
|
|
692
|
+
store.setNumberValue("count", "add", 2);
|
|
693
|
+
store.setBooleanValue("visible");
|
|
694
|
+
off();
|
|
695
|
+
```
|
|
696
|
+
|
|
697
|
+
### LibJsEmitter-事件发射器
|
|
698
|
+
|
|
699
|
+
创建一个带类型约束的轻量事件中心。
|
|
700
|
+
|
|
701
|
+
```ts
|
|
702
|
+
import { LibJsEmitter } from "lyb-js/Misc/LibJsEmitter";
|
|
703
|
+
|
|
704
|
+
type Events = {
|
|
705
|
+
loaded: { page: string };
|
|
706
|
+
resize: [number, number];
|
|
707
|
+
};
|
|
708
|
+
|
|
709
|
+
const emitter = LibJsEmitter<Events>();
|
|
710
|
+
|
|
711
|
+
emitter.on("loaded", (payload) => {
|
|
712
|
+
console.log(payload.page);
|
|
713
|
+
});
|
|
714
|
+
|
|
715
|
+
emitter.on("resize", (w, h) => {
|
|
716
|
+
console.log(w, h);
|
|
717
|
+
});
|
|
718
|
+
|
|
719
|
+
emitter.emit("loaded", { page: "home" });
|
|
720
|
+
emitter.emit("resize", 1920, 1080);
|
|
721
|
+
emitter.clear();
|
|
722
|
+
```
|
|
723
|
+
|
|
724
|
+
### LibJsEmitterClose-一次性关闭监听
|
|
725
|
+
|
|
726
|
+
注册一个事件名对应的回调,触发后会执行并自动清空这一组监听。
|
|
727
|
+
|
|
728
|
+
```ts
|
|
729
|
+
import { LibJsEmitterClose } from "lyb-js/Misc/LibJsEmitterClose";
|
|
730
|
+
|
|
731
|
+
LibJsEmitterClose.on("dialog-close", () => {
|
|
732
|
+
console.log("弹窗关闭后执行一次");
|
|
733
|
+
});
|
|
734
|
+
|
|
735
|
+
LibJsEmitterClose.emit("dialog-close");
|
|
736
|
+
LibJsEmitterClose.emit("dialog-close"); // 第二次不会再次触发上面的回调
|
|
737
|
+
```
|
|
738
|
+
|
|
739
|
+
### LibJsHorizontal-游戏横版状态
|
|
740
|
+
|
|
741
|
+
根据当前窗口或指定模式返回横竖屏状态和参考宽高。
|
|
742
|
+
|
|
743
|
+
```ts
|
|
744
|
+
import { libJsHorizontal } from "lyb-js/Misc/LibJsHorizontal";
|
|
745
|
+
|
|
746
|
+
console.log(libJsHorizontal("auto"));
|
|
747
|
+
console.log(libJsHorizontal("h")); // 强制横版参考尺寸
|
|
748
|
+
console.log(libJsHorizontal("v")); // 强制竖版参考尺寸
|
|
749
|
+
```
|
|
750
|
+
|
|
751
|
+
### LibJsNumberStepper-数字步进器
|
|
752
|
+
|
|
753
|
+
用于长按按钮连续加减数字。
|
|
754
|
+
|
|
755
|
+
```ts
|
|
756
|
+
import { LibJsNumberStepper } from "lyb-js/Misc/LibJsNumberStepper";
|
|
757
|
+
|
|
758
|
+
let count = 0;
|
|
759
|
+
const stepper = new LibJsNumberStepper((type) => {
|
|
760
|
+
count = type === "add" ? count + 1 : count - 1;
|
|
761
|
+
console.log(count);
|
|
762
|
+
});
|
|
763
|
+
|
|
764
|
+
addButton.addEventListener("pointerdown", () => stepper.down("add"));
|
|
765
|
+
subButton.addEventListener("pointerdown", () => stepper.down("sub"));
|
|
766
|
+
```
|
|
767
|
+
|
|
768
|
+
### LibJsDiffObject-筛出已修改的值
|
|
769
|
+
|
|
770
|
+
深度对比对象差异,只返回新对象中发生变更的字段。
|
|
771
|
+
|
|
772
|
+
```ts
|
|
773
|
+
import { libDiffObject } from "lyb-js/Misc/LibJsDiffObject";
|
|
774
|
+
|
|
775
|
+
const oldObj = {
|
|
776
|
+
name: "Tom",
|
|
777
|
+
profile: { city: "Shanghai", age: 18 },
|
|
778
|
+
tags: [{ id: 1 }, { id: 2 }],
|
|
779
|
+
};
|
|
780
|
+
|
|
781
|
+
const newObj = {
|
|
782
|
+
name: "Jerry",
|
|
783
|
+
profile: { city: "Shanghai", age: 20 },
|
|
784
|
+
tags: [{ id: 1 }, { id: 3 }],
|
|
785
|
+
};
|
|
786
|
+
|
|
787
|
+
console.log(libDiffObject(oldObj, newObj));
|
|
788
|
+
// { name: "Jerry", profile: { age: 20 }, tags: [{ id: 1 }, { id: 3 }] }
|
|
789
|
+
```
|
|
790
|
+
|
|
791
|
+
### LibJsPruneEmpty-对象属性去空值
|
|
792
|
+
|
|
793
|
+
递归移除对象中的空字符串、`null`、`undefined` 和空对象;空数组也会被去掉。`Date` 会保留原值,不会被递归处理。
|
|
794
|
+
|
|
795
|
+
```ts
|
|
796
|
+
import { libJsPruneEmpty } from "lyb-js/Misc/LibJsPruneEmpty";
|
|
797
|
+
|
|
798
|
+
const result = libJsPruneEmpty({
|
|
799
|
+
name: "Tom",
|
|
800
|
+
createdAt: new Date("2026-05-07T00:00:00.000Z"),
|
|
801
|
+
empty: "",
|
|
802
|
+
profile: {
|
|
803
|
+
age: undefined,
|
|
804
|
+
city: "Shanghai",
|
|
805
|
+
},
|
|
806
|
+
tags: [],
|
|
807
|
+
});
|
|
808
|
+
|
|
809
|
+
console.log(result);
|
|
810
|
+
// { name: "Tom", createdAt: 2026-05-07T00:00:00.000Z, profile: { city: "Shanghai" } }
|
|
811
|
+
```
|
|
812
|
+
|
|
813
|
+
### LibJsPullUpLoad-上拉加载
|
|
814
|
+
|
|
815
|
+
给滚动容器添加“滚到底部自动触发加载”的能力,并可观察当前加载状态。
|
|
816
|
+
|
|
817
|
+
```ts
|
|
818
|
+
import { LibJsPullUpLoad } from "lyb-js/Misc/LibJsPullUpLoad";
|
|
819
|
+
|
|
820
|
+
const pull = new LibJsPullUpLoad({
|
|
821
|
+
scrollEl: document.querySelector(".list") as HTMLElement,
|
|
822
|
+
loadStatusEl: document.querySelector(".load-status") as HTMLElement,
|
|
823
|
+
onLoad() {
|
|
824
|
+
console.log("触发加载更多");
|
|
825
|
+
},
|
|
826
|
+
});
|
|
827
|
+
|
|
828
|
+
pull.onValue("statusText", (text) => {
|
|
829
|
+
console.log("状态文案:", text);
|
|
830
|
+
});
|
|
831
|
+
|
|
832
|
+
pull.setValue("loadStatus", "idle");
|
|
833
|
+
```
|
|
834
|
+
|
|
835
|
+
### LibJsRegFormValidate-表单验证
|
|
836
|
+
|
|
837
|
+
通过正则或自定义函数批量校验表单字段。
|
|
838
|
+
|
|
839
|
+
```ts
|
|
840
|
+
import { libJsRegFormValidate } from "lyb-js/Misc/LibJsRegFormValidate";
|
|
841
|
+
|
|
842
|
+
const result = libJsRegFormValidate(
|
|
843
|
+
{ phone: "13800138000", code: "12" },
|
|
844
|
+
[
|
|
845
|
+
{
|
|
846
|
+
key: "phone",
|
|
847
|
+
name: "手机号",
|
|
848
|
+
verify: /^1\\d{10}$/,
|
|
849
|
+
msg: "手机号格式错误",
|
|
850
|
+
},
|
|
851
|
+
{
|
|
852
|
+
key: "code",
|
|
853
|
+
name: "验证码",
|
|
854
|
+
verify: (value) => String(value).length === 6,
|
|
855
|
+
msg: "验证码长度错误",
|
|
856
|
+
},
|
|
857
|
+
]
|
|
858
|
+
);
|
|
859
|
+
|
|
860
|
+
console.log(result);
|
|
861
|
+
```
|
|
862
|
+
|
|
863
|
+
### LibJsRetryRequest-请求重连
|
|
864
|
+
|
|
865
|
+
请求失败后自动按次数重试。
|
|
866
|
+
|
|
867
|
+
```ts
|
|
868
|
+
import { libJsRetryRequest } from "lyb-js/Misc/LibJsRetryRequest";
|
|
869
|
+
|
|
870
|
+
const data = await libJsRetryRequest({
|
|
871
|
+
promiseFn: (id?: number) => fetch(`/api/user/${id}`).then((res) => res.json()),
|
|
872
|
+
params: 1,
|
|
873
|
+
maxRetries: 3,
|
|
874
|
+
retryDelay: 1000,
|
|
875
|
+
});
|
|
876
|
+
|
|
877
|
+
console.log(data);
|
|
878
|
+
```
|
|
879
|
+
|
|
880
|
+
## Random-随机
|
|
881
|
+
|
|
882
|
+
### LibJsProbabilityResult-概率触发
|
|
883
|
+
|
|
884
|
+
按百分比返回是否触发成功。
|
|
885
|
+
|
|
886
|
+
```ts
|
|
887
|
+
import { libJsProbabilityResult } from "lyb-js/Random/LibJsProbabilityResult";
|
|
888
|
+
|
|
889
|
+
console.log(libJsProbabilityResult(30)); // 30% 概率返回 true
|
|
890
|
+
```
|
|
891
|
+
|
|
892
|
+
### LibJsRandom-随机数
|
|
893
|
+
|
|
894
|
+
获取指定范围内的随机数,可设置保留小数位。
|
|
895
|
+
|
|
896
|
+
```ts
|
|
897
|
+
import { libJsRandom } from "lyb-js/Random/LibJsRandom";
|
|
898
|
+
|
|
899
|
+
console.log(libJsRandom(1, 10)); // 1 ~ 10
|
|
900
|
+
console.log(libJsRandom(1, 10, 2)); // 例如 6.38
|
|
901
|
+
```
|
|
902
|
+
|
|
903
|
+
### LibJsRandomColor-随机色
|
|
904
|
+
|
|
905
|
+
生成随机 RGBA 颜色字符串。
|
|
906
|
+
|
|
907
|
+
```ts
|
|
908
|
+
import { libJsRandomColor } from "lyb-js/Random/LibJsRandomColor";
|
|
909
|
+
|
|
910
|
+
console.log(libJsRandomColor()); // "rgba(...)"
|
|
911
|
+
console.log(libJsRandomColor(0.5)); // 半透明
|
|
912
|
+
```
|
|
913
|
+
|
|
914
|
+
### LibJsUniqueRandomNumbers-随机数数组
|
|
915
|
+
|
|
916
|
+
在指定区间中生成不重复的随机数数组。
|
|
917
|
+
|
|
918
|
+
```ts
|
|
919
|
+
import { libJsUniqueRandomNumbers } from "lyb-js/Random/LibJsUniqueRandomNumbers";
|
|
920
|
+
|
|
921
|
+
console.log(libJsUniqueRandomNumbers(1, 10, 4));
|
|
922
|
+
// 例如 [3, 7, 1, 9]
|
|
923
|
+
```
|
|
924
|
+
|
|
925
|
+
## Time-时间
|
|
926
|
+
|
|
927
|
+
### LibJsCountdown-倒计时
|
|
928
|
+
|
|
929
|
+
根据结束时间返回倒计时对象,可指定保留的最小单位。
|
|
930
|
+
|
|
931
|
+
```ts
|
|
932
|
+
import { libJsCountdown } from "lyb-js/Time/LibJsCountdown";
|
|
933
|
+
|
|
934
|
+
const endTime = Date.now() + 5 * 60 * 1000;
|
|
935
|
+
console.log(libJsCountdown(endTime, "minute"));
|
|
936
|
+
// { years, months, days, hours, minutes, seconds, ended }
|
|
937
|
+
```
|
|
938
|
+
|
|
939
|
+
### LibJsSameTimeCheck-时间比对
|
|
940
|
+
|
|
941
|
+
比较传入时间和当前时间是否处于同一个时间单位。
|
|
942
|
+
|
|
943
|
+
```ts
|
|
944
|
+
import { libJsSameTimeCheck } from "lyb-js/Time/LibJsSameTimeCheck";
|
|
945
|
+
|
|
946
|
+
console.log(libJsSameTimeCheck(Date.now(), "day")); // 0
|
|
947
|
+
console.log(libJsSameTimeCheck(Date.now() - 86400000, "day")); // 1
|
|
948
|
+
```
|
|
949
|
+
|
|
950
|
+
返回值说明:
|
|
951
|
+
|
|
952
|
+
- `0`:同一时间单位
|
|
953
|
+
- `1`:比当前更早
|
|
954
|
+
- `-1`:比当前更晚
|
|
955
|
+
|
|
956
|
+
### LibJsTimeAgo-中文时间差
|
|
957
|
+
|
|
958
|
+
把时间戳转成“多久前 / 多久后”形式的中文文案。
|
|
959
|
+
|
|
960
|
+
```ts
|
|
961
|
+
import { libJsTimeAgo } from "lyb-js/Time/LibJsTimeAgo";
|
|
962
|
+
|
|
963
|
+
console.log(libJsTimeAgo(Date.now() - 60 * 60 * 1000));
|
|
964
|
+
console.log(libJsTimeAgo(Date.now() + 2 * 24 * 60 * 60 * 1000));
|
|
965
|
+
```
|
|
966
|
+
|
|
967
|
+
### LibJsTimeGreeting-时间问候
|
|
968
|
+
|
|
969
|
+
根据当前时间返回默认问候语,也可以传入自定义文案。
|
|
970
|
+
|
|
971
|
+
```ts
|
|
972
|
+
import { libJsTimeGreeting } from "lyb-js/Time/LibJsTimeGreeting";
|
|
973
|
+
|
|
974
|
+
console.log(libJsTimeGreeting());
|
|
975
|
+
|
|
976
|
+
console.log(
|
|
977
|
+
libJsTimeGreeting({
|
|
978
|
+
morning: "早安",
|
|
979
|
+
afternoon: "下午好",
|
|
980
|
+
evening: "晚上好",
|
|
981
|
+
})
|
|
982
|
+
);
|
|
983
|
+
```
|
|
984
|
+
|
|
985
|
+
## 补充说明
|
|
986
|
+
|
|
987
|
+
- 文档里的导入路径以 `npm` 目录实际产物为准。
|
|
988
|
+
- 个别工具存在“文件名和导出名不同”的情况,README 已在对应章节中显式说明。
|
|
989
|
+
- 如果你想快速查某个工具支持什么参数,除了 README,也可以直接查看导入时的 TypeScript / JSDoc 提示。
|