@yh-kit/utils 1.2.2 → 1.3.1
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 +411 -48
- package/dist/utils.umd.cjs +1 -1
- package/package.json +1 -1
- package/types/utils/lib/array/index.d.ts +13 -1
- package/types/utils/lib/boolean/index.d.ts +110 -0
- package/types/utils/lib/cookie/index.d.ts +25 -0
- package/types/utils/lib/date/index.d.ts +24 -0
- package/types/utils/lib/index.d.ts +7 -0
- package/types/utils/lib/number/index.d.ts +6 -0
- package/types/utils/lib/object/index.d.ts +11 -0
- package/types/utils/lib/random/index.d.ts +22 -0
- package/types/utils/lib/storage/index.d.ts +39 -0
- package/types/utils/lib/string/index.d.ts +6 -0
- package/types/utils/lib/url/index.d.ts +6 -0
- package/types/utils/lib/url/query.d.ts +12 -4
- package/types/utils/lib/waterfall/index.d.ts +10 -5
package/dist/utils.js
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
const
|
|
2
|
-
if (!
|
|
3
|
-
const
|
|
4
|
-
return
|
|
1
|
+
const i = (t) => {
|
|
2
|
+
if (!t || !t.length) return {};
|
|
3
|
+
const e = {};
|
|
4
|
+
return t.forEach((r) => {
|
|
5
5
|
const n = Object.assign({ label: r.label, text: r.label }, r);
|
|
6
|
-
|
|
7
|
-
}),
|
|
8
|
-
},
|
|
6
|
+
e[r == null ? void 0 : r.value] = n;
|
|
7
|
+
}), e;
|
|
8
|
+
}, u = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
9
9
|
__proto__: null,
|
|
10
|
-
toEnumObj:
|
|
11
|
-
}, Symbol.toStringTag, { value: "Module" })),
|
|
12
|
-
...
|
|
10
|
+
toEnumObj: i
|
|
11
|
+
}, Symbol.toStringTag, { value: "Module" })), h = {
|
|
12
|
+
...u,
|
|
13
13
|
/**
|
|
14
14
|
* 数组去重
|
|
15
15
|
* @param arr 数组
|
|
16
16
|
* @returns 去重后的数组
|
|
17
17
|
*/
|
|
18
|
-
unique: (
|
|
18
|
+
unique: (t) => t ? Array.from(new Set(t)) : [],
|
|
19
19
|
/**
|
|
20
20
|
* 判断某元素是否在数组中
|
|
21
21
|
* @param arr 数组
|
|
22
22
|
* @param element 查询元素
|
|
23
23
|
* @returns 是否存在。true 存在;false 不存在
|
|
24
24
|
*/
|
|
25
|
-
isExist(
|
|
26
|
-
return !
|
|
25
|
+
isExist(t, e) {
|
|
26
|
+
return !t || !e ? !1 : t.indexOf(e) !== -1;
|
|
27
27
|
},
|
|
28
28
|
/**
|
|
29
29
|
* 统计数组中某元素出现的次数:对于大型数组,手动循环的性能略优于 filter 和 reduce。
|
|
@@ -31,10 +31,10 @@ const o = (e) => {
|
|
|
31
31
|
* @param target 目标元素
|
|
32
32
|
* @returns 出现次数
|
|
33
33
|
*/
|
|
34
|
-
countOfAppear(
|
|
34
|
+
countOfAppear(t, e) {
|
|
35
35
|
let r = 0;
|
|
36
|
-
for (const n of
|
|
37
|
-
n ===
|
|
36
|
+
for (const n of t)
|
|
37
|
+
n === e && r++;
|
|
38
38
|
return r;
|
|
39
39
|
},
|
|
40
40
|
/**
|
|
@@ -43,37 +43,352 @@ const o = (e) => {
|
|
|
43
43
|
* @param order 排序方式:asc 升序;desc 降序
|
|
44
44
|
* @returns 排序后的数组
|
|
45
45
|
*/
|
|
46
|
-
sort(
|
|
47
|
-
return
|
|
46
|
+
sort(t, e = "asc") {
|
|
47
|
+
return t ? t.sort((r, n) => e === "asc" ? r > n ? 1 : -1 : r < n ? 1 : -1) : [];
|
|
48
|
+
},
|
|
49
|
+
/**
|
|
50
|
+
* 数组乱序(洗牌算法)
|
|
51
|
+
* 1. 从数组的最后一个元素开始,将其与随机位置的元素交换位置。
|
|
52
|
+
* 2. 然后,从倒数第二个元素开始,将其与随机位置的元素交换位置。
|
|
53
|
+
* 3. 以此类推,直到第一个元素。
|
|
54
|
+
* 4. 这样,数组中的元素就会被随机打乱顺序。
|
|
55
|
+
* 5. 注意,这个算法会改变原数组。如果不想改变原数组,可以先复制一份再进行操作。
|
|
56
|
+
* 6. 时间复杂度:O(n)
|
|
57
|
+
* @param arr 数组
|
|
58
|
+
* @returns
|
|
59
|
+
*/
|
|
60
|
+
shuffle(t) {
|
|
61
|
+
if (!t)
|
|
62
|
+
return [];
|
|
63
|
+
for (let e = t.length - 1; e > 0; e--) {
|
|
64
|
+
const r = Math.floor(Math.random() * (e + 1));
|
|
65
|
+
[t[e], t[r]] = [t[r], t[e]];
|
|
66
|
+
}
|
|
67
|
+
return t;
|
|
68
|
+
}
|
|
69
|
+
}, y = {
|
|
70
|
+
/**
|
|
71
|
+
* 检查是否为空字符串
|
|
72
|
+
* @param str 要检查的字符串
|
|
73
|
+
* @returns 如果字符串为空,则返回 true,否则返回 false
|
|
74
|
+
*/
|
|
75
|
+
isEmptyString(t) {
|
|
76
|
+
return typeof t == "string" && t.trim() === "";
|
|
77
|
+
},
|
|
78
|
+
/**
|
|
79
|
+
* 判断是否为数字
|
|
80
|
+
* @param val 要检查的值
|
|
81
|
+
* @returns 如果值是数字,则返回 true,否则返回 false
|
|
82
|
+
*/
|
|
83
|
+
isNumber(t) {
|
|
84
|
+
return typeof t == "number" && !isNaN(t);
|
|
85
|
+
},
|
|
86
|
+
/**
|
|
87
|
+
* 判断是否为对象
|
|
88
|
+
* @param val 要检查的值
|
|
89
|
+
* @returns 如果值是对象,则返回 true,否则返回 false
|
|
90
|
+
*/
|
|
91
|
+
isObject(t) {
|
|
92
|
+
return t !== null && typeof t == "object" && !Array.isArray(t);
|
|
93
|
+
},
|
|
94
|
+
/**
|
|
95
|
+
* 判断对象是否为空
|
|
96
|
+
* @param obj 要检查的对象
|
|
97
|
+
* @returns 如果对象为空,则返回 true,否则返回 false
|
|
98
|
+
*/
|
|
99
|
+
isEmptyObject(t) {
|
|
100
|
+
return t ? Object.keys(t).length === 0 && t.constructor === Object : !0;
|
|
101
|
+
},
|
|
102
|
+
/**
|
|
103
|
+
* 判断是否为布尔值
|
|
104
|
+
* @param val 要检查的值
|
|
105
|
+
* @returns 如果值是布尔值,则返回 true,否则返回 false
|
|
106
|
+
*/
|
|
107
|
+
isBoolean(t) {
|
|
108
|
+
return typeof t == "boolean";
|
|
109
|
+
},
|
|
110
|
+
/**
|
|
111
|
+
* 判断是否为数组
|
|
112
|
+
* @param val 要检查的值
|
|
113
|
+
* @returns 如果值是数组,则返回 true,否则返回 false
|
|
114
|
+
*/
|
|
115
|
+
isArray(t) {
|
|
116
|
+
return Array.isArray(t);
|
|
117
|
+
},
|
|
118
|
+
/**
|
|
119
|
+
* 判断是否为函数
|
|
120
|
+
* @param val 要检查的值
|
|
121
|
+
* @returns
|
|
122
|
+
*/
|
|
123
|
+
isFunction(t) {
|
|
124
|
+
return typeof t == "function";
|
|
125
|
+
},
|
|
126
|
+
/**
|
|
127
|
+
* 判断是否为 Promise 对象
|
|
128
|
+
* @param obj 要检查的对象
|
|
129
|
+
* @returns 如果对象是 Promise 对象,则返回 true,否则返回 false
|
|
130
|
+
*/
|
|
131
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
132
|
+
isPromise(t) {
|
|
133
|
+
return !!t && (typeof t == "object" || typeof t == "function") && typeof t.then == "function";
|
|
134
|
+
},
|
|
135
|
+
/**
|
|
136
|
+
* 判断是否为 DOM 元素
|
|
137
|
+
* @param obj 要检查的对象
|
|
138
|
+
* @returns 如果对象是 DOM 元素,则返回 true,否则返回 false
|
|
139
|
+
*/
|
|
140
|
+
isElement(t) {
|
|
141
|
+
return t instanceof Element;
|
|
142
|
+
},
|
|
143
|
+
/**
|
|
144
|
+
* 判断手机号格式(中国)
|
|
145
|
+
* @param str 手机号字符串
|
|
146
|
+
* @returns true 表示手机号格式正确,false 表示手机号格式错误
|
|
147
|
+
*/
|
|
148
|
+
isPhone(t) {
|
|
149
|
+
return /^1[3-9]\d{9}$/.test(t);
|
|
150
|
+
},
|
|
151
|
+
/**
|
|
152
|
+
* 判断邮箱格式
|
|
153
|
+
* @param str 邮箱字符串
|
|
154
|
+
* @returns true 表示邮箱格式正确,false 表示邮箱格式错误
|
|
155
|
+
*/
|
|
156
|
+
isEmail(t) {
|
|
157
|
+
return /^[\w.-]+@[\w.-]+\.\w+$/.test(t);
|
|
158
|
+
},
|
|
159
|
+
/**
|
|
160
|
+
* 判断字符串是否为 JSON
|
|
161
|
+
* @param str 字符串
|
|
162
|
+
* @returns true 表示是 JSON,false 表示不是 JSON
|
|
163
|
+
*/
|
|
164
|
+
isJSON(t) {
|
|
165
|
+
try {
|
|
166
|
+
return JSON.parse(t), !0;
|
|
167
|
+
} catch {
|
|
168
|
+
return !1;
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
/**
|
|
172
|
+
* 判断图片是否加载完成
|
|
173
|
+
* @param img 图片对象
|
|
174
|
+
* @returns true 表示图片加载完成,false 表示图片未加载完成
|
|
175
|
+
* @example
|
|
176
|
+
* const img = new Image();
|
|
177
|
+
* img.src = "URL_ADDRESS * img.src = "https://example.com/image.jpg";
|
|
178
|
+
*/
|
|
179
|
+
isImageLoaded(t) {
|
|
180
|
+
return t.complete && t.naturalWidth !== 0;
|
|
181
|
+
},
|
|
182
|
+
/**
|
|
183
|
+
* 判断元素是否在可视区域内
|
|
184
|
+
* @param el 元素对象
|
|
185
|
+
* @returns true 表示元素在可视区域内,false 表示元素不在可视区域内
|
|
186
|
+
*/
|
|
187
|
+
isInViewport(t) {
|
|
188
|
+
const e = t.getBoundingClientRect();
|
|
189
|
+
return e.top >= 0 && e.left >= 0 && e.bottom <= window.innerHeight && e.right <= window.innerWidth;
|
|
190
|
+
},
|
|
191
|
+
/**
|
|
192
|
+
* 判断是否为闰年
|
|
193
|
+
* @param year 年份
|
|
194
|
+
* @returns true 表示是闰年,false 表示不是闰年
|
|
195
|
+
* @example
|
|
196
|
+
* isLeapYear(2020); // true
|
|
197
|
+
* isLeapYear(2021); // false
|
|
198
|
+
* isLeapYear(2000); // true
|
|
199
|
+
* isLeapYear(1900); // false
|
|
200
|
+
* isLeapYear(2025); // false
|
|
201
|
+
* isLeapYear(2024); // true
|
|
202
|
+
*/
|
|
203
|
+
isLeapYear(t) {
|
|
204
|
+
return t % 4 === 0 && t % 100 !== 0 || t % 400 === 0;
|
|
205
|
+
},
|
|
206
|
+
/**
|
|
207
|
+
* 判断是否为移动端
|
|
208
|
+
* @returns true 表示是移动端,false 表示不是移动端
|
|
209
|
+
*/
|
|
210
|
+
isMobile() {
|
|
211
|
+
return /Mobi|Android|iPhone/i.test(navigator.userAgent);
|
|
212
|
+
}
|
|
213
|
+
}, m = {
|
|
214
|
+
/**
|
|
215
|
+
* 通过传入的name获取cookie的值
|
|
216
|
+
* @param name cookie的name
|
|
217
|
+
* @returns cookie的值
|
|
218
|
+
*/
|
|
219
|
+
getCookie(t) {
|
|
220
|
+
if (!t) return;
|
|
221
|
+
const e = document.cookie.match(new RegExp("(^| )" + t + "=([^;]+)"));
|
|
222
|
+
return e ? decodeURIComponent(e[2]) : null;
|
|
223
|
+
},
|
|
224
|
+
/**
|
|
225
|
+
* 设置 cookie
|
|
226
|
+
* @param name cookie的name
|
|
227
|
+
* @param value cookie的值
|
|
228
|
+
* @param days cookie的过期时间,默认7天。如果为0,则表示cookie在会话结束时过期。如果为负数,则表示cookie已过期。
|
|
229
|
+
* @returns void
|
|
230
|
+
*/
|
|
231
|
+
setCookie(t, e, r = 7) {
|
|
232
|
+
const n = /* @__PURE__ */ new Date();
|
|
233
|
+
n.setTime(n.getTime() + r * 24 * 60 * 60 * 1e3), document.cookie = `${t}=${encodeURIComponent(e)};expires=${n.toUTCString()};path=/`;
|
|
234
|
+
},
|
|
235
|
+
/**
|
|
236
|
+
* 删除cookie
|
|
237
|
+
* @param name cookie的name
|
|
238
|
+
* @returns void
|
|
239
|
+
*/
|
|
240
|
+
deleteCookie(t) {
|
|
241
|
+
this.setCookie(t, "", -1);
|
|
242
|
+
}
|
|
243
|
+
}, b = {
|
|
244
|
+
/**
|
|
245
|
+
* 获取当前时间字符串
|
|
246
|
+
* @param locales 区域设置。如:'zh-CN','en-US',"chinese"。默认'zh-CN'。
|
|
247
|
+
* @param hour12 是否使用12小时制。默认false,使用24小时制。
|
|
248
|
+
* @returns 当前时间的字符串。如:'2025/5/27 16:54:45'
|
|
249
|
+
*/
|
|
250
|
+
getTimeString(t, e = !1) {
|
|
251
|
+
return e ? (/* @__PURE__ */ new Date()).toLocaleString(t, { hour12: !0 }) : (/* @__PURE__ */ new Date()).toLocaleString("chinese", { hour12: !1 });
|
|
252
|
+
},
|
|
253
|
+
/**
|
|
254
|
+
* 计算两个日期相差天数
|
|
255
|
+
* @param date1 日期1
|
|
256
|
+
* @param date2 日期2
|
|
257
|
+
* @returns 相差天数
|
|
258
|
+
*/
|
|
259
|
+
diffDays(t, e) {
|
|
260
|
+
const r = new Date(t).getTime(), n = new Date(e).getTime();
|
|
261
|
+
return r > n ? Math.abs(Math.floor((r - n) / (24 * 3600 * 1e3))) : Math.abs(Math.floor((n - r) / (24 * 3600 * 1e3)));
|
|
262
|
+
},
|
|
263
|
+
/**
|
|
264
|
+
* 生成唯一ID(时间戳+随机数)
|
|
265
|
+
* @returns 唯一ID
|
|
266
|
+
*/
|
|
267
|
+
uniqueId() {
|
|
268
|
+
return Date.now().toString(36) + Math.random().toString(36).substr(2, 5);
|
|
48
269
|
}
|
|
49
|
-
},
|
|
270
|
+
}, S = {
|
|
271
|
+
/**
|
|
272
|
+
* 将选中的字母数组从A-Z排序:用于试题选择答案的排序实例
|
|
273
|
+
* @param arr 字母数组
|
|
274
|
+
* @returns
|
|
275
|
+
*/
|
|
276
|
+
sortFromA2Z: (t) => t != null && t.length ? t.sort((r, n) => {
|
|
277
|
+
const o = r.toUpperCase(), s = n.toUpperCase();
|
|
278
|
+
return o < s ? -1 : o > s ? 1 : 0;
|
|
279
|
+
}) : void 0
|
|
280
|
+
}, c = (t) => t > 25 || t < 0 ? "" : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[t], l = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
50
281
|
__proto__: null,
|
|
51
|
-
toLetter:
|
|
52
|
-
}, Symbol.toStringTag, { value: "Module" })),
|
|
282
|
+
toLetter: c
|
|
283
|
+
}, Symbol.toStringTag, { value: "Module" })), a = (t) => t.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","), f = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
53
284
|
__proto__: null,
|
|
54
|
-
toMoney:
|
|
55
|
-
}, Symbol.toStringTag, { value: "Module" })),
|
|
56
|
-
...
|
|
57
|
-
...
|
|
58
|
-
|
|
285
|
+
toMoney: a
|
|
286
|
+
}, Symbol.toStringTag, { value: "Module" })), O = {
|
|
287
|
+
...l,
|
|
288
|
+
...f,
|
|
289
|
+
/**
|
|
290
|
+
* 判断是否为数字
|
|
291
|
+
* @param val 待判断的值
|
|
292
|
+
* @returns 是否为数字。true 是;false 否
|
|
293
|
+
*/
|
|
294
|
+
isNumber(t) {
|
|
295
|
+
return typeof t == "number" && !isNaN(t);
|
|
296
|
+
}
|
|
297
|
+
}, w = {
|
|
298
|
+
/**
|
|
299
|
+
* 判断对象是否为空
|
|
300
|
+
* @param obj 对象
|
|
301
|
+
* @returns 是否为空。true 为空;false 不为空
|
|
302
|
+
*/
|
|
303
|
+
isEmptyObject: (t) => t ? Object.keys(t).length === 0 && t.constructor === Object : !0
|
|
304
|
+
}, U = {
|
|
59
305
|
/**
|
|
60
306
|
* 脱敏
|
|
61
307
|
* @param phone 电话号码/手机号码
|
|
62
308
|
* @returns 脱敏后的电话号码/手机号码
|
|
63
309
|
*/
|
|
64
|
-
desensitize: (
|
|
65
|
-
},
|
|
310
|
+
desensitize: (t) => t ? t.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2") || t.slice(0, 3) + "****" + t.slice(7) : ""
|
|
311
|
+
}, M = {
|
|
312
|
+
/**
|
|
313
|
+
* 生成随机颜色
|
|
314
|
+
* @returns 随机颜色值
|
|
315
|
+
*/
|
|
316
|
+
color: () => `#${Math.random().toString(16).slice(2, 8)}`,
|
|
317
|
+
/**
|
|
318
|
+
* 生成指定范围的随机整数
|
|
319
|
+
* @param min 最小值
|
|
320
|
+
* @param max 最大值
|
|
321
|
+
* @returns 随机数
|
|
322
|
+
*/
|
|
323
|
+
int(t, e) {
|
|
324
|
+
return Math.floor(Math.random() * (e - t + 1)) + t;
|
|
325
|
+
},
|
|
326
|
+
/**
|
|
327
|
+
* 生成唯一ID(时间戳+随机数)
|
|
328
|
+
* @returns 唯一ID
|
|
329
|
+
*/
|
|
330
|
+
uniqueId() {
|
|
331
|
+
return Date.now().toString(36) + Math.random().toString(36).substr(2, 5);
|
|
332
|
+
}
|
|
333
|
+
}, _ = {
|
|
334
|
+
/**
|
|
335
|
+
* 通过key值获取 localStorage 中存储的某个值
|
|
336
|
+
* @param key 获取的key
|
|
337
|
+
* @returns 获取的值
|
|
338
|
+
*/
|
|
339
|
+
getLocal(t) {
|
|
340
|
+
return localStorage.getItem(t);
|
|
341
|
+
},
|
|
342
|
+
/**
|
|
343
|
+
* 设置localStorage中的某个值
|
|
344
|
+
* @param key 设置的key
|
|
345
|
+
* @param value 设置的value
|
|
346
|
+
*/
|
|
347
|
+
setLocal(t, e) {
|
|
348
|
+
localStorage.setItem(t, e);
|
|
349
|
+
},
|
|
350
|
+
/**
|
|
351
|
+
* 删除localStorage中的某个值
|
|
352
|
+
* @param key 删除的key
|
|
353
|
+
*/
|
|
354
|
+
removeLocal(t) {
|
|
355
|
+
localStorage.removeItem(t);
|
|
356
|
+
},
|
|
357
|
+
/**
|
|
358
|
+
* 通过key值获取 sessionStorage 中存储的某个值
|
|
359
|
+
* @param key 获取的key
|
|
360
|
+
* @returns 获取的值
|
|
361
|
+
*/
|
|
362
|
+
getSession(t) {
|
|
363
|
+
return sessionStorage.getItem(t);
|
|
364
|
+
},
|
|
365
|
+
/**
|
|
366
|
+
* 设置sessionStorage中的某个值
|
|
367
|
+
* @param key 设置的key
|
|
368
|
+
* @param value 设置的value
|
|
369
|
+
*/
|
|
370
|
+
setSession(t, e) {
|
|
371
|
+
sessionStorage.setItem(t, e);
|
|
372
|
+
},
|
|
373
|
+
/**
|
|
374
|
+
* 删除sessionStorage中的某个值
|
|
375
|
+
* @param key 删除的key
|
|
376
|
+
*/
|
|
377
|
+
removeSession(t) {
|
|
378
|
+
sessionStorage.removeItem(t);
|
|
379
|
+
}
|
|
380
|
+
}, j = {
|
|
66
381
|
/**
|
|
67
382
|
* 判断某元素是否在字符串中-比includes()方法更兼容,includes为ES6新增方法,IE不支持。小程序也不支持
|
|
68
383
|
* @param str 字符串
|
|
69
384
|
* @param element 查询元素
|
|
70
385
|
* @returns 是否存在。true 存在;false 不存在
|
|
71
386
|
*/
|
|
72
|
-
isExist(
|
|
73
|
-
if (!
|
|
387
|
+
isExist(t, e) {
|
|
388
|
+
if (!t || !e)
|
|
74
389
|
return !1;
|
|
75
|
-
const r =
|
|
76
|
-
return console.log("判断某元素是否在字符串中", r.indexOf(
|
|
390
|
+
const r = t.split(",");
|
|
391
|
+
return console.log("判断某元素是否在字符串中", r.indexOf(e) === -1), r.indexOf(e) !== -1;
|
|
77
392
|
},
|
|
78
393
|
/**
|
|
79
394
|
* 判断某元素是否在字符串中-比isExist()方法更高效。
|
|
@@ -81,24 +396,72 @@ const o = (e) => {
|
|
|
81
396
|
* @param element 查询元素
|
|
82
397
|
* @returns 是否存在。true 存在;false 不存在
|
|
83
398
|
*/
|
|
84
|
-
includes(
|
|
85
|
-
return !
|
|
399
|
+
includes(t, e) {
|
|
400
|
+
return !t || !e ? !1 : t.includes(e);
|
|
401
|
+
},
|
|
402
|
+
/**
|
|
403
|
+
* 判断字符串是否为 JSON
|
|
404
|
+
* @param str 字符串
|
|
405
|
+
* @returns
|
|
406
|
+
*/
|
|
407
|
+
isJSON(t) {
|
|
408
|
+
try {
|
|
409
|
+
return JSON.parse(t), !0;
|
|
410
|
+
} catch {
|
|
411
|
+
return !1;
|
|
412
|
+
}
|
|
86
413
|
}
|
|
87
|
-
},
|
|
88
|
-
const r = new RegExp("[?&]" +
|
|
414
|
+
}, g = (t) => {
|
|
415
|
+
const r = new RegExp("[?&]" + t + "=([^&#]*)", "i").exec(window.location.href);
|
|
89
416
|
return r ? decodeURIComponent(r[1]) : null;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
417
|
+
};
|
|
418
|
+
function d(t) {
|
|
419
|
+
return new URLSearchParams(window.location.search).get(t);
|
|
420
|
+
}
|
|
421
|
+
const p = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
422
|
+
__proto__: null,
|
|
423
|
+
getQueryInfoByName: g,
|
|
424
|
+
getQueryParam: d
|
|
425
|
+
}, Symbol.toStringTag, { value: "Module" })), E = {
|
|
426
|
+
...p,
|
|
427
|
+
/**
|
|
428
|
+
* 获取当前域名
|
|
429
|
+
* @returns 当前url链接的host信息
|
|
430
|
+
*/
|
|
431
|
+
getHost() {
|
|
432
|
+
return window.location.host;
|
|
433
|
+
}
|
|
434
|
+
}, I = {
|
|
435
|
+
/**
|
|
436
|
+
* 处理瀑布流数据,使其适合瀑布流布局展示
|
|
437
|
+
* @param list 瀑布流数据-数组
|
|
438
|
+
* @param columnsNum 需要展示的列数
|
|
439
|
+
* @returns 适合瀑布流布局的数组
|
|
440
|
+
*/
|
|
441
|
+
toList: (t, e = 2) => {
|
|
442
|
+
console.log(t, e);
|
|
443
|
+
const r = {};
|
|
444
|
+
for (let o = 0; o < e; o++)
|
|
445
|
+
r[o] = [];
|
|
446
|
+
t.forEach((o, s) => r[s % e].push(o));
|
|
447
|
+
const n = [];
|
|
448
|
+
for (const o in r)
|
|
449
|
+
n.push(...r[o]);
|
|
450
|
+
return n;
|
|
451
|
+
}
|
|
96
452
|
};
|
|
97
453
|
export {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
454
|
+
h as arrayUtils,
|
|
455
|
+
y as booleanUtils,
|
|
456
|
+
m as cookieUtils,
|
|
457
|
+
b as dateUtils,
|
|
458
|
+
S as letterUtils,
|
|
459
|
+
O as numberUtils,
|
|
460
|
+
w as objectUtils,
|
|
461
|
+
U as phoneUtils,
|
|
462
|
+
M as randomUtils,
|
|
463
|
+
_ as storageUtils,
|
|
464
|
+
j as stringUtils,
|
|
465
|
+
E as urlUtils,
|
|
466
|
+
I as waterfallUtils
|
|
104
467
|
};
|
package/dist/utils.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(o,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(o=typeof globalThis<"u"?globalThis:o||self,s(o.yhkitUtils={}))})(this,function(o){"use strict";const l={...Object.freeze(Object.defineProperty({__proto__:null,toEnumObj:t=>{if(!t||!t.length)return{};const e={};return t.forEach(r=>{const n=Object.assign({label:r.label,text:r.label},r);e[r==null?void 0:r.value]=n}),e}},Symbol.toStringTag,{value:"Module"})),unique:t=>t?Array.from(new Set(t)):[],isExist(t,e){return!t||!e?!1:t.indexOf(e)!==-1},countOfAppear(t,e){let r=0;for(const n of t)n===e&&r++;return r},sort(t,e="asc"){return t?t.sort((r,n)=>e==="asc"?r>n?1:-1:r<n?1:-1):[]},shuffle(t){if(!t)return[];for(let e=t.length-1;e>0;e--){const r=Math.floor(Math.random()*(e+1));[t[e],t[r]]=[t[r],t[e]]}return t}},c={isEmptyString(t){return typeof t=="string"&&t.trim()===""},isNumber(t){return typeof t=="number"&&!isNaN(t)},isObject(t){return t!==null&&typeof t=="object"&&!Array.isArray(t)},isEmptyObject(t){return t?Object.keys(t).length===0&&t.constructor===Object:!0},isBoolean(t){return typeof t=="boolean"},isArray(t){return Array.isArray(t)},isFunction(t){return typeof t=="function"},isPromise(t){return!!t&&(typeof t=="object"||typeof t=="function")&&typeof t.then=="function"},isElement(t){return t instanceof Element},isPhone(t){return/^1[3-9]\d{9}$/.test(t)},isEmail(t){return/^[\w.-]+@[\w.-]+\.\w+$/.test(t)},isJSON(t){try{return JSON.parse(t),!0}catch{return!1}},isImageLoaded(t){return t.complete&&t.naturalWidth!==0},isInViewport(t){const e=t.getBoundingClientRect();return e.top>=0&&e.left>=0&&e.bottom<=window.innerHeight&&e.right<=window.innerWidth},isLeapYear(t){return t%4===0&&t%100!==0||t%400===0},isMobile(){return/Mobi|Android|iPhone/i.test(navigator.userAgent)}},a={getCookie(t){if(!t)return;const e=document.cookie.match(new RegExp("(^| )"+t+"=([^;]+)"));return e?decodeURIComponent(e[2]):null},setCookie(t,e,r=7){const n=new Date;n.setTime(n.getTime()+r*24*60*60*1e3),document.cookie=`${t}=${encodeURIComponent(e)};expires=${n.toUTCString()};path=/`},deleteCookie(t){this.setCookie(t,"",-1)}},f={getTimeString(t,e=!1){return e?new Date().toLocaleString(t,{hour12:!0}):new Date().toLocaleString("chinese",{hour12:!1})},diffDays(t,e){const r=new Date(t).getTime(),n=new Date(e).getTime();return r>n?Math.abs(Math.floor((r-n)/(24*3600*1e3))):Math.abs(Math.floor((n-r)/(24*3600*1e3)))},uniqueId(){return Date.now().toString(36)+Math.random().toString(36).substr(2,5)}},d={sortFromA2Z:t=>t!=null&&t.length?t.sort((r,n)=>{const i=r.toUpperCase(),u=n.toUpperCase();return i<u?-1:i>u?1:0}):void 0},g={...Object.freeze(Object.defineProperty({__proto__:null,toLetter:t=>t>25||t<0?"":"ABCDEFGHIJKLMNOPQRSTUVWXYZ"[t]},Symbol.toStringTag,{value:"Module"})),...Object.freeze(Object.defineProperty({__proto__:null,toMoney:t=>t.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g,",")},Symbol.toStringTag,{value:"Module"})),isNumber(t){return typeof t=="number"&&!isNaN(t)}},y={isEmptyObject:t=>t?Object.keys(t).length===0&&t.constructor===Object:!0},m={desensitize:t=>t?t.replace(/^(\d{3})\d{4}(\d{4})$/,"$1****$2")||t.slice(0,3)+"****"+t.slice(7):""},b={color:()=>`#${Math.random().toString(16).slice(2,8)}`,int(t,e){return Math.floor(Math.random()*(e-t+1))+t},uniqueId(){return Date.now().toString(36)+Math.random().toString(36).substr(2,5)}},h={getLocal(t){return localStorage.getItem(t)},setLocal(t,e){localStorage.setItem(t,e)},removeLocal(t){localStorage.removeItem(t)},getSession(t){return sessionStorage.getItem(t)},setSession(t,e){sessionStorage.setItem(t,e)},removeSession(t){sessionStorage.removeItem(t)}},p={isExist(t,e){if(!t||!e)return!1;const r=t.split(",");return console.log("判断某元素是否在字符串中",r.indexOf(e)===-1),r.indexOf(e)!==-1},includes(t,e){return!t||!e?!1:t.includes(e)},isJSON(t){try{return JSON.parse(t),!0}catch{return!1}}},S=t=>{const r=new RegExp("[?&]"+t+"=([^&#]*)","i").exec(window.location.href);return r?decodeURIComponent(r[1]):null};function U(t){return new URLSearchParams(window.location.search).get(t)}const O={...Object.freeze(Object.defineProperty({__proto__:null,getQueryInfoByName:S,getQueryParam:U},Symbol.toStringTag,{value:"Module"})),getHost(){return window.location.host}},w={toList:(t,e=2)=>{console.log(t,e);const r={};for(let i=0;i<e;i++)r[i]=[];t.forEach((i,u)=>r[u%e].push(i));const n=[];for(const i in r)n.push(...r[i]);return n}};o.arrayUtils=l,o.booleanUtils=c,o.cookieUtils=a,o.dateUtils=f,o.letterUtils=d,o.numberUtils=g,o.objectUtils=y,o.phoneUtils=m,o.randomUtils=b,o.storageUtils=h,o.stringUtils=p,o.urlUtils=O,o.waterfallUtils=w,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -29,5 +29,17 @@ export declare const arrayUtils: {
|
|
|
29
29
|
* @returns 排序后的数组
|
|
30
30
|
*/
|
|
31
31
|
sort<T>(arr: T[], order?: "asc" | "desc"): T[];
|
|
32
|
-
|
|
32
|
+
/**
|
|
33
|
+
* 数组乱序(洗牌算法)
|
|
34
|
+
* 1. 从数组的最后一个元素开始,将其与随机位置的元素交换位置。
|
|
35
|
+
* 2. 然后,从倒数第二个元素开始,将其与随机位置的元素交换位置。
|
|
36
|
+
* 3. 以此类推,直到第一个元素。
|
|
37
|
+
* 4. 这样,数组中的元素就会被随机打乱顺序。
|
|
38
|
+
* 5. 注意,这个算法会改变原数组。如果不想改变原数组,可以先复制一份再进行操作。
|
|
39
|
+
* 6. 时间复杂度:O(n)
|
|
40
|
+
* @param arr 数组
|
|
41
|
+
* @returns
|
|
42
|
+
*/
|
|
43
|
+
shuffle<T>(arr: T[]): T[];
|
|
44
|
+
toEnumObj: (arr: import("../../../typings/src").IOptionItem[]) => import("../../../typings/src").TValueEnum;
|
|
33
45
|
};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 布尔值相关工具函数
|
|
3
|
+
*/
|
|
4
|
+
export declare const booleanUtils: {
|
|
5
|
+
/**
|
|
6
|
+
* 检查是否为空字符串
|
|
7
|
+
* @param str 要检查的字符串
|
|
8
|
+
* @returns 如果字符串为空,则返回 true,否则返回 false
|
|
9
|
+
*/
|
|
10
|
+
isEmptyString(str: string): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* 判断是否为数字
|
|
13
|
+
* @param val 要检查的值
|
|
14
|
+
* @returns 如果值是数字,则返回 true,否则返回 false
|
|
15
|
+
*/
|
|
16
|
+
isNumber<T>(val: T): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* 判断是否为对象
|
|
19
|
+
* @param val 要检查的值
|
|
20
|
+
* @returns 如果值是对象,则返回 true,否则返回 false
|
|
21
|
+
*/
|
|
22
|
+
isObject<T>(val: T): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* 判断对象是否为空
|
|
25
|
+
* @param obj 要检查的对象
|
|
26
|
+
* @returns 如果对象为空,则返回 true,否则返回 false
|
|
27
|
+
*/
|
|
28
|
+
isEmptyObject(obj: object): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* 判断是否为布尔值
|
|
31
|
+
* @param val 要检查的值
|
|
32
|
+
* @returns 如果值是布尔值,则返回 true,否则返回 false
|
|
33
|
+
*/
|
|
34
|
+
isBoolean<T>(val: T): val is T & boolean;
|
|
35
|
+
/**
|
|
36
|
+
* 判断是否为数组
|
|
37
|
+
* @param val 要检查的值
|
|
38
|
+
* @returns 如果值是数组,则返回 true,否则返回 false
|
|
39
|
+
*/
|
|
40
|
+
isArray<T>(val: T): val is T & any[];
|
|
41
|
+
/**
|
|
42
|
+
* 判断是否为函数
|
|
43
|
+
* @param val 要检查的值
|
|
44
|
+
* @returns
|
|
45
|
+
*/
|
|
46
|
+
isFunction<T>(val: T): val is T & Function;
|
|
47
|
+
/**
|
|
48
|
+
* 判断是否为 Promise 对象
|
|
49
|
+
* @param obj 要检查的对象
|
|
50
|
+
* @returns 如果对象是 Promise 对象,则返回 true,否则返回 false
|
|
51
|
+
*/
|
|
52
|
+
isPromise(obj: any): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* 判断是否为 DOM 元素
|
|
55
|
+
* @param obj 要检查的对象
|
|
56
|
+
* @returns 如果对象是 DOM 元素,则返回 true,否则返回 false
|
|
57
|
+
*/
|
|
58
|
+
isElement<T>(obj: T): obj is T & Element;
|
|
59
|
+
/**
|
|
60
|
+
* 判断手机号格式(中国)
|
|
61
|
+
* @param str 手机号字符串
|
|
62
|
+
* @returns true 表示手机号格式正确,false 表示手机号格式错误
|
|
63
|
+
*/
|
|
64
|
+
isPhone(str: string): boolean;
|
|
65
|
+
/**
|
|
66
|
+
* 判断邮箱格式
|
|
67
|
+
* @param str 邮箱字符串
|
|
68
|
+
* @returns true 表示邮箱格式正确,false 表示邮箱格式错误
|
|
69
|
+
*/
|
|
70
|
+
isEmail(str: string): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* 判断字符串是否为 JSON
|
|
73
|
+
* @param str 字符串
|
|
74
|
+
* @returns true 表示是 JSON,false 表示不是 JSON
|
|
75
|
+
*/
|
|
76
|
+
isJSON(str: string): boolean;
|
|
77
|
+
/**
|
|
78
|
+
* 判断图片是否加载完成
|
|
79
|
+
* @param img 图片对象
|
|
80
|
+
* @returns true 表示图片加载完成,false 表示图片未加载完成
|
|
81
|
+
* @example
|
|
82
|
+
* const img = new Image();
|
|
83
|
+
* img.src = "URL_ADDRESS * img.src = "https://example.com/image.jpg";
|
|
84
|
+
*/
|
|
85
|
+
isImageLoaded(img: HTMLImageElement): boolean;
|
|
86
|
+
/**
|
|
87
|
+
* 判断元素是否在可视区域内
|
|
88
|
+
* @param el 元素对象
|
|
89
|
+
* @returns true 表示元素在可视区域内,false 表示元素不在可视区域内
|
|
90
|
+
*/
|
|
91
|
+
isInViewport(el: HTMLElement): boolean;
|
|
92
|
+
/**
|
|
93
|
+
* 判断是否为闰年
|
|
94
|
+
* @param year 年份
|
|
95
|
+
* @returns true 表示是闰年,false 表示不是闰年
|
|
96
|
+
* @example
|
|
97
|
+
* isLeapYear(2020); // true
|
|
98
|
+
* isLeapYear(2021); // false
|
|
99
|
+
* isLeapYear(2000); // true
|
|
100
|
+
* isLeapYear(1900); // false
|
|
101
|
+
* isLeapYear(2025); // false
|
|
102
|
+
* isLeapYear(2024); // true
|
|
103
|
+
*/
|
|
104
|
+
isLeapYear(year: number): boolean;
|
|
105
|
+
/**
|
|
106
|
+
* 判断是否为移动端
|
|
107
|
+
* @returns true 表示是移动端,false 表示不是移动端
|
|
108
|
+
*/
|
|
109
|
+
isMobile(): boolean;
|
|
110
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* cookie相关操作工具函数
|
|
3
|
+
*/
|
|
4
|
+
export declare const cookieUtils: {
|
|
5
|
+
/**
|
|
6
|
+
* 通过传入的name获取cookie的值
|
|
7
|
+
* @param name cookie的name
|
|
8
|
+
* @returns cookie的值
|
|
9
|
+
*/
|
|
10
|
+
getCookie(name: string): string | null | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* 设置 cookie
|
|
13
|
+
* @param name cookie的name
|
|
14
|
+
* @param value cookie的值
|
|
15
|
+
* @param days cookie的过期时间,默认7天。如果为0,则表示cookie在会话结束时过期。如果为负数,则表示cookie已过期。
|
|
16
|
+
* @returns void
|
|
17
|
+
*/
|
|
18
|
+
setCookie(name: string, value: string | number | boolean, days?: number): void;
|
|
19
|
+
/**
|
|
20
|
+
* 删除cookie
|
|
21
|
+
* @param name cookie的name
|
|
22
|
+
* @returns void
|
|
23
|
+
*/
|
|
24
|
+
deleteCookie(name: string): void;
|
|
25
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 日期相关操作工具函数
|
|
3
|
+
*/
|
|
4
|
+
export declare const dateUtils: {
|
|
5
|
+
/**
|
|
6
|
+
* 获取当前时间字符串
|
|
7
|
+
* @param locales 区域设置。如:'zh-CN','en-US',"chinese"。默认'zh-CN'。
|
|
8
|
+
* @param hour12 是否使用12小时制。默认false,使用24小时制。
|
|
9
|
+
* @returns 当前时间的字符串。如:'2025/5/27 16:54:45'
|
|
10
|
+
*/
|
|
11
|
+
getTimeString(locales: "zh-CN", hour12?: boolean): string;
|
|
12
|
+
/**
|
|
13
|
+
* 计算两个日期相差天数
|
|
14
|
+
* @param date1 日期1
|
|
15
|
+
* @param date2 日期2
|
|
16
|
+
* @returns 相差天数
|
|
17
|
+
*/
|
|
18
|
+
diffDays(date1: string, date2: string): number;
|
|
19
|
+
/**
|
|
20
|
+
* 生成唯一ID(时间戳+随机数)
|
|
21
|
+
* @returns 唯一ID
|
|
22
|
+
*/
|
|
23
|
+
uniqueId(): string;
|
|
24
|
+
};
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
export * from "./array";
|
|
2
|
+
export * from "./boolean";
|
|
3
|
+
export * from "./cookie";
|
|
4
|
+
export * from "./date";
|
|
5
|
+
export * from "./letter";
|
|
2
6
|
export * from "./number";
|
|
7
|
+
export * from "./object";
|
|
3
8
|
export * from "./phone";
|
|
9
|
+
export * from "./random";
|
|
10
|
+
export * from "./storage";
|
|
4
11
|
export * from "./string";
|
|
5
12
|
export * from "./url";
|
|
6
13
|
export * from "./waterfall";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 随机数相关操作工具函数
|
|
3
|
+
*/
|
|
4
|
+
export declare const randomUtils: {
|
|
5
|
+
/**
|
|
6
|
+
* 生成随机颜色
|
|
7
|
+
* @returns 随机颜色值
|
|
8
|
+
*/
|
|
9
|
+
color: () => string;
|
|
10
|
+
/**
|
|
11
|
+
* 生成指定范围的随机整数
|
|
12
|
+
* @param min 最小值
|
|
13
|
+
* @param max 最大值
|
|
14
|
+
* @returns 随机数
|
|
15
|
+
*/
|
|
16
|
+
int(min: number, max: number): number;
|
|
17
|
+
/**
|
|
18
|
+
* 生成唯一ID(时间戳+随机数)
|
|
19
|
+
* @returns 唯一ID
|
|
20
|
+
*/
|
|
21
|
+
uniqueId(): string;
|
|
22
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 本地存储相关操作工具函数
|
|
3
|
+
*/
|
|
4
|
+
export declare const storageUtils: {
|
|
5
|
+
/**
|
|
6
|
+
* 通过key值获取 localStorage 中存储的某个值
|
|
7
|
+
* @param key 获取的key
|
|
8
|
+
* @returns 获取的值
|
|
9
|
+
*/
|
|
10
|
+
getLocal(key: string): string | null;
|
|
11
|
+
/**
|
|
12
|
+
* 设置localStorage中的某个值
|
|
13
|
+
* @param key 设置的key
|
|
14
|
+
* @param value 设置的value
|
|
15
|
+
*/
|
|
16
|
+
setLocal(key: string, value: string): void;
|
|
17
|
+
/**
|
|
18
|
+
* 删除localStorage中的某个值
|
|
19
|
+
* @param key 删除的key
|
|
20
|
+
*/
|
|
21
|
+
removeLocal(key: string): void;
|
|
22
|
+
/**
|
|
23
|
+
* 通过key值获取 sessionStorage 中存储的某个值
|
|
24
|
+
* @param key 获取的key
|
|
25
|
+
* @returns 获取的值
|
|
26
|
+
*/
|
|
27
|
+
getSession(key: string): string | null;
|
|
28
|
+
/**
|
|
29
|
+
* 设置sessionStorage中的某个值
|
|
30
|
+
* @param key 设置的key
|
|
31
|
+
* @param value 设置的value
|
|
32
|
+
*/
|
|
33
|
+
setSession(key: string, value: string): void;
|
|
34
|
+
/**
|
|
35
|
+
* 删除sessionStorage中的某个值
|
|
36
|
+
* @param key 删除的key
|
|
37
|
+
*/
|
|
38
|
+
removeSession(key: string): void;
|
|
39
|
+
};
|
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 通过传入字段名称获取 URL 查询参数
|
|
3
|
+
* @param name 字段名称
|
|
4
|
+
* @returns
|
|
5
|
+
*/
|
|
6
|
+
export declare const getQueryInfoByName: (name: string) => string | null;
|
|
7
|
+
/**
|
|
8
|
+
* 通过传入字段名称 获取 URL 查询参数
|
|
9
|
+
* @param name 字段名称
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
export declare function getQueryParam(name: string): string | null;
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* @param list 瀑布流数据-数组
|
|
4
|
-
* @param columnsNum 需要展示的列数
|
|
5
|
-
* @returns 适合瀑布流布局的数组
|
|
2
|
+
* 瀑布流相关操作工具函数
|
|
6
3
|
*/
|
|
7
|
-
export declare const
|
|
4
|
+
export declare const waterfallUtils: {
|
|
5
|
+
/**
|
|
6
|
+
* 处理瀑布流数据,使其适合瀑布流布局展示
|
|
7
|
+
* @param list 瀑布流数据-数组
|
|
8
|
+
* @param columnsNum 需要展示的列数
|
|
9
|
+
* @returns 适合瀑布流布局的数组
|
|
10
|
+
*/
|
|
11
|
+
toList: <T>(list: T[], columnsNum?: number) => any[];
|
|
12
|
+
};
|