@pawover/kit 0.0.0-beta.8 → 0.0.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/package.json +89 -62
- package/packages/hooks/dist/alova.d.ts +31 -0
- package/packages/hooks/dist/alova.js +64 -0
- package/packages/hooks/dist/index.d.ts +1 -0
- package/packages/hooks/dist/index.js +0 -0
- package/packages/hooks/dist/metadata.json +16 -0
- package/packages/hooks/dist/react.d.ts +164 -0
- package/packages/hooks/dist/react.js +2112 -0
- package/packages/utils/dist/index.d.ts +4293 -0
- package/packages/utils/dist/index.js +1527 -0
- package/packages/utils/dist/math.d.ts +54 -0
- package/packages/utils/dist/math.js +56 -0
- package/packages/utils/dist/metadata.json +14 -0
- package/packages/utils/dist/string-DCWqoW4P.js +793 -0
- package/packages/utils/dist/vite.d.ts +16 -0
- package/packages/utils/dist/vite.js +26 -0
- package/packages/zod/dist/index.d.ts +58 -0
- package/packages/zod/dist/index.js +61 -0
- package/dist/enums.d.ts +0 -25
- package/dist/enums.d.ts.map +0 -1
- package/dist/enums.js +0 -25
- package/dist/enums.js.map +0 -1
- package/dist/hooks-alova.d.ts +0 -23
- package/dist/hooks-alova.d.ts.map +0 -1
- package/dist/hooks-alova.js +0 -39
- package/dist/hooks-alova.js.map +0 -1
- package/dist/hooks-react.d.ts +0 -95
- package/dist/hooks-react.d.ts.map +0 -1
- package/dist/hooks-react.js +0 -328
- package/dist/hooks-react.js.map +0 -1
- package/dist/index.d.ts +0 -3726
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -1469
- package/dist/index.js.map +0 -1
- package/dist/patches-fetchEventSource.d.ts +0 -815
- package/dist/patches-fetchEventSource.d.ts.map +0 -1
- package/dist/patches-fetchEventSource.js +0 -316
- package/dist/patches-fetchEventSource.js.map +0 -1
- package/dist/vite.d.ts +0 -13
- package/dist/vite.d.ts.map +0 -1
- package/dist/vite.js +0 -23
- package/dist/vite.js.map +0 -1
- package/dist/zod.d.ts +0 -109
- package/dist/zod.d.ts.map +0 -1
- package/dist/zod.js +0 -143
- package/dist/zod.js.map +0 -1
- package/metadata.json +0 -167
|
@@ -0,0 +1,2112 @@
|
|
|
1
|
+
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
//#region ../../node_modules/.pnpm/es-toolkit@1.46.1/node_modules/es-toolkit/dist/_internal/globalThis.mjs
|
|
3
|
+
const globalThis_ = typeof globalThis === "object" && globalThis || typeof window === "object" && window || typeof self === "object" && self || typeof global === "object" && global || (function() {
|
|
4
|
+
return this;
|
|
5
|
+
})() || Function("return this")();
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region ../../node_modules/.pnpm/es-toolkit@1.46.1/node_modules/es-toolkit/dist/function/noop.mjs
|
|
8
|
+
function noop() {}
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region ../../node_modules/.pnpm/es-toolkit@1.46.1/node_modules/es-toolkit/dist/predicate/isPrimitive.mjs
|
|
11
|
+
function isPrimitive(value) {
|
|
12
|
+
return value == null || typeof value !== "object" && typeof value !== "function";
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region ../../node_modules/.pnpm/es-toolkit@1.46.1/node_modules/es-toolkit/dist/predicate/isTypedArray.mjs
|
|
16
|
+
function isTypedArray(x) {
|
|
17
|
+
return ArrayBuffer.isView(x) && !(x instanceof DataView);
|
|
18
|
+
}
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region ../../node_modules/.pnpm/es-toolkit@1.46.1/node_modules/es-toolkit/dist/object/clone.mjs
|
|
21
|
+
function clone(obj) {
|
|
22
|
+
if (isPrimitive(obj)) return obj;
|
|
23
|
+
if (Array.isArray(obj) || isTypedArray(obj) || obj instanceof ArrayBuffer || typeof SharedArrayBuffer !== "undefined" && obj instanceof SharedArrayBuffer) return obj.slice(0);
|
|
24
|
+
const prototype = Object.getPrototypeOf(obj);
|
|
25
|
+
if (prototype == null) return Object.assign(Object.create(prototype), obj);
|
|
26
|
+
const Constructor = prototype.constructor;
|
|
27
|
+
if (obj instanceof Date || obj instanceof Map || obj instanceof Set) return new Constructor(obj);
|
|
28
|
+
if (obj instanceof RegExp) {
|
|
29
|
+
const newRegExp = new Constructor(obj);
|
|
30
|
+
newRegExp.lastIndex = obj.lastIndex;
|
|
31
|
+
return newRegExp;
|
|
32
|
+
}
|
|
33
|
+
if (obj instanceof DataView) return new Constructor(obj.buffer.slice(0));
|
|
34
|
+
if (obj instanceof Error) {
|
|
35
|
+
let newError;
|
|
36
|
+
if (obj instanceof AggregateError) newError = new Constructor(obj.errors, obj.message, { cause: obj.cause });
|
|
37
|
+
else newError = new Constructor(obj.message, { cause: obj.cause });
|
|
38
|
+
newError.stack = obj.stack;
|
|
39
|
+
Object.assign(newError, obj);
|
|
40
|
+
return newError;
|
|
41
|
+
}
|
|
42
|
+
if (typeof File !== "undefined" && obj instanceof File) return new Constructor([obj], obj.name, {
|
|
43
|
+
type: obj.type,
|
|
44
|
+
lastModified: obj.lastModified
|
|
45
|
+
});
|
|
46
|
+
if (typeof obj === "object") return Object.assign(Object.create(prototype), obj);
|
|
47
|
+
return obj;
|
|
48
|
+
}
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region ../../node_modules/.pnpm/es-toolkit@1.46.1/node_modules/es-toolkit/dist/compat/_internal/getSymbols.mjs
|
|
51
|
+
function getSymbols(object) {
|
|
52
|
+
return Object.getOwnPropertySymbols(object).filter((symbol) => Object.prototype.propertyIsEnumerable.call(object, symbol));
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region ../../node_modules/.pnpm/es-toolkit@1.46.1/node_modules/es-toolkit/dist/compat/_internal/getTag.mjs
|
|
56
|
+
function getTag(value) {
|
|
57
|
+
if (value == null) return value === void 0 ? "[object Undefined]" : "[object Null]";
|
|
58
|
+
return Object.prototype.toString.call(value);
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region ../../node_modules/.pnpm/es-toolkit@1.46.1/node_modules/es-toolkit/dist/compat/_internal/tags.mjs
|
|
62
|
+
const regexpTag = "[object RegExp]";
|
|
63
|
+
const stringTag = "[object String]";
|
|
64
|
+
const numberTag = "[object Number]";
|
|
65
|
+
const booleanTag = "[object Boolean]";
|
|
66
|
+
const symbolTag = "[object Symbol]";
|
|
67
|
+
const dateTag = "[object Date]";
|
|
68
|
+
const mapTag = "[object Map]";
|
|
69
|
+
const setTag = "[object Set]";
|
|
70
|
+
const arrayTag = "[object Array]";
|
|
71
|
+
const functionTag = "[object Function]";
|
|
72
|
+
const arrayBufferTag = "[object ArrayBuffer]";
|
|
73
|
+
const objectTag = "[object Object]";
|
|
74
|
+
const errorTag = "[object Error]";
|
|
75
|
+
const dataViewTag = "[object DataView]";
|
|
76
|
+
const uint8ArrayTag = "[object Uint8Array]";
|
|
77
|
+
const uint8ClampedArrayTag = "[object Uint8ClampedArray]";
|
|
78
|
+
const uint16ArrayTag = "[object Uint16Array]";
|
|
79
|
+
const uint32ArrayTag = "[object Uint32Array]";
|
|
80
|
+
const bigUint64ArrayTag = "[object BigUint64Array]";
|
|
81
|
+
const int8ArrayTag = "[object Int8Array]";
|
|
82
|
+
const int16ArrayTag = "[object Int16Array]";
|
|
83
|
+
const int32ArrayTag = "[object Int32Array]";
|
|
84
|
+
const bigInt64ArrayTag = "[object BigInt64Array]";
|
|
85
|
+
const float32ArrayTag = "[object Float32Array]";
|
|
86
|
+
const float64ArrayTag = "[object Float64Array]";
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region ../../node_modules/.pnpm/es-toolkit@1.46.1/node_modules/es-toolkit/dist/predicate/isBuffer.mjs
|
|
89
|
+
function isBuffer(x) {
|
|
90
|
+
return typeof globalThis_.Buffer !== "undefined" && globalThis_.Buffer.isBuffer(x);
|
|
91
|
+
}
|
|
92
|
+
//#endregion
|
|
93
|
+
//#region ../../node_modules/.pnpm/es-toolkit@1.46.1/node_modules/es-toolkit/dist/predicate/isPlainObject.mjs
|
|
94
|
+
function isPlainObject(value) {
|
|
95
|
+
if (!value || typeof value !== "object") return false;
|
|
96
|
+
const proto = Object.getPrototypeOf(value);
|
|
97
|
+
if (!(proto === null || proto === Object.prototype || Object.getPrototypeOf(proto) === null)) return false;
|
|
98
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
99
|
+
}
|
|
100
|
+
//#endregion
|
|
101
|
+
//#region ../../node_modules/.pnpm/es-toolkit@1.46.1/node_modules/es-toolkit/dist/_internal/isEqualsSameValueZero.mjs
|
|
102
|
+
function isEqualsSameValueZero(value, other) {
|
|
103
|
+
return value === other || Number.isNaN(value) && Number.isNaN(other);
|
|
104
|
+
}
|
|
105
|
+
//#endregion
|
|
106
|
+
//#region ../../node_modules/.pnpm/es-toolkit@1.46.1/node_modules/es-toolkit/dist/predicate/isEqualWith.mjs
|
|
107
|
+
function isEqualWith(a, b, areValuesEqual) {
|
|
108
|
+
return isEqualWithImpl(a, b, void 0, void 0, void 0, void 0, areValuesEqual);
|
|
109
|
+
}
|
|
110
|
+
function isEqualWithImpl(a, b, property, aParent, bParent, stack, areValuesEqual) {
|
|
111
|
+
const result = areValuesEqual(a, b, property, aParent, bParent, stack);
|
|
112
|
+
if (result !== void 0) return result;
|
|
113
|
+
if (typeof a === typeof b) switch (typeof a) {
|
|
114
|
+
case "bigint":
|
|
115
|
+
case "string":
|
|
116
|
+
case "boolean":
|
|
117
|
+
case "symbol":
|
|
118
|
+
case "undefined": return a === b;
|
|
119
|
+
case "number": return a === b || Object.is(a, b);
|
|
120
|
+
case "function": return a === b;
|
|
121
|
+
case "object": return areObjectsEqual(a, b, stack, areValuesEqual);
|
|
122
|
+
}
|
|
123
|
+
return areObjectsEqual(a, b, stack, areValuesEqual);
|
|
124
|
+
}
|
|
125
|
+
function areObjectsEqual(a, b, stack, areValuesEqual) {
|
|
126
|
+
if (Object.is(a, b)) return true;
|
|
127
|
+
let aTag = getTag(a);
|
|
128
|
+
let bTag = getTag(b);
|
|
129
|
+
if (aTag === "[object Arguments]") aTag = objectTag;
|
|
130
|
+
if (bTag === "[object Arguments]") bTag = objectTag;
|
|
131
|
+
if (aTag !== bTag) return false;
|
|
132
|
+
switch (aTag) {
|
|
133
|
+
case stringTag: return a.toString() === b.toString();
|
|
134
|
+
case numberTag: return isEqualsSameValueZero(a.valueOf(), b.valueOf());
|
|
135
|
+
case booleanTag:
|
|
136
|
+
case dateTag:
|
|
137
|
+
case symbolTag: return Object.is(a.valueOf(), b.valueOf());
|
|
138
|
+
case regexpTag: return a.source === b.source && a.flags === b.flags;
|
|
139
|
+
case functionTag: return a === b;
|
|
140
|
+
}
|
|
141
|
+
stack = stack ?? /* @__PURE__ */ new Map();
|
|
142
|
+
const aStack = stack.get(a);
|
|
143
|
+
const bStack = stack.get(b);
|
|
144
|
+
if (aStack != null && bStack != null) return aStack === b;
|
|
145
|
+
stack.set(a, b);
|
|
146
|
+
stack.set(b, a);
|
|
147
|
+
try {
|
|
148
|
+
switch (aTag) {
|
|
149
|
+
case mapTag:
|
|
150
|
+
if (a.size !== b.size) return false;
|
|
151
|
+
for (const [key, value] of a.entries()) if (!b.has(key) || !isEqualWithImpl(value, b.get(key), key, a, b, stack, areValuesEqual)) return false;
|
|
152
|
+
return true;
|
|
153
|
+
case setTag: {
|
|
154
|
+
if (a.size !== b.size) return false;
|
|
155
|
+
const aValues = Array.from(a.values());
|
|
156
|
+
const bValues = Array.from(b.values());
|
|
157
|
+
for (let i = 0; i < aValues.length; i++) {
|
|
158
|
+
const aValue = aValues[i];
|
|
159
|
+
const index = bValues.findIndex((bValue) => {
|
|
160
|
+
return isEqualWithImpl(aValue, bValue, void 0, a, b, stack, areValuesEqual);
|
|
161
|
+
});
|
|
162
|
+
if (index === -1) return false;
|
|
163
|
+
bValues.splice(index, 1);
|
|
164
|
+
}
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
case arrayTag:
|
|
168
|
+
case uint8ArrayTag:
|
|
169
|
+
case uint8ClampedArrayTag:
|
|
170
|
+
case uint16ArrayTag:
|
|
171
|
+
case uint32ArrayTag:
|
|
172
|
+
case bigUint64ArrayTag:
|
|
173
|
+
case int8ArrayTag:
|
|
174
|
+
case int16ArrayTag:
|
|
175
|
+
case int32ArrayTag:
|
|
176
|
+
case bigInt64ArrayTag:
|
|
177
|
+
case float32ArrayTag:
|
|
178
|
+
case float64ArrayTag:
|
|
179
|
+
if (isBuffer(a) !== isBuffer(b)) return false;
|
|
180
|
+
if (a.length !== b.length) return false;
|
|
181
|
+
for (let i = 0; i < a.length; i++) if (!isEqualWithImpl(a[i], b[i], i, a, b, stack, areValuesEqual)) return false;
|
|
182
|
+
return true;
|
|
183
|
+
case arrayBufferTag:
|
|
184
|
+
if (a.byteLength !== b.byteLength) return false;
|
|
185
|
+
return areObjectsEqual(new Uint8Array(a), new Uint8Array(b), stack, areValuesEqual);
|
|
186
|
+
case dataViewTag:
|
|
187
|
+
if (a.byteLength !== b.byteLength || a.byteOffset !== b.byteOffset) return false;
|
|
188
|
+
return areObjectsEqual(new Uint8Array(a), new Uint8Array(b), stack, areValuesEqual);
|
|
189
|
+
case errorTag: return a.name === b.name && a.message === b.message;
|
|
190
|
+
case objectTag: {
|
|
191
|
+
if (!(areObjectsEqual(a.constructor, b.constructor, stack, areValuesEqual) || isPlainObject(a) && isPlainObject(b))) return false;
|
|
192
|
+
const aKeys = [...Object.keys(a), ...getSymbols(a)];
|
|
193
|
+
const bKeys = [...Object.keys(b), ...getSymbols(b)];
|
|
194
|
+
if (aKeys.length !== bKeys.length) return false;
|
|
195
|
+
for (let i = 0; i < aKeys.length; i++) {
|
|
196
|
+
const propKey = aKeys[i];
|
|
197
|
+
const aProp = a[propKey];
|
|
198
|
+
if (!Object.hasOwn(b, propKey)) return false;
|
|
199
|
+
const bProp = b[propKey];
|
|
200
|
+
if (!isEqualWithImpl(aProp, bProp, propKey, a, b, stack, areValuesEqual)) return false;
|
|
201
|
+
}
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
default: return false;
|
|
205
|
+
}
|
|
206
|
+
} finally {
|
|
207
|
+
stack.delete(a);
|
|
208
|
+
stack.delete(b);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
//#endregion
|
|
212
|
+
//#region ../../node_modules/.pnpm/es-toolkit@1.46.1/node_modules/es-toolkit/dist/predicate/isEqual.mjs
|
|
213
|
+
function isEqual(a, b) {
|
|
214
|
+
return isEqualWith(a, b, noop);
|
|
215
|
+
}
|
|
216
|
+
//#endregion
|
|
217
|
+
//#region src/react/useCreation.ts
|
|
218
|
+
/**
|
|
219
|
+
* 确保实例不会被重复创建
|
|
220
|
+
*
|
|
221
|
+
* @param factory
|
|
222
|
+
* @param deps
|
|
223
|
+
*/
|
|
224
|
+
function useCreation(factory, deps) {
|
|
225
|
+
const { current } = useRef({
|
|
226
|
+
deps,
|
|
227
|
+
result: void 0,
|
|
228
|
+
isInitialized: false
|
|
229
|
+
});
|
|
230
|
+
if (current.isInitialized === false || !isEqual(current.deps, deps)) {
|
|
231
|
+
current.deps = deps;
|
|
232
|
+
current.result = factory();
|
|
233
|
+
current.isInitialized = true;
|
|
234
|
+
}
|
|
235
|
+
return current.result;
|
|
236
|
+
}
|
|
237
|
+
//#endregion
|
|
238
|
+
//#region src/react/useLatest.ts
|
|
239
|
+
/**
|
|
240
|
+
* 返回当前最新值的 Hook
|
|
241
|
+
*
|
|
242
|
+
* @param value
|
|
243
|
+
*/
|
|
244
|
+
function useLatest(value) {
|
|
245
|
+
const ref = useRef(value);
|
|
246
|
+
ref.current = value;
|
|
247
|
+
return ref;
|
|
248
|
+
}
|
|
249
|
+
//#endregion
|
|
250
|
+
//#region ../utils/dist/string-DCWqoW4P.js
|
|
251
|
+
function _typeof(o) {
|
|
252
|
+
"@babel/helpers - typeof";
|
|
253
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
254
|
+
return typeof o;
|
|
255
|
+
} : function(o) {
|
|
256
|
+
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
257
|
+
}, _typeof(o);
|
|
258
|
+
}
|
|
259
|
+
function toPrimitive(t, r) {
|
|
260
|
+
if ("object" != _typeof(t) || !t) return t;
|
|
261
|
+
var e = t[Symbol.toPrimitive];
|
|
262
|
+
if (void 0 !== e) {
|
|
263
|
+
var i = e.call(t, r || "default");
|
|
264
|
+
if ("object" != _typeof(i)) return i;
|
|
265
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
266
|
+
}
|
|
267
|
+
return ("string" === r ? String : Number)(t);
|
|
268
|
+
}
|
|
269
|
+
function toPropertyKey(t) {
|
|
270
|
+
var i = toPrimitive(t, "string");
|
|
271
|
+
return "symbol" == _typeof(i) ? i : i + "";
|
|
272
|
+
}
|
|
273
|
+
function _defineProperty(e, r, t) {
|
|
274
|
+
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
275
|
+
value: t,
|
|
276
|
+
enumerable: !0,
|
|
277
|
+
configurable: !0,
|
|
278
|
+
writable: !0
|
|
279
|
+
}) : e[r] = t, e;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* 类型工具类
|
|
283
|
+
*/
|
|
284
|
+
var TypeUtil = class {
|
|
285
|
+
/**
|
|
286
|
+
* 获取值的 [[Prototype]] 标签
|
|
287
|
+
*
|
|
288
|
+
* @param value - 任意 JavaScript 值
|
|
289
|
+
* @returns 标准化的类型标签字符串
|
|
290
|
+
*/
|
|
291
|
+
static getPrototypeString(value) {
|
|
292
|
+
return Object.prototype.toString.call(value);
|
|
293
|
+
}
|
|
294
|
+
static isConstructable(fn) {
|
|
295
|
+
try {
|
|
296
|
+
Reflect.construct(fn, []);
|
|
297
|
+
return true;
|
|
298
|
+
} catch {
|
|
299
|
+
return false;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* 检查 value 是否为 string 类型
|
|
304
|
+
*
|
|
305
|
+
* @param value 待检查值
|
|
306
|
+
* @param checkEmpty 是否检查空字符串
|
|
307
|
+
* @returns 是否为字符串
|
|
308
|
+
* @example
|
|
309
|
+
* ```ts
|
|
310
|
+
* TypeUtil.isString("abc"); // true
|
|
311
|
+
* TypeUtil.isString(""); // true
|
|
312
|
+
* TypeUtil.isString("", true); // false
|
|
313
|
+
* ```
|
|
314
|
+
*/
|
|
315
|
+
static isString(value, checkEmpty = false) {
|
|
316
|
+
return typeof value === "string" && (!checkEmpty || !!value.length);
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* 检查 value 是否为 number 类型
|
|
320
|
+
*
|
|
321
|
+
* @param value 待检查值
|
|
322
|
+
* @param checkNaN 是否排除 `NaN`,默认为 `true`
|
|
323
|
+
* @returns 是否为 number
|
|
324
|
+
* @example
|
|
325
|
+
* ```ts
|
|
326
|
+
* TypeUtil.isNumber(1); // true
|
|
327
|
+
* TypeUtil.isNumber(NaN); // false (default)
|
|
328
|
+
* TypeUtil.isNumber(NaN, false); // true
|
|
329
|
+
* ```
|
|
330
|
+
*/
|
|
331
|
+
static isNumber(value, checkNaN = true) {
|
|
332
|
+
return typeof value === "number" && (!checkNaN || !this.isNaN(value));
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* 检查 value 是否为 NaN
|
|
336
|
+
*
|
|
337
|
+
* @param value 待检查值
|
|
338
|
+
* @returns 是否为 NaN
|
|
339
|
+
*/
|
|
340
|
+
static isNaN(value) {
|
|
341
|
+
return Number.isNaN(value);
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* 检查 value 是否为整数
|
|
345
|
+
*
|
|
346
|
+
* @param value 待检查值
|
|
347
|
+
* @param checkSafe 是否附加安全整数检查
|
|
348
|
+
* @returns 是否为整数
|
|
349
|
+
*/
|
|
350
|
+
static isInteger(value, checkSafe = true) {
|
|
351
|
+
const check = Number.isInteger(value);
|
|
352
|
+
return checkSafe ? check && Number.isSafeInteger(value) : check;
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* 检查 value 是否为正整数
|
|
356
|
+
* - 此函数中 `0` 不被视为正整数
|
|
357
|
+
*
|
|
358
|
+
* @param value 待检查值
|
|
359
|
+
* @param checkSafe 是否附加安全整数检查
|
|
360
|
+
*/
|
|
361
|
+
static isPositiveInteger(value, checkSafe = true) {
|
|
362
|
+
return this.isInteger(value, checkSafe) && value > 0;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* 检查 value 是否为负整数
|
|
366
|
+
* - 此函数中 `0` 不被视为负整数
|
|
367
|
+
*
|
|
368
|
+
* @param value 待检查值
|
|
369
|
+
* @param checkSafe 是否附加安全整数检查
|
|
370
|
+
*/
|
|
371
|
+
static isNegativeInteger(value, checkSafe = true) {
|
|
372
|
+
return this.isInteger(value, checkSafe) && value < 0;
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* 检查 value 是否为 Infinity
|
|
376
|
+
* - 排除 `NaN`
|
|
377
|
+
*
|
|
378
|
+
* @param value 待检查值
|
|
379
|
+
*/
|
|
380
|
+
static isInfinity(value) {
|
|
381
|
+
return this.isNumber(value) && (Number.POSITIVE_INFINITY === value || Number.NEGATIVE_INFINITY === value);
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* 检查 value 是否类似 Infinity
|
|
385
|
+
* - 排除 `NaN`
|
|
386
|
+
*
|
|
387
|
+
* @param value 待检查值
|
|
388
|
+
*/
|
|
389
|
+
static isInfinityLike(value) {
|
|
390
|
+
const check = this.isInfinity(value);
|
|
391
|
+
if (check) return check;
|
|
392
|
+
if (typeof value === "string") return [
|
|
393
|
+
"infinity",
|
|
394
|
+
"-infinity",
|
|
395
|
+
"+infinity",
|
|
396
|
+
"Infinity",
|
|
397
|
+
"-Infinity",
|
|
398
|
+
"+Infinity"
|
|
399
|
+
].includes(value.trim());
|
|
400
|
+
return false;
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* 检查 value 是否为 Boolean
|
|
404
|
+
* @param value 待检查值
|
|
405
|
+
* @returns 是否为 Boolean
|
|
406
|
+
*/
|
|
407
|
+
static isBoolean(value) {
|
|
408
|
+
return typeof value === "boolean";
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* 检查 value 是否为 BigInt
|
|
412
|
+
* @param value 待检查值
|
|
413
|
+
* @returns 是否为 BigInt
|
|
414
|
+
*/
|
|
415
|
+
static isBigInt(value) {
|
|
416
|
+
return typeof value === "bigint";
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* 检查 value 是否为 Symbol
|
|
420
|
+
* @param value 待检查值
|
|
421
|
+
* @returns 是否为 Symbol
|
|
422
|
+
*/
|
|
423
|
+
static isSymbol(value) {
|
|
424
|
+
return typeof value === "symbol";
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* 检查 value 是否为 undefined
|
|
428
|
+
* @param value 待检查值
|
|
429
|
+
* @returns 是否为 undefined
|
|
430
|
+
*/
|
|
431
|
+
static isUndefined(value) {
|
|
432
|
+
return typeof value === "undefined";
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* 检查 value 是否为 null
|
|
436
|
+
* @param value 待检查值
|
|
437
|
+
* @returns 是否为 null
|
|
438
|
+
*/
|
|
439
|
+
static isNull(value) {
|
|
440
|
+
return value === null;
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* 检查 value 是否为 Function
|
|
444
|
+
* @param value 待检查值
|
|
445
|
+
* @returns 是否为 Function
|
|
446
|
+
*/
|
|
447
|
+
static isFunction(value) {
|
|
448
|
+
return typeof value === "function";
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* 检查 value 是否为 AsyncFunction
|
|
452
|
+
* @param value 待检查值
|
|
453
|
+
* @returns 是否为 AsyncFunction
|
|
454
|
+
*/
|
|
455
|
+
static isAsyncFunction(value) {
|
|
456
|
+
return this.isFunction(value) && this.getPrototypeString(value) === this.PROTOTYPE_TAGS.ASYNC_FUNCTION;
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* 检查 value 是否为 GeneratorFunction
|
|
460
|
+
* @param value 待检查值
|
|
461
|
+
* @returns 是否为 GeneratorFunction
|
|
462
|
+
*/
|
|
463
|
+
static isGeneratorFunction(value) {
|
|
464
|
+
return this.isFunction(value) && this.getPrototypeString(value) === this.PROTOTYPE_TAGS.GENERATOR_FUNCTION;
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* 检查 value 是否为 AsyncGeneratorFunction
|
|
468
|
+
* @param value 待检查值
|
|
469
|
+
* @returns 是否为 AsyncGeneratorFunction
|
|
470
|
+
*/
|
|
471
|
+
static isAsyncGeneratorFunction(value) {
|
|
472
|
+
return this.isFunction(value) && this.getPrototypeString(value) === this.PROTOTYPE_TAGS.ASYNC_GENERATOR_FUNCTION;
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* 检查 value 是否为 Promise
|
|
476
|
+
* @param value 待检查值
|
|
477
|
+
* @returns 是否为 Promise
|
|
478
|
+
*/
|
|
479
|
+
static isPromise(value) {
|
|
480
|
+
return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.PROMISE;
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* 检查 value 是否为 PromiseLike
|
|
484
|
+
* - 可识别拥有 then 方法的非 Promise 对象
|
|
485
|
+
* @param value 待检查值
|
|
486
|
+
* @returns 是否为 PromiseLike
|
|
487
|
+
*/
|
|
488
|
+
static isPromiseLike(value) {
|
|
489
|
+
return this.isPromise(value) || this.isObject(value, false) && this.isFunction(value["then"]);
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* 判断是否为普通对象类型
|
|
493
|
+
* - 可选是否检查原型为 `Object.prototype`,防止原型链污染
|
|
494
|
+
*
|
|
495
|
+
* @param value 待检查值
|
|
496
|
+
* @param prototypeCheck 是否进行原型检查,默认 `true`
|
|
497
|
+
* @returns 是否为 Plain Object (当 checkPrototype=true) 或 object
|
|
498
|
+
* @example
|
|
499
|
+
* ```ts
|
|
500
|
+
* TypeUtil.isObject({}); // true
|
|
501
|
+
* TypeUtil.isObject([]); // false
|
|
502
|
+
* TypeUtil.isObject(new Date()); // false
|
|
503
|
+
* TypeUtil.isObject(new Date(), false); // true
|
|
504
|
+
* TypeUtil.isObject(Object.create(null)) // false
|
|
505
|
+
* TypeUtil.isObject(Object.create(null), false) // true
|
|
506
|
+
* ```
|
|
507
|
+
*/
|
|
508
|
+
static isObject(value, prototypeCheck = true) {
|
|
509
|
+
const check = this.getPrototypeString(value) === this.PROTOTYPE_TAGS.OBJECT;
|
|
510
|
+
return prototypeCheck ? check && Object.getPrototypeOf(value) === Object.prototype : check;
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* 判断一个对象是否为有效的枚举
|
|
514
|
+
* - 枚举成员不能为空
|
|
515
|
+
* - 枚举成员的键不能具有数值名
|
|
516
|
+
* - 枚举成员的值必须类型一致且为 `string` 或 `number` 类型
|
|
517
|
+
* - 枚举成员的值不能重复
|
|
518
|
+
* - 枚举成员的值必须全部为双向映射或非双向映射
|
|
519
|
+
*
|
|
520
|
+
* @param enumeration 待检查值
|
|
521
|
+
* @returns [是否为有效的枚举, 是否为双向枚举]
|
|
522
|
+
*/
|
|
523
|
+
static isEnumeration(enumeration) {
|
|
524
|
+
if (typeof enumeration !== "object" || enumeration === null) return [false, false];
|
|
525
|
+
const keys = Object.keys(enumeration);
|
|
526
|
+
if (keys.length === 0) return [false, false];
|
|
527
|
+
const originalKeys = [];
|
|
528
|
+
const numericKeys = [];
|
|
529
|
+
for (const key of keys) if (/^\d+$/.test(key)) numericKeys.push(key);
|
|
530
|
+
else originalKeys.push(key);
|
|
531
|
+
if (originalKeys.length === 0) return [false, false];
|
|
532
|
+
let valueType = null;
|
|
533
|
+
const values = [];
|
|
534
|
+
for (const key of originalKeys) {
|
|
535
|
+
const value = enumeration[key];
|
|
536
|
+
const type = typeof value;
|
|
537
|
+
if (type !== "string" && type !== "number") return [false, false];
|
|
538
|
+
if (valueType === null) valueType = type;
|
|
539
|
+
else if (type !== valueType) return [false, false];
|
|
540
|
+
values.push(value);
|
|
541
|
+
}
|
|
542
|
+
if (new Set(values).size !== values.length) return [false, false];
|
|
543
|
+
let isBidirectional = false;
|
|
544
|
+
if (numericKeys.length > 0) {
|
|
545
|
+
if (numericKeys.length !== originalKeys.length) return [false, false];
|
|
546
|
+
const reverseMappedNames = /* @__PURE__ */ new Set();
|
|
547
|
+
for (const numKey of numericKeys) {
|
|
548
|
+
const reverseValue = enumeration[numKey];
|
|
549
|
+
if (typeof reverseValue !== "string") return [false, false];
|
|
550
|
+
if (!originalKeys.includes(reverseValue)) return [false, false];
|
|
551
|
+
reverseMappedNames.add(reverseValue);
|
|
552
|
+
}
|
|
553
|
+
if (reverseMappedNames.size !== originalKeys.length) return [false, false];
|
|
554
|
+
isBidirectional = true;
|
|
555
|
+
}
|
|
556
|
+
return [true, isBidirectional];
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* 检查 value 是否为 Class
|
|
560
|
+
*
|
|
561
|
+
* @param value 待检查值
|
|
562
|
+
* @returns 是否为 Class
|
|
563
|
+
* @example
|
|
564
|
+
* ```ts
|
|
565
|
+
* class A {}
|
|
566
|
+
* TypeUtil.isClass(A); // true
|
|
567
|
+
* TypeUtil.isClass(() => {}); // false
|
|
568
|
+
* ```
|
|
569
|
+
*/
|
|
570
|
+
static isClass(value) {
|
|
571
|
+
return this.isFunction(value) && !this.isAsyncFunction(value) && Function.prototype.toString.call(value).startsWith("class ") && this.isConstructable(value) && value.prototype !== void 0;
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* 检查 value 是否为数组
|
|
575
|
+
*
|
|
576
|
+
* @param value 待检查值
|
|
577
|
+
* @returns 是否为数组
|
|
578
|
+
* @example
|
|
579
|
+
* ```ts
|
|
580
|
+
* TypeUtil.isArray([]); // true
|
|
581
|
+
* ```
|
|
582
|
+
*/
|
|
583
|
+
static isArray(value) {
|
|
584
|
+
return Array.isArray(value);
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* 检查 value 是否为 TypedArray
|
|
588
|
+
*
|
|
589
|
+
* @param value 待检查值
|
|
590
|
+
* @returns 是否为 TypedArray
|
|
591
|
+
* @example
|
|
592
|
+
* ```ts
|
|
593
|
+
* TypeUtil.isTypedArray(new Int8Array()); // true
|
|
594
|
+
* ```
|
|
595
|
+
*/
|
|
596
|
+
static isTypedArray(value) {
|
|
597
|
+
return typeof value === "object" && value !== null && this.TYPED_ARRAY_TAGS.has(this.getPrototypeString(value));
|
|
598
|
+
}
|
|
599
|
+
/**
|
|
600
|
+
* 检查 value 是否为 Map
|
|
601
|
+
* @param value 待检查值
|
|
602
|
+
* @returns 是否为 Map
|
|
603
|
+
*/
|
|
604
|
+
static isMap(value) {
|
|
605
|
+
return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.MAP;
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* 检查 value 是否为 WeakMap
|
|
609
|
+
* @param value 待检查值
|
|
610
|
+
* @returns 是否为 WeakMap
|
|
611
|
+
*/
|
|
612
|
+
static isWeakMap(value) {
|
|
613
|
+
return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.WEAK_MAP;
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
616
|
+
* 检查 value 是否为 Set
|
|
617
|
+
* @param value 待检查值
|
|
618
|
+
* @returns 是否为 Set
|
|
619
|
+
*/
|
|
620
|
+
static isSet(value) {
|
|
621
|
+
return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.SET;
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* 检查 value 是否为 WeakSet
|
|
625
|
+
* @param value 待检查值
|
|
626
|
+
* @returns 是否为 WeakSet
|
|
627
|
+
*/
|
|
628
|
+
static isWeakSet(value) {
|
|
629
|
+
return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.WEAK_SET;
|
|
630
|
+
}
|
|
631
|
+
/**
|
|
632
|
+
* 检查 value 是否为 Blob
|
|
633
|
+
* @param value 待检查值
|
|
634
|
+
* @returns 是否为 Blob
|
|
635
|
+
*/
|
|
636
|
+
static isBlob(value) {
|
|
637
|
+
return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.BLOB;
|
|
638
|
+
}
|
|
639
|
+
/**
|
|
640
|
+
* 检查 value 是否为 File
|
|
641
|
+
* @param value 待检查值
|
|
642
|
+
* @returns 是否为 File
|
|
643
|
+
*/
|
|
644
|
+
static isFile(value) {
|
|
645
|
+
return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.FILE;
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* 检查 value 是否为 ReadableStream
|
|
649
|
+
* - Uses `Object.prototype.toString` where supported (modern browsers, Node.js ≥18).
|
|
650
|
+
* - Falls back to duck-typing in older environments.
|
|
651
|
+
* - Resistant to basic forgery, but not 100% secure in all polyfill scenarios.
|
|
652
|
+
* - ⚠️ Note: In older Node.js (<18) or with non-compliant polyfills, this may return false positives or negatives.
|
|
653
|
+
*
|
|
654
|
+
* @param value 待检查值
|
|
655
|
+
* @returns 是否为 ReadableStream
|
|
656
|
+
*/
|
|
657
|
+
static isReadableStream(value) {
|
|
658
|
+
if (this.getPrototypeString(value) === this.PROTOTYPE_TAGS.READABLE_STREAM) return true;
|
|
659
|
+
return this.isObject(value) && this.isFunction(value["getReader"]) && this.isFunction(value["pipeThrough"]);
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* 检查 value 是否为 Window
|
|
663
|
+
* @param value 待检查值
|
|
664
|
+
* @returns 是否为 Window
|
|
665
|
+
*/
|
|
666
|
+
static isWindow(value) {
|
|
667
|
+
return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.WINDOW;
|
|
668
|
+
}
|
|
669
|
+
/**
|
|
670
|
+
* 检查 value 是否为 HTMLIFrameElement
|
|
671
|
+
* @param value 待检查值
|
|
672
|
+
* @returns 是否为 HTMLIFrameElement
|
|
673
|
+
*/
|
|
674
|
+
static isIframe(value) {
|
|
675
|
+
if (typeof window === "undefined") return false;
|
|
676
|
+
return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.IFRAME;
|
|
677
|
+
}
|
|
678
|
+
/**
|
|
679
|
+
* 检查 value 是否为 Date 对象
|
|
680
|
+
*
|
|
681
|
+
* @param value 待检查值
|
|
682
|
+
* @param invalidCheck 是否要求日期有效(非 Invalid Date)。默认 true
|
|
683
|
+
* - true: 仅当是有效 Date 对象时返回 true(排除 new Date('invalid'))
|
|
684
|
+
* - false: 只要 [[Prototype]] 是 Date 即返回 true(包含 Invalid Date)
|
|
685
|
+
* @returns 是否为 Date 对象,根据 invalidCheck 返回不同语义的 Date 判定
|
|
686
|
+
*
|
|
687
|
+
* @example
|
|
688
|
+
* ```ts
|
|
689
|
+
* TypeUtil.isDate(new Date()); // true
|
|
690
|
+
* TypeUtil.isDate(new Date('invalid')); // false
|
|
691
|
+
* TypeUtil.isDate(new Date('invalid'), false); // true
|
|
692
|
+
* TypeUtil.isDate(null); // false
|
|
693
|
+
* TypeUtil.isDate({}); // false
|
|
694
|
+
* ```
|
|
695
|
+
*/
|
|
696
|
+
static isDate(value, invalidCheck = true) {
|
|
697
|
+
if (!value || typeof value !== "object") return false;
|
|
698
|
+
if (this.getPrototypeString(value) !== this.PROTOTYPE_TAGS.DATE) return false;
|
|
699
|
+
if (!invalidCheck) return true;
|
|
700
|
+
try {
|
|
701
|
+
const time = value.getTime();
|
|
702
|
+
return typeof time === "number" && !Number.isNaN(time);
|
|
703
|
+
} catch {
|
|
704
|
+
return false;
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
/**
|
|
708
|
+
* 检查 value 是否为 Error 对象
|
|
709
|
+
* @param value 待检查值
|
|
710
|
+
* @returns 是否为 Error
|
|
711
|
+
*/
|
|
712
|
+
static isError(value) {
|
|
713
|
+
return value instanceof Error || this.getPrototypeString(value) === this.PROTOTYPE_TAGS.ERROR;
|
|
714
|
+
}
|
|
715
|
+
/**
|
|
716
|
+
* 检查 value 是否为 RegExp
|
|
717
|
+
* @param value 待检查值
|
|
718
|
+
* @returns 是否为 RegExp
|
|
719
|
+
*/
|
|
720
|
+
static isRegExp(value) {
|
|
721
|
+
if (typeof value !== "object" || value === null) return false;
|
|
722
|
+
try {
|
|
723
|
+
const regex = value;
|
|
724
|
+
return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.REG_EXP && this.isString(regex.source) && this.isString(regex.flags) && this.isBoolean(regex.global) && this.isFunction(regex.test);
|
|
725
|
+
} catch (error) {
|
|
726
|
+
return false;
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
/**
|
|
730
|
+
* 检查 value 是否为 WebSocket
|
|
731
|
+
* @param value 待检查值
|
|
732
|
+
* @returns 是否为 WebSocket
|
|
733
|
+
*/
|
|
734
|
+
static isWebSocket(value) {
|
|
735
|
+
return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.WEB_SOCKET;
|
|
736
|
+
}
|
|
737
|
+
/**
|
|
738
|
+
* 检查 value 是否为 URLSearchParams
|
|
739
|
+
* @param value 待检查值
|
|
740
|
+
* @returns 是否为 URLSearchParams
|
|
741
|
+
*/
|
|
742
|
+
static isURLSearchParams(value) {
|
|
743
|
+
return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.URL_SEARCH_PARAMS;
|
|
744
|
+
}
|
|
745
|
+
/**
|
|
746
|
+
* 检查 value 是否为 AbortSignal
|
|
747
|
+
* @param value 待检查值
|
|
748
|
+
* @returns 是否为 AbortSignal
|
|
749
|
+
*/
|
|
750
|
+
static isAbortSignal(value) {
|
|
751
|
+
return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.ABORT_SIGNAL;
|
|
752
|
+
}
|
|
753
|
+
/**
|
|
754
|
+
* 检查 value 是否为可迭代对象 (Iterable)
|
|
755
|
+
* @param value 待检查值
|
|
756
|
+
* @returns 是否为 Iterable
|
|
757
|
+
*/
|
|
758
|
+
static isIterable(value) {
|
|
759
|
+
return !!value && typeof value[Symbol.iterator] === "function";
|
|
760
|
+
}
|
|
761
|
+
/**
|
|
762
|
+
* 检查 value 是否为 Falsy 值 (false, 0, "", null, undefined, NaN)
|
|
763
|
+
* @param value 待检查值
|
|
764
|
+
* @returns 是否为 Falsy
|
|
765
|
+
*/
|
|
766
|
+
static isFalsy(value) {
|
|
767
|
+
if (this.isNaN(value) || this.isNull(value) || this.isUndefined(value)) return true;
|
|
768
|
+
return value === false || value === 0 || value === 0n || value === "";
|
|
769
|
+
}
|
|
770
|
+
static isFalsyLike(value) {
|
|
771
|
+
if (this.isFalsy(value)) return true;
|
|
772
|
+
return typeof value === "string" && (value === "null" || value === "undefined" || value === "NaN" || value === "false" || value === "0" || value === "-0" || value === "0n");
|
|
773
|
+
}
|
|
774
|
+
};
|
|
775
|
+
_defineProperty(TypeUtil, "PROTOTYPE_TAGS", {
|
|
776
|
+
STRING: "[object String]",
|
|
777
|
+
NUMBER: "[object Number]",
|
|
778
|
+
BOOLEAN: "[object Boolean]",
|
|
779
|
+
BIGINT: "[object BigInt]",
|
|
780
|
+
SYMBOL: "[object Symbol]",
|
|
781
|
+
UNDEFINED: "[object Undefined]",
|
|
782
|
+
NULL: "[object Null]",
|
|
783
|
+
OBJECT: "[object Object]",
|
|
784
|
+
FUNCTION: "[object Function]",
|
|
785
|
+
GENERATOR_FUNCTION: "[object GeneratorFunction]",
|
|
786
|
+
ASYNC_FUNCTION: "[object AsyncFunction]",
|
|
787
|
+
ASYNC_GENERATOR_FUNCTION: "[object AsyncGeneratorFunction]",
|
|
788
|
+
PROMISE: "[object Promise]",
|
|
789
|
+
MAP: "[object Map]",
|
|
790
|
+
SET: "[object Set]",
|
|
791
|
+
WEAK_MAP: "[object WeakMap]",
|
|
792
|
+
WEAK_SET: "[object WeakSet]",
|
|
793
|
+
BLOB: "[object Blob]",
|
|
794
|
+
FILE: "[object File]",
|
|
795
|
+
READABLE_STREAM: "[object ReadableStream]",
|
|
796
|
+
GLOBAL: "[object global]",
|
|
797
|
+
WINDOW: "[object Window]",
|
|
798
|
+
IFRAME: "[object HTMLIFrameElement]",
|
|
799
|
+
DATE: "[object Date]",
|
|
800
|
+
ERROR: "[object Error]",
|
|
801
|
+
REG_EXP: "[object RegExp]",
|
|
802
|
+
WEB_SOCKET: "[object WebSocket]",
|
|
803
|
+
URL_SEARCH_PARAMS: "[object URLSearchParams]",
|
|
804
|
+
ABORT_SIGNAL: "[object AbortSignal]"
|
|
805
|
+
});
|
|
806
|
+
_defineProperty(TypeUtil, "TYPED_ARRAY_TAGS", new Set([
|
|
807
|
+
"[object Int8Array]",
|
|
808
|
+
"[object Uint8Array]",
|
|
809
|
+
"[object Uint8ClampedArray]",
|
|
810
|
+
"[object Int16Array]",
|
|
811
|
+
"[object Uint16Array]",
|
|
812
|
+
"[object Int32Array]",
|
|
813
|
+
"[object Uint32Array]",
|
|
814
|
+
"[object Float32Array]",
|
|
815
|
+
"[object Float64Array]",
|
|
816
|
+
"[object BigInt64Array]",
|
|
817
|
+
"[object BigUint64Array]"
|
|
818
|
+
]));
|
|
819
|
+
/**
|
|
820
|
+
* 字符串工具类
|
|
821
|
+
*/
|
|
822
|
+
var StringUtil = class {
|
|
823
|
+
/**
|
|
824
|
+
* 从字符串中提取数字字符串
|
|
825
|
+
* - 移除非数字字符,保留符号和小数点
|
|
826
|
+
*
|
|
827
|
+
* @param input 待处理字符串
|
|
828
|
+
* @returns 提取出的数字字符串
|
|
829
|
+
* @example
|
|
830
|
+
* ```ts
|
|
831
|
+
* StringUtil.toNumber("$1,234.56"); // "1234.56"
|
|
832
|
+
* StringUtil.toNumber("abc-123"); // "-123"
|
|
833
|
+
* ```
|
|
834
|
+
*/
|
|
835
|
+
static toNumber(input) {
|
|
836
|
+
if (!TypeUtil.isString(input, true)) return "0";
|
|
837
|
+
const cleaned = input.replace(/[^0-9.-]/g, "");
|
|
838
|
+
if (!cleaned) return "0";
|
|
839
|
+
let isDecimal = false;
|
|
840
|
+
let signCount = 0;
|
|
841
|
+
let firstIndex = -1;
|
|
842
|
+
const stringList = cleaned.split("").map((s, i) => {
|
|
843
|
+
if (s === ".") {
|
|
844
|
+
if (isDecimal) return "";
|
|
845
|
+
isDecimal = true;
|
|
846
|
+
return ".";
|
|
847
|
+
}
|
|
848
|
+
if (s === "-") {
|
|
849
|
+
firstIndex === -1 && signCount++;
|
|
850
|
+
return "";
|
|
851
|
+
}
|
|
852
|
+
firstIndex === -1 && (firstIndex = i);
|
|
853
|
+
return s;
|
|
854
|
+
});
|
|
855
|
+
const sign = signCount % 2 === 1 ? "-" : "";
|
|
856
|
+
if (firstIndex === -1) return sign + "0";
|
|
857
|
+
let result = stringList.join("");
|
|
858
|
+
if (result.startsWith(".")) result = "0" + result;
|
|
859
|
+
if (result.endsWith(".")) result = result.slice(0, -1);
|
|
860
|
+
return sign + result;
|
|
861
|
+
}
|
|
862
|
+
static toLowerCase(input) {
|
|
863
|
+
if (!TypeUtil.isString(input, true)) return "";
|
|
864
|
+
return input.toLowerCase();
|
|
865
|
+
}
|
|
866
|
+
static toUpperCase(input) {
|
|
867
|
+
if (!TypeUtil.isString(input, true)) return "";
|
|
868
|
+
return input.toUpperCase();
|
|
869
|
+
}
|
|
870
|
+
/**
|
|
871
|
+
* 字符串首字母大小写
|
|
872
|
+
* - 包含非西欧字母字符时,不处理
|
|
873
|
+
* - 纯字母且全大写时,不处理
|
|
874
|
+
* - 纯字母且非全大写时,首字母小写,其余保留
|
|
875
|
+
* - 纯字母且非全大写时,首字母大写,其余保留
|
|
876
|
+
*
|
|
877
|
+
* @param input 待处理字符串
|
|
878
|
+
* @param caseType 大小写类型
|
|
879
|
+
* @returns 处理后的字符串
|
|
880
|
+
* @example
|
|
881
|
+
* ```ts
|
|
882
|
+
* StringUtil.toInitialCase("Hello", "lower"); // "hello"
|
|
883
|
+
* StringUtil.toInitialCase("hello", "upper"); // "Hello"
|
|
884
|
+
* ```
|
|
885
|
+
*/
|
|
886
|
+
static toInitialCase(input, caseType) {
|
|
887
|
+
if (!TypeUtil.isString(input, true)) return "";
|
|
888
|
+
return input.replace(/\S+/g, (word) => {
|
|
889
|
+
if (/[^a-zA-Z\u00C0-\u017F]/.test(word)) return word;
|
|
890
|
+
if (word === word.toLocaleUpperCase()) return word;
|
|
891
|
+
if (caseType === "lower" && word[0]) return word[0].toLocaleLowerCase() + word.slice(1);
|
|
892
|
+
if (caseType === "upper" && word[0]) return word[0].toLocaleUpperCase() + word.slice(1);
|
|
893
|
+
return word;
|
|
894
|
+
});
|
|
895
|
+
}
|
|
896
|
+
/**
|
|
897
|
+
* 将路径转换为 POSIX 风格
|
|
898
|
+
* - 统一使用正斜杠 (/)
|
|
899
|
+
* - 可选移除 Windows 盘符 (如 C:)
|
|
900
|
+
* - 可选移除开头的斜杠
|
|
901
|
+
* - 规范化连续斜杠为单个斜杠
|
|
902
|
+
*
|
|
903
|
+
* @param input 待处理字符串
|
|
904
|
+
* @param removeLeadingSlash 是否移除开头斜杠,默认为 `false`。如果移除了盘符,路径通常会以 / 开头,此参数可控制是否保留该 /
|
|
905
|
+
* @returns 转换后的路径,如果输入无效则返回空字符串
|
|
906
|
+
*
|
|
907
|
+
* @example
|
|
908
|
+
* ```ts
|
|
909
|
+
* StringUtil.toPosix("C:\\Windows\\System32"); // 默认: "/Windows/System32" (移除了 C: 并标准化)
|
|
910
|
+
*
|
|
911
|
+
* StringUtil.toPosix("C:\\Windows\\System32", true); // 移除开头斜杠: "Windows/System32"
|
|
912
|
+
*
|
|
913
|
+
* StringUtil.toPosix("\\\\server\\share\\file.txt"); // UNC 路径: "/server/share/file.txt"
|
|
914
|
+
*
|
|
915
|
+
* StringUtil.toPosix("folder\\subfolder\\file.txt"); // 相对路径: "folder/subfolder/file.txt"
|
|
916
|
+
* ```
|
|
917
|
+
*/
|
|
918
|
+
static toPosix(input, removeLeadingSlash = false) {
|
|
919
|
+
if (!TypeUtil.isString(input, true)) return "";
|
|
920
|
+
let normalized = input.replace(/^[A-Za-z]:([\\/])?/, (_, separator) => {
|
|
921
|
+
return separator ? "/" : "";
|
|
922
|
+
});
|
|
923
|
+
normalized = normalized.replace(/\\/g, "/");
|
|
924
|
+
normalized = normalized.replace(/\/+/g, "/");
|
|
925
|
+
if (removeLeadingSlash && normalized.startsWith("/")) normalized = normalized.substring(1);
|
|
926
|
+
return normalized;
|
|
927
|
+
}
|
|
928
|
+
static toJson(input, fallback) {
|
|
929
|
+
if (!TypeUtil.isString(input, true)) return fallback;
|
|
930
|
+
try {
|
|
931
|
+
return JSON.parse(input);
|
|
932
|
+
} catch (error) {
|
|
933
|
+
return fallback;
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
static toValues(input, valueType = "number", splitSymbol = ",") {
|
|
937
|
+
if (!TypeUtil.isString(input, true)) return [];
|
|
938
|
+
try {
|
|
939
|
+
const values = input.split(splitSymbol);
|
|
940
|
+
if (valueType === "number") return values.map((d) => Number(d));
|
|
941
|
+
return values;
|
|
942
|
+
} catch (error) {
|
|
943
|
+
return [];
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
/**
|
|
947
|
+
* 从字符串中裁切掉所有的前缀和后缀字符
|
|
948
|
+
*
|
|
949
|
+
* @param input 待处理字符串
|
|
950
|
+
* @param charsToTrim 裁切字符,默认为 `" "`
|
|
951
|
+
* @returns 裁切后的字符串
|
|
952
|
+
* @example
|
|
953
|
+
* ```ts
|
|
954
|
+
* StringUtil.trim(" hello "); // "hello"
|
|
955
|
+
* StringUtil.trim("__hello__", "_"); // "hello"
|
|
956
|
+
* ```
|
|
957
|
+
*/
|
|
958
|
+
static trim(input, charsToTrim = " ") {
|
|
959
|
+
if (!TypeUtil.isString(input, true)) return "";
|
|
960
|
+
const toTrim = charsToTrim.replace(/[\W]{1}/g, "\\$&");
|
|
961
|
+
const regex = new RegExp(`^[${toTrim}]+|[${toTrim}]+$`, "g");
|
|
962
|
+
return input.replace(regex, "");
|
|
963
|
+
}
|
|
964
|
+
/**
|
|
965
|
+
* 截取字符串
|
|
966
|
+
* - 支持自定义省略符,不会截断在汉字中间(因为JS字符串本身按字符处理)
|
|
967
|
+
*
|
|
968
|
+
* @param input 待处理字符串
|
|
969
|
+
* @param maxLength 最大长度 (包含省略符)
|
|
970
|
+
* @param ellipsis 省略符,默认为 `...`
|
|
971
|
+
* @returns 截取后的字符串
|
|
972
|
+
* @example
|
|
973
|
+
* ```ts
|
|
974
|
+
* StringUtil.truncate("hello world", 8); // "hello..."
|
|
975
|
+
* ```
|
|
976
|
+
*/
|
|
977
|
+
static truncate(input, maxLength, ellipsis = "...") {
|
|
978
|
+
if (!TypeUtil.isString(input, true)) return "";
|
|
979
|
+
const codePoints = Array.from(input);
|
|
980
|
+
if (!TypeUtil.isInteger(maxLength) || maxLength < 0) return input;
|
|
981
|
+
if (codePoints.length <= maxLength) return input;
|
|
982
|
+
const availableLength = maxLength - ellipsis.length;
|
|
983
|
+
if (availableLength <= 0) return "";
|
|
984
|
+
return codePoints.slice(0, availableLength).join("") + ellipsis;
|
|
985
|
+
}
|
|
986
|
+
/**
|
|
987
|
+
* 字符串模板替换
|
|
988
|
+
* - 使用对象的属性值替换字符串中的 {{key}} 模板
|
|
989
|
+
*
|
|
990
|
+
* @param input 待处理字符串
|
|
991
|
+
* @param template 模板对象
|
|
992
|
+
* @param regex 模板匹配正则 (默认: `\{\{(.+?)\}\}`)
|
|
993
|
+
* @returns 替换后的字符串
|
|
994
|
+
* @example
|
|
995
|
+
* ```ts
|
|
996
|
+
* StringUtil.template("Hello {{name}}", { name: "World" }); // "Hello World"
|
|
997
|
+
* ```
|
|
998
|
+
*/
|
|
999
|
+
static template(input, template, regex = /\{\{(.+?)\}\}/g) {
|
|
1000
|
+
if (!TypeUtil.isString(input, true)) return "";
|
|
1001
|
+
regex.lastIndex = 0;
|
|
1002
|
+
let result = "";
|
|
1003
|
+
let from = 0;
|
|
1004
|
+
let match;
|
|
1005
|
+
while (match = regex.exec(input)) {
|
|
1006
|
+
const replacement = template[match[1]];
|
|
1007
|
+
const valueToInsert = replacement === null || replacement === void 0 ? match[0] : replacement;
|
|
1008
|
+
result += input.slice(from, match.index) + valueToInsert;
|
|
1009
|
+
from = regex.lastIndex;
|
|
1010
|
+
}
|
|
1011
|
+
return result + input.slice(from);
|
|
1012
|
+
}
|
|
1013
|
+
/**
|
|
1014
|
+
* 字符串替换
|
|
1015
|
+
* - 替换第一个匹配项
|
|
1016
|
+
*
|
|
1017
|
+
* @param input 待处理字符串
|
|
1018
|
+
* @param search 匹配项
|
|
1019
|
+
* @param replacement 替换项
|
|
1020
|
+
* @returns 替换后的字符串
|
|
1021
|
+
* @example
|
|
1022
|
+
* ```ts
|
|
1023
|
+
* StringUtil.replace("hello world", "world", "context"); // "hello context"
|
|
1024
|
+
* ```
|
|
1025
|
+
*/
|
|
1026
|
+
static replace(input, search, replacement) {
|
|
1027
|
+
if (!TypeUtil.isString(input, true)) return "";
|
|
1028
|
+
return input.replace(search, replacement);
|
|
1029
|
+
}
|
|
1030
|
+
};
|
|
1031
|
+
//#endregion
|
|
1032
|
+
//#region ../utils/dist/index.js
|
|
1033
|
+
/**
|
|
1034
|
+
* 数组工具类
|
|
1035
|
+
*/
|
|
1036
|
+
var ArrayUtil = class {
|
|
1037
|
+
static cast(candidate, checkEmpty = true) {
|
|
1038
|
+
if (checkEmpty && (TypeUtil.isUndefined(candidate) || TypeUtil.isNull(candidate))) return [];
|
|
1039
|
+
return TypeUtil.isArray(candidate) ? [...candidate] : [candidate];
|
|
1040
|
+
}
|
|
1041
|
+
static first(initialList, fallback) {
|
|
1042
|
+
if (!TypeUtil.isArray(initialList) || initialList.length === 0) return fallback;
|
|
1043
|
+
return initialList[0];
|
|
1044
|
+
}
|
|
1045
|
+
static last(initialList, fallback) {
|
|
1046
|
+
if (!TypeUtil.isArray(initialList) || initialList.length === 0) return fallback;
|
|
1047
|
+
return initialList[initialList.length - 1];
|
|
1048
|
+
}
|
|
1049
|
+
/**
|
|
1050
|
+
* 数组竞选
|
|
1051
|
+
* - 返回在匹配函数的比较条件中获胜的最终项目,适用于更复杂的最小值/最大值计算
|
|
1052
|
+
*
|
|
1053
|
+
* @param initialList 数组
|
|
1054
|
+
* @param match 匹配函数
|
|
1055
|
+
* @returns 获胜的元素,如果数组为空或参数无效则返回 `null`
|
|
1056
|
+
* @example
|
|
1057
|
+
* ```ts
|
|
1058
|
+
* const list = [1, 10, 5];
|
|
1059
|
+
* ArrayUtil.compete(list, (a, b) => (a > b ? a : b)); // 10
|
|
1060
|
+
* ArrayUtil.compete(list, (a, b) => (a < b ? a : b)); // 1
|
|
1061
|
+
* ```
|
|
1062
|
+
*/
|
|
1063
|
+
static compete(initialList, match) {
|
|
1064
|
+
if (!TypeUtil.isArray(initialList) || initialList.length === 0 || !TypeUtil.isFunction(match)) return null;
|
|
1065
|
+
return initialList.reduce(match);
|
|
1066
|
+
}
|
|
1067
|
+
/**
|
|
1068
|
+
* 统计数组的项目出现次数
|
|
1069
|
+
* - 通过给定的标识符匹配函数,返回一个对象,其中键是回调函数返回的 key 值,每个值是一个整数,表示该 key 出现的次数
|
|
1070
|
+
*
|
|
1071
|
+
* @param initialList 初始数组
|
|
1072
|
+
* @param match 匹配函数
|
|
1073
|
+
* @returns 统计对象
|
|
1074
|
+
* @example
|
|
1075
|
+
* ```ts
|
|
1076
|
+
* const list = ["a", "b", "a", "c"];
|
|
1077
|
+
* ArrayUtil.count(list, (x) => x); // { a: 2, b: 1, c: 1 }
|
|
1078
|
+
*
|
|
1079
|
+
* const users = [{ id: 1, group: "A" }, { id: 2, group: "B" }, { id: 3, group: "A" }];
|
|
1080
|
+
* ArrayUtil.count(users, (u) => u.group); // { A: 2, B: 1 }
|
|
1081
|
+
* ```
|
|
1082
|
+
*/
|
|
1083
|
+
static count(initialList, match) {
|
|
1084
|
+
if (!TypeUtil.isArray(initialList) || !TypeUtil.isFunction(match)) return {};
|
|
1085
|
+
return initialList.reduce((prev, curr, index) => {
|
|
1086
|
+
const id = match(curr, index).toString();
|
|
1087
|
+
prev[id] = (prev[id] ?? 0) + 1;
|
|
1088
|
+
return prev;
|
|
1089
|
+
}, {});
|
|
1090
|
+
}
|
|
1091
|
+
/**
|
|
1092
|
+
* 获取数组差集
|
|
1093
|
+
* - 返回在 `initialList` 中存在,但在 `diffList` 中不存在的元素
|
|
1094
|
+
*
|
|
1095
|
+
* @param initialList 初始数组
|
|
1096
|
+
* @param diffList 对比数组
|
|
1097
|
+
* @param match 匹配函数
|
|
1098
|
+
* @returns 差集数组
|
|
1099
|
+
* @example
|
|
1100
|
+
* ```ts
|
|
1101
|
+
* ArrayUtil.difference([1, 2, 3], [2, 3, 4]); // [1]
|
|
1102
|
+
* ArrayUtil.difference([{ id: 1 }, { id: 2 }], [{ id: 2 }], (x) => x.id); // [{ id: 1 }]
|
|
1103
|
+
* ```
|
|
1104
|
+
*/
|
|
1105
|
+
static difference(initialList, diffList, match) {
|
|
1106
|
+
if (!TypeUtil.isArray(initialList) && !TypeUtil.isArray(diffList)) return [];
|
|
1107
|
+
if (!TypeUtil.isArray(initialList) || !initialList.length) return [];
|
|
1108
|
+
if (!TypeUtil.isArray(diffList) || !diffList.length) return [...initialList];
|
|
1109
|
+
if (!TypeUtil.isFunction(match)) {
|
|
1110
|
+
const arraySet = new Set(diffList);
|
|
1111
|
+
return Array.from(new Set(initialList.filter((item) => !arraySet.has(item))));
|
|
1112
|
+
}
|
|
1113
|
+
const map = /* @__PURE__ */ new Map();
|
|
1114
|
+
diffList.forEach((item, index) => {
|
|
1115
|
+
map.set(match(item, index), true);
|
|
1116
|
+
});
|
|
1117
|
+
return initialList.filter((item, index) => !map.get(match(item, index)));
|
|
1118
|
+
}
|
|
1119
|
+
static intersection(initialList, diffList, match) {
|
|
1120
|
+
if (!TypeUtil.isArray(initialList) || !TypeUtil.isArray(diffList)) return [];
|
|
1121
|
+
if (!initialList.length || !diffList.length) return [];
|
|
1122
|
+
if (!TypeUtil.isFunction(match)) {
|
|
1123
|
+
const diffSet = new Set(diffList);
|
|
1124
|
+
return initialList.filter((item) => diffSet.has(item));
|
|
1125
|
+
}
|
|
1126
|
+
const diffKeys = new Set(diffList.map((item, index) => match(item, index)));
|
|
1127
|
+
return initialList.filter((item, index) => diffKeys.has(match(item, index)));
|
|
1128
|
+
}
|
|
1129
|
+
static merge(initialList, mergeList, match) {
|
|
1130
|
+
if (!TypeUtil.isArray(initialList)) return [];
|
|
1131
|
+
if (!TypeUtil.isArray(mergeList)) return [...initialList];
|
|
1132
|
+
if (!TypeUtil.isFunction(match)) return Array.from(new Set([...initialList, ...mergeList]));
|
|
1133
|
+
const keys = /* @__PURE__ */ new Map();
|
|
1134
|
+
mergeList.forEach((item, index) => {
|
|
1135
|
+
keys.set(match(item, index), item);
|
|
1136
|
+
});
|
|
1137
|
+
return initialList.map((prevItem, index) => {
|
|
1138
|
+
const key = match(prevItem, index);
|
|
1139
|
+
return keys.has(key) ? keys.get(key) : prevItem;
|
|
1140
|
+
});
|
|
1141
|
+
}
|
|
1142
|
+
static pick(initialList, filter, mapper) {
|
|
1143
|
+
if (!TypeUtil.isArray(initialList)) return [];
|
|
1144
|
+
if (!TypeUtil.isFunction(filter)) return [...initialList];
|
|
1145
|
+
const hasMapper = TypeUtil.isFunction(mapper);
|
|
1146
|
+
return initialList.reduce((prev, curr, index) => {
|
|
1147
|
+
if (!filter(curr, index)) return prev;
|
|
1148
|
+
if (hasMapper) prev.push(mapper(curr, index));
|
|
1149
|
+
else prev.push(curr);
|
|
1150
|
+
return prev;
|
|
1151
|
+
}, []);
|
|
1152
|
+
}
|
|
1153
|
+
static replace(initialList, newItem, match) {
|
|
1154
|
+
if (!TypeUtil.isArray(initialList) || !initialList.length) return [];
|
|
1155
|
+
if (!TypeUtil.isFunction(match)) return [...initialList];
|
|
1156
|
+
for (let i = 0; i < initialList.length; i++) {
|
|
1157
|
+
const item = initialList[i];
|
|
1158
|
+
if (match(item, i)) return [
|
|
1159
|
+
...initialList.slice(0, i),
|
|
1160
|
+
newItem,
|
|
1161
|
+
...initialList.slice(i + 1, initialList.length)
|
|
1162
|
+
];
|
|
1163
|
+
}
|
|
1164
|
+
return [...initialList];
|
|
1165
|
+
}
|
|
1166
|
+
/**
|
|
1167
|
+
* 数组项替换并移动
|
|
1168
|
+
* - 在给定的数组中,替换并移动符合匹配函数结果的项目
|
|
1169
|
+
* - 只替换和移动第一个匹配项
|
|
1170
|
+
* - 未匹配时,根据 `position` 在指定位置插入 `newItem`
|
|
1171
|
+
*
|
|
1172
|
+
* @param initialList 初始数组
|
|
1173
|
+
* @param newItem 替换项
|
|
1174
|
+
* @param match 匹配函数
|
|
1175
|
+
* @param position 移动位置,可选 `start` | `end` | 索引位置, 默认为 `end`
|
|
1176
|
+
* @returns
|
|
1177
|
+
* @example
|
|
1178
|
+
* ```ts
|
|
1179
|
+
* ArrayUtil.replaceMove([1, 2, 3, 4], 5, (n) => n === 2, 0); // [5, 1, 3, 4]
|
|
1180
|
+
* ArrayUtil.replaceMove([1, 2, 3, 4], 5, (n) => n === 2, 2); // [1, 3, 5, 4]
|
|
1181
|
+
* ArrayUtil.replaceMove([1, 2, 3, 4], 5, (n) => n === 2, "start"); // [5, 1, 3, 4]
|
|
1182
|
+
* ArrayUtil.replaceMove([1, 2, 3, 4], 5, (n) => n === 2); // [1, 3, 4, 5]
|
|
1183
|
+
* ```
|
|
1184
|
+
*/
|
|
1185
|
+
static replaceMove(initialList, newItem, match, position) {
|
|
1186
|
+
if (!TypeUtil.isArray(initialList)) return [];
|
|
1187
|
+
if (!initialList.length) return [newItem];
|
|
1188
|
+
if (!TypeUtil.isFunction(match)) return [...initialList];
|
|
1189
|
+
const result = [...initialList];
|
|
1190
|
+
const matchIndex = initialList.findIndex(match);
|
|
1191
|
+
if (matchIndex !== -1) result.splice(matchIndex, 1);
|
|
1192
|
+
if (position === "start") result.unshift(newItem);
|
|
1193
|
+
else if (position === 0 || TypeUtil.isPositiveInteger(position, false)) result.splice(Math.min(position, result.length), 0, newItem);
|
|
1194
|
+
else result.push(newItem);
|
|
1195
|
+
return result;
|
|
1196
|
+
}
|
|
1197
|
+
/**
|
|
1198
|
+
* 数组切分
|
|
1199
|
+
* - 将数组以指定的长度切分后,组合在高维数组中
|
|
1200
|
+
*
|
|
1201
|
+
* @param initialList 初始数组
|
|
1202
|
+
* @param size 分割尺寸,默认 `10`
|
|
1203
|
+
* @returns 切分后的二维数组
|
|
1204
|
+
* @example
|
|
1205
|
+
* ```ts
|
|
1206
|
+
* ArrayUtil.split([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
|
|
1207
|
+
* ```
|
|
1208
|
+
*/
|
|
1209
|
+
static split(initialList, size = 10) {
|
|
1210
|
+
if (!TypeUtil.isArray(initialList)) return [];
|
|
1211
|
+
if (!TypeUtil.isPositiveInteger(size, false)) return [];
|
|
1212
|
+
const count = Math.ceil(initialList.length / size);
|
|
1213
|
+
return Array.from({ length: count }).fill(null).map((_c, i) => {
|
|
1214
|
+
return initialList.slice(i * size, i * size + size);
|
|
1215
|
+
});
|
|
1216
|
+
}
|
|
1217
|
+
/**
|
|
1218
|
+
* 数组分组过滤
|
|
1219
|
+
* - 给定一个数组和一个条件,返回一个由两个数组组成的元组,其中第一个数组包含所有满足条件的项,第二个数组包含所有不满足条件的项
|
|
1220
|
+
*
|
|
1221
|
+
* @param initialList 初始数组
|
|
1222
|
+
* @param match 条件匹配函数
|
|
1223
|
+
* @returns [满足条件的项[], 不满足条件的项[]]
|
|
1224
|
+
* @example
|
|
1225
|
+
* ```ts
|
|
1226
|
+
* ArrayUtil.fork([1, 2, 3, 4], (n) => n % 2 === 0); // [[2, 4], [1, 3]]
|
|
1227
|
+
* ```
|
|
1228
|
+
*/
|
|
1229
|
+
static fork(initialList, match) {
|
|
1230
|
+
const forked = [[], []];
|
|
1231
|
+
if (TypeUtil.isArray(initialList)) initialList.forEach((item, index) => {
|
|
1232
|
+
forked[match(item, index) ? 0 : 1].push(item);
|
|
1233
|
+
});
|
|
1234
|
+
return forked;
|
|
1235
|
+
}
|
|
1236
|
+
/**
|
|
1237
|
+
* 数组解压
|
|
1238
|
+
* - `ArrayUtil.zip` 的反向操作
|
|
1239
|
+
*
|
|
1240
|
+
* @param arrayList 压缩后的数组
|
|
1241
|
+
* @returns 解压后的二维数组
|
|
1242
|
+
* @example
|
|
1243
|
+
* ```ts
|
|
1244
|
+
* ArrayUtil.unzip([[1, "a"], [2, "b"]]); // [[1, 2], ["a", "b"]]
|
|
1245
|
+
* ```
|
|
1246
|
+
*/
|
|
1247
|
+
static unzip(arrayList) {
|
|
1248
|
+
if (!TypeUtil.isArray(arrayList) || !arrayList.length) return [];
|
|
1249
|
+
const out = new Array(arrayList.reduce((max, arr) => Math.max(max, arr.length), 0));
|
|
1250
|
+
let index = 0;
|
|
1251
|
+
const get = (array) => array[index];
|
|
1252
|
+
for (; index < out.length; index++) out[index] = Array.from(arrayList, get);
|
|
1253
|
+
return out;
|
|
1254
|
+
}
|
|
1255
|
+
static zip(...arrays) {
|
|
1256
|
+
return this.unzip(arrays);
|
|
1257
|
+
}
|
|
1258
|
+
static zipToObject(keys, values) {
|
|
1259
|
+
const result = {};
|
|
1260
|
+
if (!TypeUtil.isArray(keys) || !keys.length) return result;
|
|
1261
|
+
const getValue = TypeUtil.isFunction(values) ? values : TypeUtil.isArray(values) ? (_k, i) => values[i] : (_k, _i) => values;
|
|
1262
|
+
return keys.reduce((acc, key, idx) => {
|
|
1263
|
+
acc[key] = getValue(key, idx);
|
|
1264
|
+
return acc;
|
|
1265
|
+
}, result);
|
|
1266
|
+
}
|
|
1267
|
+
};
|
|
1268
|
+
var _DateTimeUtil;
|
|
1269
|
+
/**
|
|
1270
|
+
* 日期工具类
|
|
1271
|
+
*/
|
|
1272
|
+
var DateTimeUtil = class {
|
|
1273
|
+
/**
|
|
1274
|
+
* 获取当前时区信息
|
|
1275
|
+
*
|
|
1276
|
+
* @returns 时区信息对象 (UTC偏移和时区名称)
|
|
1277
|
+
* @example
|
|
1278
|
+
* ```ts
|
|
1279
|
+
* DateTimeUtil.getTimeZone(); // { UTC: "UTC+8", timeZone: "Asia/Shanghai" }
|
|
1280
|
+
* ```
|
|
1281
|
+
*/
|
|
1282
|
+
static getTimeZone() {
|
|
1283
|
+
const hour = 0 - (/* @__PURE__ */ new Date()).getTimezoneOffset() / this.MINUTE_PER_HOUR;
|
|
1284
|
+
return {
|
|
1285
|
+
UTC: "UTC" + (hour >= 0 ? "+" + hour : hour),
|
|
1286
|
+
timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone
|
|
1287
|
+
};
|
|
1288
|
+
}
|
|
1289
|
+
};
|
|
1290
|
+
_DateTimeUtil = DateTimeUtil;
|
|
1291
|
+
_defineProperty(DateTimeUtil, "MILLISECONDS_PER_SECOND", 1e3);
|
|
1292
|
+
_defineProperty(DateTimeUtil, "SECOND_PER_MINUTE", 60);
|
|
1293
|
+
_defineProperty(DateTimeUtil, "MINUTE_PER_HOUR", 60);
|
|
1294
|
+
_defineProperty(DateTimeUtil, "SECOND_PER_HOUR", _DateTimeUtil.SECOND_PER_MINUTE ** 2);
|
|
1295
|
+
_defineProperty(DateTimeUtil, "HOUR_PER_DAY", 24);
|
|
1296
|
+
_defineProperty(DateTimeUtil, "SECOND_PER_DAY", _DateTimeUtil.SECOND_PER_HOUR * _DateTimeUtil.HOUR_PER_DAY);
|
|
1297
|
+
_defineProperty(DateTimeUtil, "DAY_PER_WEEK", 7);
|
|
1298
|
+
_defineProperty(DateTimeUtil, "DAY_PER_MONTH", 30);
|
|
1299
|
+
_defineProperty(DateTimeUtil, "DAY_PER_YEAR", 365);
|
|
1300
|
+
_defineProperty(DateTimeUtil, "MONTH_PER_YEAR", 12);
|
|
1301
|
+
_defineProperty(DateTimeUtil, "WEEK_PER_YEAR", 52);
|
|
1302
|
+
_defineProperty(DateTimeUtil, "WEEK_PER_MONTH", 4);
|
|
1303
|
+
_defineProperty(DateTimeUtil, "FORMAT", {
|
|
1304
|
+
ISO_DATE: "yyyy-MM-dd",
|
|
1305
|
+
ISO_TIME: "HH:mm:ss",
|
|
1306
|
+
ISO_DATE_TIME: "yyyy-MM-dd HH:mm:ss",
|
|
1307
|
+
ISO_DATE_TIME_MS: "yyyy-MM-dd HH:mm:ss.SSS",
|
|
1308
|
+
ISO_DATETIME_TZ: "yyyy-MM-dd'T'HH:mm:ssXXX",
|
|
1309
|
+
ISO_DATETIME_TZ_MS: "yyyy-MM-dd'T'HH:mm:ss.SSSXXX",
|
|
1310
|
+
US_DATE: "MM/dd/yyyy",
|
|
1311
|
+
US_DATE_TIME: "MM/dd/yyyy HH:mm:ss",
|
|
1312
|
+
US_DATE_SHORT_YEAR: "MM/dd/yy",
|
|
1313
|
+
EU_DATE: "dd/MM/yyyy",
|
|
1314
|
+
EU_DATE_TIME: "dd/MM/yyyy HH:mm:ss",
|
|
1315
|
+
CN_DATE: "yyyy年MM月dd日",
|
|
1316
|
+
CN_DATE_TIME: "yyyy年MM月dd日 HH时mm分ss秒",
|
|
1317
|
+
CN_DATE_WEEKDAY: "yyyy年MM月dd日 EEE",
|
|
1318
|
+
CN_WEEKDAY_FULL: "EEEE",
|
|
1319
|
+
SHORT_DATE: "yy-MM-dd",
|
|
1320
|
+
SHORT_DATE_SLASH: "yy/MM/dd",
|
|
1321
|
+
MONTH_DAY: "MM-dd",
|
|
1322
|
+
MONTH_DAY_CN: "MM月dd日",
|
|
1323
|
+
DATE_WITH_WEEKDAY_SHORT: "yyyy-MM-dd (EEE)",
|
|
1324
|
+
DATE_WITH_WEEKDAY_FULL: "yyyy-MM-dd (EEEE)",
|
|
1325
|
+
TIME_24: "HH:mm:ss",
|
|
1326
|
+
TIME_24_NO_SEC: "HH:mm",
|
|
1327
|
+
TIME_12: "hh:mm:ss a",
|
|
1328
|
+
TIME_12_NO_SEC: "hh:mm a",
|
|
1329
|
+
TIMESTAMP: "yyyyMMddHHmmss",
|
|
1330
|
+
TIMESTAMP_MS: "yyyyMMddHHmmssSSS",
|
|
1331
|
+
RFC2822: "EEE, dd MMM yyyy HH:mm:ss xxx",
|
|
1332
|
+
READABLE_DATE: "MMM dd, yyyy",
|
|
1333
|
+
READABLE_DATE_TIME: "MMM dd, yyyy HH:mm",
|
|
1334
|
+
COMPACT_DATETIME: "yyyyMMdd_HHmmss"
|
|
1335
|
+
});
|
|
1336
|
+
/**
|
|
1337
|
+
* 环境检查工具类
|
|
1338
|
+
*/
|
|
1339
|
+
var EnvUtil = class {
|
|
1340
|
+
static isBrowser() {
|
|
1341
|
+
return this._isBrowser;
|
|
1342
|
+
}
|
|
1343
|
+
static isWebWorker() {
|
|
1344
|
+
return this._isWebWorker;
|
|
1345
|
+
}
|
|
1346
|
+
static isReactNative() {
|
|
1347
|
+
return this._isReactNative;
|
|
1348
|
+
}
|
|
1349
|
+
/**
|
|
1350
|
+
* 检查是否在 iframe 环境中
|
|
1351
|
+
*/
|
|
1352
|
+
static isIframe() {
|
|
1353
|
+
if (typeof window === "undefined") return false;
|
|
1354
|
+
try {
|
|
1355
|
+
return window.top !== window.self;
|
|
1356
|
+
} catch (error) {
|
|
1357
|
+
if (error.name === "SecurityError") return true;
|
|
1358
|
+
return false;
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
/**
|
|
1362
|
+
* 检测当前设备是否为桌面设备
|
|
1363
|
+
*
|
|
1364
|
+
* @param minWidth - 桌面设备最小宽度(默认 1200px)
|
|
1365
|
+
* @param minScreenSize - 桌面设备最小屏幕尺寸(默认 10英寸)
|
|
1366
|
+
* @param dpi - 标准 DPI 基准(默认 160)
|
|
1367
|
+
* @returns 是否为桌面设备
|
|
1368
|
+
* @example
|
|
1369
|
+
* ```ts
|
|
1370
|
+
* // 假设 window.innerWidth = 1920
|
|
1371
|
+
* EnvUtil.isDesktop(); // true
|
|
1372
|
+
*
|
|
1373
|
+
* // 自定义阈值
|
|
1374
|
+
* EnvUtil.isDesktop(1440, 13); // 更严格的桌面检测
|
|
1375
|
+
* ```
|
|
1376
|
+
*/
|
|
1377
|
+
static isDesktop(minWidth = 1200, minScreenSize = 10, dpi = 160) {
|
|
1378
|
+
if (typeof window === "undefined" || !TypeUtil.isPositiveInteger(minWidth) || !TypeUtil.isPositiveInteger(minScreenSize)) return false;
|
|
1379
|
+
if (window.innerWidth < minWidth) return false;
|
|
1380
|
+
try {
|
|
1381
|
+
const widthPx = window.screen.width;
|
|
1382
|
+
const heightPx = window.screen.height;
|
|
1383
|
+
const DPI = dpi * (window.devicePixelRatio || 1);
|
|
1384
|
+
const widthInch = widthPx / DPI;
|
|
1385
|
+
const heightInch = heightPx / DPI;
|
|
1386
|
+
return Math.sqrt(widthInch ** 2 + heightInch ** 2) >= minScreenSize;
|
|
1387
|
+
} catch {
|
|
1388
|
+
return true;
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
/**
|
|
1392
|
+
* 检测当前设备是否为 Windows 桌面设备
|
|
1393
|
+
*
|
|
1394
|
+
* @param minWidth - 桌面设备最小宽度(默认 1200px)
|
|
1395
|
+
* @param minScreenSize - 桌面设备最小屏幕尺寸(默认 10英寸)
|
|
1396
|
+
* @param dpi - 标准 DPI 基准(默认 160)
|
|
1397
|
+
* @returns 是否为 Windows 桌面设备
|
|
1398
|
+
* @example
|
|
1399
|
+
* ```ts
|
|
1400
|
+
* // UA contains Windows
|
|
1401
|
+
* EnvUtil.isWindowsDesktop(); // true
|
|
1402
|
+
* ```
|
|
1403
|
+
*/
|
|
1404
|
+
static isWindowsDesktop(minWidth = 1200, minScreenSize = 10, dpi = 160) {
|
|
1405
|
+
if (typeof navigator === "undefined" || !navigator.userAgent) return false;
|
|
1406
|
+
return /Windows/i.test(navigator.userAgent) && this.isDesktop(minWidth, minScreenSize, dpi);
|
|
1407
|
+
}
|
|
1408
|
+
/**
|
|
1409
|
+
* 检测当前设备是否为 macOS 桌面设备
|
|
1410
|
+
*
|
|
1411
|
+
* @param minWidth - 桌面设备最小宽度(默认 1200px)
|
|
1412
|
+
* @param minScreenSize - 桌面设备最小屏幕尺寸(默认 10英寸)
|
|
1413
|
+
* @param dpi - 标准 DPI 基准(默认 160)
|
|
1414
|
+
* @returns 是否为 macOS 桌面设备
|
|
1415
|
+
* @example
|
|
1416
|
+
* ```ts
|
|
1417
|
+
* // UA contains Macintosh
|
|
1418
|
+
* EnvUtil.isMacOSDesktop(); // true
|
|
1419
|
+
* ```
|
|
1420
|
+
*/
|
|
1421
|
+
static isMacOSDesktop(minWidth = 1200, minScreenSize = 10, dpi = 160) {
|
|
1422
|
+
if (typeof navigator === "undefined" || !navigator.userAgent) return false;
|
|
1423
|
+
return /Macintosh/i.test(navigator.userAgent) && this.isDesktop(minWidth, minScreenSize, dpi);
|
|
1424
|
+
}
|
|
1425
|
+
/**
|
|
1426
|
+
* 检测当前设备是否为移动设备
|
|
1427
|
+
*
|
|
1428
|
+
* @param maxWidth - 移动设备最大宽度(默认 768px)
|
|
1429
|
+
* @param dpi - 标准 DPI 基准(默认 160)
|
|
1430
|
+
* @returns 是否为移动设备
|
|
1431
|
+
* @example
|
|
1432
|
+
* ```ts
|
|
1433
|
+
* // 假设 window.innerWidth = 500
|
|
1434
|
+
* EnvUtil.isMobile(); // true
|
|
1435
|
+
* ```
|
|
1436
|
+
*/
|
|
1437
|
+
static isMobile(maxWidth = 768, dpi = 160) {
|
|
1438
|
+
if (typeof window === "undefined" || !TypeUtil.isPositiveInteger(maxWidth)) return false;
|
|
1439
|
+
if (window.innerWidth >= maxWidth) return false;
|
|
1440
|
+
try {
|
|
1441
|
+
const widthPx = window.screen.width;
|
|
1442
|
+
const heightPx = window.screen.height;
|
|
1443
|
+
const DPI = dpi * (window.devicePixelRatio || 1);
|
|
1444
|
+
const widthInch = widthPx / DPI;
|
|
1445
|
+
const heightInch = heightPx / DPI;
|
|
1446
|
+
return Math.sqrt(widthInch ** 2 + heightInch ** 2) < 7;
|
|
1447
|
+
} catch {
|
|
1448
|
+
return true;
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1451
|
+
/**
|
|
1452
|
+
* 检测当前设备是否为IOS移动设备
|
|
1453
|
+
*
|
|
1454
|
+
* @param maxWidth - 移动设备最大宽度(默认 768px)
|
|
1455
|
+
* @param dpi - 标准 DPI 基准(默认 160)
|
|
1456
|
+
* @returns 是否为 iOS 移动设备 (iPhone/iPod)
|
|
1457
|
+
* @example
|
|
1458
|
+
* ```ts
|
|
1459
|
+
* // UA contains iPhone
|
|
1460
|
+
* EnvUtil.isIOSMobile(); // true
|
|
1461
|
+
* ```
|
|
1462
|
+
*/
|
|
1463
|
+
static isIOSMobile(maxWidth = 768, dpi = 160) {
|
|
1464
|
+
if (typeof navigator === "undefined" || !navigator.userAgent) return false;
|
|
1465
|
+
return /iPhone|iPad|iPod/i.test(navigator.userAgent) && this.isMobile(maxWidth, dpi);
|
|
1466
|
+
}
|
|
1467
|
+
/**
|
|
1468
|
+
* 检测当前设备是否为平板
|
|
1469
|
+
*
|
|
1470
|
+
* @param minWidth - 平板最小宽度(默认 768px)
|
|
1471
|
+
* @param maxWidth - 平板最大宽度(默认 1200px)
|
|
1472
|
+
* @param dpi - 标准 DPI 基准(默认 160)
|
|
1473
|
+
* @returns 是否为平板设备
|
|
1474
|
+
* @example
|
|
1475
|
+
* ```ts
|
|
1476
|
+
* // 假设 window.innerWidth = 1000
|
|
1477
|
+
* EnvUtil.isTablet(); // true
|
|
1478
|
+
* ```
|
|
1479
|
+
*/
|
|
1480
|
+
static isTablet(minWidth = 768, maxWidth = 1200, dpi = 160) {
|
|
1481
|
+
if (typeof window === "undefined" || !TypeUtil.isPositiveInteger(minWidth) || !TypeUtil.isPositiveInteger(maxWidth)) return false;
|
|
1482
|
+
const width = window.innerWidth;
|
|
1483
|
+
const isWithinWidthRange = width >= minWidth && width <= maxWidth;
|
|
1484
|
+
try {
|
|
1485
|
+
const widthPx = window.screen.width;
|
|
1486
|
+
const heightPx = window.screen.height;
|
|
1487
|
+
const DPI = dpi * (window.devicePixelRatio || 1);
|
|
1488
|
+
const widthInch = widthPx / DPI;
|
|
1489
|
+
const heightInch = heightPx / DPI;
|
|
1490
|
+
const screenInches = Math.sqrt(widthInch ** 2 + heightInch ** 2);
|
|
1491
|
+
return isWithinWidthRange || screenInches >= 7;
|
|
1492
|
+
} catch {
|
|
1493
|
+
return isWithinWidthRange;
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
};
|
|
1497
|
+
_defineProperty(EnvUtil, "_isBrowser", typeof window !== "undefined" && TypeUtil.isFunction(window?.document?.createElement));
|
|
1498
|
+
_defineProperty(EnvUtil, "_isWebWorker", typeof window === "undefined" && typeof self !== "undefined" && "importScripts" in self);
|
|
1499
|
+
_defineProperty(EnvUtil, "_isReactNative", typeof navigator !== "undefined" && navigator.product === "ReactNative");
|
|
1500
|
+
/**
|
|
1501
|
+
* MIME 工具类
|
|
1502
|
+
*/
|
|
1503
|
+
var MimeUtil = class {};
|
|
1504
|
+
_defineProperty(MimeUtil, "MIME", {
|
|
1505
|
+
/** 普通文本文件 */
|
|
1506
|
+
TEXT: "text/plain",
|
|
1507
|
+
/** 超文本标记语言文档 */
|
|
1508
|
+
HTML: "text/html",
|
|
1509
|
+
/** 层叠样式表文件 */
|
|
1510
|
+
CSS: "text/css",
|
|
1511
|
+
/** 逗号分隔值文件(表格数据) */
|
|
1512
|
+
CSV: "text/csv",
|
|
1513
|
+
/** 制表符分隔值文件 */
|
|
1514
|
+
TSV: "text/tab-separated-values",
|
|
1515
|
+
/** XML 文档 */
|
|
1516
|
+
XML: "text/xml",
|
|
1517
|
+
/** XHTML 文档(XML 严格格式的 HTML) */
|
|
1518
|
+
XHTML: "application/xhtml+xml",
|
|
1519
|
+
/** JavaScript 脚本文件(标准推荐) */
|
|
1520
|
+
JS: "text/javascript",
|
|
1521
|
+
/** Markdown 格式文档 */
|
|
1522
|
+
MARKDOWN: "text/markdown",
|
|
1523
|
+
/** 富文本格式文档(.rtf) */
|
|
1524
|
+
RTF: "application/rtf",
|
|
1525
|
+
/** iCalendar 日历格式(.ics) */
|
|
1526
|
+
CALENDAR: "text/calendar",
|
|
1527
|
+
/** JPEG 图像(.jpg/.jpeg) */
|
|
1528
|
+
JPEG: "image/jpeg",
|
|
1529
|
+
/** PNG 图像(无损压缩,支持透明) */
|
|
1530
|
+
PNG: "image/png",
|
|
1531
|
+
/** GIF 图像(支持动画) */
|
|
1532
|
+
GIF: "image/gif",
|
|
1533
|
+
/** Windows 位图(.bmp) */
|
|
1534
|
+
BMP: "image/bmp",
|
|
1535
|
+
/** SVG 向量图形(.svg) */
|
|
1536
|
+
SVG: "image/svg+xml",
|
|
1537
|
+
/** APNG 动态图像(.apng) */
|
|
1538
|
+
APNG: "image/apng",
|
|
1539
|
+
/** AVIF 图像(高效压缩) */
|
|
1540
|
+
AVIF: "image/avif",
|
|
1541
|
+
/** 图标文件格式(.ico) */
|
|
1542
|
+
ICO: "image/vnd.microsoft.icon",
|
|
1543
|
+
/** WebP 图像(高效压缩) */
|
|
1544
|
+
WEBP: "image/webp",
|
|
1545
|
+
/** MP3 音频(.mp3) */
|
|
1546
|
+
MP3: "audio/mpeg",
|
|
1547
|
+
/** AAC 音频(.aac) */
|
|
1548
|
+
AAC: "audio/aac",
|
|
1549
|
+
/** MIDI 音乐文件(.mid/.midi) */
|
|
1550
|
+
MIDI: "audio/midi",
|
|
1551
|
+
/** OGG 音频(.oga) */
|
|
1552
|
+
OGG_AUDIO: "audio/ogg",
|
|
1553
|
+
/** Opus 音频(.opus) */
|
|
1554
|
+
OPUS: "audio/opus",
|
|
1555
|
+
/** WAV 音频(.wav) */
|
|
1556
|
+
WAV: "audio/wav",
|
|
1557
|
+
/** RealAudio 音频(.ra/.ram) */
|
|
1558
|
+
REAL_AUDIO: "audio/x-pn-realaudio",
|
|
1559
|
+
/** MP4 视频(.mp4) */
|
|
1560
|
+
MP4: "video/mp4",
|
|
1561
|
+
/** MPEG 视频(.mpeg/.mpg) */
|
|
1562
|
+
MPEG: "video/mpeg",
|
|
1563
|
+
/** OGG 视频(.ogv) */
|
|
1564
|
+
OGG_VIDEO: "video/ogg",
|
|
1565
|
+
/** AVI 视频(.avi) */
|
|
1566
|
+
AVI: "video/x-msvideo",
|
|
1567
|
+
/** 3GPP 视频(.3gp) */
|
|
1568
|
+
THREE_GPP: "video/3gpp",
|
|
1569
|
+
/** 3GPP2 视频(.3g2) */
|
|
1570
|
+
THREE_GPP2: "video/3gpp2",
|
|
1571
|
+
/** WebM 视频(.webm) */
|
|
1572
|
+
WEBM: "video/webm",
|
|
1573
|
+
/** PDF 文档 */
|
|
1574
|
+
PDF: "application/pdf",
|
|
1575
|
+
/** Word 97-2003 文档(.doc) */
|
|
1576
|
+
DOC: "application/msword",
|
|
1577
|
+
/** Word 2007+ 文档(.docx) */
|
|
1578
|
+
DOCX: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
1579
|
+
/** Excel 2007+ 工作簿(.xlsx) */
|
|
1580
|
+
XLSX: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
1581
|
+
/** 启用宏的Excel工作簿(.xlsm) */
|
|
1582
|
+
XLSM: "application/vnd.ms-excel.sheet.macroEnabled.12",
|
|
1583
|
+
/** Excel模板文件(.xltx) */
|
|
1584
|
+
XLTX: "application/vnd.openxmlformats-officedocument.spreadsheetml.template",
|
|
1585
|
+
/** PowerPoint 2007+ 演示文稿(.pptx) */
|
|
1586
|
+
PPTX: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
1587
|
+
/** PowerPoint 97-2003 演示文稿(.ppt) */
|
|
1588
|
+
PPT: "application/vnd.ms-powerpoint",
|
|
1589
|
+
/** OpenDocument 文本文档(.odt) */
|
|
1590
|
+
ODT: "application/vnd.oasis.opendocument.text",
|
|
1591
|
+
/** OpenDocument 表格文档(.ods) */
|
|
1592
|
+
ODS: "application/vnd.oasis.opendocument.spreadsheet",
|
|
1593
|
+
/** OpenDocument 演示文稿(.odp) */
|
|
1594
|
+
ODP: "application/vnd.oasis.opendocument.presentation",
|
|
1595
|
+
/** EPUB 电子书(.epub) */
|
|
1596
|
+
EPUB: "application/epub+zip",
|
|
1597
|
+
/** Kindle 电子书(.azw) */
|
|
1598
|
+
AZW: "application/vnd.amazon.ebook",
|
|
1599
|
+
/** ZIP 压缩文件(.zip) */
|
|
1600
|
+
ZIP: "application/zip",
|
|
1601
|
+
/** GZIP 压缩文件(.gz) */
|
|
1602
|
+
GZIP: "application/gzip",
|
|
1603
|
+
/** GZIP 压缩文件(旧格式) */
|
|
1604
|
+
X_GZIP: "application/x-gzip",
|
|
1605
|
+
/** TAR 归档文件(.tar) */
|
|
1606
|
+
TAR: "application/x-tar",
|
|
1607
|
+
/** BZip 归档(.bz) */
|
|
1608
|
+
BZIP: "application/x-bzip",
|
|
1609
|
+
/** BZip2 归档(.bz2) */
|
|
1610
|
+
BZIP2: "application/x-bzip2",
|
|
1611
|
+
/** 7-Zip 压缩文件(.7z) */
|
|
1612
|
+
SEVEN_Z: "application/x-7z-compressed",
|
|
1613
|
+
/** 通用二进制数据(默认类型) */
|
|
1614
|
+
OCTET_STREAM: "application/octet-stream",
|
|
1615
|
+
/** JSON 数据格式(.json) */
|
|
1616
|
+
JSON: "application/json",
|
|
1617
|
+
/** JSON-LD 格式(.jsonld) */
|
|
1618
|
+
LD_JSON: "application/ld+json",
|
|
1619
|
+
/** Java 归档文件(.jar) */
|
|
1620
|
+
JAR: "application/java-archive",
|
|
1621
|
+
/** MS 嵌入式 OpenType 字体(.eot) */
|
|
1622
|
+
EOT: "application/vnd.ms-fontobject",
|
|
1623
|
+
/** OpenType 字体(.otf) */
|
|
1624
|
+
OTF: "font/otf",
|
|
1625
|
+
/** Excel 97-2003 工作簿(.xls) */
|
|
1626
|
+
XLS: "application/vnd.ms-excel",
|
|
1627
|
+
/** Microsoft XPS 文档(.xps) */
|
|
1628
|
+
XPS: "application/vnd.ms-xpsdocument",
|
|
1629
|
+
/** Word 启用宏文档(.docm) */
|
|
1630
|
+
DOCM: "application/vnd.ms-word.document.macroEnabled.12"
|
|
1631
|
+
});
|
|
1632
|
+
/**
|
|
1633
|
+
* 对象工具类
|
|
1634
|
+
*/
|
|
1635
|
+
var ObjectUtil = class {
|
|
1636
|
+
static keys(value) {
|
|
1637
|
+
return Object.keys(value);
|
|
1638
|
+
}
|
|
1639
|
+
static values(value) {
|
|
1640
|
+
return Object.values(value);
|
|
1641
|
+
}
|
|
1642
|
+
static entries(value) {
|
|
1643
|
+
return Object.entries(value);
|
|
1644
|
+
}
|
|
1645
|
+
/**
|
|
1646
|
+
* 映射对象条目
|
|
1647
|
+
* - 将对象的键值对映射为新的键值对
|
|
1648
|
+
*
|
|
1649
|
+
* @param plainObject 对象
|
|
1650
|
+
* @param toEntry 映射函数
|
|
1651
|
+
* @returns 映射后的新对象
|
|
1652
|
+
* @example
|
|
1653
|
+
* ```ts
|
|
1654
|
+
* const obj = { a: 1, b: 2 };
|
|
1655
|
+
* ObjectUtil.entriesMap(obj, (k, v) => [k, v * 2]); // { a: 2, b: 4 }
|
|
1656
|
+
* ```
|
|
1657
|
+
*/
|
|
1658
|
+
static entriesMap(plainObject, toEntry) {
|
|
1659
|
+
const defaultResult = {};
|
|
1660
|
+
if (!TypeUtil.isObject(plainObject)) return defaultResult;
|
|
1661
|
+
return this.entries(plainObject).reduce((acc, [key, value]) => {
|
|
1662
|
+
const [newKey, newValue] = toEntry(key, value);
|
|
1663
|
+
acc[newKey] = newValue;
|
|
1664
|
+
return acc;
|
|
1665
|
+
}, defaultResult);
|
|
1666
|
+
}
|
|
1667
|
+
static pick(obj, keys) {
|
|
1668
|
+
const result = {};
|
|
1669
|
+
if (!TypeUtil.isObject(obj)) return result;
|
|
1670
|
+
if (!TypeUtil.isArray(keys)) return obj;
|
|
1671
|
+
return keys.reduce((acc, key) => {
|
|
1672
|
+
if (key in obj) acc[key] = obj[key];
|
|
1673
|
+
return acc;
|
|
1674
|
+
}, result);
|
|
1675
|
+
}
|
|
1676
|
+
static omit(obj, keys) {
|
|
1677
|
+
const result = {};
|
|
1678
|
+
if (!TypeUtil.isObject(obj)) return result;
|
|
1679
|
+
if (!TypeUtil.isArray(keys)) return obj;
|
|
1680
|
+
const keysToOmit = new Set(keys);
|
|
1681
|
+
return Object.keys(obj).reduce((acc, key) => {
|
|
1682
|
+
if (!keysToOmit.has(key)) acc[key] = obj[key];
|
|
1683
|
+
return acc;
|
|
1684
|
+
}, result);
|
|
1685
|
+
}
|
|
1686
|
+
static invert(obj) {
|
|
1687
|
+
const result = {};
|
|
1688
|
+
if (!TypeUtil.isObject(obj)) return result;
|
|
1689
|
+
for (const [k, v] of this.entries(obj)) if (TypeUtil.isString(v) || TypeUtil.isNumber(v) || TypeUtil.isSymbol(v)) result[v] = k;
|
|
1690
|
+
return result;
|
|
1691
|
+
}
|
|
1692
|
+
static crush(obj) {
|
|
1693
|
+
if (!obj) return {};
|
|
1694
|
+
function crushReducer(crushed, value, path) {
|
|
1695
|
+
if (TypeUtil.isObject(value) || TypeUtil.isArray(value)) for (const [prop, propValue] of Object.entries(value)) crushReducer(crushed, propValue, path ? `${path}.${prop}` : prop);
|
|
1696
|
+
else crushed[path] = value;
|
|
1697
|
+
return crushed;
|
|
1698
|
+
}
|
|
1699
|
+
return crushReducer({}, obj, "");
|
|
1700
|
+
}
|
|
1701
|
+
static enumKeys(enumeration) {
|
|
1702
|
+
const [isEnum, isBidirectionalEnum] = TypeUtil.isEnumeration(enumeration);
|
|
1703
|
+
if (!isEnum) throw Error("function [enumKeys] expected parameter to be a enum, and requires at least one member");
|
|
1704
|
+
const keys = this.keys(enumeration);
|
|
1705
|
+
if (isBidirectionalEnum) return keys.splice(keys.length / 2, keys.length / 2);
|
|
1706
|
+
return keys;
|
|
1707
|
+
}
|
|
1708
|
+
static enumValues(enumeration) {
|
|
1709
|
+
const [isEnum, isBidirectionalEnum] = TypeUtil.isEnumeration(enumeration);
|
|
1710
|
+
if (!isEnum) throw Error("function [enumValues] expected parameter to be a enum, and requires at least one member");
|
|
1711
|
+
const values = this.values(enumeration);
|
|
1712
|
+
if (isBidirectionalEnum) return values.splice(values.length / 2, values.length / 2);
|
|
1713
|
+
return values;
|
|
1714
|
+
}
|
|
1715
|
+
static enumEntries(enumeration) {
|
|
1716
|
+
const [isEnum, isBidirectionalEnum] = TypeUtil.isEnumeration(enumeration);
|
|
1717
|
+
if (!isEnum) throw Error("function [enumEntries] expected parameter to be a enum, and requires at least one member");
|
|
1718
|
+
const entries = this.entries(enumeration);
|
|
1719
|
+
if (isBidirectionalEnum) return entries.splice(entries.length / 2, entries.length / 2);
|
|
1720
|
+
return entries;
|
|
1721
|
+
}
|
|
1722
|
+
};
|
|
1723
|
+
/**
|
|
1724
|
+
* 主题工具类
|
|
1725
|
+
*/
|
|
1726
|
+
var ThemeUtil = class {};
|
|
1727
|
+
_defineProperty(ThemeUtil, "THEME", {
|
|
1728
|
+
LIGHT: "light",
|
|
1729
|
+
DARK: "dark"
|
|
1730
|
+
});
|
|
1731
|
+
_defineProperty(ThemeUtil, "THEME_MODE", {
|
|
1732
|
+
LIGHT: "light",
|
|
1733
|
+
DARK: "dark",
|
|
1734
|
+
SYSTEM: "system"
|
|
1735
|
+
});
|
|
1736
|
+
/**
|
|
1737
|
+
* 验证工具类
|
|
1738
|
+
*/
|
|
1739
|
+
var ValidateUtil = class {
|
|
1740
|
+
/**
|
|
1741
|
+
* 验证是否为手机号码
|
|
1742
|
+
*/
|
|
1743
|
+
static isPhone(input) {
|
|
1744
|
+
return this._phone.test(input.toString());
|
|
1745
|
+
}
|
|
1746
|
+
/**
|
|
1747
|
+
* 验证是否为固定电话
|
|
1748
|
+
*/
|
|
1749
|
+
static isTelephone(input) {
|
|
1750
|
+
return this._telephone.test(input.toString());
|
|
1751
|
+
}
|
|
1752
|
+
/**
|
|
1753
|
+
* 验证是否为移动设备识别码
|
|
1754
|
+
*/
|
|
1755
|
+
static isIMEI(input) {
|
|
1756
|
+
return this._IMEI.test(input.toString());
|
|
1757
|
+
}
|
|
1758
|
+
/**
|
|
1759
|
+
* 验证是否为电子邮箱
|
|
1760
|
+
*/
|
|
1761
|
+
static isEmail(input) {
|
|
1762
|
+
return this._email.test(input.toString());
|
|
1763
|
+
}
|
|
1764
|
+
/**
|
|
1765
|
+
* 验证是否为 http(s) 链接
|
|
1766
|
+
*/
|
|
1767
|
+
static isHttpLink(input) {
|
|
1768
|
+
return this._link.test(input.toString());
|
|
1769
|
+
}
|
|
1770
|
+
/**
|
|
1771
|
+
* 验证是否为端口号链接
|
|
1772
|
+
*/
|
|
1773
|
+
static isPortLink(input) {
|
|
1774
|
+
return this._portLink.test(input.toString());
|
|
1775
|
+
}
|
|
1776
|
+
/**
|
|
1777
|
+
* 验证是否为迅雷链接
|
|
1778
|
+
*/
|
|
1779
|
+
static isThunderLink(input) {
|
|
1780
|
+
return this._thunderLink.test(input.toString());
|
|
1781
|
+
}
|
|
1782
|
+
/**
|
|
1783
|
+
* 验证是否为统一社会信用代码
|
|
1784
|
+
*/
|
|
1785
|
+
static isUSCC(input) {
|
|
1786
|
+
return this._uscc.test(input.toString());
|
|
1787
|
+
}
|
|
1788
|
+
/**
|
|
1789
|
+
* 验证是否为统一社会信用代码 - 15位/18位/20位数字/字母
|
|
1790
|
+
*/
|
|
1791
|
+
static isUSCCS(input) {
|
|
1792
|
+
return this._usccs.test(input.toString());
|
|
1793
|
+
}
|
|
1794
|
+
/**
|
|
1795
|
+
* 验证是否为 Windows 系统文件夹路径
|
|
1796
|
+
*/
|
|
1797
|
+
static isDirPathWindows(input) {
|
|
1798
|
+
return this._dirPathWindows.test(input.toString());
|
|
1799
|
+
}
|
|
1800
|
+
/**
|
|
1801
|
+
* 验证是否为 Windows 系统文件路径
|
|
1802
|
+
*/
|
|
1803
|
+
static isFilePathWindows(input) {
|
|
1804
|
+
return this._filePathWindows.test(input.toString());
|
|
1805
|
+
}
|
|
1806
|
+
/**
|
|
1807
|
+
* 验证是否为 Linux 系统文件夹路径
|
|
1808
|
+
*/
|
|
1809
|
+
static isDirPathLinux(input) {
|
|
1810
|
+
return this._dirPathLinux.test(input.toString());
|
|
1811
|
+
}
|
|
1812
|
+
/**
|
|
1813
|
+
* 验证是否为 Linux 系统文件路径
|
|
1814
|
+
*/
|
|
1815
|
+
static isFilePathLinux(input) {
|
|
1816
|
+
return this._filePathLinux.test(input.toString());
|
|
1817
|
+
}
|
|
1818
|
+
/**
|
|
1819
|
+
* 验证是否为新能源车牌号
|
|
1820
|
+
*/
|
|
1821
|
+
static isEVCarNumber(input) {
|
|
1822
|
+
return this._EVCarNumber.test(input.toString());
|
|
1823
|
+
}
|
|
1824
|
+
/**
|
|
1825
|
+
* 验证是否为燃油车车牌号
|
|
1826
|
+
*/
|
|
1827
|
+
static isGVCarNumber(input) {
|
|
1828
|
+
return this._GVCarNumber.test(input.toString());
|
|
1829
|
+
}
|
|
1830
|
+
/**
|
|
1831
|
+
* 验证是否为中文姓名
|
|
1832
|
+
*/
|
|
1833
|
+
static isChineseName(input) {
|
|
1834
|
+
return this._chineseName.test(input.toString());
|
|
1835
|
+
}
|
|
1836
|
+
/**
|
|
1837
|
+
* 验证是否为中国身份证号
|
|
1838
|
+
*/
|
|
1839
|
+
static isChineseID(input) {
|
|
1840
|
+
return this._chineseId.test(input.toString());
|
|
1841
|
+
}
|
|
1842
|
+
/**
|
|
1843
|
+
* 验证是否为中国省份
|
|
1844
|
+
*/
|
|
1845
|
+
static isChineseProvince(input) {
|
|
1846
|
+
return this._chineseProvince.test(input.toString());
|
|
1847
|
+
}
|
|
1848
|
+
/**
|
|
1849
|
+
* 验证是否为中华民族
|
|
1850
|
+
*/
|
|
1851
|
+
static isChineseNation(input) {
|
|
1852
|
+
return this._chineseNation.test(input.toString());
|
|
1853
|
+
}
|
|
1854
|
+
/**
|
|
1855
|
+
* 验证是否只包含字母
|
|
1856
|
+
*/
|
|
1857
|
+
static isLetter(input) {
|
|
1858
|
+
return this._letter.test(input.toString());
|
|
1859
|
+
}
|
|
1860
|
+
/**
|
|
1861
|
+
* 验证是否只包含小写字母
|
|
1862
|
+
*/
|
|
1863
|
+
static isLetterLowercase(input) {
|
|
1864
|
+
return this._letterLowercase.test(input.toString());
|
|
1865
|
+
}
|
|
1866
|
+
/**
|
|
1867
|
+
* 验证是否只包含大写字母
|
|
1868
|
+
*/
|
|
1869
|
+
static isLetterUppercase(input) {
|
|
1870
|
+
return this._letterUppercase.test(input.toString());
|
|
1871
|
+
}
|
|
1872
|
+
/**
|
|
1873
|
+
* 验证是否不包含字母
|
|
1874
|
+
*/
|
|
1875
|
+
static isLetterOmit(input) {
|
|
1876
|
+
return this._letterOmit.test(input.toString());
|
|
1877
|
+
}
|
|
1878
|
+
/**
|
|
1879
|
+
* 验证是否为数字和字母组合
|
|
1880
|
+
*/
|
|
1881
|
+
static isLetterAndNumber(input) {
|
|
1882
|
+
return this._LetterAndNumber.test(input.toString());
|
|
1883
|
+
}
|
|
1884
|
+
/**
|
|
1885
|
+
* 验证是否为有符号浮点数
|
|
1886
|
+
*/
|
|
1887
|
+
static isSignedFloat(input) {
|
|
1888
|
+
return this._signedFloat.test(input.toString());
|
|
1889
|
+
}
|
|
1890
|
+
/**
|
|
1891
|
+
* 验证是否为无符号浮点数
|
|
1892
|
+
*/
|
|
1893
|
+
static isUnsignedFloat(input) {
|
|
1894
|
+
return this._unsignedFloat.test(input.toString());
|
|
1895
|
+
}
|
|
1896
|
+
/**
|
|
1897
|
+
* 验证是否为有符号整数
|
|
1898
|
+
*/
|
|
1899
|
+
static isSignedInteger(input) {
|
|
1900
|
+
return this._signedInteger.test(input.toString());
|
|
1901
|
+
}
|
|
1902
|
+
/**
|
|
1903
|
+
* 验证是否为无符号整数
|
|
1904
|
+
*/
|
|
1905
|
+
static isUnsignedInteger(input) {
|
|
1906
|
+
return this._unsignedInteger.test(input.toString());
|
|
1907
|
+
}
|
|
1908
|
+
/**
|
|
1909
|
+
* 验证是否包含空格
|
|
1910
|
+
*/
|
|
1911
|
+
static isSpaceInclude(input) {
|
|
1912
|
+
return this._spaceInclude.test(input.toString());
|
|
1913
|
+
}
|
|
1914
|
+
/**
|
|
1915
|
+
* 验证是否以空格开头
|
|
1916
|
+
*/
|
|
1917
|
+
static isSpaceStart(input) {
|
|
1918
|
+
return this._spaceStart.test(input.toString());
|
|
1919
|
+
}
|
|
1920
|
+
/**
|
|
1921
|
+
* 验证是否以空格结尾
|
|
1922
|
+
*/
|
|
1923
|
+
static isSpaceEnd(input) {
|
|
1924
|
+
return this._spaceEnd.test(input.toString());
|
|
1925
|
+
}
|
|
1926
|
+
/**
|
|
1927
|
+
* 验证是否以空格开头或结尾
|
|
1928
|
+
*/
|
|
1929
|
+
static isSpaceStartOrEnd(input) {
|
|
1930
|
+
return this.isSpaceStart(input) || this.isSpaceEnd(input);
|
|
1931
|
+
}
|
|
1932
|
+
};
|
|
1933
|
+
_defineProperty(ValidateUtil, "_phone", /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/);
|
|
1934
|
+
_defineProperty(ValidateUtil, "_telephone", /^(((0\d{2,3})-)?((\d{7,8})|(400\d{7})|(800\d{7}))(-(\d{1,4}))?)$/);
|
|
1935
|
+
_defineProperty(ValidateUtil, "_IMEI", /^\d{15,17}$/);
|
|
1936
|
+
_defineProperty(ValidateUtil, "_email", /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\])|(([a-z\-0-9]+\.)+[a-z]{2,}))$/i);
|
|
1937
|
+
_defineProperty(ValidateUtil, "_link", /^(https?:\/\/)?(([\w-]+(\.[\w-]+)*\.[a-z]{2,6})|((\d{1,3}\.){3}\d{1,3}))(:\d+)?(\/\S*)?$/i);
|
|
1938
|
+
_defineProperty(ValidateUtil, "_portLink", /^(https?:\/\/)?[\w-]+(\.[\w-]+)+:\d{1,5}\/?$/i);
|
|
1939
|
+
_defineProperty(ValidateUtil, "_thunderLink", /^thunderx?:\/\/[a-zA-Z\d]+=$/i);
|
|
1940
|
+
_defineProperty(ValidateUtil, "_uscc", /^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/);
|
|
1941
|
+
_defineProperty(ValidateUtil, "_usccs", /^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/);
|
|
1942
|
+
_defineProperty(ValidateUtil, "_dirPathWindows", /^[a-z]:\\(?:\w+\\?)*$/i);
|
|
1943
|
+
_defineProperty(ValidateUtil, "_filePathWindows", /^[a-z]:\\(?:\w+\\)*\w+\.\w+$/i);
|
|
1944
|
+
_defineProperty(ValidateUtil, "_dirPathLinux", /^\/(?:[^\\/\s]+\/)*$/);
|
|
1945
|
+
_defineProperty(ValidateUtil, "_filePathLinux", /^(\/$|\/(?:[^\\/\s]+\/)*[^\\/\s]+$)/);
|
|
1946
|
+
_defineProperty(ValidateUtil, "_EVCarNumber", /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z](([DF]((?![IO])[a-zA-Z0-9](?![IO]))\d{4})|(\d{5}[DF]))$/);
|
|
1947
|
+
_defineProperty(ValidateUtil, "_GVCarNumber", /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$/);
|
|
1948
|
+
_defineProperty(ValidateUtil, "_chineseName", /^[一-龢][一·-龢]*$/);
|
|
1949
|
+
_defineProperty(ValidateUtil, "_chineseId", /^\d{6}((((((19|20)\d{2})(0[13-9]|1[012])(0[1-9]|[12]\d|30))|(((19|20)\d{2})(0[13578]|1[02])31)|((19|20)\d{2})02(0[1-9]|1\d|2[0-8])|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))0229))\d{3})|((((\d{2})(0[13-9]|1[012])(0[1-9]|[12]\d|30))|((\d{2})(0[13578]|1[02])31)|((\d{2})02(0[1-9]|1\d|2[0-8]))|(([13579][26]|[2468][048]|0[048])0229))\d{2}))([\dX])$/i);
|
|
1950
|
+
_defineProperty(ValidateUtil, "_chineseProvince", /^安徽|澳门|北京|重庆|福建|甘肃|广东|广西|贵州|海南|河北|河南|黑龙江|湖北|湖南|吉林|江苏|江西|辽宁|内蒙古|宁夏|青海|山东|山西|陕西|上海|四川|台湾|天津|西藏|香港|新疆|云南|浙江$/);
|
|
1951
|
+
_defineProperty(ValidateUtil, "_chineseNation", /^汉族|蒙古族|回族|藏族|维吾尔族|苗族|彝族|壮族|布依族|朝鲜族|满族|侗族|瑶族|白族|土家族|哈尼族|哈萨克族|傣族|黎族|傈僳族|佤族|畲族|高山族|拉祜族|水族|东乡族|纳西族|景颇族|柯尔克孜族|土族|达斡尔族|仫佬族|羌族|布朗族|撒拉族|毛南族|仡佬族|锡伯族|阿昌族|普米族|塔吉克族|怒族|乌孜别克族|俄罗斯族|鄂温克族|德昂族|保安族|裕固族|京族|塔塔尔族|独龙族|鄂伦春族|赫哲族|门巴族|珞巴族|基诺族|其它未识别民族|外国人入中国籍$/);
|
|
1952
|
+
_defineProperty(ValidateUtil, "_letter", /^[a-z]+$/i);
|
|
1953
|
+
_defineProperty(ValidateUtil, "_letterLowercase", /^[a-z]+$/);
|
|
1954
|
+
_defineProperty(ValidateUtil, "_letterUppercase", /^[A-Z]+$/);
|
|
1955
|
+
_defineProperty(ValidateUtil, "_letterOmit", /^[^A-Z]*$/i);
|
|
1956
|
+
_defineProperty(ValidateUtil, "_LetterAndNumber", /^[A-Z0-9]+$/i);
|
|
1957
|
+
_defineProperty(ValidateUtil, "_signedFloat", /^[+-]?(\d+(\.\d+)?|\.\d+)$/);
|
|
1958
|
+
_defineProperty(ValidateUtil, "_unsignedFloat", /^\+?(\d+(\.\d+)?|\.\d+)$/);
|
|
1959
|
+
_defineProperty(ValidateUtil, "_signedInteger", /^[+-]?\d+$/);
|
|
1960
|
+
_defineProperty(ValidateUtil, "_unsignedInteger", /^\+?\d+$/);
|
|
1961
|
+
_defineProperty(ValidateUtil, "_spaceInclude", /\s/);
|
|
1962
|
+
_defineProperty(ValidateUtil, "_spaceStart", /^\s/);
|
|
1963
|
+
_defineProperty(ValidateUtil, "_spaceEnd", /\s$/);
|
|
1964
|
+
//#endregion
|
|
1965
|
+
//#region src/react/useMount.ts
|
|
1966
|
+
/**
|
|
1967
|
+
* 在组件初始化时执行的 Hook
|
|
1968
|
+
* - 即使在严格模式(React StrictMode)也只执行一次
|
|
1969
|
+
* - 自动使用最新版 effect 函数
|
|
1970
|
+
*
|
|
1971
|
+
* @param effect 副作用函数(必须为同步函数;若为异步函数,清理逻辑需自行处理)
|
|
1972
|
+
* @example
|
|
1973
|
+
* useMount(() => {
|
|
1974
|
+
* console.log('组件挂载');
|
|
1975
|
+
* return () => console.log('组件卸载');
|
|
1976
|
+
* });
|
|
1977
|
+
*
|
|
1978
|
+
* useMount(async () => {
|
|
1979
|
+
* const data = await fetchData();
|
|
1980
|
+
* // 清理逻辑需通过 ref/AbortController 自行管理
|
|
1981
|
+
* // ❌ 不要 return cleanupFn(async 函数返回 Promise,无法作为清理函数)
|
|
1982
|
+
* });
|
|
1983
|
+
*/
|
|
1984
|
+
function useMount(effect) {
|
|
1985
|
+
const isMountedRef = useRef(false);
|
|
1986
|
+
const effectRef = useLatest(effect);
|
|
1987
|
+
useEffect(() => {
|
|
1988
|
+
if (!TypeUtil.isFunction(effectRef.current)) {
|
|
1989
|
+
console.error(`hook [useMount] Expected parameter to be a function, but got ${typeof effectRef.current}. This effect will not execute.`);
|
|
1990
|
+
return;
|
|
1991
|
+
}
|
|
1992
|
+
if (isMountedRef.current) return;
|
|
1993
|
+
isMountedRef.current = true;
|
|
1994
|
+
const cleanup = effectRef.current?.();
|
|
1995
|
+
return TypeUtil.isFunction(cleanup) && !TypeUtil.isPromise(cleanup) ? cleanup : void 0;
|
|
1996
|
+
}, [effectRef]);
|
|
1997
|
+
}
|
|
1998
|
+
//#endregion
|
|
1999
|
+
//#region src/react/useResponsive.ts
|
|
2000
|
+
/** 屏幕响应断点 token 配置 */
|
|
2001
|
+
const BREAK_POINT_TOKEN = {
|
|
2002
|
+
XS: 480,
|
|
2003
|
+
XSMax: 575,
|
|
2004
|
+
XSMin: 480,
|
|
2005
|
+
SM: 576,
|
|
2006
|
+
SMMax: 767,
|
|
2007
|
+
SMMin: 576,
|
|
2008
|
+
MD: 768,
|
|
2009
|
+
MDMax: 991,
|
|
2010
|
+
MDMin: 768,
|
|
2011
|
+
LG: 992,
|
|
2012
|
+
LGMax: 1199,
|
|
2013
|
+
LGMin: 992,
|
|
2014
|
+
XL: 1200,
|
|
2015
|
+
XLMax: 1599,
|
|
2016
|
+
XLMin: 1200,
|
|
2017
|
+
XXL: 1600,
|
|
2018
|
+
XXLMax: 1919,
|
|
2019
|
+
XXLMin: 1600,
|
|
2020
|
+
XXXL: 1920,
|
|
2021
|
+
XXXLMin: 1920
|
|
2022
|
+
};
|
|
2023
|
+
const SUBSCRIBER_SET = /* @__PURE__ */ new Set();
|
|
2024
|
+
const BREAK_POINTS = [
|
|
2025
|
+
"xxxl",
|
|
2026
|
+
"xxl",
|
|
2027
|
+
"xl",
|
|
2028
|
+
"lg",
|
|
2029
|
+
"md",
|
|
2030
|
+
"sm",
|
|
2031
|
+
"xs"
|
|
2032
|
+
];
|
|
2033
|
+
const DEFAULT_VALUES = Object.freeze(ArrayUtil.zipToObject(BREAK_POINTS, false));
|
|
2034
|
+
let responsiveValues = { ...DEFAULT_VALUES };
|
|
2035
|
+
let responsiveTokens = clone(BREAK_POINT_TOKEN);
|
|
2036
|
+
function useResponsive(options) {
|
|
2037
|
+
const { breakPointTokens = {} } = options || {};
|
|
2038
|
+
const tokens = useMemo(() => Object.assign(BREAK_POINT_TOKEN, breakPointTokens), [breakPointTokens]);
|
|
2039
|
+
const [responsive, setResponsive] = useState(() => calculateResponsive(tokens));
|
|
2040
|
+
const current = ObjectUtil.keys(DEFAULT_VALUES).find((key) => responsive[key] === true) || "xs";
|
|
2041
|
+
useLayoutEffect(() => {
|
|
2042
|
+
responsiveTokens = tokens;
|
|
2043
|
+
window.addEventListener("resize", resizeListener);
|
|
2044
|
+
const subscriber = () => {
|
|
2045
|
+
setResponsive(responsiveValues);
|
|
2046
|
+
};
|
|
2047
|
+
SUBSCRIBER_SET.add(subscriber);
|
|
2048
|
+
return () => {
|
|
2049
|
+
SUBSCRIBER_SET.delete(subscriber);
|
|
2050
|
+
if (!SUBSCRIBER_SET.size) window.removeEventListener("resize", resizeListener);
|
|
2051
|
+
};
|
|
2052
|
+
}, [tokens]);
|
|
2053
|
+
return {
|
|
2054
|
+
responsive,
|
|
2055
|
+
current,
|
|
2056
|
+
breakPointTokens: tokens
|
|
2057
|
+
};
|
|
2058
|
+
}
|
|
2059
|
+
function resizeListener() {
|
|
2060
|
+
const newValues = calculateResponsive(responsiveTokens);
|
|
2061
|
+
if (!isEqual(responsiveValues, newValues)) {
|
|
2062
|
+
responsiveValues = newValues;
|
|
2063
|
+
for (const subscriber of SUBSCRIBER_SET) subscriber();
|
|
2064
|
+
}
|
|
2065
|
+
}
|
|
2066
|
+
function calculateResponsive(tokens) {
|
|
2067
|
+
const config = ArrayUtil.zipToObject(BREAK_POINTS, BREAK_POINTS.map((t) => tokens[StringUtil.toUpperCase(t)]));
|
|
2068
|
+
return ObjectUtil.entriesMap(DEFAULT_VALUES, (key) => [key, window.innerWidth >= config[key]]);
|
|
2069
|
+
}
|
|
2070
|
+
//#endregion
|
|
2071
|
+
//#region src/react/useUnmount.ts
|
|
2072
|
+
/**
|
|
2073
|
+
* 在组件卸载时执行的 Hook
|
|
2074
|
+
*
|
|
2075
|
+
* @param effect 副作用函数
|
|
2076
|
+
*/
|
|
2077
|
+
function useUnmount(effect) {
|
|
2078
|
+
const effectRef = useLatest(effect);
|
|
2079
|
+
useEffect(() => () => {
|
|
2080
|
+
if (!TypeUtil.isFunction(effectRef.current)) {
|
|
2081
|
+
console.error(`hook [useUnmount] Expected parameter to be a function, but got ${typeof effectRef.current}. This effect will not execute.`);
|
|
2082
|
+
return;
|
|
2083
|
+
}
|
|
2084
|
+
effectRef.current?.();
|
|
2085
|
+
}, [effectRef]);
|
|
2086
|
+
}
|
|
2087
|
+
//#endregion
|
|
2088
|
+
//#region src/react/useTitle.ts
|
|
2089
|
+
/**
|
|
2090
|
+
* 设置页面标题
|
|
2091
|
+
* - 轻量级,适用于无路由库时设置页面标题
|
|
2092
|
+
* - 多个 `useTitle` 实例会互相干扰,需在顶层组件使用
|
|
2093
|
+
* - 无法处理 `document.title` 固有的竞态问题
|
|
2094
|
+
*
|
|
2095
|
+
* @param title 页面标题
|
|
2096
|
+
* @param options 配置选项
|
|
2097
|
+
*/
|
|
2098
|
+
function useTitle(title, options) {
|
|
2099
|
+
const titleRef = useRef(EnvUtil.isBrowser() ? document.title : "");
|
|
2100
|
+
useEffect(() => {
|
|
2101
|
+
if (!TypeUtil.isString(title)) {
|
|
2102
|
+
console.error(`hook [useTitle] Expected parameter to be a string, but got ${typeof title}. This effect will not execute.`);
|
|
2103
|
+
return;
|
|
2104
|
+
}
|
|
2105
|
+
if (EnvUtil.isBrowser()) document.title = title;
|
|
2106
|
+
}, [title]);
|
|
2107
|
+
useUnmount(() => {
|
|
2108
|
+
if (EnvUtil.isBrowser() && options?.isRestoreOnUnmount) document.title = titleRef.current;
|
|
2109
|
+
});
|
|
2110
|
+
}
|
|
2111
|
+
//#endregion
|
|
2112
|
+
export { BREAK_POINT_TOKEN, useCreation, useLatest, useMount, useResponsive, useTitle, useUnmount };
|