lyb-js 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +406 -9
- package/dist/Base/LibGetDataType/index.d.ts +3 -3
- package/dist/Base/LibGetDataType/index.js +3 -3
- package/dist/Browser/LibColorConsole/index.d.ts +2 -2
- package/dist/Browser/LibColorConsole/index.js +3 -3
- package/dist/Browser/LibIsMobile/index.d.ts +1 -1
- package/dist/Browser/LibIsMobile/index.js +1 -1
- package/dist/Browser/LibIsPad/index.d.ts +1 -1
- package/dist/Browser/LibIsPad/index.js +1 -1
- package/dist/Browser/LibPathParams/index.d.ts +1 -1
- package/dist/Browser/LibPathParams/index.js +1 -1
- package/dist/Data/LibChunkArray/index.d.ts +1 -1
- package/dist/Data/LibChunkArray/index.js +1 -1
- package/dist/Data/LibDeepJSONParse/index.d.ts +1 -1
- package/dist/Data/LibDeepJSONParse/index.js +7 -7
- package/dist/Data/LibGroupArrayByKey/index.d.ts +4 -4
- package/dist/Data/LibGroupArrayByKey/index.js +4 -4
- package/dist/Data/LibMatchEmail/index.d.ts +1 -1
- package/dist/Data/LibMatchEmail/index.js +1 -1
- package/dist/Data/LibShuffleArray/index.d.ts +2 -2
- package/dist/Data/LibShuffleArray/index.js +2 -2
- package/dist/Data/LibStepArray/index.d.ts +1 -1
- package/dist/Data/LibStepArray/index.js +1 -1
- package/dist/File/LibDownloadImageLink/index.d.ts +2 -2
- package/dist/File/LibDownloadImageLink/index.js +2 -2
- package/dist/File/LibImageOptimizer/index.d.ts +5 -5
- package/dist/File/LibImageOptimizer/index.js +4 -4
- package/dist/File/LibSaveJson/index.d.ts +1 -0
- package/dist/File/LibSaveJson/index.js +1 -0
- package/dist/Formatter/LibFormatterByte/index.d.ts +1 -1
- package/dist/Formatter/LibFormatterByte/index.js +1 -1
- package/dist/Formatter/LibMaskPhoneNumber/index.d.ts +3 -3
- package/dist/Formatter/LibMaskPhoneNumber/index.js +3 -3
- package/dist/Formatter/LibNumComma/index.d.ts +1 -1
- package/dist/Formatter/LibNumComma/index.js +1 -1
- package/dist/Formatter/LibNumberUnit/index.d.ts +1 -1
- package/dist/Formatter/LibNumberUnit/index.js +1 -1
- package/dist/Formatter/LibSecondsFormatterChinese/index.d.ts +11 -0
- package/dist/Formatter/LibSecondsFormatterChinese/index.js +42 -0
- package/dist/Math/LibCalculateExpression/index.d.ts +1 -2
- package/dist/Math/LibCalculateExpression/index.js +17 -18
- package/dist/Math/LibConvertAngle/index.d.ts +4 -4
- package/dist/Math/LibConvertAngle/index.js +4 -4
- package/dist/Math/LibCoordsAngle/index.d.ts +3 -3
- package/dist/Math/LibCoordsAngle/index.js +8 -8
- package/dist/Math/LibCoordsDistance/index.d.ts +3 -3
- package/dist/Math/LibCoordsDistance/index.js +5 -5
- package/dist/Math/LibDecimal/index.d.ts +3 -4
- package/dist/Math/LibDecimal/index.js +3 -4
- package/dist/Misc/LibRegFormValidate/index.d.ts +9 -9
- package/dist/Misc/LibRegFormValidate/index.js +9 -9
- package/dist/Random/LibProbabilityResult/index.d.ts +3 -3
- package/dist/Random/LibProbabilityResult/index.js +3 -3
- package/dist/Random/LibRandom/index.d.ts +3 -4
- package/dist/Random/LibRandom/index.js +3 -4
- package/dist/Random/LibRandomColor/index.d.ts +2 -2
- package/dist/Random/LibRandomColor/index.js +2 -2
- package/dist/Random/LibUniqueRandomNumbers/index.d.ts +3 -3
- package/dist/Random/LibUniqueRandomNumbers/index.js +3 -3
- package/dist/Time/LibSameTimeCheck/index.d.ts +5 -1
- package/dist/Time/LibSameTimeCheck/index.js +5 -1
- package/dist/Time/LibTimeAgo/index.d.ts +4 -4
- package/dist/Time/LibTimeAgo/index.js +4 -4
- package/dist/Time/LibTimeGreeting/index.d.ts +5 -5
- package/dist/Time/LibTimeGreeting/index.js +5 -5
- package/dist/index.d.ts +1 -395
- package/dist/index.js +1 -405
- package/dist/lib.d.ts +394 -39
- package/dist/lib.js +404 -39
- package/package.json +1 -1
- 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,419 @@
|
|
|
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"
|
|
11
17
|
```
|
|
12
18
|
|
|
13
|
-
>
|
|
19
|
+
> 按需引入,打包时就不会把整个库打进去
|
|
14
20
|
|
|
15
21
|
```ts
|
|
16
|
-
import { libGetDataType } from "
|
|
22
|
+
import { libGetDataType } from "lyb-js/dist/Base/LibGetDataType";
|
|
17
23
|
|
|
18
|
-
libGetDataType("Hellow World!");
|
|
24
|
+
libGetDataType("Hellow World!"); //"string"
|
|
19
25
|
```
|
|
20
26
|
|
|
21
|
-
>
|
|
27
|
+
> 如果在多个文件使用到同一个方法,建议采用按需引入聚合导出
|
|
28
|
+
|
|
29
|
+
```ts
|
|
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";
|
|
36
|
+
|
|
37
|
+
libGetDataType("Hellow World!"); //"string"
|
|
38
|
+
libCalculateExpression("(1+2)-(3*4)/5"); //0.6
|
|
39
|
+
```
|
|
22
40
|
|
|
23
41
|
## Base-基础
|
|
24
42
|
|
|
25
|
-
### LibGetDataType
|
|
43
|
+
### LibGetDataType-数据类型
|
|
26
44
|
|
|
27
45
|
> 返回数据类型
|
|
28
46
|
|
|
29
47
|
```ts
|
|
30
|
-
libGetDataType(123); //
|
|
31
|
-
libGetDataType("hello"); //
|
|
32
|
-
libGetDataType([1, 2, 3]); //
|
|
48
|
+
libGetDataType(123); //"number"
|
|
49
|
+
libGetDataType("hello"); //"string"
|
|
50
|
+
libGetDataType([1, 2, 3]); //"array"
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### LibPromiseTimeout-延时执行
|
|
54
|
+
|
|
55
|
+
> 延时执行,切换到其他页面会暂停
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
libPromiseTimeout(3000, () => {
|
|
59
|
+
console.log("执行延时函数");
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Browser-浏览器
|
|
64
|
+
|
|
65
|
+
### LibColorConsole-有色打印
|
|
66
|
+
|
|
67
|
+
> `console`有色打印
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
//使用红色打印日志
|
|
71
|
+
libColorConsole("错误提示", "red", [{ label: "错误代码", value: 500 }]);
|
|
72
|
+
|
|
73
|
+
//使用蓝色打印简单日志
|
|
74
|
+
libColorConsole("信息", "blue", "操作成功");
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### LibIsMobile-判断手机
|
|
78
|
+
|
|
79
|
+
> 判断是否为移动设备
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
const isMobile = libIsMobile();
|
|
83
|
+
console.log(isMobile); //true 或 false
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### LibIsPad-判断平板
|
|
87
|
+
|
|
88
|
+
> 判断是否为平板
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
const isPad = libIsPad();
|
|
92
|
+
console.log(isPad); //true 或 false
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### LibPathParams-地址栏参数
|
|
96
|
+
|
|
97
|
+
> 获取浏览器地址栏参数
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
const params = libPathParams();
|
|
101
|
+
console.log(params); //{ param1: "value1", param2: "value2" }
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### LibSetTitleIcon-网站标题图标
|
|
105
|
+
|
|
106
|
+
> 动态设置网站标题及图标,涉及到不同平台的打包,可以根据不同环境来设置网站标题和图标
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
libSetTitleIcon("我的网站", "https://example.com/favicon.ico");
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### LibTagTitleTip-网站标题交互
|
|
113
|
+
|
|
114
|
+
> 网站标题交互,当从当前网页切换到其他网页,网站标题自动切换
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
libTagTitleTip("欢迎回来", "来和妲己玩耍吧!");
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Data-数据
|
|
121
|
+
|
|
122
|
+
### LibChunkArray-数组拆分
|
|
123
|
+
|
|
124
|
+
> 将数组拆分成指定数组元素数量的多个数组
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
const chunks = libChunkArray([1, 2, 3, 4, 5, 6], 2);
|
|
128
|
+
console.log(chunks); //[[1, 2], [3, 4], [5, 6]]
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### LibDeepJSONParse-深度解析JSON
|
|
132
|
+
|
|
133
|
+
> 递归将JSON字符串深度解析为对象
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
const obj = libDeepJSONParse('{"a": 1, "b": "{\"c\": 2}"}');
|
|
137
|
+
console.log(obj); //{ a: 1, b: { c: 2 } }
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### LibGroupArrayByKey-分类汇总
|
|
141
|
+
|
|
142
|
+
> 将数组对象按照指定键值整理成一个以键值为键名的对象
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
const grouped = libGroupArrayByKey([{ id: 1, name: 'A' }, { id: 2, name: 'B' }, { id: 1, name: 'C' }], 'id');
|
|
146
|
+
console.log(grouped); //{ 1: [{ id: 1, name: 'A' }, { id: 1, name: 'C' }], 2: [{ id: 2, name: 'B' }] }
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### LibMatchEmail-匹配E-Mail
|
|
150
|
+
|
|
151
|
+
> 可用于实时输入时,自动补全常用邮箱后缀
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
const emails = libMatchEmail("user", ["@gmail.com", "@yahoo.com"]);
|
|
155
|
+
console.log(emails); //["user@gmail.com", "user@yahoo.com"]
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### LibShuffleArray-数组乱序
|
|
159
|
+
|
|
160
|
+
> 将数组打乱顺序
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
const shuffled = libShuffleArray([1, 2, 3, 4, 5]);
|
|
164
|
+
console.log(shuffled); //[3, 5, 2, 1, 4] (结果每次不同)
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### LibStepArray-数组偏移
|
|
168
|
+
|
|
169
|
+
> 数组元素整体步数移动
|
|
170
|
+
|
|
171
|
+
```ts
|
|
172
|
+
const moved = libStepArray([1, 2, 3, 4, 5], 2);
|
|
173
|
+
console.log(moved); //[4, 5, 1, 2, 3]
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## File-文件
|
|
177
|
+
|
|
178
|
+
### LibDownloadImageLink-图片下载
|
|
179
|
+
|
|
180
|
+
> 将链接图片下载到本地
|
|
181
|
+
|
|
182
|
+
```ts
|
|
183
|
+
libDownloadImageLink("https://example.com/image.jpg", "图片.jpg");
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### LibImageOptimizer-图片压缩
|
|
187
|
+
|
|
188
|
+
> 支持`png`压缩,保留透明背景
|
|
189
|
+
|
|
190
|
+
```ts
|
|
191
|
+
//图片压缩使用示例
|
|
192
|
+
libImageOptimizerOptionsParams({
|
|
193
|
+
file: myFile,
|
|
194
|
+
ratio: 0.8,
|
|
195
|
+
width: 800,
|
|
196
|
+
maxSize: 1024,
|
|
197
|
+
success: (formData, file, url) => {
|
|
198
|
+
console.log('压缩成功', formData, file, url);
|
|
199
|
+
},
|
|
200
|
+
fail: (error) => {
|
|
201
|
+
console.error('压缩失败', error);
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### LibSaveJson-保存文件
|
|
207
|
+
|
|
208
|
+
> 保存`JSON`文件到本地,也支持保存纯文本的`txt`文件
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
libSaveJson(JSON.stringify({ key: "value" }), "example.json");、
|
|
212
|
+
libSaveJson("Hellow World!", "example.txt");
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## Formatter-格式化
|
|
216
|
+
|
|
217
|
+
### LibFormatterByte-字节格式化
|
|
218
|
+
|
|
219
|
+
> 将字节单位的数字格式化
|
|
220
|
+
|
|
221
|
+
```ts
|
|
222
|
+
const [size, unit, formatted] = libFormatterByte(2048);
|
|
223
|
+
console.log(size, unit, formatted); //2.00 KB 2.00 KB
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
### LibMaskPhoneNumber-隐藏手机号码
|
|
227
|
+
|
|
228
|
+
> 隐藏手机号码中间的四位数字
|
|
229
|
+
|
|
230
|
+
```ts
|
|
231
|
+
const masked = libMaskPhoneNumber("13812345678");
|
|
232
|
+
console.log(masked); //138****5678
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### LibNumberUnit-数字单位
|
|
236
|
+
|
|
237
|
+
> 大于指定数字,用单位标识,你甚至可以用中文
|
|
238
|
+
|
|
239
|
+
```ts
|
|
240
|
+
const [value, unit] = libNumberUnit(1500, { K: 1000, M: 1000000 });
|
|
241
|
+
console.log(value, unit); //1.50 K
|
|
242
|
+
|
|
243
|
+
const [value, unit] = libNumberUnit(0.05, { 分: 0.01, 角: 0.1, 元: 1 });
|
|
244
|
+
console.log(value, unit); //0.05 分
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### LibNumComma-数字逗号
|
|
248
|
+
|
|
249
|
+
> 数字每三位添加逗号
|
|
250
|
+
|
|
251
|
+
```ts
|
|
252
|
+
const formatted = libNumComma(1234567.89);
|
|
253
|
+
console.log(formatted); //1,234,567.89
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
### LibSecondsFormatterChinese-中文时间
|
|
257
|
+
|
|
258
|
+
> 将秒数格式化为中文时间描述,支持扩展到年
|
|
259
|
+
|
|
260
|
+
```ts
|
|
261
|
+
libSecondsFormatterChinese(100000); //"1天3小时46分40秒"
|
|
262
|
+
libSecondsFormatterChinese(31536000); //"1年"
|
|
263
|
+
libSecondsFormatterChinese(3600); //"1小时"
|
|
264
|
+
libSecondsFormatterChinese(90); //"1分30秒"
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
## Math-数学
|
|
268
|
+
|
|
269
|
+
### LibCalculateExpression-表达式字符串
|
|
270
|
+
|
|
271
|
+
> 计算表达式字符串
|
|
272
|
+
|
|
273
|
+
```ts
|
|
274
|
+
const result = libCalculateExpression("(1+2)-(3*4)/5");
|
|
275
|
+
console.log(result); //0.6
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
### LibConvertAngle-角弧度互转
|
|
279
|
+
|
|
280
|
+
> 角度和弧度互相转换
|
|
281
|
+
|
|
282
|
+
```ts
|
|
283
|
+
//角度转弧度
|
|
284
|
+
libConvertAngle(90, "rad"); //返回 1.5708... (π/2)
|
|
285
|
+
|
|
286
|
+
//弧度转角度
|
|
287
|
+
libConvertAngle(Math.PI, "deg"); //返回 180
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
### LibCoordsAngle-两点角度
|
|
291
|
+
|
|
292
|
+
> 计算两点角度
|
|
293
|
+
|
|
294
|
+
```ts
|
|
295
|
+
libCoordsAngle({ x: 0, y: 0 }, { x: 1, y: 0 }); //0
|
|
296
|
+
libCoordsAngle({ x: 0, y: 0 }, { x: 1, y: 1 }); //45
|
|
297
|
+
libCoordsAngle({ x: 0, y: 0 }, { x: 0, y: 1 }); //90
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
### LibCoordsDistance-两点距离
|
|
301
|
+
|
|
302
|
+
> 计算两点距离
|
|
303
|
+
|
|
304
|
+
```ts
|
|
305
|
+
libCoordsDistance({ x: 0, y: 0 }, { x: 3, y: 4 }); //5
|
|
306
|
+
libCoordsDistance({ x: 1, y: 1 }, { x: 4, y: 5 }); //5
|
|
307
|
+
libCoordsDistance({ x: 0, y: 0 }, { x: 0, y: 0 }); //0
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
### LibDecimal-高精度计算
|
|
311
|
+
|
|
312
|
+
> 计算两个数的运算结果,并保留指定位数的小数
|
|
313
|
+
|
|
314
|
+
```ts
|
|
315
|
+
libDecimal(10, 3, "+"); //13
|
|
316
|
+
libDecimal(10, 3, "-"); //7
|
|
317
|
+
libDecimal(10, 3, "/", 2); //3.33
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
## Misc
|
|
321
|
+
|
|
322
|
+
### LibRegFormValidate-表单验证
|
|
323
|
+
|
|
324
|
+
> 通过传递对象数字的方式进行正则或自定义函数进行验证
|
|
325
|
+
|
|
326
|
+
```ts
|
|
327
|
+
const form = { username: "john", email: "john@example.com" };
|
|
328
|
+
const rules = [
|
|
329
|
+
{ key: "username", verify: /^[a-zA-Z0-9]{3,}$/, msg: "用户名不合法", name: "用户名" },
|
|
330
|
+
{ key: "email", verify: /^\S+@\S+\.\S+$/, msg: "邮箱格式不正确", name: "邮箱" },
|
|
331
|
+
];
|
|
332
|
+
libRegFormValidate(form, rules);
|
|
333
|
+
//返回结果: []
|
|
334
|
+
|
|
335
|
+
const invalidForm = { username: "jo", email: "invalid-email" };
|
|
336
|
+
libRegFormValidate(invalidForm, rules);
|
|
337
|
+
//返回结果: [
|
|
338
|
+
// { key: "username", msg: "用户名不合法", name: "用户名" },
|
|
339
|
+
// { key: "email", msg: "邮箱格式不正确", name: "邮箱" }
|
|
340
|
+
//]
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### LibRetryRequest-请求重连
|
|
344
|
+
|
|
345
|
+
> 请求失败重连
|
|
346
|
+
|
|
347
|
+
```ts
|
|
348
|
+
const requestFn = (params: { url: string }) => fetch(params.url).then(res => res.json());
|
|
349
|
+
const params = { url: "https://api.example.com/data" };
|
|
350
|
+
libRetryRequest({
|
|
351
|
+
promiseFn: requestFn,
|
|
352
|
+
params,
|
|
353
|
+
maxRetries: 5,
|
|
354
|
+
retryDelay: 1000
|
|
355
|
+
})
|
|
356
|
+
.then(data => console.log(data))
|
|
357
|
+
.catch(err => console.error(err));
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
## Random-随机
|
|
361
|
+
|
|
362
|
+
### LibProbabilityResult-概率触发
|
|
363
|
+
|
|
364
|
+
> 百分比概率结果
|
|
365
|
+
|
|
366
|
+
```ts
|
|
367
|
+
libProbabilityResult(50); //50% 概率为 true
|
|
368
|
+
libProbabilityResult(80); //80% 概率为 true
|
|
369
|
+
libProbabilityResult(100); //100% 概率为 true
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
### LibRandom-随机数
|
|
373
|
+
|
|
374
|
+
> 随机获取两个数之间的值,包含两数自身
|
|
375
|
+
|
|
376
|
+
```ts
|
|
377
|
+
libRandom(1, 10); //1 到 10 之间的随机整数
|
|
378
|
+
libRandom(1, 10, 2); //1 到 10 之间保留两位小数的随机数
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
### LibRandomColor-随机色
|
|
382
|
+
|
|
383
|
+
> 随机 RGBA 颜色
|
|
384
|
+
|
|
385
|
+
```ts
|
|
386
|
+
libRandomColor(); //生成随机的 RGBA 颜色,默认透明度 1
|
|
387
|
+
libRandomColor(0.5); //生成随机的 RGBA 颜色,透明度为 0.5
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
### LibUniqueRandomNumbers-随机数数组
|
|
391
|
+
|
|
392
|
+
> 随机生成指定个数、指定范围不重复的随机数数组
|
|
393
|
+
|
|
394
|
+
```ts
|
|
395
|
+
libUniqueRandomNumbers(1, 10, 5); //从 1 到 10 中随机生成 5 个唯一数字
|
|
396
|
+
libUniqueRandomNumbers(1, 100, 10); //从 1 到 100 中随机生成 10 个唯一数字
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
## Time-时间
|
|
400
|
+
|
|
401
|
+
### LibSameTimeCheck-时间比对
|
|
402
|
+
|
|
403
|
+
> 传入时间戳与当前时间判断是否为同一分、同一时、同一天、同一周、同一月、同一年
|
|
404
|
+
|
|
405
|
+
```ts
|
|
406
|
+
const timestamp = 1679872800000; //时间戳
|
|
407
|
+
const result = libSameTimeCheck(timestamp, 'day'); //判断是否为同一天
|
|
408
|
+
console.log(result); //0: 同一天, 1: 新的一天, -1: 时间戳大于当前时间
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
### LibTimeAgo-中文时间差
|
|
412
|
+
|
|
413
|
+
> 时间差计算
|
|
414
|
+
|
|
415
|
+
```ts
|
|
416
|
+
libTimeAgotamp(Date.now() - 3600000); //"1 小时前"
|
|
417
|
+
libTimeAgotamp(Date.now() - 86400000); //"1 天前"
|
|
418
|
+
libTimeAgotamp(Date.now() - 31536000000); //"1 年前"
|
|
419
|
+
libTimeAgotamp(Date.now() - 10000); //"刚刚"
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
### LibTimeGreeting-时间问候
|
|
423
|
+
|
|
424
|
+
> 根据当前时间返回问候语
|
|
425
|
+
|
|
426
|
+
```ts
|
|
427
|
+
libTimeGreeting(); //根据当前时间返回默认问候语
|
|
428
|
+
libTimeGreeting({ morning: "早安" }); //自定义早上问候语
|
|
429
|
+
libTimeGreeting({ afternoon: "午后好" }); //自定义下午问候语
|
|
33
430
|
```
|
|
34
431
|
|
|
@@ -3,8 +3,8 @@ export type LibGetDataTypeReturnType = "string" | "number" | "boolean" | "array"
|
|
|
3
3
|
* @description 返回数据类型
|
|
4
4
|
* @param v 需要判断类型的数据
|
|
5
5
|
* @example
|
|
6
|
-
* libGetDataType(123); //
|
|
7
|
-
* libGetDataType("hello"); //
|
|
8
|
-
* libGetDataType([1, 2, 3]); //
|
|
6
|
+
* libGetDataType(123); //"number"
|
|
7
|
+
* libGetDataType("hello"); //"string"
|
|
8
|
+
* libGetDataType([1, 2, 3]); //"array"
|
|
9
9
|
*/
|
|
10
10
|
export declare const libGetDataType: (v: any) => LibGetDataTypeReturnType;
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
* @description 返回数据类型
|
|
3
3
|
* @param v 需要判断类型的数据
|
|
4
4
|
* @example
|
|
5
|
-
* libGetDataType(123); //
|
|
6
|
-
* libGetDataType("hello"); //
|
|
7
|
-
* libGetDataType([1, 2, 3]); //
|
|
5
|
+
* libGetDataType(123); //"number"
|
|
6
|
+
* libGetDataType("hello"); //"string"
|
|
7
|
+
* libGetDataType([1, 2, 3]); //"array"
|
|
8
8
|
*/
|
|
9
9
|
export const libGetDataType = (v) => {
|
|
10
10
|
return Object.prototype.toString
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
* @param color 颜色
|
|
4
4
|
* @param logs 信息
|
|
5
5
|
* @example
|
|
6
|
-
*
|
|
6
|
+
* //使用红色打印日志
|
|
7
7
|
* libColorConsole("错误提示", "red", [{ label: "错误代码", value: 500 }]);
|
|
8
8
|
*
|
|
9
|
-
*
|
|
9
|
+
* //使用蓝色打印简单日志
|
|
10
10
|
* libColorConsole("信息", "blue", "操作成功");
|
|
11
11
|
*/
|
|
12
12
|
export declare const libColorConsole: (title: string, color: "red" | "orange" | "yellow" | "green" | "blue" | "purple", logs?: {
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
* @param color 颜色
|
|
4
4
|
* @param logs 信息
|
|
5
5
|
* @example
|
|
6
|
-
*
|
|
6
|
+
* //使用红色打印日志
|
|
7
7
|
* libColorConsole("错误提示", "red", [{ label: "错误代码", value: 500 }]);
|
|
8
8
|
*
|
|
9
|
-
*
|
|
9
|
+
* //使用蓝色打印简单日志
|
|
10
10
|
* libColorConsole("信息", "blue", "操作成功");
|
|
11
11
|
*/
|
|
12
12
|
export const libColorConsole = (title, color, logs = []) => {
|
|
@@ -18,7 +18,7 @@ export const libColorConsole = (title, color, logs = []) => {
|
|
|
18
18
|
blue: "#2980b9",
|
|
19
19
|
purple: "#be2edd",
|
|
20
20
|
};
|
|
21
|
-
|
|
21
|
+
//时间戳生成函数
|
|
22
22
|
const getTimestamp = () => {
|
|
23
23
|
const now = new Date();
|
|
24
24
|
const hour = String(now.getHours()).padStart(2, "0");
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/** @description 获取浏览器地址栏参数
|
|
2
2
|
* @example
|
|
3
3
|
* const params = libPathParams();
|
|
4
|
-
* console.log(params); //
|
|
4
|
+
* console.log(params); //{ param1: "value1", param2: "value2" }
|
|
5
5
|
*/
|
|
6
6
|
export declare const libPathParams: () => Record<string, string>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/** @description 获取浏览器地址栏参数
|
|
2
2
|
* @example
|
|
3
3
|
* const params = libPathParams();
|
|
4
|
-
* console.log(params); //
|
|
4
|
+
* console.log(params); //{ param1: "value1", param2: "value2" }
|
|
5
5
|
*/
|
|
6
6
|
export const libPathParams = () => {
|
|
7
7
|
const v = location.href;
|
|
@@ -4,6 +4,6 @@
|
|
|
4
4
|
* @param chunkSize 每个数组的元素数量
|
|
5
5
|
* @example
|
|
6
6
|
* const chunks = libChunkArray([1, 2, 3, 4, 5, 6], 2);
|
|
7
|
-
* console.log(chunks); //
|
|
7
|
+
* console.log(chunks); //[[1, 2], [3, 4], [5, 6]]
|
|
8
8
|
*/
|
|
9
9
|
export declare const libChunkArray: <T>(arr: T[], chunkSize: number) => T[][];
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @param chunkSize 每个数组的元素数量
|
|
5
5
|
* @example
|
|
6
6
|
* const chunks = libChunkArray([1, 2, 3, 4, 5, 6], 2);
|
|
7
|
-
* console.log(chunks); //
|
|
7
|
+
* console.log(chunks); //[[1, 2], [3, 4], [5, 6]]
|
|
8
8
|
*/
|
|
9
9
|
export const libChunkArray = (arr, chunkSize) => {
|
|
10
10
|
const result = [];
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/** @description 递归将JSON字符串深度解析为对象
|
|
2
2
|
* @example
|
|
3
3
|
* const obj = libDeepJSONParse('{"a": 1, "b": "{\"c\": 2}"}');
|
|
4
|
-
* console.log(obj); //
|
|
4
|
+
* console.log(obj); //{ a: 1, b: { c: 2 } }
|
|
5
5
|
*/
|
|
6
6
|
export declare const libDeepJSONParse: <T>(data: any) => T;
|
|
@@ -1,32 +1,32 @@
|
|
|
1
1
|
/** @description 递归将JSON字符串深度解析为对象
|
|
2
2
|
* @example
|
|
3
3
|
* const obj = libDeepJSONParse('{"a": 1, "b": "{\"c\": 2}"}');
|
|
4
|
-
* console.log(obj); //
|
|
4
|
+
* console.log(obj); //{ a: 1, b: { c: 2 } }
|
|
5
5
|
*/
|
|
6
6
|
export const libDeepJSONParse = (data) => {
|
|
7
|
-
|
|
7
|
+
//检查是否为字符串并尝试解析
|
|
8
8
|
if (typeof data === "string") {
|
|
9
9
|
try {
|
|
10
10
|
const parsed = JSON.parse(data);
|
|
11
|
-
|
|
11
|
+
//递归解析解析后的结果
|
|
12
12
|
return libDeepJSONParse(parsed);
|
|
13
13
|
}
|
|
14
14
|
catch {
|
|
15
|
-
|
|
15
|
+
//如果解析失败,返回原始字符串
|
|
16
16
|
return data;
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
|
-
|
|
19
|
+
//如果是数组,递归处理每个元素
|
|
20
20
|
if (Array.isArray(data)) {
|
|
21
21
|
return data.map((item) => libDeepJSONParse(item));
|
|
22
22
|
}
|
|
23
|
-
|
|
23
|
+
//如果是对象,递归处理每个属性值
|
|
24
24
|
if (data !== null && typeof data === "object") {
|
|
25
25
|
return Object.keys(data).reduce((acc, key) => {
|
|
26
26
|
acc[key] = libDeepJSONParse(data[key]);
|
|
27
27
|
return acc;
|
|
28
28
|
}, {});
|
|
29
29
|
}
|
|
30
|
-
|
|
30
|
+
//其他情况返回原始值
|
|
31
31
|
return data;
|
|
32
32
|
};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @description 分类汇总,将数组对象按照指定键值整理成一个以键值为键名的对象
|
|
3
|
-
* @param arr
|
|
4
|
-
* @param key
|
|
5
|
-
* @returns
|
|
3
|
+
* @param arr 要分组的数组
|
|
4
|
+
* @param key 分组的键
|
|
5
|
+
* @returns 分组后的对象
|
|
6
6
|
* @example
|
|
7
7
|
* const grouped = libGroupArrayByKey([{ id: 1, name: 'A' }, { id: 2, name: 'B' }, { id: 1, name: 'C' }], 'id');
|
|
8
|
-
* console.log(grouped); //
|
|
8
|
+
* console.log(grouped); //{ 1: [{ id: 1, name: 'A' }, { id: 1, name: 'C' }], 2: [{ id: 2, name: 'B' }] }
|
|
9
9
|
*/
|
|
10
10
|
export declare const libGroupArrayByKey: (arr: any[] | undefined, key: string) => any;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @description 分类汇总,将数组对象按照指定键值整理成一个以键值为键名的对象
|
|
3
|
-
* @param arr
|
|
4
|
-
* @param key
|
|
5
|
-
* @returns
|
|
3
|
+
* @param arr 要分组的数组
|
|
4
|
+
* @param key 分组的键
|
|
5
|
+
* @returns 分组后的对象
|
|
6
6
|
* @example
|
|
7
7
|
* const grouped = libGroupArrayByKey([{ id: 1, name: 'A' }, { id: 2, name: 'B' }, { id: 1, name: 'C' }], 'id');
|
|
8
|
-
* console.log(grouped); //
|
|
8
|
+
* console.log(grouped); //{ 1: [{ id: 1, name: 'A' }, { id: 1, name: 'C' }], 2: [{ id: 2, name: 'B' }] }
|
|
9
9
|
*/
|
|
10
10
|
export const libGroupArrayByKey = (arr = [], key) => {
|
|
11
11
|
return key
|
|
@@ -5,6 +5,6 @@
|
|
|
5
5
|
* @returns 匹配结果数组
|
|
6
6
|
* @example
|
|
7
7
|
* const emails = libMatchEmail("user", ["@gmail.com", "@yahoo.com"]);
|
|
8
|
-
* console.log(emails); //
|
|
8
|
+
* console.log(emails); //["user@gmail.com", "user@yahoo.com"]
|
|
9
9
|
*/
|
|
10
10
|
export declare const libMatchEmail: (str: string, emailList: string[]) => string[];
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @returns 匹配结果数组
|
|
6
6
|
* @example
|
|
7
7
|
* const emails = libMatchEmail("user", ["@gmail.com", "@yahoo.com"]);
|
|
8
|
-
* console.log(emails); //
|
|
8
|
+
* console.log(emails); //["user@gmail.com", "user@yahoo.com"]
|
|
9
9
|
*/
|
|
10
10
|
export const libMatchEmail = (str, emailList) => {
|
|
11
11
|
return str.includes("@")
|