lyb-js 1.0.2 → 1.0.4

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.
Files changed (89) hide show
  1. package/README.md +507 -9
  2. package/package.json +1 -1
  3. package/dist/Base/LibGetDataType/index.d.ts +0 -10
  4. package/dist/Base/LibGetDataType/index.js +0 -15
  5. package/dist/Base/LibJsGetDataType/index.d.ts +0 -7
  6. package/dist/Base/LibJsGetDataType/index.js +0 -11
  7. package/dist/Base/LibPromiseTimeout/index.d.ts +0 -10
  8. package/dist/Base/LibPromiseTimeout/index.js +0 -40
  9. package/dist/Browser/LibColorConsole/index.d.ts +0 -15
  10. package/dist/Browser/LibColorConsole/index.js +0 -38
  11. package/dist/Browser/LibIsMobile/index.d.ts +0 -6
  12. package/dist/Browser/LibIsMobile/index.js +0 -9
  13. package/dist/Browser/LibIsPad/index.d.ts +0 -6
  14. package/dist/Browser/LibIsPad/index.js +0 -15
  15. package/dist/Browser/LibPathParams/index.d.ts +0 -6
  16. package/dist/Browser/LibPathParams/index.js +0 -16
  17. package/dist/Browser/LibSetTitleIcon/index.d.ts +0 -7
  18. package/dist/Browser/LibSetTitleIcon/index.js +0 -14
  19. package/dist/Browser/LibTagTitleTip/index.d.ts +0 -7
  20. package/dist/Browser/LibTagTitleTip/index.js +0 -24
  21. package/dist/Data/LibChunkArray/index.d.ts +0 -9
  22. package/dist/Data/LibChunkArray/index.js +0 -15
  23. package/dist/Data/LibDeepJSONParse/index.d.ts +0 -6
  24. package/dist/Data/LibDeepJSONParse/index.js +0 -32
  25. package/dist/Data/LibGroupArrayByKey/index.d.ts +0 -10
  26. package/dist/Data/LibGroupArrayByKey/index.js +0 -20
  27. package/dist/Data/LibMatchEmail/index.d.ts +0 -10
  28. package/dist/Data/LibMatchEmail/index.js +0 -16
  29. package/dist/Data/LibSearch/index.d.ts +0 -8
  30. package/dist/Data/LibSearch/index.js +0 -45
  31. package/dist/Data/LibShuffleArray/index.d.ts +0 -7
  32. package/dist/Data/LibShuffleArray/index.js +0 -14
  33. package/dist/Data/LibStepArray/index.d.ts +0 -8
  34. package/dist/Data/LibStepArray/index.js +0 -14
  35. package/dist/File/LibDownloadImageLink/index.d.ts +0 -7
  36. package/dist/File/LibDownloadImageLink/index.js +0 -17
  37. package/dist/File/LibFileSize/index.d.ts +0 -5
  38. package/dist/File/LibFileSize/index.js +0 -11
  39. package/dist/File/LibImageOptimizer/index.d.ts +0 -32
  40. package/dist/File/LibImageOptimizer/index.js +0 -83
  41. package/dist/File/LibImageOptimizerOptions/index.d.ts +0 -18
  42. package/dist/File/LibImageOptimizerOptions/index.js +0 -69
  43. package/dist/File/LibSaveExcel/index.d.ts +0 -11
  44. package/dist/File/LibSaveExcel/index.js +0 -17
  45. package/dist/File/LibSaveJson/index.d.ts +0 -8
  46. package/dist/File/LibSaveJson/index.js +0 -16
  47. package/dist/Formatter/LibFormatterByte/index.d.ts +0 -9
  48. package/dist/Formatter/LibFormatterByte/index.js +0 -17
  49. package/dist/Formatter/LibMaskPhoneNumber/index.d.ts +0 -8
  50. package/dist/Formatter/LibMaskPhoneNumber/index.js +0 -11
  51. package/dist/Formatter/LibNumComma/index.d.ts +0 -9
  52. package/dist/Formatter/LibNumComma/index.js +0 -13
  53. package/dist/Formatter/LibNumberUnit/index.d.ts +0 -12
  54. package/dist/Formatter/LibNumberUnit/index.js +0 -23
  55. package/dist/Math/LibCalculateExpression/index.d.ts +0 -10
  56. package/dist/Math/LibCalculateExpression/index.js +0 -108
  57. package/dist/Math/LibConvertAngle/index.d.ts +0 -12
  58. package/dist/Math/LibConvertAngle/index.js +0 -22
  59. package/dist/Math/LibCoordsAngle/index.d.ts +0 -15
  60. package/dist/Math/LibCoordsAngle/index.js +0 -24
  61. package/dist/Math/LibCoordsDistance/index.d.ts +0 -15
  62. package/dist/Math/LibCoordsDistance/index.js +0 -16
  63. package/dist/Math/LibDecimal/index.d.ts +0 -11
  64. package/dist/Math/LibDecimal/index.js +0 -26
  65. package/dist/Misc/LibRegFormValidate/index.d.ts +0 -32
  66. package/dist/Misc/LibRegFormValidate/index.js +0 -34
  67. package/dist/Misc/LibRetryRequest/index.d.ts +0 -24
  68. package/dist/Misc/LibRetryRequest/index.js +0 -37
  69. package/dist/Random/LibProbabilityResult/index.d.ts +0 -8
  70. package/dist/Random/LibProbabilityResult/index.js +0 -8
  71. package/dist/Random/LibRandom/index.d.ts +0 -10
  72. package/dist/Random/LibRandom/index.js +0 -12
  73. package/dist/Random/LibRandomColor/index.d.ts +0 -7
  74. package/dist/Random/LibRandomColor/index.js +0 -12
  75. package/dist/Random/LibUniqueRandomNumbers/index.d.ts +0 -9
  76. package/dist/Random/LibUniqueRandomNumbers/index.js +0 -16
  77. package/dist/Time/LibSameTimeCheck/index.d.ts +0 -8
  78. package/dist/Time/LibSameTimeCheck/index.js +0 -20
  79. package/dist/Time/LibSecondsFormatterChinese/index.d.ts +0 -11
  80. package/dist/Time/LibSecondsFormatterChinese/index.js +0 -42
  81. package/dist/Time/LibTimeAgo/index.d.ts +0 -9
  82. package/dist/Time/LibTimeAgo/index.js +0 -27
  83. package/dist/Time/LibTimeGreeting/index.d.ts +0 -17
  84. package/dist/Time/LibTimeGreeting/index.js +0 -23
  85. package/dist/index.d.ts +0 -1
  86. package/dist/index.js +0 -1
  87. package/dist/lib.d.ts +0 -395
  88. package/dist/lib.js +0 -405
  89. package/test/index.js +0 -0
