lyb-js 1.0.7 → 1.0.8
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 +143 -144
- package/dist/Base/LibJsGetDataType/index.d.ts +10 -0
- package/dist/Base/LibJsGetDataType/index.js +15 -0
- package/dist/Base/LibJsPromiseTimeout/index.d.ts +10 -0
- package/dist/Base/LibJsPromiseTimeout/index.js +40 -0
- package/dist/Browser/LibJsColorConsole/index.d.ts +15 -0
- package/dist/Browser/LibJsColorConsole/index.js +38 -0
- package/dist/Browser/LibJsIsMobile/index.d.ts +6 -0
- package/dist/Browser/LibJsIsMobile/index.js +9 -0
- package/dist/Browser/LibJsIsPad/index.d.ts +6 -0
- package/dist/Browser/LibJsIsPad/index.js +15 -0
- package/dist/Browser/LibJsPathParams/index.d.ts +6 -0
- package/dist/Browser/LibJsPathParams/index.js +16 -0
- package/dist/Browser/LibJsSetTitleIcon/index.d.ts +7 -0
- package/dist/Browser/LibJsSetTitleIcon/index.js +14 -0
- package/dist/Browser/LibJsTagTitleTip/index.d.ts +7 -0
- package/dist/Browser/LibJsTagTitleTip/index.js +24 -0
- package/dist/Data/LibJsChunkArray/index.d.ts +9 -0
- package/dist/Data/LibJsChunkArray/index.js +15 -0
- package/dist/Data/LibJsDeepJSONParse/index.d.ts +6 -0
- package/dist/Data/LibJsDeepJSONParse/index.js +32 -0
- package/dist/Data/LibJsGroupArrayByKey/index.d.ts +10 -0
- package/dist/Data/LibJsGroupArrayByKey/index.js +20 -0
- package/dist/Data/LibJsMatchEmail/index.d.ts +10 -0
- package/dist/Data/LibJsMatchEmail/index.js +16 -0
- package/dist/Data/LibJsShuffleArray/index.d.ts +7 -0
- package/dist/Data/LibJsShuffleArray/index.js +14 -0
- package/dist/Data/LibJsStepArray/index.d.ts +8 -0
- package/dist/Data/LibJsStepArray/index.js +14 -0
- package/dist/File/LibJsDownloadImageLink/index.d.ts +7 -0
- package/dist/File/LibJsDownloadImageLink/index.js +17 -0
- package/dist/File/LibJsImageOptimizer/index.d.ts +31 -0
- package/dist/File/LibJsImageOptimizer/index.js +82 -0
- package/dist/File/LibJsSaveJson/index.d.ts +9 -0
- package/dist/File/LibJsSaveJson/index.js +17 -0
- package/dist/Formatter/LibJsFormatterByte/index.d.ts +9 -0
- package/dist/Formatter/LibJsFormatterByte/index.js +17 -0
- package/dist/Formatter/LibJsMaskPhoneNumber/index.d.ts +8 -0
- package/dist/Formatter/LibJsMaskPhoneNumber/index.js +11 -0
- package/dist/Formatter/LibJsNumComma/index.d.ts +9 -0
- package/dist/Formatter/LibJsNumComma/index.js +13 -0
- package/dist/Formatter/LibJsNumberUnit/index.d.ts +12 -0
- package/dist/Formatter/LibJsNumberUnit/index.js +23 -0
- package/dist/Formatter/LibJsSecondsFormatterChinese/index.d.ts +11 -0
- package/dist/Formatter/LibJsSecondsFormatterChinese/index.js +42 -0
- package/dist/Math/LibJsCalculateExpression/index.d.ts +9 -0
- package/dist/Math/LibJsCalculateExpression/index.js +107 -0
- package/dist/Math/LibJsConvertAngle/index.d.ts +12 -0
- package/dist/Math/LibJsConvertAngle/index.js +22 -0
- package/dist/Math/LibJsCoordsAngle/index.d.ts +15 -0
- package/dist/Math/LibJsCoordsAngle/index.js +24 -0
- package/dist/Math/LibJsCoordsDistance/index.d.ts +15 -0
- package/dist/Math/LibJsCoordsDistance/index.js +16 -0
- package/dist/Math/LibJsDecimal/index.d.ts +10 -0
- package/dist/Math/LibJsDecimal/index.js +25 -0
- package/dist/Misc/LibJsRegFormValidate/index.d.ts +32 -0
- package/dist/Misc/LibJsRegFormValidate/index.js +34 -0
- package/dist/Misc/LibJsRetryRequest/index.d.ts +24 -0
- package/dist/Misc/LibJsRetryRequest/index.js +37 -0
- package/dist/Random/LibJsProbabilityResult/index.d.ts +8 -0
- package/dist/Random/LibJsProbabilityResult/index.js +8 -0
- package/dist/Random/LibJsRandom/index.d.ts +9 -0
- package/dist/Random/LibJsRandom/index.js +11 -0
- package/dist/Random/LibJsRandomColor/index.d.ts +7 -0
- package/dist/Random/LibJsRandomColor/index.js +12 -0
- package/dist/Random/LibJsUniqueRandomNumbers/index.d.ts +9 -0
- package/dist/Random/LibJsUniqueRandomNumbers/index.js +16 -0
- package/dist/Time/LibJsSameTimeCheck/index.d.ts +12 -0
- package/dist/Time/LibJsSameTimeCheck/index.js +24 -0
- package/dist/Time/LibJsTimeAgo/index.d.ts +9 -0
- package/dist/Time/LibJsTimeAgo/index.js +27 -0
- package/dist/Time/LibJsTimeGreeting/index.d.ts +17 -0
- package/dist/Time/LibJsTimeGreeting/index.js +23 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/libJs.d.ts +394 -0
- package/dist/libJs.js +404 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,286 +11,285 @@
|
|
|
11
11
|
> 完整使用
|
|
12
12
|
|
|
13
13
|
```ts
|
|
14
|
-
import {
|
|
14
|
+
import { LibJs } from "lib-js-utils";
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
LibJs.Base.libJsGetDataType("Hellow World!"); //"string"
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
> 按需引入,打包时就不会把整个库打进去
|
|
20
20
|
|
|
21
21
|
```ts
|
|
22
|
-
import {
|
|
22
|
+
import { libJsGetDataType } from "lyb-js/dist/Base/LibJsGetDataType";
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
libJsGetDataType("Hellow World!"); //"string"
|
|
25
25
|
```
|
|
26
26
|
|
|
27
27
|
> 如果在多个文件使用到同一个方法,建议采用按需引入聚合导出
|
|
28
28
|
|
|
29
29
|
```ts
|
|
30
30
|
//你的公共工具函数文件 utils.ts
|
|
31
|
-
export * from "lyb-js/dist/Base/
|
|
32
|
-
export * from "lyb-js/dist/Math/
|
|
31
|
+
export * from "lyb-js/dist/Base/LibJsGetDataType";
|
|
32
|
+
export * from "lyb-js/dist/Math/LibJsCalculateExpression";
|
|
33
33
|
|
|
34
34
|
//你的项目文件 index.ts
|
|
35
|
-
import {
|
|
35
|
+
import { libJsGetDataType,libJsCalculateExpression } from "utils";
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
libJsGetDataType("Hellow World!"); //"string"
|
|
38
|
+
libJsCalculateExpression("(1+2)-(3*4)/5"); //0.6
|
|
39
39
|
```
|
|
40
40
|
|
|
41
41
|
## 目录
|
|
42
42
|
|
|
43
43
|
### 基础
|
|
44
44
|
|
|
45
|
-
\- [
|
|
45
|
+
\- [LibJsGetDataType-数据类型](#LibJsGetDataType-数据类型)
|
|
46
46
|
|
|
47
|
-
\- [
|
|
47
|
+
\- [LibJsPromiseTimeout-延时执行](#LibJsPromiseTimeout-延时执行)
|
|
48
48
|
|
|
49
49
|
|
|
50
50
|
### Browser-浏览器
|
|
51
51
|
|
|
52
|
-
\- [
|
|
52
|
+
\- [LibJsColorConsole-有色打印](#LibJsColorConsole-有色打印)
|
|
53
53
|
|
|
54
|
-
\- [
|
|
54
|
+
\- [LibJsIsMobile-判断手机](#LibJsIsMobile-判断手机)
|
|
55
55
|
|
|
56
|
-
\- [
|
|
56
|
+
\- [LibJsIsPad-判断平板](#LibJsIsPad-判断平板)
|
|
57
57
|
|
|
58
|
-
\- [
|
|
58
|
+
\- [LibJsPathParams-地址栏参数](#LibJsPathParams-地址栏参数)
|
|
59
59
|
|
|
60
|
-
\- [
|
|
60
|
+
\- [LibJsSetTitleIcon-网站标题图标](#LibJsSetTitleIcon-网站标题图标)
|
|
61
61
|
|
|
62
|
-
\- [
|
|
62
|
+
\- [LibJsTagTitleTip-网站标题交互](#LibJsTagTitleTip-网站标题交互)
|
|
63
63
|
|
|
64
64
|
|
|
65
65
|
### Data-数据
|
|
66
66
|
|
|
67
|
-
\- [
|
|
67
|
+
\- [LibJsChunkArray-数组拆分](#LibJsChunkArray-数组拆分)
|
|
68
68
|
|
|
69
|
-
\- [
|
|
69
|
+
\- [LibJsDeepJSONParse-深度解析JSON](#LibJsDeepJSONParse-深度解析JSON)
|
|
70
70
|
|
|
71
|
-
\- [
|
|
71
|
+
\- [LibJsGroupArrayByKey-分类汇总](#LibJsGroupArrayByKey-分类汇总)
|
|
72
72
|
|
|
73
|
-
\- [
|
|
73
|
+
\- [LibJsMatchEmail-匹配E-Mail](#LibJsMatchEmail-匹配E-Mail)
|
|
74
74
|
|
|
75
|
-
\- [
|
|
75
|
+
\- [LibJsShuffleArray-数组乱序](#LibJsShuffleArray-数组乱序)
|
|
76
76
|
|
|
77
|
-
\- [
|
|
77
|
+
\- [LibJsStepArray-数组偏移](#LibJsStepArray-数组偏移)
|
|
78
78
|
|
|
79
79
|
|
|
80
80
|
### File-文件
|
|
81
81
|
|
|
82
|
-
\- [
|
|
82
|
+
\- [LibJsDownloadImageLink-图片下载](#LibJsDownloadImageLink-图片下载)
|
|
83
83
|
|
|
84
|
-
\- [
|
|
84
|
+
\- [LibJsImageOptimizer-图片压缩](#LibJsImageOptimizer-图片压缩)
|
|
85
85
|
|
|
86
|
-
\- [
|
|
86
|
+
\- [LibJsSaveJson-保存文件](#LibJsSaveJson-保存文件)
|
|
87
87
|
|
|
88
88
|
|
|
89
89
|
### Formatter-格式化
|
|
90
90
|
|
|
91
|
-
\- [
|
|
91
|
+
\- [LibJsFormatterByte-字节格式化](#LibJsFormatterByte-字节格式化)
|
|
92
92
|
|
|
93
|
-
\- [
|
|
93
|
+
\- [LibJsMaskPhoneNumber-隐藏手机号码](#LibJsMaskPhoneNumber-隐藏手机号码)
|
|
94
94
|
|
|
95
|
-
\- [
|
|
95
|
+
\- [LibJsNumberUnit-数字单位](#LibJsNumberUnit-数字单位)
|
|
96
96
|
|
|
97
|
-
\- [
|
|
97
|
+
\- [LibJsNumComma-数字逗号](#LibJsNumComma-数字逗号)
|
|
98
98
|
|
|
99
|
-
\- [
|
|
99
|
+
\- [LibJsSecondsFormatterChinese-中文时间](#LibJsSecondsFormatterChinese-中文时间)
|
|
100
100
|
|
|
101
101
|
|
|
102
102
|
### Math-数学
|
|
103
103
|
|
|
104
|
-
\- [
|
|
104
|
+
\- [LibJsCalculateExpression-表达式字符串](#LibJsCalculateExpression-表达式字符串)
|
|
105
105
|
|
|
106
|
-
\- [
|
|
106
|
+
\- [LibJsConvertAngle-角弧度互转](#LibJsConvertAngle-角弧度互转)
|
|
107
107
|
|
|
108
|
-
\- [
|
|
108
|
+
\- [LibJsCoordsAngle-两点角度](#LibJsCoordsAngle-两点角度)
|
|
109
109
|
|
|
110
|
-
\- [
|
|
111
|
-
|
|
112
|
-
\- [LibDecimal-高精度计算](#LibDecimal-高精度计算)
|
|
110
|
+
\- [LibJsCoordsDistance-两点距离](#LibJsCoordsDistance-两点距离)
|
|
113
111
|
|
|
112
|
+
\- [LibJsDecimal-高精度计算](#LibJsDecimal-高精度计算)
|
|
114
113
|
|
|
115
114
|
### Misc-杂项
|
|
116
115
|
|
|
117
|
-
\- [
|
|
116
|
+
\- [LibJsRegFormValidate-表单验证](#LibJsRegFormValidate-表单验证)
|
|
118
117
|
|
|
119
|
-
\- [
|
|
118
|
+
\- [LibJsRetryRequest-请求重连](#LibJsRetryRequest-请求重连)
|
|
120
119
|
|
|
121
120
|
|
|
122
121
|
### Random-随机
|
|
123
122
|
|
|
124
|
-
\- [
|
|
123
|
+
\- [LibJsProbabilityResult-概率触发](#LibJsProbabilityResult-概率触发)
|
|
125
124
|
|
|
126
|
-
\- [
|
|
125
|
+
\- [LibJsRandom-随机数](#LibJsRandom-随机数)
|
|
127
126
|
|
|
128
|
-
\- [
|
|
127
|
+
\- [LibJsRandomColor-随机色](#LibJsRandomColor-随机色)
|
|
129
128
|
|
|
130
|
-
\- [
|
|
129
|
+
\- [LibJsUniqueRandomNumbers-随机数数组](#LibJsUniqueRandomNumbers-随机数数组)
|
|
131
130
|
|
|
132
131
|
|
|
133
132
|
### Time-时间
|
|
134
133
|
|
|
135
|
-
\- [
|
|
134
|
+
\- [LibJsSameTimeCheck-时间比对](#LibJsSameTimeCheck-时间比对)
|
|
136
135
|
|
|
137
|
-
\- [
|
|
136
|
+
\- [LibJsTimeAgo-中文时间差](#LibJsTimeAgo-中文时间差)
|
|
138
137
|
|
|
139
|
-
\- [
|
|
138
|
+
\- [LibJsTimeGreeting-时间问候](#LibJsTimeGreeting-时间问候)
|
|
140
139
|
|
|
141
140
|
|
|
142
141
|
## Base-基础
|
|
143
142
|
|
|
144
|
-
###
|
|
143
|
+
### LibJsGetDataType-数据类型
|
|
145
144
|
|
|
146
145
|
> 返回数据类型
|
|
147
146
|
|
|
148
147
|
```ts
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
148
|
+
libJsGetDataType(123); //"number"
|
|
149
|
+
libJsGetDataType("hello"); //"string"
|
|
150
|
+
libJsGetDataType([1, 2, 3]); //"array"
|
|
152
151
|
```
|
|
153
152
|
|
|
154
|
-
###
|
|
153
|
+
### LibJsPromiseTimeout-延时执行
|
|
155
154
|
|
|
156
155
|
> 延时执行,切换到其他页面会暂停
|
|
157
156
|
|
|
158
157
|
```ts
|
|
159
|
-
|
|
158
|
+
libJsPromiseTimeout(3000, () => {
|
|
160
159
|
console.log("执行延时函数");
|
|
161
160
|
});
|
|
162
161
|
```
|
|
163
162
|
|
|
164
163
|
## Browser-浏览器
|
|
165
164
|
|
|
166
|
-
###
|
|
165
|
+
### LibJsColorConsole-有色打印
|
|
167
166
|
|
|
168
167
|
> `console`有色打印
|
|
169
168
|
|
|
170
169
|
```ts
|
|
171
170
|
//使用红色打印日志
|
|
172
|
-
|
|
171
|
+
libJsColorConsole("错误提示", "red", [{ label: "错误代码", value: 500 }]);
|
|
173
172
|
|
|
174
173
|
//使用蓝色打印简单日志
|
|
175
|
-
|
|
174
|
+
libJsColorConsole("信息", "blue", "操作成功");
|
|
176
175
|
```
|
|
177
176
|
|
|
178
|
-
###
|
|
177
|
+
### LibJsIsMobile-判断手机
|
|
179
178
|
|
|
180
179
|
> 判断是否为移动设备
|
|
181
180
|
|
|
182
181
|
```ts
|
|
183
|
-
const isMobile =
|
|
182
|
+
const isMobile = libJsIsMobile();
|
|
184
183
|
console.log(isMobile); //true 或 false
|
|
185
184
|
```
|
|
186
185
|
|
|
187
|
-
###
|
|
186
|
+
### LibJsIsPad-判断平板
|
|
188
187
|
|
|
189
188
|
> 判断是否为平板
|
|
190
189
|
|
|
191
190
|
```ts
|
|
192
|
-
const isPad =
|
|
191
|
+
const isPad = libJsIsPad();
|
|
193
192
|
console.log(isPad); //true 或 false
|
|
194
193
|
```
|
|
195
194
|
|
|
196
|
-
###
|
|
195
|
+
### LibJsPathParams-地址栏参数
|
|
197
196
|
|
|
198
197
|
> 获取浏览器地址栏参数
|
|
199
198
|
|
|
200
199
|
```ts
|
|
201
|
-
const params =
|
|
200
|
+
const params = libJsPathParams();
|
|
202
201
|
console.log(params); //{ param1: "value1", param2: "value2" }
|
|
203
202
|
```
|
|
204
203
|
|
|
205
|
-
###
|
|
204
|
+
### LibJsSetTitleIcon-网站标题图标
|
|
206
205
|
|
|
207
206
|
> 动态设置网站标题及图标,涉及到不同平台的打包,可以根据不同环境来设置网站标题和图标
|
|
208
207
|
|
|
209
208
|
```ts
|
|
210
|
-
|
|
209
|
+
libJsSetTitleIcon("我的网站", "https://example.com/favicon.ico");
|
|
211
210
|
```
|
|
212
211
|
|
|
213
|
-
###
|
|
212
|
+
### LibJsTagTitleTip-网站标题交互
|
|
214
213
|
|
|
215
214
|
> 网站标题交互,当从当前网页切换到其他网页,网站标题自动切换
|
|
216
215
|
|
|
217
216
|
```ts
|
|
218
|
-
|
|
217
|
+
libJsTagTitleTip("欢迎回来", "来和妲己玩耍吧!");
|
|
219
218
|
```
|
|
220
219
|
|
|
221
220
|
## Data-数据
|
|
222
221
|
|
|
223
|
-
###
|
|
222
|
+
### LibJsChunkArray-数组拆分
|
|
224
223
|
|
|
225
224
|
> 将数组拆分成指定数组元素数量的多个数组
|
|
226
225
|
|
|
227
226
|
```ts
|
|
228
|
-
const chunks =
|
|
227
|
+
const chunks = libJsChunkArray([1, 2, 3, 4, 5, 6], 2);
|
|
229
228
|
console.log(chunks); //[[1, 2], [3, 4], [5, 6]]
|
|
230
229
|
```
|
|
231
230
|
|
|
232
|
-
###
|
|
231
|
+
### LibJsDeepJSONParse-深度解析JSON
|
|
233
232
|
|
|
234
233
|
> 递归将JSON字符串深度解析为对象
|
|
235
234
|
|
|
236
235
|
```ts
|
|
237
|
-
const obj =
|
|
236
|
+
const obj = libJsDeepJSONParse('{"a": 1, "b": "{\"c\": 2}"}');
|
|
238
237
|
console.log(obj); //{ a: 1, b: { c: 2 } }
|
|
239
238
|
```
|
|
240
239
|
|
|
241
|
-
###
|
|
240
|
+
### LibJsGroupArrayByKey-分类汇总
|
|
242
241
|
|
|
243
242
|
> 将数组对象按照指定键值整理成一个以键值为键名的对象
|
|
244
243
|
|
|
245
244
|
```ts
|
|
246
|
-
const grouped =
|
|
245
|
+
const grouped = libJsGroupArrayByKey([{ id: 1, name: 'A' }, { id: 2, name: 'B' }, { id: 1, name: 'C' }], 'id');
|
|
247
246
|
console.log(grouped); //{ 1: [{ id: 1, name: 'A' }, { id: 1, name: 'C' }], 2: [{ id: 2, name: 'B' }] }
|
|
248
247
|
```
|
|
249
248
|
|
|
250
|
-
###
|
|
249
|
+
### LibJsMatchEmail-匹配E-Mail
|
|
251
250
|
|
|
252
251
|
> 可用于实时输入时,自动补全常用邮箱后缀
|
|
253
252
|
|
|
254
253
|
```ts
|
|
255
|
-
const emails =
|
|
254
|
+
const emails = libJsMatchEmail("user", ["@gmail.com", "@yahoo.com"]);
|
|
256
255
|
console.log(emails); //["user@gmail.com", "user@yahoo.com"]
|
|
257
256
|
```
|
|
258
257
|
|
|
259
|
-
###
|
|
258
|
+
### LibJsShuffleArray-数组乱序
|
|
260
259
|
|
|
261
260
|
> 将数组打乱顺序
|
|
262
261
|
|
|
263
262
|
```ts
|
|
264
|
-
const shuffled =
|
|
263
|
+
const shuffled = libJsShuffleArray([1, 2, 3, 4, 5]);
|
|
265
264
|
console.log(shuffled); //[3, 5, 2, 1, 4] (结果每次不同)
|
|
266
265
|
```
|
|
267
266
|
|
|
268
|
-
###
|
|
267
|
+
### LibJsStepArray-数组偏移
|
|
269
268
|
|
|
270
269
|
> 数组元素整体步数移动
|
|
271
270
|
|
|
272
271
|
```ts
|
|
273
|
-
const moved =
|
|
272
|
+
const moved = libJsStepArray([1, 2, 3, 4, 5], 2);
|
|
274
273
|
console.log(moved); //[4, 5, 1, 2, 3]
|
|
275
274
|
```
|
|
276
275
|
|
|
277
276
|
## File-文件
|
|
278
277
|
|
|
279
|
-
###
|
|
278
|
+
### LibJsDownloadImageLink-图片下载
|
|
280
279
|
|
|
281
280
|
> 将链接图片下载到本地
|
|
282
281
|
|
|
283
282
|
```ts
|
|
284
|
-
|
|
283
|
+
libJsDownloadImageLink("https://example.com/image.jpg", "图片.jpg");
|
|
285
284
|
```
|
|
286
285
|
|
|
287
|
-
###
|
|
286
|
+
### LibJsImageOptimizer-图片压缩
|
|
288
287
|
|
|
289
288
|
> 支持`png`压缩,保留透明背景
|
|
290
289
|
|
|
291
290
|
```ts
|
|
292
291
|
//图片压缩使用示例
|
|
293
|
-
|
|
292
|
+
libJsImageOptimizerOptionsParams({
|
|
294
293
|
file: myFile,
|
|
295
294
|
ratio: 0.8,
|
|
296
295
|
width: 800,
|
|
@@ -304,123 +303,123 @@ libImageOptimizerOptionsParams({
|
|
|
304
303
|
});
|
|
305
304
|
```
|
|
306
305
|
|
|
307
|
-
###
|
|
306
|
+
### LibJsSaveJson-保存文件
|
|
308
307
|
|
|
309
308
|
> 保存`JSON`文件到本地,也支持保存纯文本的`txt`文件
|
|
310
309
|
|
|
311
310
|
```ts
|
|
312
|
-
|
|
313
|
-
|
|
311
|
+
libJsSaveJson(JSON.stringify({ key: "value" }), "example.json");、
|
|
312
|
+
libJsSaveJson("Hellow World!", "example.txt");
|
|
314
313
|
```
|
|
315
314
|
|
|
316
315
|
## Formatter-格式化
|
|
317
316
|
|
|
318
|
-
###
|
|
317
|
+
### LibJsFormatterByte-字节格式化
|
|
319
318
|
|
|
320
319
|
> 将字节单位的数字格式化
|
|
321
320
|
|
|
322
321
|
```ts
|
|
323
|
-
const [size, unit, formatted] =
|
|
322
|
+
const [size, unit, formatted] = libJsFormatterByte(2048);
|
|
324
323
|
console.log(size, unit, formatted); //2.00 KB 2.00 KB
|
|
325
324
|
```
|
|
326
325
|
|
|
327
|
-
###
|
|
326
|
+
### LibJsMaskPhoneNumber-隐藏手机号码
|
|
328
327
|
|
|
329
328
|
> 隐藏手机号码中间的四位数字
|
|
330
329
|
|
|
331
330
|
```ts
|
|
332
|
-
const masked =
|
|
331
|
+
const masked = libJsMaskPhoneNumber("13812345678");
|
|
333
332
|
console.log(masked); //138****5678
|
|
334
333
|
```
|
|
335
334
|
|
|
336
|
-
###
|
|
335
|
+
### LibJsNumberUnit-数字单位
|
|
337
336
|
|
|
338
337
|
> 大于指定数字,用单位标识,你甚至可以用中文
|
|
339
338
|
|
|
340
339
|
```ts
|
|
341
|
-
const [value, unit] =
|
|
340
|
+
const [value, unit] = libJsNumberUnit(1500, { K: 1000, M: 1000000 });
|
|
342
341
|
console.log(value, unit); //1.50 K
|
|
343
342
|
|
|
344
|
-
const [value, unit] =
|
|
343
|
+
const [value, unit] = libJsNumberUnit(0.05, { 分: 0.01, 角: 0.1, 元: 1 });
|
|
345
344
|
console.log(value, unit); //0.05 分
|
|
346
345
|
```
|
|
347
346
|
|
|
348
|
-
###
|
|
347
|
+
### LibJsNumComma-数字逗号
|
|
349
348
|
|
|
350
349
|
> 数字每三位添加逗号
|
|
351
350
|
|
|
352
351
|
```ts
|
|
353
|
-
const formatted =
|
|
352
|
+
const formatted = libJsNumComma(1234567.89);
|
|
354
353
|
console.log(formatted); //1,234,567.89
|
|
355
354
|
```
|
|
356
355
|
|
|
357
|
-
###
|
|
356
|
+
### LibJsSecondsFormatterChinese-中文时间
|
|
358
357
|
|
|
359
358
|
> 将秒数格式化为中文时间描述,支持扩展到年
|
|
360
359
|
|
|
361
360
|
```ts
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
361
|
+
libJsSecondsFormatterChinese(100000); //"1天3小时46分40秒"
|
|
362
|
+
libJsSecondsFormatterChinese(31536000); //"1年"
|
|
363
|
+
libJsSecondsFormatterChinese(3600); //"1小时"
|
|
364
|
+
libJsSecondsFormatterChinese(90); //"1分30秒"
|
|
366
365
|
```
|
|
367
366
|
|
|
368
367
|
## Math-数学
|
|
369
368
|
|
|
370
|
-
###
|
|
369
|
+
### LibJsCalculateExpression-表达式字符串
|
|
371
370
|
|
|
372
371
|
> 计算表达式字符串
|
|
373
372
|
|
|
374
373
|
```ts
|
|
375
|
-
const result =
|
|
374
|
+
const result = libJsCalculateExpression("(1+2)-(3*4)/5");
|
|
376
375
|
console.log(result); //0.6
|
|
377
376
|
```
|
|
378
377
|
|
|
379
|
-
###
|
|
378
|
+
### LibJsConvertAngle-角弧度互转
|
|
380
379
|
|
|
381
380
|
> 角度和弧度互相转换
|
|
382
381
|
|
|
383
382
|
```ts
|
|
384
383
|
//角度转弧度
|
|
385
|
-
|
|
384
|
+
libJsConvertAngle(90, "rad"); //返回 1.5708... (π/2)
|
|
386
385
|
|
|
387
386
|
//弧度转角度
|
|
388
|
-
|
|
387
|
+
libJsConvertAngle(Math.PI, "deg"); //返回 180
|
|
389
388
|
```
|
|
390
389
|
|
|
391
|
-
###
|
|
390
|
+
### LibJsCoordsAngle-两点角度
|
|
392
391
|
|
|
393
392
|
> 计算两点角度
|
|
394
393
|
|
|
395
394
|
```ts
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
395
|
+
libJsCoordsAngle({ x: 0, y: 0 }, { x: 1, y: 0 }); //0
|
|
396
|
+
libJsCoordsAngle({ x: 0, y: 0 }, { x: 1, y: 1 }); //45
|
|
397
|
+
libJsCoordsAngle({ x: 0, y: 0 }, { x: 0, y: 1 }); //90
|
|
399
398
|
```
|
|
400
399
|
|
|
401
|
-
###
|
|
400
|
+
### LibJsCoordsDistance-两点距离
|
|
402
401
|
|
|
403
402
|
> 计算两点距离
|
|
404
403
|
|
|
405
404
|
```ts
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
405
|
+
libJsCoordsDistance({ x: 0, y: 0 }, { x: 3, y: 4 }); //5
|
|
406
|
+
libJsCoordsDistance({ x: 1, y: 1 }, { x: 4, y: 5 }); //5
|
|
407
|
+
libJsCoordsDistance({ x: 0, y: 0 }, { x: 0, y: 0 }); //0
|
|
409
408
|
```
|
|
410
409
|
|
|
411
|
-
###
|
|
410
|
+
### LibJsDecimal-高精度计算
|
|
412
411
|
|
|
413
412
|
> 计算两个数的运算结果,并保留指定位数的小数
|
|
414
413
|
|
|
415
414
|
```ts
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
415
|
+
libJsDecimal(10, 3, "+"); //13
|
|
416
|
+
libJsDecimal(10, 3, "-"); //7
|
|
417
|
+
libJsDecimal(10, 3, "/", 2); //3.33
|
|
419
418
|
```
|
|
420
419
|
|
|
421
420
|
## Misc
|
|
422
421
|
|
|
423
|
-
###
|
|
422
|
+
### LibJsRegFormValidate-表单验证
|
|
424
423
|
|
|
425
424
|
> 通过传递对象数字的方式进行正则或自定义函数进行验证
|
|
426
425
|
|
|
@@ -430,25 +429,25 @@ const rules = [
|
|
|
430
429
|
{ key: "username", verify: /^[a-zA-Z0-9]{3,}$/, msg: "用户名不合法", name: "用户名" },
|
|
431
430
|
{ key: "email", verify: /^\S+@\S+\.\S+$/, msg: "邮箱格式不正确", name: "邮箱" },
|
|
432
431
|
];
|
|
433
|
-
|
|
432
|
+
libJsRegFormValidate(form, rules);
|
|
434
433
|
//返回结果: []
|
|
435
434
|
|
|
436
435
|
const invalidForm = { username: "jo", email: "invalid-email" };
|
|
437
|
-
|
|
436
|
+
libJsRegFormValidate(invalidForm, rules);
|
|
438
437
|
//返回结果: [
|
|
439
438
|
// { key: "username", msg: "用户名不合法", name: "用户名" },
|
|
440
439
|
// { key: "email", msg: "邮箱格式不正确", name: "邮箱" }
|
|
441
440
|
//]
|
|
442
441
|
```
|
|
443
442
|
|
|
444
|
-
###
|
|
443
|
+
### LibJsRetryRequest-请求重连
|
|
445
444
|
|
|
446
445
|
> 请求失败重连
|
|
447
446
|
|
|
448
447
|
```ts
|
|
449
448
|
const requestFn = (params: { url: string }) => fetch(params.url).then(res => res.json());
|
|
450
449
|
const params = { url: "https://api.example.com/data" };
|
|
451
|
-
|
|
450
|
+
libJsRetryRequest({
|
|
452
451
|
promiseFn: requestFn,
|
|
453
452
|
params,
|
|
454
453
|
maxRetries: 5,
|
|
@@ -460,73 +459,73 @@ libRetryRequest({
|
|
|
460
459
|
|
|
461
460
|
## Random-随机
|
|
462
461
|
|
|
463
|
-
###
|
|
462
|
+
### LibJsProbabilityResult-概率触发
|
|
464
463
|
|
|
465
464
|
> 百分比概率结果
|
|
466
465
|
|
|
467
466
|
```ts
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
467
|
+
libJsProbabilityResult(50); //50% 概率为 true
|
|
468
|
+
libJsProbabilityResult(80); //80% 概率为 true
|
|
469
|
+
libJsProbabilityResult(100); //100% 概率为 true
|
|
471
470
|
```
|
|
472
471
|
|
|
473
|
-
###
|
|
472
|
+
### LibJsRandom-随机数
|
|
474
473
|
|
|
475
474
|
> 随机获取两个数之间的值,包含两数自身
|
|
476
475
|
|
|
477
476
|
```ts
|
|
478
|
-
|
|
479
|
-
|
|
477
|
+
libJsRandom(1, 10); //1 到 10 之间的随机整数
|
|
478
|
+
libJsRandom(1, 10, 2); //1 到 10 之间保留两位小数的随机数
|
|
480
479
|
```
|
|
481
480
|
|
|
482
|
-
###
|
|
481
|
+
### LibJsRandomColor-随机色
|
|
483
482
|
|
|
484
483
|
> 随机 RGBA 颜色
|
|
485
484
|
|
|
486
485
|
```ts
|
|
487
|
-
|
|
488
|
-
|
|
486
|
+
libJsRandomColor(); //生成随机的 RGBA 颜色,默认透明度 1
|
|
487
|
+
libJsRandomColor(0.5); //生成随机的 RGBA 颜色,透明度为 0.5
|
|
489
488
|
```
|
|
490
489
|
|
|
491
|
-
###
|
|
490
|
+
### LibJsUniqueRandomNumbers-随机数数组
|
|
492
491
|
|
|
493
492
|
> 随机生成指定个数、指定范围不重复的随机数数组
|
|
494
493
|
|
|
495
494
|
```ts
|
|
496
|
-
|
|
497
|
-
|
|
495
|
+
libJsUniqueRandomNumbers(1, 10, 5); //从 1 到 10 中随机生成 5 个唯一数字
|
|
496
|
+
libJsUniqueRandomNumbers(1, 100, 10); //从 1 到 100 中随机生成 10 个唯一数字
|
|
498
497
|
```
|
|
499
498
|
|
|
500
499
|
## Time-时间
|
|
501
500
|
|
|
502
|
-
###
|
|
501
|
+
### LibJsSameTimeCheck-时间比对
|
|
503
502
|
|
|
504
503
|
> 传入时间戳与当前时间判断是否为同一分、同一时、同一天、同一周、同一月、同一年
|
|
505
504
|
|
|
506
505
|
```ts
|
|
507
506
|
const timestamp = 1679872800000; //时间戳
|
|
508
|
-
const result =
|
|
507
|
+
const result = libJsSameTimeCheck(timestamp, 'day'); //判断是否为同一天
|
|
509
508
|
console.log(result); //0: 同一天, 1: 新的一天, -1: 时间戳大于当前时间
|
|
510
509
|
```
|
|
511
510
|
|
|
512
|
-
###
|
|
511
|
+
### LibJsTimeAgo-中文时间差
|
|
513
512
|
|
|
514
513
|
> 时间差计算
|
|
515
514
|
|
|
516
515
|
```ts
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
516
|
+
libJsTimeAgotamp(Date.now() - 3600000); //"1 小时前"
|
|
517
|
+
libJsTimeAgotamp(Date.now() - 86400000); //"1 天前"
|
|
518
|
+
libJsTimeAgotamp(Date.now() - 31536000000); //"1 年前"
|
|
519
|
+
libJsTimeAgotamp(Date.now() - 10000); //"刚刚"
|
|
521
520
|
```
|
|
522
521
|
|
|
523
|
-
###
|
|
522
|
+
### LibJsTimeGreeting-时间问候
|
|
524
523
|
|
|
525
524
|
> 根据当前时间返回问候语
|
|
526
525
|
|
|
527
526
|
```ts
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
527
|
+
libJsTimeGreeting(); //根据当前时间返回默认问候语
|
|
528
|
+
libJsTimeGreeting({ morning: "早安" }); //自定义早上问候语
|
|
529
|
+
libJsTimeGreeting({ afternoon: "午后好" }); //自定义下午问候语
|
|
531
530
|
```
|
|
532
531
|
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type LibJsGetDataTypeReturnType = "string" | "number" | "boolean" | "array" | "function" | "object";
|
|
2
|
+
/**
|
|
3
|
+
* @description 返回数据类型
|
|
4
|
+
* @param v 需要判断类型的数据
|
|
5
|
+
* @example
|
|
6
|
+
* libJsGetDataType(123); //"number"
|
|
7
|
+
* libJsGetDataType("hello"); //"string"
|
|
8
|
+
* libJsGetDataType([1, 2, 3]); //"array"
|
|
9
|
+
*/
|
|
10
|
+
export declare const libJsGetDataType: (v: any) => LibJsGetDataTypeReturnType;
|