@yh-kit/utils 1.3.1 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/utils.js +288 -188
- package/dist/utils.umd.cjs +1 -1
- package/package.json +2 -2
- package/types/utils/lib/array/index.d.ts +3 -2
- package/types/utils/lib/base64/index.d.ts +26 -0
- package/types/utils/lib/download/index.d.ts +11 -0
- package/types/utils/lib/element/index.d.ts +12 -0
- package/types/utils/lib/index.d.ts +3 -0
- package/types/utils/lib/letter/index.d.ts +1 -1
- package/types/utils/lib/object/index.d.ts +1 -1
- package/types/utils/lib/phone/index.d.ts +1 -1
- package/types/utils/lib/url/index.d.ts +5 -0
- package/types/utils/lib/waterfall/index.d.ts +1 -1
package/dist/utils.js
CHANGED
|
@@ -1,127 +1,180 @@
|
|
|
1
|
-
const
|
|
2
|
-
if (!
|
|
3
|
-
const
|
|
4
|
-
return
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
}),
|
|
8
|
-
},
|
|
1
|
+
const p = (e) => {
|
|
2
|
+
if (!e || !e.length) return {};
|
|
3
|
+
const t = {};
|
|
4
|
+
return e.forEach((n) => {
|
|
5
|
+
const r = Object.assign({ label: n.label, text: n.label }, n);
|
|
6
|
+
t[n == null ? void 0 : n.value] = r;
|
|
7
|
+
}), t;
|
|
8
|
+
}, b = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
9
9
|
__proto__: null,
|
|
10
|
-
toEnumObj:
|
|
11
|
-
}, Symbol.toStringTag, { value: "Module" })),
|
|
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
|
-
n
|
|
38
|
-
|
|
10
|
+
toEnumObj: p
|
|
11
|
+
}, Symbol.toStringTag, { value: "Module" })), A = Object.assign(
|
|
12
|
+
{
|
|
13
|
+
/**
|
|
14
|
+
* 数组去重
|
|
15
|
+
* @param arr 数组
|
|
16
|
+
* @returns 去重后的数组
|
|
17
|
+
*/
|
|
18
|
+
unique(e) {
|
|
19
|
+
return e ? Array.from(new Set(e)) : [];
|
|
20
|
+
},
|
|
21
|
+
/**
|
|
22
|
+
* 判断某元素是否在数组中
|
|
23
|
+
* @param arr 数组
|
|
24
|
+
* @param element 查询元素
|
|
25
|
+
* @returns 是否存在。true 存在;false 不存在
|
|
26
|
+
*/
|
|
27
|
+
isExist(e, t) {
|
|
28
|
+
return !e || !t ? !1 : e.indexOf(t) !== -1;
|
|
29
|
+
},
|
|
30
|
+
/**
|
|
31
|
+
* 统计数组中某元素出现的次数:对于大型数组,手动循环的性能略优于 filter 和 reduce。
|
|
32
|
+
* @param arr 数组
|
|
33
|
+
* @param target 目标元素
|
|
34
|
+
* @returns 出现次数
|
|
35
|
+
*/
|
|
36
|
+
countOfAppear(e, t) {
|
|
37
|
+
let n = 0;
|
|
38
|
+
for (const r of e)
|
|
39
|
+
r === t && n++;
|
|
40
|
+
return n;
|
|
41
|
+
},
|
|
42
|
+
/**
|
|
43
|
+
* 数组排序-默认升序
|
|
44
|
+
* @param arr 数组
|
|
45
|
+
* @param order 排序方式:asc 升序;desc 降序
|
|
46
|
+
* @returns 排序后的数组
|
|
47
|
+
*/
|
|
48
|
+
sort(e, t = "asc") {
|
|
49
|
+
return e ? e.sort((n, r) => t === "asc" ? n > r ? 1 : -1 : n < r ? 1 : -1) : [];
|
|
50
|
+
},
|
|
51
|
+
/**
|
|
52
|
+
* 数组乱序(洗牌算法)
|
|
53
|
+
* 1. 从数组的最后一个元素开始,将其与随机位置的元素交换位置。
|
|
54
|
+
* 2. 然后,从倒数第二个元素开始,将其与随机位置的元素交换位置。
|
|
55
|
+
* 3. 以此类推,直到第一个元素。
|
|
56
|
+
* 4. 这样,数组中的元素就会被随机打乱顺序。
|
|
57
|
+
* 5. 注意,这个算法会改变原数组。如果不想改变原数组,可以先复制一份再进行操作。
|
|
58
|
+
* 6. 时间复杂度:O(n)
|
|
59
|
+
* @param arr 数组
|
|
60
|
+
* @returns
|
|
61
|
+
*/
|
|
62
|
+
shuffle(e) {
|
|
63
|
+
if (!e)
|
|
64
|
+
return [];
|
|
65
|
+
for (let t = e.length - 1; t > 0; t--) {
|
|
66
|
+
const n = Math.floor(Math.random() * (t + 1));
|
|
67
|
+
[e[t], e[n]] = [e[n], e[t]];
|
|
68
|
+
}
|
|
69
|
+
return e;
|
|
70
|
+
}
|
|
39
71
|
},
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
72
|
+
{
|
|
73
|
+
...b
|
|
74
|
+
}
|
|
75
|
+
), j = {
|
|
76
|
+
/**
|
|
77
|
+
* 将Base64编码的字符串转换为Blob对象。
|
|
78
|
+
* Blob对象用于表示二进制大型对象,可以在浏览器环境中处理大文件或二进制数据。
|
|
79
|
+
*
|
|
80
|
+
* @param base64 Base64编码的字符串。
|
|
81
|
+
* @param contentType 可选参数,表示生成的Blob对象的MIME类型,默认为空字符串。
|
|
82
|
+
* @param sliceSize 可选参数,表示分片大小,默认为512字节。用于控制每次处理的Base64字符串长度,以优化大文件的处理。
|
|
83
|
+
* @returns 返回转换后的Blob对象,如果转换失败,则返回null。
|
|
84
|
+
*/
|
|
85
|
+
toBlob(e, t = "", n = 512) {
|
|
86
|
+
const o = (e.split(",")[1] || e).replace(/-/g, "+").replace(/_/g, "/"), i = "=".repeat((4 - o.length % 4) % 4), g = o + i;
|
|
87
|
+
try {
|
|
88
|
+
const l = atob(g), f = [];
|
|
89
|
+
for (let a = 0; a < l.length; a += n) {
|
|
90
|
+
const c = l.slice(a, a + n), u = new Array(c.length);
|
|
91
|
+
for (let s = 0; s < c.length; s++)
|
|
92
|
+
u[s] = c.charCodeAt(s);
|
|
93
|
+
const d = new Uint8Array(u);
|
|
94
|
+
f.push(d);
|
|
95
|
+
}
|
|
96
|
+
return new Blob(f, { type: t });
|
|
97
|
+
} catch (l) {
|
|
98
|
+
return console.error("Failed to convert base64 to blob:", l), null;
|
|
99
|
+
}
|
|
48
100
|
},
|
|
49
101
|
/**
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
* @
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
102
|
+
* 将Base64编码的字符串转换为File对象。
|
|
103
|
+
* 这个函数接受一个Base64编码的字符串,一个可选的文件名和一个可选的MIME类型,
|
|
104
|
+
* 并返回一个代表原始数据的File对象。
|
|
105
|
+
*
|
|
106
|
+
* @param base64 Base64编码的字符串。
|
|
107
|
+
* @param fileName 文件名,默认为"file.txt"。
|
|
108
|
+
* @param mimeType MIME类型,默认为"text/plain"。
|
|
109
|
+
* @returns 返回一个File对象,包含解码后的数据。
|
|
110
|
+
*/
|
|
111
|
+
toFile(e, t = "file.txt", n = "text/plain") {
|
|
112
|
+
const o = e.replace(/^data:.+;base64,/, "").replace(/-/g, "+").replace(/_/g, "/"), i = "=".repeat((4 - o.length % 4) % 4), g = o + i, l = atob(g), f = [];
|
|
113
|
+
for (let c = 0; c < l.length; c += 512) {
|
|
114
|
+
const u = l.slice(c, c + 512), d = new Array(u.length);
|
|
115
|
+
for (let s = 0; s < u.length; s++)
|
|
116
|
+
d[s] = u.charCodeAt(s);
|
|
117
|
+
f.push(new Uint8Array(d));
|
|
66
118
|
}
|
|
67
|
-
|
|
119
|
+
const a = new Blob(f, { type: n });
|
|
120
|
+
return new File([a], t, { type: n });
|
|
68
121
|
}
|
|
69
|
-
},
|
|
122
|
+
}, E = {
|
|
70
123
|
/**
|
|
71
124
|
* 检查是否为空字符串
|
|
72
125
|
* @param str 要检查的字符串
|
|
73
126
|
* @returns 如果字符串为空,则返回 true,否则返回 false
|
|
74
127
|
*/
|
|
75
|
-
isEmptyString(
|
|
76
|
-
return typeof
|
|
128
|
+
isEmptyString(e) {
|
|
129
|
+
return typeof e == "string" && e.trim() === "";
|
|
77
130
|
},
|
|
78
131
|
/**
|
|
79
132
|
* 判断是否为数字
|
|
80
133
|
* @param val 要检查的值
|
|
81
134
|
* @returns 如果值是数字,则返回 true,否则返回 false
|
|
82
135
|
*/
|
|
83
|
-
isNumber(
|
|
84
|
-
return typeof
|
|
136
|
+
isNumber(e) {
|
|
137
|
+
return typeof e == "number" && !isNaN(e);
|
|
85
138
|
},
|
|
86
139
|
/**
|
|
87
140
|
* 判断是否为对象
|
|
88
141
|
* @param val 要检查的值
|
|
89
142
|
* @returns 如果值是对象,则返回 true,否则返回 false
|
|
90
143
|
*/
|
|
91
|
-
isObject(
|
|
92
|
-
return
|
|
144
|
+
isObject(e) {
|
|
145
|
+
return e !== null && typeof e == "object" && !Array.isArray(e);
|
|
93
146
|
},
|
|
94
147
|
/**
|
|
95
148
|
* 判断对象是否为空
|
|
96
149
|
* @param obj 要检查的对象
|
|
97
150
|
* @returns 如果对象为空,则返回 true,否则返回 false
|
|
98
151
|
*/
|
|
99
|
-
isEmptyObject(
|
|
100
|
-
return
|
|
152
|
+
isEmptyObject(e) {
|
|
153
|
+
return e ? Object.keys(e).length === 0 && e.constructor === Object : !0;
|
|
101
154
|
},
|
|
102
155
|
/**
|
|
103
156
|
* 判断是否为布尔值
|
|
104
157
|
* @param val 要检查的值
|
|
105
158
|
* @returns 如果值是布尔值,则返回 true,否则返回 false
|
|
106
159
|
*/
|
|
107
|
-
isBoolean(
|
|
108
|
-
return typeof
|
|
160
|
+
isBoolean(e) {
|
|
161
|
+
return typeof e == "boolean";
|
|
109
162
|
},
|
|
110
163
|
/**
|
|
111
164
|
* 判断是否为数组
|
|
112
165
|
* @param val 要检查的值
|
|
113
166
|
* @returns 如果值是数组,则返回 true,否则返回 false
|
|
114
167
|
*/
|
|
115
|
-
isArray(
|
|
116
|
-
return Array.isArray(
|
|
168
|
+
isArray(e) {
|
|
169
|
+
return Array.isArray(e);
|
|
117
170
|
},
|
|
118
171
|
/**
|
|
119
172
|
* 判断是否为函数
|
|
120
173
|
* @param val 要检查的值
|
|
121
174
|
* @returns
|
|
122
175
|
*/
|
|
123
|
-
isFunction(
|
|
124
|
-
return typeof
|
|
176
|
+
isFunction(e) {
|
|
177
|
+
return typeof e == "function";
|
|
125
178
|
},
|
|
126
179
|
/**
|
|
127
180
|
* 判断是否为 Promise 对象
|
|
@@ -129,41 +182,41 @@ const i = (t) => {
|
|
|
129
182
|
* @returns 如果对象是 Promise 对象,则返回 true,否则返回 false
|
|
130
183
|
*/
|
|
131
184
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
132
|
-
isPromise(
|
|
133
|
-
return !!
|
|
185
|
+
isPromise(e) {
|
|
186
|
+
return !!e && (typeof e == "object" || typeof e == "function") && typeof e.then == "function";
|
|
134
187
|
},
|
|
135
188
|
/**
|
|
136
189
|
* 判断是否为 DOM 元素
|
|
137
190
|
* @param obj 要检查的对象
|
|
138
191
|
* @returns 如果对象是 DOM 元素,则返回 true,否则返回 false
|
|
139
192
|
*/
|
|
140
|
-
isElement(
|
|
141
|
-
return
|
|
193
|
+
isElement(e) {
|
|
194
|
+
return e instanceof Element;
|
|
142
195
|
},
|
|
143
196
|
/**
|
|
144
197
|
* 判断手机号格式(中国)
|
|
145
198
|
* @param str 手机号字符串
|
|
146
199
|
* @returns true 表示手机号格式正确,false 表示手机号格式错误
|
|
147
200
|
*/
|
|
148
|
-
isPhone(
|
|
149
|
-
return /^1[3-9]\d{9}$/.test(
|
|
201
|
+
isPhone(e) {
|
|
202
|
+
return /^1[3-9]\d{9}$/.test(e);
|
|
150
203
|
},
|
|
151
204
|
/**
|
|
152
205
|
* 判断邮箱格式
|
|
153
206
|
* @param str 邮箱字符串
|
|
154
207
|
* @returns true 表示邮箱格式正确,false 表示邮箱格式错误
|
|
155
208
|
*/
|
|
156
|
-
isEmail(
|
|
157
|
-
return /^[\w.-]+@[\w.-]+\.\w+$/.test(
|
|
209
|
+
isEmail(e) {
|
|
210
|
+
return /^[\w.-]+@[\w.-]+\.\w+$/.test(e);
|
|
158
211
|
},
|
|
159
212
|
/**
|
|
160
213
|
* 判断字符串是否为 JSON
|
|
161
214
|
* @param str 字符串
|
|
162
215
|
* @returns true 表示是 JSON,false 表示不是 JSON
|
|
163
216
|
*/
|
|
164
|
-
isJSON(
|
|
217
|
+
isJSON(e) {
|
|
165
218
|
try {
|
|
166
|
-
return JSON.parse(
|
|
219
|
+
return JSON.parse(e), !0;
|
|
167
220
|
} catch {
|
|
168
221
|
return !1;
|
|
169
222
|
}
|
|
@@ -176,17 +229,17 @@ const i = (t) => {
|
|
|
176
229
|
* const img = new Image();
|
|
177
230
|
* img.src = "URL_ADDRESS * img.src = "https://example.com/image.jpg";
|
|
178
231
|
*/
|
|
179
|
-
isImageLoaded(
|
|
180
|
-
return
|
|
232
|
+
isImageLoaded(e) {
|
|
233
|
+
return e.complete && e.naturalWidth !== 0;
|
|
181
234
|
},
|
|
182
235
|
/**
|
|
183
236
|
* 判断元素是否在可视区域内
|
|
184
237
|
* @param el 元素对象
|
|
185
238
|
* @returns true 表示元素在可视区域内,false 表示元素不在可视区域内
|
|
186
239
|
*/
|
|
187
|
-
isInViewport(
|
|
188
|
-
const
|
|
189
|
-
return
|
|
240
|
+
isInViewport(e) {
|
|
241
|
+
const t = e.getBoundingClientRect();
|
|
242
|
+
return t.top >= 0 && t.left >= 0 && t.bottom <= window.innerHeight && t.right <= window.innerWidth;
|
|
190
243
|
},
|
|
191
244
|
/**
|
|
192
245
|
* 判断是否为闰年
|
|
@@ -200,8 +253,8 @@ const i = (t) => {
|
|
|
200
253
|
* isLeapYear(2025); // false
|
|
201
254
|
* isLeapYear(2024); // true
|
|
202
255
|
*/
|
|
203
|
-
isLeapYear(
|
|
204
|
-
return
|
|
256
|
+
isLeapYear(e) {
|
|
257
|
+
return e % 4 === 0 && e % 100 !== 0 || e % 400 === 0;
|
|
205
258
|
},
|
|
206
259
|
/**
|
|
207
260
|
* 判断是否为移动端
|
|
@@ -210,16 +263,16 @@ const i = (t) => {
|
|
|
210
263
|
isMobile() {
|
|
211
264
|
return /Mobi|Android|iPhone/i.test(navigator.userAgent);
|
|
212
265
|
}
|
|
213
|
-
},
|
|
266
|
+
}, _ = {
|
|
214
267
|
/**
|
|
215
268
|
* 通过传入的name获取cookie的值
|
|
216
269
|
* @param name cookie的name
|
|
217
270
|
* @returns cookie的值
|
|
218
271
|
*/
|
|
219
|
-
getCookie(
|
|
220
|
-
if (!
|
|
221
|
-
const
|
|
222
|
-
return
|
|
272
|
+
getCookie(e) {
|
|
273
|
+
if (!e) return;
|
|
274
|
+
const t = document.cookie.match(new RegExp("(^| )" + e + "=([^;]+)"));
|
|
275
|
+
return t ? decodeURIComponent(t[2]) : null;
|
|
223
276
|
},
|
|
224
277
|
/**
|
|
225
278
|
* 设置 cookie
|
|
@@ -228,27 +281,27 @@ const i = (t) => {
|
|
|
228
281
|
* @param days cookie的过期时间,默认7天。如果为0,则表示cookie在会话结束时过期。如果为负数,则表示cookie已过期。
|
|
229
282
|
* @returns void
|
|
230
283
|
*/
|
|
231
|
-
setCookie(
|
|
232
|
-
const
|
|
233
|
-
|
|
284
|
+
setCookie(e, t, n = 7) {
|
|
285
|
+
const r = /* @__PURE__ */ new Date();
|
|
286
|
+
r.setTime(r.getTime() + n * 24 * 60 * 60 * 1e3), document.cookie = `${e}=${encodeURIComponent(t)};expires=${r.toUTCString()};path=/`;
|
|
234
287
|
},
|
|
235
288
|
/**
|
|
236
289
|
* 删除cookie
|
|
237
290
|
* @param name cookie的name
|
|
238
291
|
* @returns void
|
|
239
292
|
*/
|
|
240
|
-
deleteCookie(
|
|
241
|
-
this.setCookie(
|
|
293
|
+
deleteCookie(e) {
|
|
294
|
+
this.setCookie(e, "", -1);
|
|
242
295
|
}
|
|
243
|
-
},
|
|
296
|
+
}, C = {
|
|
244
297
|
/**
|
|
245
298
|
* 获取当前时间字符串
|
|
246
299
|
* @param locales 区域设置。如:'zh-CN','en-US',"chinese"。默认'zh-CN'。
|
|
247
300
|
* @param hour12 是否使用12小时制。默认false,使用24小时制。
|
|
248
301
|
* @returns 当前时间的字符串。如:'2025/5/27 16:54:45'
|
|
249
302
|
*/
|
|
250
|
-
getTimeString(
|
|
251
|
-
return
|
|
303
|
+
getTimeString(e, t = !1) {
|
|
304
|
+
return t ? (/* @__PURE__ */ new Date()).toLocaleString(e, { hour12: !0 }) : (/* @__PURE__ */ new Date()).toLocaleString("chinese", { hour12: !1 });
|
|
252
305
|
},
|
|
253
306
|
/**
|
|
254
307
|
* 计算两个日期相差天数
|
|
@@ -256,9 +309,9 @@ const i = (t) => {
|
|
|
256
309
|
* @param date2 日期2
|
|
257
310
|
* @returns 相差天数
|
|
258
311
|
*/
|
|
259
|
-
diffDays(
|
|
260
|
-
const
|
|
261
|
-
return
|
|
312
|
+
diffDays(e, t) {
|
|
313
|
+
const n = new Date(e).getTime(), r = new Date(t).getTime();
|
|
314
|
+
return n > r ? Math.abs(Math.floor((n - r) / (24 * 3600 * 1e3))) : Math.abs(Math.floor((r - n) / (24 * 3600 * 1e3)));
|
|
262
315
|
},
|
|
263
316
|
/**
|
|
264
317
|
* 生成唯一ID(时间戳+随机数)
|
|
@@ -267,61 +320,98 @@ const i = (t) => {
|
|
|
267
320
|
uniqueId() {
|
|
268
321
|
return Date.now().toString(36) + Math.random().toString(36).substr(2, 5);
|
|
269
322
|
}
|
|
270
|
-
},
|
|
323
|
+
}, M = {
|
|
324
|
+
/**
|
|
325
|
+
* 将blob对象转化成文件并导出到本地
|
|
326
|
+
* @param blob blob对象
|
|
327
|
+
* @param filename 文件名
|
|
328
|
+
*/
|
|
329
|
+
saveAsBlob(e, t) {
|
|
330
|
+
const n = document.createElement("a"), r = window.URL.createObjectURL(e);
|
|
331
|
+
n.href = r, n.download = t, document.body.appendChild(n), n.click(), URL.revokeObjectURL(r), document.body.removeChild(n);
|
|
332
|
+
}
|
|
333
|
+
}, x = {
|
|
334
|
+
/**
|
|
335
|
+
* 获取元素相对于文档顶部的偏移量(距离)
|
|
336
|
+
* 处理了SVG元素的特殊情况。
|
|
337
|
+
* @param el 元素
|
|
338
|
+
* @returns 偏移量(距离值)
|
|
339
|
+
*/
|
|
340
|
+
getOffsetTop(e) {
|
|
341
|
+
if (!e)
|
|
342
|
+
throw new Error("Element is not provided");
|
|
343
|
+
if (e instanceof SVGElement) {
|
|
344
|
+
const n = e.getBoundingClientRect(), r = window.pageYOffset || document.documentElement.scrollTop;
|
|
345
|
+
return n.top + r;
|
|
346
|
+
}
|
|
347
|
+
let t = 0;
|
|
348
|
+
for (; e; )
|
|
349
|
+
t += e.offsetTop, e = e.offsetParent;
|
|
350
|
+
return t;
|
|
351
|
+
}
|
|
352
|
+
}, B = {
|
|
271
353
|
/**
|
|
272
354
|
* 将选中的字母数组从A-Z排序:用于试题选择答案的排序实例
|
|
273
355
|
* @param arr 字母数组
|
|
274
356
|
* @returns
|
|
275
357
|
*/
|
|
276
|
-
sortFromA2Z
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
}
|
|
358
|
+
sortFromA2Z(e) {
|
|
359
|
+
return e != null && e.length ? e.sort((n, r) => {
|
|
360
|
+
const o = n.toUpperCase(), i = r.toUpperCase();
|
|
361
|
+
return o < i ? -1 : o > i ? 1 : 0;
|
|
362
|
+
}) : void 0;
|
|
363
|
+
}
|
|
364
|
+
}, h = (e) => e > 25 || e < 0 ? "" : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[e], y = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
281
365
|
__proto__: null,
|
|
282
|
-
toLetter:
|
|
283
|
-
}, Symbol.toStringTag, { value: "Module" })),
|
|
366
|
+
toLetter: h
|
|
367
|
+
}, Symbol.toStringTag, { value: "Module" })), m = (e) => e.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","), w = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
284
368
|
__proto__: null,
|
|
285
|
-
toMoney:
|
|
286
|
-
}, Symbol.toStringTag, { value: "Module" })),
|
|
287
|
-
...
|
|
288
|
-
...
|
|
369
|
+
toMoney: m
|
|
370
|
+
}, Symbol.toStringTag, { value: "Module" })), L = {
|
|
371
|
+
...y,
|
|
372
|
+
...w,
|
|
289
373
|
/**
|
|
290
374
|
* 判断是否为数字
|
|
291
375
|
* @param val 待判断的值
|
|
292
376
|
* @returns 是否为数字。true 是;false 否
|
|
293
377
|
*/
|
|
294
|
-
isNumber(
|
|
295
|
-
return typeof
|
|
378
|
+
isNumber(e) {
|
|
379
|
+
return typeof e == "number" && !isNaN(e);
|
|
296
380
|
}
|
|
297
|
-
},
|
|
381
|
+
}, I = {
|
|
298
382
|
/**
|
|
299
383
|
* 判断对象是否为空
|
|
300
384
|
* @param obj 对象
|
|
301
385
|
* @returns 是否为空。true 为空;false 不为空
|
|
302
386
|
*/
|
|
303
|
-
isEmptyObject
|
|
304
|
-
|
|
387
|
+
isEmptyObject(e) {
|
|
388
|
+
return e ? Object.keys(e).length === 0 && e.constructor === Object : !0;
|
|
389
|
+
}
|
|
390
|
+
}, T = {
|
|
305
391
|
/**
|
|
306
392
|
* 脱敏
|
|
307
393
|
* @param phone 电话号码/手机号码
|
|
308
394
|
* @returns 脱敏后的电话号码/手机号码
|
|
309
395
|
*/
|
|
310
|
-
desensitize
|
|
311
|
-
},
|
|
396
|
+
desensitize(e) {
|
|
397
|
+
return e ? e.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2") || e.slice(0, 3) + "****" + e.slice(7) : "";
|
|
398
|
+
}
|
|
399
|
+
}, k = {
|
|
312
400
|
/**
|
|
313
401
|
* 生成随机颜色
|
|
314
402
|
* @returns 随机颜色值
|
|
315
403
|
*/
|
|
316
|
-
color: ()
|
|
404
|
+
color: function() {
|
|
405
|
+
return `#${Math.random().toString(16).slice(2, 8)}`;
|
|
406
|
+
},
|
|
317
407
|
/**
|
|
318
408
|
* 生成指定范围的随机整数
|
|
319
409
|
* @param min 最小值
|
|
320
410
|
* @param max 最大值
|
|
321
411
|
* @returns 随机数
|
|
322
412
|
*/
|
|
323
|
-
int(
|
|
324
|
-
return Math.floor(Math.random() * (
|
|
413
|
+
int(e, t) {
|
|
414
|
+
return Math.floor(Math.random() * (t - e + 1)) + e;
|
|
325
415
|
},
|
|
326
416
|
/**
|
|
327
417
|
* 生成唯一ID(时间戳+随机数)
|
|
@@ -330,65 +420,65 @@ const i = (t) => {
|
|
|
330
420
|
uniqueId() {
|
|
331
421
|
return Date.now().toString(36) + Math.random().toString(36).substr(2, 5);
|
|
332
422
|
}
|
|
333
|
-
},
|
|
423
|
+
}, v = {
|
|
334
424
|
/**
|
|
335
425
|
* 通过key值获取 localStorage 中存储的某个值
|
|
336
426
|
* @param key 获取的key
|
|
337
427
|
* @returns 获取的值
|
|
338
428
|
*/
|
|
339
|
-
getLocal(
|
|
340
|
-
return localStorage.getItem(
|
|
429
|
+
getLocal(e) {
|
|
430
|
+
return localStorage.getItem(e);
|
|
341
431
|
},
|
|
342
432
|
/**
|
|
343
433
|
* 设置localStorage中的某个值
|
|
344
434
|
* @param key 设置的key
|
|
345
435
|
* @param value 设置的value
|
|
346
436
|
*/
|
|
347
|
-
setLocal(
|
|
348
|
-
localStorage.setItem(
|
|
437
|
+
setLocal(e, t) {
|
|
438
|
+
localStorage.setItem(e, t);
|
|
349
439
|
},
|
|
350
440
|
/**
|
|
351
441
|
* 删除localStorage中的某个值
|
|
352
442
|
* @param key 删除的key
|
|
353
443
|
*/
|
|
354
|
-
removeLocal(
|
|
355
|
-
localStorage.removeItem(
|
|
444
|
+
removeLocal(e) {
|
|
445
|
+
localStorage.removeItem(e);
|
|
356
446
|
},
|
|
357
447
|
/**
|
|
358
448
|
* 通过key值获取 sessionStorage 中存储的某个值
|
|
359
449
|
* @param key 获取的key
|
|
360
450
|
* @returns 获取的值
|
|
361
451
|
*/
|
|
362
|
-
getSession(
|
|
363
|
-
return sessionStorage.getItem(
|
|
452
|
+
getSession(e) {
|
|
453
|
+
return sessionStorage.getItem(e);
|
|
364
454
|
},
|
|
365
455
|
/**
|
|
366
456
|
* 设置sessionStorage中的某个值
|
|
367
457
|
* @param key 设置的key
|
|
368
458
|
* @param value 设置的value
|
|
369
459
|
*/
|
|
370
|
-
setSession(
|
|
371
|
-
sessionStorage.setItem(
|
|
460
|
+
setSession(e, t) {
|
|
461
|
+
sessionStorage.setItem(e, t);
|
|
372
462
|
},
|
|
373
463
|
/**
|
|
374
464
|
* 删除sessionStorage中的某个值
|
|
375
465
|
* @param key 删除的key
|
|
376
466
|
*/
|
|
377
|
-
removeSession(
|
|
378
|
-
sessionStorage.removeItem(
|
|
467
|
+
removeSession(e) {
|
|
468
|
+
sessionStorage.removeItem(e);
|
|
379
469
|
}
|
|
380
|
-
},
|
|
470
|
+
}, R = {
|
|
381
471
|
/**
|
|
382
472
|
* 判断某元素是否在字符串中-比includes()方法更兼容,includes为ES6新增方法,IE不支持。小程序也不支持
|
|
383
473
|
* @param str 字符串
|
|
384
474
|
* @param element 查询元素
|
|
385
475
|
* @returns 是否存在。true 存在;false 不存在
|
|
386
476
|
*/
|
|
387
|
-
isExist(
|
|
388
|
-
if (!
|
|
477
|
+
isExist(e, t) {
|
|
478
|
+
if (!e || !t)
|
|
389
479
|
return !1;
|
|
390
|
-
const
|
|
391
|
-
return console.log("判断某元素是否在字符串中",
|
|
480
|
+
const n = e.split(",");
|
|
481
|
+
return console.log("判断某元素是否在字符串中", n.indexOf(t) === -1), n.indexOf(t) !== -1;
|
|
392
482
|
},
|
|
393
483
|
/**
|
|
394
484
|
* 判断某元素是否在字符串中-比isExist()方法更高效。
|
|
@@ -396,72 +486,82 @@ const i = (t) => {
|
|
|
396
486
|
* @param element 查询元素
|
|
397
487
|
* @returns 是否存在。true 存在;false 不存在
|
|
398
488
|
*/
|
|
399
|
-
includes(
|
|
400
|
-
return !
|
|
489
|
+
includes(e, t) {
|
|
490
|
+
return !e || !t ? !1 : e.includes(t);
|
|
401
491
|
},
|
|
402
492
|
/**
|
|
403
493
|
* 判断字符串是否为 JSON
|
|
404
494
|
* @param str 字符串
|
|
405
495
|
* @returns
|
|
406
496
|
*/
|
|
407
|
-
isJSON(
|
|
497
|
+
isJSON(e) {
|
|
408
498
|
try {
|
|
409
|
-
return JSON.parse(
|
|
499
|
+
return JSON.parse(e), !0;
|
|
410
500
|
} catch {
|
|
411
501
|
return !1;
|
|
412
502
|
}
|
|
413
503
|
}
|
|
414
|
-
},
|
|
415
|
-
const
|
|
416
|
-
return
|
|
504
|
+
}, S = (e) => {
|
|
505
|
+
const n = new RegExp("[?&]" + e + "=([^&#]*)", "i").exec(window.location.href);
|
|
506
|
+
return n ? decodeURIComponent(n[1]) : null;
|
|
417
507
|
};
|
|
418
|
-
function
|
|
419
|
-
return new URLSearchParams(window.location.search).get(
|
|
508
|
+
function O(e) {
|
|
509
|
+
return new URLSearchParams(window.location.search).get(e);
|
|
420
510
|
}
|
|
421
|
-
const
|
|
511
|
+
const U = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
422
512
|
__proto__: null,
|
|
423
|
-
getQueryInfoByName:
|
|
424
|
-
getQueryParam:
|
|
425
|
-
}, Symbol.toStringTag, { value: "Module" })),
|
|
426
|
-
...
|
|
513
|
+
getQueryInfoByName: S,
|
|
514
|
+
getQueryParam: O
|
|
515
|
+
}, Symbol.toStringTag, { value: "Module" })), N = {
|
|
516
|
+
...U,
|
|
427
517
|
/**
|
|
428
518
|
* 获取当前域名
|
|
429
519
|
* @returns 当前url链接的host信息
|
|
430
520
|
*/
|
|
431
521
|
getHost() {
|
|
432
522
|
return window.location.host;
|
|
523
|
+
},
|
|
524
|
+
/**
|
|
525
|
+
* 获取当前页面路径
|
|
526
|
+
* @returns 当前url链接的pathname信息
|
|
527
|
+
*/
|
|
528
|
+
getPath() {
|
|
529
|
+
return window.location.pathname;
|
|
433
530
|
}
|
|
434
|
-
},
|
|
531
|
+
}, P = {
|
|
435
532
|
/**
|
|
436
533
|
* 处理瀑布流数据,使其适合瀑布流布局展示
|
|
437
534
|
* @param list 瀑布流数据-数组
|
|
438
535
|
* @param columnsNum 需要展示的列数
|
|
439
536
|
* @returns 适合瀑布流布局的数组
|
|
440
537
|
*/
|
|
441
|
-
toList
|
|
442
|
-
console.log(
|
|
443
|
-
const
|
|
444
|
-
for (let o = 0; o <
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
const
|
|
448
|
-
for (const o in
|
|
449
|
-
|
|
450
|
-
return
|
|
538
|
+
toList(e, t = 2) {
|
|
539
|
+
console.log(e, t);
|
|
540
|
+
const n = {};
|
|
541
|
+
for (let o = 0; o < t; o++)
|
|
542
|
+
n[o] = [];
|
|
543
|
+
e.forEach((o, i) => n[i % t].push(o));
|
|
544
|
+
const r = [];
|
|
545
|
+
for (const o in n)
|
|
546
|
+
r.push(...n[o]);
|
|
547
|
+
return r;
|
|
451
548
|
}
|
|
452
549
|
};
|
|
453
550
|
export {
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
551
|
+
A as arrayUtils,
|
|
552
|
+
j as base64Utils,
|
|
553
|
+
E as booleanUtils,
|
|
554
|
+
_ as cookieUtils,
|
|
555
|
+
C as dateUtils,
|
|
556
|
+
M as downloadUtils,
|
|
557
|
+
x as elementUtils,
|
|
558
|
+
B as letterUtils,
|
|
559
|
+
L as numberUtils,
|
|
560
|
+
I as objectUtils,
|
|
561
|
+
T as phoneUtils,
|
|
562
|
+
k as randomUtils,
|
|
563
|
+
v as storageUtils,
|
|
564
|
+
R as stringUtils,
|
|
565
|
+
N as urlUtils,
|
|
566
|
+
P as waterfallUtils
|
|
467
567
|
};
|
package/dist/utils.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(r,g){typeof exports=="object"&&typeof module<"u"?g(exports):typeof define=="function"&&define.amd?define(["exports"],g):(r=typeof globalThis<"u"?globalThis:r||self,g(r.yhkitUtils={}))})(this,function(r){"use strict";const h=Object.assign({unique(e){return e?Array.from(new Set(e)):[]},isExist(e,t){return!e||!t?!1:e.indexOf(t)!==-1},countOfAppear(e,t){let n=0;for(const o of e)o===t&&n++;return n},sort(e,t="asc"){return e?e.sort((n,o)=>t==="asc"?n>o?1:-1:n<o?1:-1):[]},shuffle(e){if(!e)return[];for(let t=e.length-1;t>0;t--){const n=Math.floor(Math.random()*(t+1));[e[t],e[n]]=[e[n],e[t]]}return e}},{...Object.freeze(Object.defineProperty({__proto__:null,toEnumObj:e=>{if(!e||!e.length)return{};const t={};return e.forEach(n=>{const o=Object.assign({label:n.label,text:n.label},n);t[n==null?void 0:n.value]=o}),t}},Symbol.toStringTag,{value:"Module"}))}),p={toBlob(e,t="",n=512){const s=(e.split(",")[1]||e).replace(/-/g,"+").replace(/_/g,"/"),l="=".repeat((4-s.length%4)%4),y=s+l;try{const a=atob(y),d=[];for(let u=0;u<a.length;u+=n){const c=a.slice(u,u+n),f=new Array(c.length);for(let i=0;i<c.length;i++)f[i]=c.charCodeAt(i);const b=new Uint8Array(f);d.push(b)}return new Blob(d,{type:t})}catch(a){return console.error("Failed to convert base64 to blob:",a),null}},toFile(e,t="file.txt",n="text/plain"){const s=e.replace(/^data:.+;base64,/,"").replace(/-/g,"+").replace(/_/g,"/"),l="=".repeat((4-s.length%4)%4),y=s+l,a=atob(y),d=[];for(let c=0;c<a.length;c+=512){const f=a.slice(c,c+512),b=new Array(f.length);for(let i=0;i<f.length;i++)b[i]=f.charCodeAt(i);d.push(new Uint8Array(b))}const u=new Blob(d,{type:n});return new File([u],t,{type:n})}},m={isEmptyString(e){return typeof e=="string"&&e.trim()===""},isNumber(e){return typeof e=="number"&&!isNaN(e)},isObject(e){return e!==null&&typeof e=="object"&&!Array.isArray(e)},isEmptyObject(e){return e?Object.keys(e).length===0&&e.constructor===Object:!0},isBoolean(e){return typeof e=="boolean"},isArray(e){return Array.isArray(e)},isFunction(e){return typeof e=="function"},isPromise(e){return!!e&&(typeof e=="object"||typeof e=="function")&&typeof e.then=="function"},isElement(e){return e instanceof Element},isPhone(e){return/^1[3-9]\d{9}$/.test(e)},isEmail(e){return/^[\w.-]+@[\w.-]+\.\w+$/.test(e)},isJSON(e){try{return JSON.parse(e),!0}catch{return!1}},isImageLoaded(e){return e.complete&&e.naturalWidth!==0},isInViewport(e){const t=e.getBoundingClientRect();return t.top>=0&&t.left>=0&&t.bottom<=window.innerHeight&&t.right<=window.innerWidth},isLeapYear(e){return e%4===0&&e%100!==0||e%400===0},isMobile(){return/Mobi|Android|iPhone/i.test(navigator.userAgent)}},U={getCookie(e){if(!e)return;const t=document.cookie.match(new RegExp("(^| )"+e+"=([^;]+)"));return t?decodeURIComponent(t[2]):null},setCookie(e,t,n=7){const o=new Date;o.setTime(o.getTime()+n*24*60*60*1e3),document.cookie=`${e}=${encodeURIComponent(t)};expires=${o.toUTCString()};path=/`},deleteCookie(e){this.setCookie(e,"",-1)}},w={getTimeString(e,t=!1){return t?new Date().toLocaleString(e,{hour12:!0}):new Date().toLocaleString("chinese",{hour12:!1})},diffDays(e,t){const n=new Date(e).getTime(),o=new Date(t).getTime();return n>o?Math.abs(Math.floor((n-o)/(24*3600*1e3))):Math.abs(Math.floor((o-n)/(24*3600*1e3)))},uniqueId(){return Date.now().toString(36)+Math.random().toString(36).substr(2,5)}},S={saveAsBlob(e,t){const n=document.createElement("a"),o=window.URL.createObjectURL(e);n.href=o,n.download=t,document.body.appendChild(n),n.click(),URL.revokeObjectURL(o),document.body.removeChild(n)}},O={getOffsetTop(e){if(!e)throw new Error("Element is not provided");if(e instanceof SVGElement){const n=e.getBoundingClientRect(),o=window.pageYOffset||document.documentElement.scrollTop;return n.top+o}let t=0;for(;e;)t+=e.offsetTop,e=e.offsetParent;return t}},j={sortFromA2Z(e){return e!=null&&e.length?e.sort((n,o)=>{const s=n.toUpperCase(),l=o.toUpperCase();return s<l?-1:s>l?1:0}):void 0}},A={...Object.freeze(Object.defineProperty({__proto__:null,toLetter:e=>e>25||e<0?"":"ABCDEFGHIJKLMNOPQRSTUVWXYZ"[e]},Symbol.toStringTag,{value:"Module"})),...Object.freeze(Object.defineProperty({__proto__:null,toMoney:e=>e.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g,",")},Symbol.toStringTag,{value:"Module"})),isNumber(e){return typeof e=="number"&&!isNaN(e)}},E={isEmptyObject(e){return e?Object.keys(e).length===0&&e.constructor===Object:!0}},M={desensitize(e){return e?e.replace(/^(\d{3})\d{4}(\d{4})$/,"$1****$2")||e.slice(0,3)+"****"+e.slice(7):""}},_={color:function(){return`#${Math.random().toString(16).slice(2,8)}`},int(e,t){return Math.floor(Math.random()*(t-e+1))+e},uniqueId(){return Date.now().toString(36)+Math.random().toString(36).substr(2,5)}},C={getLocal(e){return localStorage.getItem(e)},setLocal(e,t){localStorage.setItem(e,t)},removeLocal(e){localStorage.removeItem(e)},getSession(e){return sessionStorage.getItem(e)},setSession(e,t){sessionStorage.setItem(e,t)},removeSession(e){sessionStorage.removeItem(e)}},T={isExist(e,t){if(!e||!t)return!1;const n=e.split(",");return console.log("判断某元素是否在字符串中",n.indexOf(t)===-1),n.indexOf(t)!==-1},includes(e,t){return!e||!t?!1:e.includes(t)},isJSON(e){try{return JSON.parse(e),!0}catch{return!1}}},L=e=>{const n=new RegExp("[?&]"+e+"=([^&#]*)","i").exec(window.location.href);return n?decodeURIComponent(n[1]):null};function B(e){return new URLSearchParams(window.location.search).get(e)}const k={...Object.freeze(Object.defineProperty({__proto__:null,getQueryInfoByName:L,getQueryParam:B},Symbol.toStringTag,{value:"Module"})),getHost(){return window.location.host},getPath(){return window.location.pathname}},I={toList(e,t=2){console.log(e,t);const n={};for(let s=0;s<t;s++)n[s]=[];e.forEach((s,l)=>n[l%t].push(s));const o=[];for(const s in n)o.push(...n[s]);return o}};r.arrayUtils=h,r.base64Utils=p,r.booleanUtils=m,r.cookieUtils=U,r.dateUtils=w,r.downloadUtils=S,r.elementUtils=O,r.letterUtils=j,r.numberUtils=A,r.objectUtils=E,r.phoneUtils=M,r.randomUtils=_,r.storageUtils=C,r.stringUtils=T,r.urlUtils=k,r.waterfallUtils=I,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -7,7 +7,7 @@ export declare const arrayUtils: {
|
|
|
7
7
|
* @param arr 数组
|
|
8
8
|
* @returns 去重后的数组
|
|
9
9
|
*/
|
|
10
|
-
unique
|
|
10
|
+
unique<T>(arr: T[]): T[];
|
|
11
11
|
/**
|
|
12
12
|
* 判断某元素是否在数组中
|
|
13
13
|
* @param arr 数组
|
|
@@ -41,5 +41,6 @@ export declare const arrayUtils: {
|
|
|
41
41
|
* @returns
|
|
42
42
|
*/
|
|
43
43
|
shuffle<T>(arr: T[]): T[];
|
|
44
|
-
|
|
44
|
+
} & {
|
|
45
|
+
toEnumObj: (arr: import("@yh-kit/types").IOptionItem[]) => import("@yh-kit/types").TValueEnum;
|
|
45
46
|
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base64 相关操作工具函数
|
|
3
|
+
*/
|
|
4
|
+
export declare const base64Utils: {
|
|
5
|
+
/**
|
|
6
|
+
* 将Base64编码的字符串转换为Blob对象。
|
|
7
|
+
* Blob对象用于表示二进制大型对象,可以在浏览器环境中处理大文件或二进制数据。
|
|
8
|
+
*
|
|
9
|
+
* @param base64 Base64编码的字符串。
|
|
10
|
+
* @param contentType 可选参数,表示生成的Blob对象的MIME类型,默认为空字符串。
|
|
11
|
+
* @param sliceSize 可选参数,表示分片大小,默认为512字节。用于控制每次处理的Base64字符串长度,以优化大文件的处理。
|
|
12
|
+
* @returns 返回转换后的Blob对象,如果转换失败,则返回null。
|
|
13
|
+
*/
|
|
14
|
+
toBlob(base64: string, contentType?: string, sliceSize?: number): Blob | null;
|
|
15
|
+
/**
|
|
16
|
+
* 将Base64编码的字符串转换为File对象。
|
|
17
|
+
* 这个函数接受一个Base64编码的字符串,一个可选的文件名和一个可选的MIME类型,
|
|
18
|
+
* 并返回一个代表原始数据的File对象。
|
|
19
|
+
*
|
|
20
|
+
* @param base64 Base64编码的字符串。
|
|
21
|
+
* @param fileName 文件名,默认为"file.txt"。
|
|
22
|
+
* @param mimeType MIME类型,默认为"text/plain"。
|
|
23
|
+
* @returns 返回一个File对象,包含解码后的数据。
|
|
24
|
+
*/
|
|
25
|
+
toFile(base64: string, fileName?: string, mimeType?: string): File;
|
|
26
|
+
};
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
export * from "./array";
|
|
2
|
+
export * from "./base64";
|
|
2
3
|
export * from "./boolean";
|
|
3
4
|
export * from "./cookie";
|
|
4
5
|
export * from "./date";
|
|
6
|
+
export * from "./download";
|
|
7
|
+
export * from "./element";
|
|
5
8
|
export * from "./letter";
|
|
6
9
|
export * from "./number";
|
|
7
10
|
export * from "./object";
|
|
@@ -7,6 +7,11 @@ export declare const urlUtils: {
|
|
|
7
7
|
* @returns 当前url链接的host信息
|
|
8
8
|
*/
|
|
9
9
|
getHost(): string;
|
|
10
|
+
/**
|
|
11
|
+
* 获取当前页面路径
|
|
12
|
+
* @returns 当前url链接的pathname信息
|
|
13
|
+
*/
|
|
14
|
+
getPath(): string;
|
|
10
15
|
getQueryParam(name: string): string | null;
|
|
11
16
|
getQueryInfoByName: (name: string) => string | null;
|
|
12
17
|
};
|