package/README.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Lib自用JS工具方法
2
2
 
3
+ ## 介绍
4
+
5
+ > 该库为作者在写项目时收集的常用方法,代码简陋,没有严格的边缘处理
6
+ >
7
+ > 鼠标悬浮在每一个方法上都有较为完整的`Jsdoc`提示
8
+
3
9
  ## 起步
4
10
 
5
11
  > 完整使用
@@ -7,28 +13,520 @@
7
13
  ```ts
8
14
  import { Lib } from "lib-js-utils";
9
15
 
10
- Lib.Base.libGetDataType("Hellow World!");
16
+ Lib.Base.libGetDataType("Hellow World!"); //"string"
17
+ ```
18
+
19
+ > 按需引入,打包时就不会把整个库打进去
20
+
21
+ ```ts
22
+ import { libGetDataType } from "lyb-js/dist/Base/LibGetDataType";
23
+
24
+ libGetDataType("Hellow World!"); //"string"
11
25
  ```
12
26
 
13
- > 按需引入
27
+ > 如果在多个文件使用到同一个方法,建议采用按需引入聚合导出
14
28
 
15
29
  ```ts
16
- import { libGetDataType } from "lib-js-utils/dist/libGetDataType";
30
+ //你的公共工具函数文件 utils.ts
31
+ export * from "lyb-js/dist/Base/LibGetDataType";
32
+ export * from "lyb-js/dist/Math/LibCalculateExpression";
33
+
34
+ //你的项目文件 index.ts
35
+ import { libGetDataType,libCalculateExpression } from "utils";
17
36
 
18
- libGetDataType("Hellow World!");
37
+ libGetDataType("Hellow World!"); //"string"
38
+ libCalculateExpression("(1+2)-(3*4)/5"); //0.6
19
39
  ```
20
40
 
21
- > 按需使用
41
+ ## 目录
42
+
43
+ ### 基础
44
+
45
+ \- [LibGetDataType-数据类型](###LibGetDataType-数据类型)
46
+
47
+ \- [LibPromiseTimeout-延时执行](###LibPromiseTimeout-延时执行)
48
+
49
+
50
+ ## Browser-浏览器
51
+
52
+ \- [LibColorConsole-有色打印](###LibColorConsole-有色打印)
53
+
54
+ \- [LibIsMobile-判断手机](###LibIsMobile-判断手机)
55
+
56
+ \- [LibIsPad-判断平板](###LibIsPad-判断平板)
57
+
58
+ \- [LibPathParams-地址栏参数](###LibPathParams-地址栏参数)
59
+
60
+ \- [LibSetTitleIcon-网站标题图标](###LibSetTitleIcon-网站标题图标)
61
+
62
+ \- [LibTagTitleTip-网站标题交互](###LibTagTitleTip-网站标题交互)
63
+
64
+
65
+ ## Data-数据
66
+
67
+ \- [LibChunkArray-数组拆分](###LibChunkArray-数组拆分)
68
+
69
+ \- [LibDeepJSONParse-深度解析JSON](###LibDeepJSONParse-深度解析JSON)
70
+
71
+ \- [LibGroupArrayByKey-分类汇总](###LibGroupArrayByKey-分类汇总)
72
+
73
+ \- [LibMatchEmail-匹配E-Mail](###LibMatchEmail-匹配E-Mail)
74
+
75
+ \- [LibShuffleArray-数组乱序](###LibShuffleArray-数组乱序)
76
+
77
+ \- [LibStepArray-数组偏移](###LibStepArray-数组偏移)
78
+
79
+
80
+ ## File-文件
81
+
82
+ \- [LibDownloadImageLink-图片下载](###LibDownloadImageLink-图片下载)
83
+
84
+ \- [LibImageOptimizer-图片压缩](###LibImageOptimizer-图片压缩)
85
+
86
+ \- [LibSaveJson-保存文件](###LibSaveJson-保存文件)
87
+
88
+
89
+ ## Formatter-格式化
90
+
91
+ \- [LibFormatterByte-字节格式化](###LibFormatterByte-字节格式化)
92
+
93
+ \- [LibMaskPhoneNumber-隐藏手机号码](###LibMaskPhoneNumber-隐藏手机号码)
94
+
95
+ \- [LibNumberUnit-数字单位](###LibNumberUnit-数字单位)
96
+
97
+ \- [LibNumComma-数字逗号](###LibNumComma-数字逗号)
98
+
99
+ \- [LibSecondsFormatterChinese-中文时间](###LibSecondsFormatterChinese-中文时间)
100
+
101
+
102
+ ## Math-数学
103
+
104
+ \- [LibCalculateExpression-表达式字符串](###LibCalculateExpression-表达式字符串)
105
+
106
+ \- [LibConvertAngle-角弧度互转](###LibConvertAngle-角弧度互转)
107
+
108
+ \- [LibCoordsAngle-两点角度](###LibCoordsAngle-两点角度)
109
+
110
+ \- [LibCoordsDistance-两点距离](###LibCoordsDistance-两点距离)
111
+
112
+ \- [LibDecimal-高精度计算](###LibDecimal-高精度计算)
113
+
114
+
115
+ ## Misc-杂项
116
+
117
+ \- [LibRegFormValidate-表单验证](###LibRegFormValidate-表单验证)
118
+
119
+ \- [LibRetryRequest-请求重连](###LibRetryRequest-请求重连)
120
+
121
+
122
+ ## Random-随机
123
+
124
+ \- [LibProbabilityResult-概率触发](###LibProbabilityResult-概率触发)
125
+
126
+ \- [LibRandom-随机数](###LibRandom-随机数)
127
+
128
+ \- [LibRandomColor-随机色](###LibRandomColor-随机色)
129
+
130
+ \- [LibUniqueRandomNumbers-随机数数组](###LibUniqueRandomNumbers-随机数数组)
131
+
132
+
133
+ ## Time-时间
134
+
135
+ \- [LibSameTimeCheck-时间比对](###LibSameTimeCheck-时间比对)
136
+
137
+ \- [LibTimeAgo-中文时间差](###LibTimeAgo-中文时间差)
138
+
139
+ \- [LibTimeGreeting-时间问候](###LibTimeGreeting-时间问候)
140
+
22
141
 
23
142
  ## Base-基础
24
143
 
25
- ### LibGetDataType
144
+ ### LibGetDataType-数据类型
26
145
 
27
146
  > 返回数据类型
28
147
 
29
148
  ```ts
30
- libGetDataType(123); // "number"
31
- libGetDataType("hello"); // "string"
32
- libGetDataType([1, 2, 3]); // "array"
149
+ libGetDataType(123); //"number"
150
+ libGetDataType("hello"); //"string"
151
+ libGetDataType([1, 2, 3]); //"array"
152
+ ```
153
+
154
+ ### LibPromiseTimeout-延时执行
155
+
156
+ > 延时执行,切换到其他页面会暂停
157
+
158
+ ```ts
159
+ libPromiseTimeout(3000, () => {
160
+ console.log("执行延时函数");
161
+ });
162
+ ```
163
+
164
+ ## Browser-浏览器
165
+
166
+ ### LibColorConsole-有色打印
167
+
168
+ > `console`有色打印
169
+
170
+ ```ts
171
+ //使用红色打印日志
172
+ libColorConsole("错误提示", "red", [{ label: "错误代码", value: 500 }]);
173
+
174
+ //使用蓝色打印简单日志
175
+ libColorConsole("信息", "blue", "操作成功");
176
+ ```
177
+
178
+ ### LibIsMobile-判断手机
179
+
180
+ > 判断是否为移动设备
181
+
182
+ ```ts
183
+ const isMobile = libIsMobile();
184
+ console.log(isMobile); //true 或 false
185
+ ```
186
+
187
+ ### LibIsPad-判断平板
188
+
189
+ > 判断是否为平板
190
+
191
+ ```ts
192
+ const isPad = libIsPad();
193
+ console.log(isPad); //true 或 false
194
+ ```
195
+
196
+ ### LibPathParams-地址栏参数
197
+
198
+ > 获取浏览器地址栏参数
199
+
200
+ ```ts
201
+ const params = libPathParams();
202
+ console.log(params); //{ param1: "value1", param2: "value2" }
203
+ ```
204
+
205
+ ### LibSetTitleIcon-网站标题图标
206
+
207
+ > 动态设置网站标题及图标,涉及到不同平台的打包,可以根据不同环境来设置网站标题和图标
208
+
209
+ ```ts
210
+ libSetTitleIcon("我的网站", "https://example.com/favicon.ico");
211
+ ```
212
+
213
+ ### LibTagTitleTip-网站标题交互
214
+
215
+ > 网站标题交互,当从当前网页切换到其他网页,网站标题自动切换
216
+
217
+ ```ts
218
+ libTagTitleTip("欢迎回来", "来和妲己玩耍吧!");
219
+ ```
220
+
221
+ ## Data-数据
222
+
223
+ ### LibChunkArray-数组拆分
224
+
225
+ > 将数组拆分成指定数组元素数量的多个数组
226
+
227
+ ```ts
228
+ const chunks = libChunkArray([1, 2, 3, 4, 5, 6], 2);
229
+ console.log(chunks); //[[1, 2], [3, 4], [5, 6]]
230
+ ```
231
+
232
+ ### LibDeepJSONParse-深度解析JSON
233
+
234
+ > 递归将JSON字符串深度解析为对象
235
+
236
+ ```ts
237
+ const obj = libDeepJSONParse('{"a": 1, "b": "{\"c\": 2}"}');
238
+ console.log(obj); //{ a: 1, b: { c: 2 } }
239
+ ```
240
+
241
+ ### LibGroupArrayByKey-分类汇总
242
+
243
+ > 将数组对象按照指定键值整理成一个以键值为键名的对象
244
+
245
+ ```ts
246
+ const grouped = libGroupArrayByKey([{ id: 1, name: 'A' }, { id: 2, name: 'B' }, { id: 1, name: 'C' }], 'id');
247
+ console.log(grouped); //{ 1: [{ id: 1, name: 'A' }, { id: 1, name: 'C' }], 2: [{ id: 2, name: 'B' }] }
248
+ ```
249
+
250
+ ### LibMatchEmail-匹配E-Mail
251
+
252
+ > 可用于实时输入时,自动补全常用邮箱后缀
253
+
254
+ ```ts
255
+ const emails = libMatchEmail("user", ["@gmail.com", "@yahoo.com"]);
256
+ console.log(emails); //["user@gmail.com", "user@yahoo.com"]
257
+ ```
258
+
259
+ ### LibShuffleArray-数组乱序
260
+
261
+ > 将数组打乱顺序
262
+
263
+ ```ts
264
+ const shuffled = libShuffleArray([1, 2, 3, 4, 5]);
265
+ console.log(shuffled); //[3, 5, 2, 1, 4] (结果每次不同)
266
+ ```
267
+
268
+ ### LibStepArray-数组偏移
269
+
270
+ > 数组元素整体步数移动
271
+
272
+ ```ts
273
+ const moved = libStepArray([1, 2, 3, 4, 5], 2);
274
+ console.log(moved); //[4, 5, 1, 2, 3]
275
+ ```
276
+
277
+ ## File-文件
278
+
279
+ ### LibDownloadImageLink-图片下载
280
+
281
+ > 将链接图片下载到本地
282
+
283
+ ```ts
284
+ libDownloadImageLink("https://example.com/image.jpg", "图片.jpg");
285
+ ```
286
+
287
+ ### LibImageOptimizer-图片压缩
288
+
289
+ > 支持`png`压缩,保留透明背景
290
+
291
+ ```ts
292
+ //图片压缩使用示例
293
+ libImageOptimizerOptionsParams({
294
+ file: myFile,
295
+ ratio: 0.8,
296
+ width: 800,
297
+ maxSize: 1024,
298
+ success: (formData, file, url) => {
299
+ console.log('压缩成功', formData, file, url);
300
+ },
301
+ fail: (error) => {
302
+ console.error('压缩失败', error);
303
+ }
304
+ });
305
+ ```
306
+
307
+ ### LibSaveJson-保存文件
308
+
309
+ > 保存`JSON`文件到本地,也支持保存纯文本的`txt`文件
310
+
311
+ ```ts
312
+ libSaveJson(JSON.stringify({ key: "value" }), "example.json");、
313
+ libSaveJson("Hellow World!", "example.txt");
314
+ ```
315
+
316
+ ## Formatter-格式化
317
+
318
+ ### LibFormatterByte-字节格式化
319
+
320
+ > 将字节单位的数字格式化
321
+
322
+ ```ts
323
+ const [size, unit, formatted] = libFormatterByte(2048);
324
+ console.log(size, unit, formatted); //2.00 KB 2.00 KB
325
+ ```
326
+
327
+ ### LibMaskPhoneNumber-隐藏手机号码
328
+
329
+ > 隐藏手机号码中间的四位数字
330
+
331
+ ```ts
332
+ const masked = libMaskPhoneNumber("13812345678");
333
+ console.log(masked); //138****5678
334
+ ```
335
+
336
+ ### LibNumberUnit-数字单位
337
+
338
+ > 大于指定数字,用单位标识,你甚至可以用中文
339
+
340
+ ```ts
341
+ const [value, unit] = libNumberUnit(1500, { K: 1000, M: 1000000 });
342
+ console.log(value, unit); //1.50 K
343
+
344
+ const [value, unit] = libNumberUnit(0.05, { 分: 0.01, 角: 0.1, 元: 1 });
345
+ console.log(value, unit); //0.05 分
346
+ ```
347
+
348
+ ### LibNumComma-数字逗号
349
+
350
+ > 数字每三位添加逗号
351
+
352
+ ```ts
353
+ const formatted = libNumComma(1234567.89);
354
+ console.log(formatted); //1,234,567.89
355
+ ```
356
+
357
+ ### LibSecondsFormatterChinese-中文时间
358
+
359
+ > 将秒数格式化为中文时间描述,支持扩展到年
360
+
361
+ ```ts
362
+ libSecondsFormatterChinese(100000); //"1天3小时46分40秒"
363
+ libSecondsFormatterChinese(31536000); //"1年"
364
+ libSecondsFormatterChinese(3600); //"1小时"
365
+ libSecondsFormatterChinese(90); //"1分30秒"
366
+ ```
367
+
368
+ ## Math-数学
369
+
370
+ ### LibCalculateExpression-表达式字符串
371
+
372
+ > 计算表达式字符串
373
+
374
+ ```ts
375
+ const result = libCalculateExpression("(1+2)-(3*4)/5");
376
+ console.log(result); //0.6
377
+ ```
378
+
379
+ ### LibConvertAngle-角弧度互转
380
+
381
+ > 角度和弧度互相转换
382
+
383
+ ```ts
384
+ //角度转弧度
385
+ libConvertAngle(90, "rad"); //返回 1.5708... (π/2)
386
+
387
+ //弧度转角度
388
+ libConvertAngle(Math.PI, "deg"); //返回 180
389
+ ```
390
+
391
+ ### LibCoordsAngle-两点角度
392
+
393
+ > 计算两点角度
394
+
395
+ ```ts
396
+ libCoordsAngle({ x: 0, y: 0 }, { x: 1, y: 0 }); //0
397
+ libCoordsAngle({ x: 0, y: 0 }, { x: 1, y: 1 }); //45
398
+ libCoordsAngle({ x: 0, y: 0 }, { x: 0, y: 1 }); //90
399
+ ```
400
+
401
+ ### LibCoordsDistance-两点距离
402
+
403
+ > 计算两点距离
404
+
405
+ ```ts
406
+ libCoordsDistance({ x: 0, y: 0 }, { x: 3, y: 4 }); //5
407
+ libCoordsDistance({ x: 1, y: 1 }, { x: 4, y: 5 }); //5
408
+ libCoordsDistance({ x: 0, y: 0 }, { x: 0, y: 0 }); //0
409
+ ```
410
+
411
+ ### LibDecimal-高精度计算
412
+
413
+ > 计算两个数的运算结果,并保留指定位数的小数
414
+
415
+ ```ts
416
+ libDecimal(10, 3, "+"); //13
417
+ libDecimal(10, 3, "-"); //7
418
+ libDecimal(10, 3, "/", 2); //3.33
419
+ ```
420
+
421
+ ## Misc
422
+
423
+ ### LibRegFormValidate-表单验证
424
+
425
+ > 通过传递对象数字的方式进行正则或自定义函数进行验证
426
+
427
+ ```ts
428
+ const form = { username: "john", email: "john@example.com" };
429
+ const rules = [
430
+ { key: "username", verify: /^[a-zA-Z0-9]{3,}$/, msg: "用户名不合法", name: "用户名" },
431
+ { key: "email", verify: /^\S+@\S+\.\S+$/, msg: "邮箱格式不正确", name: "邮箱" },
432
+ ];
433
+ libRegFormValidate(form, rules);
434
+ //返回结果: []
435
+
436
+ const invalidForm = { username: "jo", email: "invalid-email" };
437
+ libRegFormValidate(invalidForm, rules);
438
+ //返回结果: [
439
+ // { key: "username", msg: "用户名不合法", name: "用户名" },
440
+ // { key: "email", msg: "邮箱格式不正确", name: "邮箱" }
441
+ //]
442
+ ```
443
+
444
+ ### LibRetryRequest-请求重连
445
+
446
+ > 请求失败重连
447
+
448
+ ```ts
449
+ const requestFn = (params: { url: string }) => fetch(params.url).then(res => res.json());
450
+ const params = { url: "https://api.example.com/data" };
451
+ libRetryRequest({
452
+ promiseFn: requestFn,
453
+ params,
454
+ maxRetries: 5,
455
+ retryDelay: 1000
456
+ })
457
+ .then(data => console.log(data))
458
+ .catch(err => console.error(err));
459
+ ```
460
+
461
+ ## Random-随机
462
+
463
+ ### LibProbabilityResult-概率触发
464
+
465
+ > 百分比概率结果
466
+
467
+ ```ts
468
+ libProbabilityResult(50); //50% 概率为 true
469
+ libProbabilityResult(80); //80% 概率为 true
470
+ libProbabilityResult(100); //100% 概率为 true
471
+ ```
472
+
473
+ ### LibRandom-随机数
474
+
475
+ > 随机获取两个数之间的值,包含两数自身
476
+
477
+ ```ts
478
+ libRandom(1, 10); //1 到 10 之间的随机整数
479
+ libRandom(1, 10, 2); //1 到 10 之间保留两位小数的随机数
480
+ ```
481
+
482
+ ### LibRandomColor-随机色
483
+
484
+ > 随机 RGBA 颜色
485
+
486
+ ```ts
487
+ libRandomColor(); //生成随机的 RGBA 颜色,默认透明度 1
488
+ libRandomColor(0.5); //生成随机的 RGBA 颜色,透明度为 0.5
489
+ ```
490
+
491
+ ### LibUniqueRandomNumbers-随机数数组
492
+
493
+ > 随机生成指定个数、指定范围不重复的随机数数组
494
+
495
+ ```ts
496
+ libUniqueRandomNumbers(1, 10, 5); //从 1 到 10 中随机生成 5 个唯一数字
497
+ libUniqueRandomNumbers(1, 100, 10); //从 1 到 100 中随机生成 10 个唯一数字
498
+ ```
499
+
500
+ ## Time-时间
501
+
502
+ ### LibSameTimeCheck-时间比对
503
+
504
+ > 传入时间戳与当前时间判断是否为同一分、同一时、同一天、同一周、同一月、同一年
505
+
506
+ ```ts
507
+ const timestamp = 1679872800000; //时间戳
508
+ const result = libSameTimeCheck(timestamp, 'day'); //判断是否为同一天
509
+ console.log(result); //0: 同一天, 1: 新的一天, -1: 时间戳大于当前时间
510
+ ```
511
+
512
+ ### LibTimeAgo-中文时间差
513
+
514
+ > 时间差计算
515
+
516
+ ```ts
517
+ libTimeAgotamp(Date.now() - 3600000); //"1 小时前"
518
+ libTimeAgotamp(Date.now() - 86400000); //"1 天前"
519
+ libTimeAgotamp(Date.now() - 31536000000); //"1 年前"
520
+ libTimeAgotamp(Date.now() - 10000); //"刚刚"
521
+ ```
522
+
523
+ ### LibTimeGreeting-时间问候
524
+
525
+ > 根据当前时间返回问候语
526
+
527
+ ```ts
528
+ libTimeGreeting(); //根据当前时间返回默认问候语
529
+ libTimeGreeting({ morning: "早安" }); //自定义早上问候语
530
+ libTimeGreeting({ afternoon: "午后好" }); //自定义下午问候语
33
531
  ```
34
532
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lyb-js",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "author": "冷弋白",
5
5
  "description": "lyb自用JS方法库",
6
6
  "repository": {
@@ -1,10 +0,0 @@
1
- export type LibGetDataTypeReturnType = "string" | "number" | "boolean" | "array" | "function" | "object";
2
- /**
3
- * @description 返回数据类型
4
- * @param v 需要判断类型的数据
5
- * @example
6
- * libGetDataType(123); // "number"
7
- * libGetDataType("hello"); // "string"
8
- * libGetDataType([1, 2, 3]); // "array"
9
- */
10
- export declare const libGetDataType: (v: any) => LibGetDataTypeReturnType;
@@ -1,15 +0,0 @@
1
- /**
2
- * @description 返回数据类型
3
- * @param v 需要判断类型的数据
4
- * @example
5
- * libGetDataType(123); // "number"
6
- * libGetDataType("hello"); // "string"
7
- * libGetDataType([1, 2, 3]); // "array"
8
- */
9
- export const libGetDataType = (v) => {
10
- return Object.prototype.toString
11
- .call(v)
12
- .substring(8)
13
- .replace(/]/g, "")
14
- .toLowerCase();
15
- };
@@ -1,7 +0,0 @@
1
- type ReturnType = "string" | "number" | "boolean" | "array" | "function" | "object";
2
- /**
3
- * @description 返回数据类型
4
- * @param v 需要判断类型的数据
5
- */
6
- export declare const libJsGetDataType: (v: any) => ReturnType;
7
- export {};
@@ -1,11 +0,0 @@
1
- /**
2
- * @description 返回数据类型
3
- * @param v 需要判断类型的数据
4
- */
5
- export const libJsGetDataType = (v) => {
6
- return Object.prototype.toString
7
- .call(v)
8
- .substring(8)
9
- .replace(/]/g, "")
10
- .toLowerCase();
11
- };
@@ -1,10 +0,0 @@
1
- /**
2
- * @description 延时执行,切换到其他页面会暂停
3
- * @param delay 延时毫秒数
4
- * @param fn 延时执行函数
5
- * @example
6
- * libPromiseTimeout(3000, () => {
7
- * console.log("执行延时函数");
8
- * });
9
- */
10
- export declare const libPromiseTimeout: (delay?: number, fn?: () => void) => Promise<void>;
@@ -1,40 +0,0 @@
1
- /**
2
- * @description 延时执行,切换到其他页面会暂停
3
- * @param delay 延时毫秒数
4
- * @param fn 延时执行函数
5
- * @example
6
- * libPromiseTimeout(3000, () => {
7
- * console.log("执行延时函数");
8
- * });
9
- */
10
- export const libPromiseTimeout = (delay = 1, fn) => {
11
- let timeoutId;
12
- let remainingTime = delay;
13
- let startTime = Date.now();
14
- let isPaused = false;
15
- return new Promise((resolve) => {
16
- const handleVisibilityChange = () => {
17
- if (document.hidden) {
18
- isPaused = true;
19
- clearTimeout(timeoutId);
20
- remainingTime -= Date.now() - startTime;
21
- }
22
- else {
23
- if (isPaused) {
24
- isPaused = false;
25
- startTime = Date.now();
26
- startTimeout(resolve);
27
- }
28
- }
29
- };
30
- const startTimeout = (resolve) => {
31
- timeoutId = setTimeout(() => {
32
- fn?.();
33
- resolve();
34
- document.removeEventListener("visibilitychange", handleVisibilityChange);
35
- }, remainingTime);
36
- };
37
- document.addEventListener("visibilitychange", handleVisibilityChange);
38
- startTimeout(resolve);
39
- });
40
- };
@@ -1,15 +0,0 @@
1
- /** @description console颜色打印
2
- * @param title 标题
3
- * @param color 颜色
4
- * @param logs 信息
5
- * @example
6
- * // 使用红色打印日志
7
- * libColorConsole("错误提示", "red", [{ label: "错误代码", value: 500 }]);
8
- *
9
- * // 使用蓝色打印简单日志
10
- * libColorConsole("信息", "blue", "操作成功");
11
- */
12
- export declare const libColorConsole: (title: string, color: "red" | "orange" | "yellow" | "green" | "blue" | "purple", logs?: {
13
- label: string;
14
- value: any;
15
- }[] | any) => void;
@@ -1,38 +0,0 @@
1
- /** @description console颜色打印
2
- * @param title 标题
3
- * @param color 颜色
4
- * @param logs 信息
5
- * @example
6
- * // 使用红色打印日志
7
- * libColorConsole("错误提示", "red", [{ label: "错误代码", value: 500 }]);
8
- *
9
- * // 使用蓝色打印简单日志
10
- * libColorConsole("信息", "blue", "操作成功");
11
- */
12
- export const libColorConsole = (title, color, logs = []) => {
13
- const colors = {
14
- red: "#c0392b",
15
- orange: "#d35400",
16
- yellow: "#f1c40f",
17
- green: "#27ae60",
18
- blue: "#2980b9",
19
- purple: "#be2edd",
20
- };
21
- // 时间戳生成函数
22
- const getTimestamp = () => {
23
- const now = new Date();
24
- const hour = String(now.getHours()).padStart(2, "0");
25
- const minutes = String(now.getMinutes()).padStart(2, "0");
26
- const seconds = String(now.getSeconds()).padStart(2, "0");
27
- return `[${hour}:${minutes}:${seconds}]`;
28
- };
29
- if (Array.isArray(logs)) {
30
- const v = logs.map((log) => {
31
- return [`\n${log.label}:`, log.value];
32
- });
33
- console.log(`%c${getTimestamp()}-${title}`, `color: #fff;background: ${colors[color]}; font-size:14px;border-radius:5px;padding:0.25em;margin:0.5em 0`, ...v.flat(Infinity));
34
- }
35
- else {
36
- console.log(`%c${getTimestamp()}-${title}`, `color: #fff;background: ${colors[color]}; font-size:14px;border-radius:5px;padding:0.25em;margin:0.5em 0`, logs);
37
- }
38
- };