@pawover/kit 0.6.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/README.en.md +123 -0
  2. package/README.md +145 -1
  3. package/entry/eslint-rules.cjs +1 -0
  4. package/entry/eslint-rules.d.cts +1 -0
  5. package/entry/eslint-rules.d.ts +2 -0
  6. package/entry/eslint-rules.js +2 -0
  7. package/entry/hooks-alova.cjs +1 -0
  8. package/entry/hooks-alova.d.cts +1 -0
  9. package/entry/hooks-alova.d.ts +1 -0
  10. package/entry/hooks-alova.js +1 -0
  11. package/entry/hooks-react.cjs +1 -0
  12. package/entry/hooks-react.d.cts +1 -0
  13. package/entry/hooks-react.d.ts +1 -0
  14. package/entry/hooks-react.js +1 -0
  15. package/entry/index.cjs +1 -0
  16. package/entry/index.d.cts +1 -0
  17. package/entry/index.d.ts +1 -0
  18. package/entry/index.js +1 -0
  19. package/entry/math.cjs +1 -0
  20. package/entry/math.d.cts +1 -0
  21. package/entry/math.d.ts +1 -0
  22. package/entry/math.js +1 -0
  23. package/entry/types-react.cjs +1 -0
  24. package/entry/types-react.d.cts +1 -0
  25. package/entry/types-react.d.ts +1 -0
  26. package/entry/types-react.js +1 -0
  27. package/entry/types.cjs +1 -0
  28. package/entry/types.d.cts +1 -0
  29. package/entry/types.d.ts +1 -0
  30. package/entry/types.js +1 -0
  31. package/entry/vite.cjs +1 -0
  32. package/entry/vite.d.cts +1 -0
  33. package/entry/vite.d.ts +1 -0
  34. package/entry/vite.js +1 -0
  35. package/entry/zod.cjs +1 -0
  36. package/entry/zod.d.cts +1 -0
  37. package/entry/zod.d.ts +1 -0
  38. package/entry/zod.js +1 -0
  39. package/package.json +81 -96
  40. package/packages/eslint-rules/dist/core.antfu.cjs +0 -78
  41. package/packages/eslint-rules/dist/core.antfu.d.cts +0 -1
  42. package/packages/eslint-rules/dist/core.antfu.d.ts +0 -74
  43. package/packages/eslint-rules/dist/core.antfu.js +0 -79
  44. package/packages/eslint-rules/dist/core.imports.cjs +0 -59
  45. package/packages/eslint-rules/dist/core.imports.d.cts +0 -1
  46. package/packages/eslint-rules/dist/core.imports.d.ts +0 -61
  47. package/packages/eslint-rules/dist/core.imports.js +0 -59
  48. package/packages/eslint-rules/dist/core.importsSort.cjs +0 -7
  49. package/packages/eslint-rules/dist/core.importsSort.d.cts +0 -1
  50. package/packages/eslint-rules/dist/core.importsSort.d.ts +0 -7
  51. package/packages/eslint-rules/dist/core.importsSort.js +0 -7
  52. package/packages/eslint-rules/dist/core.javascript.cjs +0 -1208
  53. package/packages/eslint-rules/dist/core.javascript.d.cts +0 -1
  54. package/packages/eslint-rules/dist/core.javascript.d.ts +0 -1216
  55. package/packages/eslint-rules/dist/core.javascript.js +0 -1208
  56. package/packages/eslint-rules/dist/core.react.cjs +0 -551
  57. package/packages/eslint-rules/dist/core.react.d.cts +0 -1
  58. package/packages/eslint-rules/dist/core.react.d.ts +0 -551
  59. package/packages/eslint-rules/dist/core.react.js +0 -551
  60. package/packages/eslint-rules/dist/core.reactHooks.cjs +0 -107
  61. package/packages/eslint-rules/dist/core.reactHooks.d.cts +0 -1
  62. package/packages/eslint-rules/dist/core.reactHooks.d.ts +0 -107
  63. package/packages/eslint-rules/dist/core.reactHooks.js +0 -107
  64. package/packages/eslint-rules/dist/core.stylistic.cjs +0 -608
  65. package/packages/eslint-rules/dist/core.stylistic.d.cts +0 -1
  66. package/packages/eslint-rules/dist/core.stylistic.d.ts +0 -595
  67. package/packages/eslint-rules/dist/core.stylistic.js +0 -608
  68. package/packages/eslint-rules/dist/core.typescript.cjs +0 -905
  69. package/packages/eslint-rules/dist/core.typescript.d.cts +0 -1
  70. package/packages/eslint-rules/dist/core.typescript.d.ts +0 -917
  71. package/packages/eslint-rules/dist/core.typescript.js +0 -906
  72. package/packages/eslint-rules/dist/core.vue.cjs +0 -312
  73. package/packages/eslint-rules/dist/core.vue.d.cts +0 -1
  74. package/packages/eslint-rules/dist/core.vue.d.ts +0 -293
  75. package/packages/eslint-rules/dist/core.vue.js +0 -313
  76. package/packages/eslint-rules/dist/index.cjs +0 -126
  77. package/packages/eslint-rules/dist/index.d.cts +0 -1
  78. package/packages/eslint-rules/dist/index.d.ts +0 -113
  79. package/packages/eslint-rules/dist/index.js +0 -126
  80. package/packages/eslint-rules/dist/preset-Bk6lxNP0.cjs +0 -107
  81. package/packages/eslint-rules/dist/preset-CQJST7pW.js +0 -102
  82. package/packages/hooks/dist/alova.cjs +0 -53
  83. package/packages/hooks/dist/alova.d.cts +0 -1
  84. package/packages/hooks/dist/alova.d.ts +0 -33
  85. package/packages/hooks/dist/alova.js +0 -50
  86. package/packages/hooks/dist/index.cjs +0 -0
  87. package/packages/hooks/dist/index.d.cts +0 -1
  88. package/packages/hooks/dist/index.d.ts +0 -1
  89. package/packages/hooks/dist/index.js +0 -0
  90. package/packages/hooks/dist/react.cjs +0 -169
  91. package/packages/hooks/dist/react.d.cts +0 -1
  92. package/packages/hooks/dist/react.d.ts +0 -95
  93. package/packages/hooks/dist/react.js +0 -163
  94. package/packages/types/dist/index.cjs +0 -0
  95. package/packages/types/dist/index.d.cts +0 -1
  96. package/packages/types/dist/index.d.ts +0 -271
  97. package/packages/types/dist/index.js +0 -0
  98. package/packages/types/dist/react.cjs +0 -0
  99. package/packages/types/dist/react.d.cts +0 -1
  100. package/packages/types/dist/react.d.ts +0 -64
  101. package/packages/types/dist/react.js +0 -0
  102. package/packages/utils/dist/index.cjs +0 -2300
  103. package/packages/utils/dist/index.d.cts +0 -1
  104. package/packages/utils/dist/index.d.ts +0 -2740
  105. package/packages/utils/dist/index.js +0 -2287
  106. package/packages/utils/dist/math-BznvO4qI.cjs +0 -1029
  107. package/packages/utils/dist/math-DRbCtLQH.js +0 -1012
  108. package/packages/utils/dist/math.cjs +0 -3
  109. package/packages/utils/dist/math.d.cts +0 -1
  110. package/packages/utils/dist/math.d.ts +0 -60
  111. package/packages/utils/dist/math.js +0 -2
  112. package/packages/utils/dist/vite.cjs +0 -44
  113. package/packages/utils/dist/vite.d.cts +0 -1
  114. package/packages/utils/dist/vite.d.ts +0 -28
  115. package/packages/utils/dist/vite.js +0 -43
  116. package/packages/zod/dist/index.cjs +0 -91
  117. package/packages/zod/dist/index.d.cts +0 -1
  118. package/packages/zod/dist/index.d.ts +0 -57
  119. package/packages/zod/dist/index.js +0 -61
  120. /package/{packages/hooks/dist/metadata.json → entry/hooks-metadata.json} +0 -0
  121. /package/{packages/utils/dist → entry}/metadata.json +0 -0
@@ -1,1029 +0,0 @@
1
- //#region src/type/typeUtil.ts
2
- /**
3
- * 类型工具类
4
- */
5
- var TypeUtil = class {
6
- static PROTOTYPE_TAGS = {
7
- STRING: "[object String]",
8
- NUMBER: "[object Number]",
9
- BOOLEAN: "[object Boolean]",
10
- BIGINT: "[object BigInt]",
11
- SYMBOL: "[object Symbol]",
12
- UNDEFINED: "[object Undefined]",
13
- NULL: "[object Null]",
14
- OBJECT: "[object Object]",
15
- FUNCTION: "[object Function]",
16
- GENERATOR_FUNCTION: "[object GeneratorFunction]",
17
- ASYNC_FUNCTION: "[object AsyncFunction]",
18
- ASYNC_GENERATOR_FUNCTION: "[object AsyncGeneratorFunction]",
19
- PROMISE: "[object Promise]",
20
- MAP: "[object Map]",
21
- SET: "[object Set]",
22
- WEAK_MAP: "[object WeakMap]",
23
- WEAK_SET: "[object WeakSet]",
24
- BLOB: "[object Blob]",
25
- FILE: "[object File]",
26
- READABLE_STREAM: "[object ReadableStream]",
27
- GLOBAL: "[object global]",
28
- WINDOW: "[object Window]",
29
- IFRAME: "[object HTMLIFrameElement]",
30
- DATE: "[object Date]",
31
- ERROR: "[object Error]",
32
- REG_EXP: "[object RegExp]",
33
- WEB_SOCKET: "[object WebSocket]",
34
- URL_SEARCH_PARAMS: "[object URLSearchParams]",
35
- ABORT_SIGNAL: "[object AbortSignal]"
36
- };
37
- static TYPED_ARRAY_TAGS = /* @__PURE__ */ new Set([
38
- "[object Int8Array]",
39
- "[object Uint8Array]",
40
- "[object Uint8ClampedArray]",
41
- "[object Int16Array]",
42
- "[object Uint16Array]",
43
- "[object Int32Array]",
44
- "[object Uint32Array]",
45
- "[object Float32Array]",
46
- "[object Float64Array]",
47
- "[object BigInt64Array]",
48
- "[object BigUint64Array]"
49
- ]);
50
- /**
51
- * 获取值的 [[Prototype]] 标签
52
- *
53
- * @param value - 任意 JavaScript 值
54
- * @returns 标准化的类型标签字符串
55
- */
56
- static getPrototypeString(value) {
57
- return Object.prototype.toString.call(value);
58
- }
59
- /**
60
- * 检查 value 是否为 string 类型
61
- * - 当 `checkNullish` 为 `true` 时,会先 trim 再判断是否为空
62
- *
63
- * @param value 待检查值
64
- * @param checkNullish 是否检查空字符串(含空白字符串),默认为 `false`
65
- * @returns 是否为字符串
66
- * @example
67
- * ```ts
68
- * TypeUtil.isString("abc"); // true
69
- * TypeUtil.isString(""); // true
70
- * TypeUtil.isString("", true); // false
71
- * TypeUtil.isString(" ", true); // false
72
- * TypeUtil.isString(" a ", true); // true
73
- * ```
74
- */
75
- static isString(value, checkNullish = false) {
76
- return typeof value === "string" && (!checkNullish || value.trim().length > 0);
77
- }
78
- /**
79
- * 检查 value 是否为 number 类型
80
- * - 默认会调用 `TypeUtil.isNaN`(内部基于 `Number.isNaN`)过滤掉 `NaN`
81
- *
82
- * @param value 待检查值
83
- * @param checkNaN 是否检查 `NaN`,默认为 `true`
84
- * @returns 是否为 number
85
- * @example
86
- * ```ts
87
- * TypeUtil.isNumber(1); // true
88
- * TypeUtil.isNumber(NaN); // false (default)
89
- * TypeUtil.isNumber(NaN, false); // true
90
- * ```
91
- */
92
- static isNumber(value, checkNaN = true) {
93
- return typeof value === "number" && (!checkNaN || !this.isNaN(value));
94
- }
95
- /**
96
- * 检查 value 是否为 NaN
97
- * - 禁止使用全局 `isNaN`,其会先进行隐式数字转换,可能导致误判(例如 `isNaN("foo") === true`)
98
- * - 使用 `Number.isNaN` 仅在值本身就是 `NaN` 时返回 `true`,语义更严格且更安全
99
- *
100
- * @param value 待检查值
101
- * @returns 是否为 NaN
102
- * @example
103
- * ```ts
104
- * TypeUtil.isNaN(NaN); // true
105
- * ```
106
- */
107
- static isNaN(value) {
108
- return Number.isNaN(value);
109
- }
110
- /**
111
- * 检查 value 是否为整数
112
- *
113
- * @param value 待检查值
114
- * @param checkSafe 是否附加安全整数检查
115
- * @returns 是否为整数
116
- * @example
117
- * ```ts
118
- * TypeUtil.isInteger(1); // true
119
- * TypeUtil.isInteger(1.1); // false
120
- * ```
121
- */
122
- static isInteger(value, checkSafe = true) {
123
- const check = Number.isInteger(value);
124
- return checkSafe ? check && Number.isSafeInteger(value) : check;
125
- }
126
- /**
127
- * 检查 value 是否为正整数
128
- * - 此函数中 `0` 不被视为正整数
129
- *
130
- * @param value 待检查值
131
- * @param checkSafe 是否附加安全整数检查
132
- * @example
133
- * ```ts
134
- * TypeUtil.isPositiveInteger(1); // true
135
- * TypeUtil.isPositiveInteger(0); // false
136
- * ```
137
- */
138
- static isPositiveInteger(value, checkSafe = true) {
139
- return this.isInteger(value, checkSafe) && value > 0;
140
- }
141
- /**
142
- * 检查 value 是否为负整数
143
- * - 此函数中 `0` 不被视为负整数
144
- *
145
- * @param value 待检查值
146
- * @param checkSafe 是否附加安全整数检查
147
- * @example
148
- * ```ts
149
- * TypeUtil.isNegativeInteger(-1); // true
150
- * TypeUtil.isNegativeInteger(0); // false
151
- * ```
152
- */
153
- static isNegativeInteger(value, checkSafe = true) {
154
- return this.isInteger(value, checkSafe) && value < 0;
155
- }
156
- /**
157
- * 检查 value 是否为 Infinity
158
- * - 排除 `NaN`
159
- *
160
- * @param value 待检查值
161
- * @example
162
- * ```ts
163
- * TypeUtil.isInfinity(Infinity); // true
164
- * TypeUtil.isInfinity(1); // false
165
- * ```
166
- */
167
- static isInfinity(value) {
168
- return this.isNumber(value) && (Number.POSITIVE_INFINITY === value || Number.NEGATIVE_INFINITY === value);
169
- }
170
- /**
171
- * 检查 value 是否类似 Infinity
172
- * - 排除 `NaN`
173
- *
174
- * @param value 待检查值
175
- * @example
176
- * ```ts
177
- * TypeUtil.isInfinityLike("Infinity"); // true
178
- * TypeUtil.isInfinityLike("123"); // false
179
- * ```
180
- */
181
- static isInfinityLike(value) {
182
- const check = this.isInfinity(value);
183
- if (check) return check;
184
- if (typeof value === "string") return [
185
- "infinity",
186
- "-infinity",
187
- "+infinity",
188
- "Infinity",
189
- "-Infinity",
190
- "+Infinity"
191
- ].includes(value.trim());
192
- return false;
193
- }
194
- /**
195
- * 检查 value 是否为 Boolean
196
- * @param value 待检查值
197
- * @returns 是否为 Boolean
198
- * @example
199
- * ```ts
200
- * TypeUtil.isBoolean(false); // true
201
- * ```
202
- */
203
- static isBoolean(value) {
204
- return typeof value === "boolean";
205
- }
206
- /**
207
- * 检查 value 是否为 BigInt
208
- * @param value 待检查值
209
- * @returns 是否为 BigInt
210
- * @example
211
- * ```ts
212
- * TypeUtil.isBigInt(1n); // true
213
- * ```
214
- */
215
- static isBigInt(value) {
216
- return typeof value === "bigint";
217
- }
218
- /**
219
- * 检查 value 是否为 Symbol
220
- * @param value 待检查值
221
- * @returns 是否为 Symbol
222
- * @example
223
- * ```ts
224
- * TypeUtil.isSymbol(Symbol("a")); // true
225
- * ```
226
- */
227
- static isSymbol(value) {
228
- return typeof value === "symbol";
229
- }
230
- /**
231
- * 检查 value 是否为 undefined
232
- * @param value 待检查值
233
- * @returns 是否为 undefined
234
- * @example
235
- * ```ts
236
- * TypeUtil.isUndefined(undefined); // true
237
- * ```
238
- */
239
- static isUndefined(value) {
240
- return typeof value === "undefined";
241
- }
242
- /**
243
- * 检查 value 是否为 null
244
- * @param value 待检查值
245
- * @returns 是否为 null
246
- * @example
247
- * ```ts
248
- * TypeUtil.isNull(null); // true
249
- * ```
250
- */
251
- static isNull(value) {
252
- return value === null;
253
- }
254
- /**
255
- * 检查 value 是否为 null 或 undefined
256
- * @param value 待检查值
257
- * @returns 是否为 Nullish
258
- * @example
259
- * ```ts
260
- * TypeUtil.isNullish(null); // true
261
- * TypeUtil.isNullish(undefined); // true
262
- * ```
263
- */
264
- static isNullish(value) {
265
- return this.isNull(value) || this.isUndefined(value);
266
- }
267
- /**
268
- * 检查 value 是否为 Function
269
- * @param value 待检查值
270
- * @returns 是否为 Function
271
- * @example
272
- * ```ts
273
- * TypeUtil.isFunction(() => {}); // true
274
- * ```
275
- */
276
- static isFunction(value) {
277
- return typeof value === "function";
278
- }
279
- /**
280
- * 检查 value 是否为 AsyncFunction
281
- * @param value 待检查值
282
- * @returns 是否为 AsyncFunction
283
- * @example
284
- * ```ts
285
- * TypeUtil.isAsyncFunction(async () => {}); // true
286
- * ```
287
- */
288
- static isAsyncFunction(value) {
289
- return this.isFunction(value) && this.getPrototypeString(value) === this.PROTOTYPE_TAGS.ASYNC_FUNCTION;
290
- }
291
- /**
292
- * 检查 value 是否为 GeneratorFunction
293
- * @param value 待检查值
294
- * @returns 是否为 GeneratorFunction
295
- * @example
296
- * ```ts
297
- * TypeUtil.isGeneratorFunction(function * a () {}); // true
298
- * ```
299
- */
300
- static isGeneratorFunction(value) {
301
- return this.isFunction(value) && this.getPrototypeString(value) === this.PROTOTYPE_TAGS.GENERATOR_FUNCTION;
302
- }
303
- /**
304
- * 检查 value 是否为 AsyncGeneratorFunction
305
- * @param value 待检查值
306
- * @returns 是否为 AsyncGeneratorFunction
307
- * @example
308
- * ```ts
309
- * TypeUtil.isAsyncGeneratorFunction(async function * a () {}); // true
310
- * ```
311
- */
312
- static isAsyncGeneratorFunction(value) {
313
- return this.isFunction(value) && this.getPrototypeString(value) === this.PROTOTYPE_TAGS.ASYNC_GENERATOR_FUNCTION;
314
- }
315
- /**
316
- * 检查 value 是否为 Promise
317
- * @param value 待检查值
318
- * @returns 是否为 Promise
319
- * @example
320
- * ```ts
321
- * TypeUtil.isPromise(Promise.resolve(1)); // true
322
- * ```
323
- */
324
- static isPromise(value) {
325
- return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.PROMISE;
326
- }
327
- /**
328
- * 检查 value 是否为 PromiseLike
329
- * - 可识别拥有 then 方法的非 Promise 对象
330
- * @param value 待检查值
331
- * @returns 是否为 PromiseLike
332
- * @example
333
- * ```ts
334
- * TypeUtil.isPromiseLike({ then: () => {} }); // true
335
- * ```
336
- */
337
- static isPromiseLike(value) {
338
- return this.isPromise(value) || this.isPlainObject(value, false) && this.isFunction(value["then"]);
339
- }
340
- /**
341
- * 判断是否为普通对象类型
342
- * - 可选是否检查原型为 `Object.prototype`,防止原型链污染
343
- *
344
- * @param value 待检查值
345
- * @param prototypeCheck 是否进行原型检查,默认 `true`
346
- * @returns 是否为 Plain Object (当 prototypeCheck=true) 或 object
347
- * @example
348
- * ```ts
349
- * TypeUtil.isPlainObject({}); // true
350
- * TypeUtil.isPlainObject([]); // false
351
- * TypeUtil.isPlainObject(new Date()); // false
352
- * TypeUtil.isPlainObject(new (class {})()); // false
353
- * TypeUtil.isPlainObject(new (class {})(), false); // true
354
- * TypeUtil.isPlainObject(Object.create(null)) // false
355
- * TypeUtil.isPlainObject(Object.create(null), false) // true
356
- * ```
357
- */
358
- static isPlainObject(value, prototypeCheck = true) {
359
- const check = this.getPrototypeString(value) === this.PROTOTYPE_TAGS.OBJECT;
360
- return prototypeCheck ? check && Object.getPrototypeOf(value) === Object.prototype : check;
361
- }
362
- /**
363
- * 判断是否为广义对象类型
364
- *
365
- * @param value 待检查值
366
- * @returns 是否为对象
367
- * @example
368
- * ```ts
369
- * TypeUtil.isObject({}); // true
370
- * TypeUtil.isObject([]); // true
371
- * TypeUtil.isObject(new Date()); // true
372
- * TypeUtil.isObject(null); // false
373
- * TypeUtil.isObject("string"); // false
374
- * ```
375
- */
376
- static isObject(value) {
377
- return typeof value === "object" && value !== null;
378
- }
379
- /**
380
- * 判断一个对象是否为有效的枚举
381
- * - 枚举成员不能为空
382
- * - 枚举成员的键不能具有数值名
383
- * - 枚举成员的值必须类型一致且为 `string` 或 `number` 类型
384
- * - 枚举成员的值不能重复
385
- * - 枚举成员的值必须全部为双向映射或非双向映射
386
- *
387
- * @param enumeration 待检查值
388
- * @returns [是否为有效的枚举, 是否为双向枚举]
389
- * @example
390
- * ```ts
391
- * enum A { X, Y }
392
- * TypeUtil.isEnumeration(A); // [true, true]
393
- * ```
394
- */
395
- static isEnumeration(enumeration) {
396
- if (typeof enumeration !== "object" || enumeration === null) return [false, false];
397
- const keys = Object.keys(enumeration);
398
- if (keys.length === 0) return [false, false];
399
- const originalKeys = [];
400
- const numericKeys = [];
401
- for (const key of keys) if (/^\d+$/.test(key)) numericKeys.push(key);
402
- else originalKeys.push(key);
403
- if (originalKeys.length === 0) return [false, false];
404
- let valueType = null;
405
- const values = [];
406
- for (const key of originalKeys) {
407
- const value = enumeration[key];
408
- const type = typeof value;
409
- if (type !== "string" && type !== "number") return [false, false];
410
- if (valueType === null) valueType = type;
411
- else if (type !== valueType) return [false, false];
412
- values.push(value);
413
- }
414
- if (new Set(values).size !== values.length) return [false, false];
415
- let isBidirectional = false;
416
- if (numericKeys.length > 0) {
417
- if (numericKeys.length !== originalKeys.length) return [false, false];
418
- const reverseMappedNames = /* @__PURE__ */ new Set();
419
- for (const numKey of numericKeys) {
420
- const reverseValue = enumeration[numKey];
421
- if (typeof reverseValue !== "string") return [false, false];
422
- if (!originalKeys.includes(reverseValue)) return [false, false];
423
- reverseMappedNames.add(reverseValue);
424
- }
425
- if (reverseMappedNames.size !== originalKeys.length) return [false, false];
426
- isBidirectional = true;
427
- }
428
- return [true, isBidirectional];
429
- }
430
- /**
431
- * 检查 value 是否为 Class
432
- *
433
- * @param value 待检查值
434
- * @returns 是否为 Class
435
- * @example
436
- * ```ts
437
- * class A {}
438
- * TypeUtil.isClass(A); // true
439
- * TypeUtil.isClass(() => {}); // false
440
- * ```
441
- */
442
- static isClass(value) {
443
- return this.isFunction(value) && !this.isAsyncFunction(value) && Function.prototype.toString.call(value).startsWith("class ") && value.prototype !== void 0;
444
- }
445
- /**
446
- * 检查 value 是否为数组
447
- *
448
- * @param value 待检查值
449
- * @returns 是否为数组
450
- * @example
451
- * ```ts
452
- * TypeUtil.isArray([]); // true
453
- * ```
454
- */
455
- static isArray(value) {
456
- return Array.isArray(value);
457
- }
458
- /**
459
- * 检查 value 是否为 TypedArray
460
- *
461
- * @param value 待检查值
462
- * @returns 是否为 TypedArray
463
- * @example
464
- * ```ts
465
- * TypeUtil.isTypedArray(new Int8Array()); // true
466
- * ```
467
- */
468
- static isTypedArray(value) {
469
- return typeof value === "object" && value !== null && this.TYPED_ARRAY_TAGS.has(this.getPrototypeString(value));
470
- }
471
- /**
472
- * 检查 value 是否为 Map
473
- * @param value 待检查值
474
- * @returns 是否为 Map
475
- * @example
476
- * ```ts
477
- * TypeUtil.isMap(new Map()); // true
478
- * ```
479
- */
480
- static isMap(value) {
481
- return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.MAP;
482
- }
483
- /**
484
- * 检查 value 是否为 WeakMap
485
- * @param value 待检查值
486
- * @returns 是否为 WeakMap
487
- * @example
488
- * ```ts
489
- * TypeUtil.isWeakMap(new WeakMap()); // true
490
- * ```
491
- */
492
- static isWeakMap(value) {
493
- return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.WEAK_MAP;
494
- }
495
- /**
496
- * 检查 value 是否为 Set
497
- * @param value 待检查值
498
- * @returns 是否为 Set
499
- * @example
500
- * ```ts
501
- * TypeUtil.isSet(new Set()); // true
502
- * ```
503
- */
504
- static isSet(value) {
505
- return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.SET;
506
- }
507
- /**
508
- * 检查 value 是否为 WeakSet
509
- * @param value 待检查值
510
- * @returns 是否为 WeakSet
511
- * @example
512
- * ```ts
513
- * TypeUtil.isWeakSet(new WeakSet()); // true
514
- * ```
515
- */
516
- static isWeakSet(value) {
517
- return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.WEAK_SET;
518
- }
519
- /**
520
- * 检查 value 是否为 Blob
521
- * @param value 待检查值
522
- * @returns 是否为 Blob
523
- * @example
524
- * ```ts
525
- * TypeUtil.isBlob(new Blob(["a"])); // true
526
- * ```
527
- */
528
- static isBlob(value) {
529
- return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.BLOB;
530
- }
531
- /**
532
- * 检查 value 是否为 File
533
- * @param value 待检查值
534
- * @returns 是否为 File
535
- * @example
536
- * ```ts
537
- * TypeUtil.isFile(new File(["a"], "a.txt")); // true
538
- * ```
539
- */
540
- static isFile(value) {
541
- return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.FILE;
542
- }
543
- /**
544
- * 检查 value 是否为 ReadableStream
545
- * - Uses `Object.prototype.toString` where supported (modern browsers, Node.js ≥18).
546
- * - Falls back to duck-typing in older environments.
547
- * - Resistant to basic forgery, but not 100% secure in all polyfill scenarios.
548
- * - ⚠️ Note: In older Node.js (<18) or with non-compliant polyfills, this may return false positives or negatives.
549
- *
550
- * @param value 待检查值
551
- * @returns 是否为 ReadableStream
552
- * @example
553
- * ```ts
554
- * TypeUtil.isReadableStream(new ReadableStream()); // true
555
- * ```
556
- */
557
- static isReadableStream(value) {
558
- if (this.getPrototypeString(value) === this.PROTOTYPE_TAGS.READABLE_STREAM) return true;
559
- return this.isPlainObject(value) && this.isFunction(value["getReader"]) && this.isFunction(value["pipeThrough"]);
560
- }
561
- /**
562
- * 检查 value 是否为 Window
563
- * @param value 待检查值
564
- * @returns 是否为 Window
565
- * @example
566
- * ```ts
567
- * TypeUtil.isWindow(window); // true
568
- * ```
569
- */
570
- static isWindow(value) {
571
- return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.WINDOW;
572
- }
573
- /**
574
- * 检查 value 是否为 HTMLIFrameElement
575
- * @param value 待检查值
576
- * @returns 是否为 HTMLIFrameElement
577
- * @example
578
- * ```ts
579
- * TypeUtil.isIframe(document.createElement("iframe")); // true
580
- * ```
581
- */
582
- static isIframe(value) {
583
- if (typeof window === "undefined") return false;
584
- return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.IFRAME;
585
- }
586
- /**
587
- * 检查 value 是否为 Date 对象
588
- *
589
- * @param value 待检查值
590
- * @param invalidCheck 是否要求日期有效(非 Invalid Date)。默认 true
591
- * - true: 仅当是有效 Date 对象时返回 true(排除 new Date('invalid'))
592
- * - false: 只要 [[Prototype]] 是 Date 即返回 true(包含 Invalid Date)
593
- * @returns 是否为 Date 对象,根据 invalidCheck 返回不同语义的 Date 判定
594
- *
595
- * @example
596
- * ```ts
597
- * TypeUtil.isDate(new Date()); // true
598
- * TypeUtil.isDate(new Date('invalid')); // false
599
- * TypeUtil.isDate(new Date('invalid'), false); // true
600
- * TypeUtil.isDate(null); // false
601
- * TypeUtil.isDate({}); // false
602
- * ```
603
- */
604
- static isDate(value, invalidCheck = true) {
605
- if (!value || typeof value !== "object") return false;
606
- if (this.getPrototypeString(value) !== this.PROTOTYPE_TAGS.DATE) return false;
607
- if (!invalidCheck) return true;
608
- try {
609
- const time = value.getTime();
610
- return typeof time === "number" && !Number.isNaN(time);
611
- } catch {
612
- return false;
613
- }
614
- }
615
- /**
616
- * 检查 value 是否为 Error 对象
617
- * @param value 待检查值
618
- * @returns 是否为 Error
619
- * @example
620
- * ```ts
621
- * TypeUtil.isError(new Error("x")); // true
622
- * ```
623
- */
624
- static isError(value) {
625
- return value instanceof Error || this.getPrototypeString(value) === this.PROTOTYPE_TAGS.ERROR;
626
- }
627
- /**
628
- * 检查 value 是否为 RegExp
629
- * @param value 待检查值
630
- * @returns 是否为 RegExp
631
- * @example
632
- * ```ts
633
- * TypeUtil.isRegExp(/a/); // true
634
- * ```
635
- */
636
- static isRegExp(value) {
637
- if (typeof value !== "object" || value === null) return false;
638
- try {
639
- const regex = value;
640
- 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);
641
- } catch (error) {
642
- return false;
643
- }
644
- }
645
- /**
646
- * 检查 value 是否为 WebSocket
647
- * @param value 待检查值
648
- * @returns 是否为 WebSocket
649
- * @example
650
- * ```ts
651
- * TypeUtil.isWebSocket(new WebSocket("wss://echo.websocket.events")); // true
652
- * ```
653
- */
654
- static isWebSocket(value) {
655
- return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.WEB_SOCKET;
656
- }
657
- /**
658
- * 检查 value 是否为 URLSearchParams
659
- * @param value 待检查值
660
- * @returns 是否为 URLSearchParams
661
- * @example
662
- * ```ts
663
- * TypeUtil.isURLSearchParams(new URLSearchParams("a=1")); // true
664
- * ```
665
- */
666
- static isURLSearchParams(value) {
667
- return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.URL_SEARCH_PARAMS;
668
- }
669
- /**
670
- * 检查 value 是否为 AbortSignal
671
- * @param value 待检查值
672
- * @returns 是否为 AbortSignal
673
- * @example
674
- * ```ts
675
- * TypeUtil.isAbortSignal(new AbortController().signal); // true
676
- * ```
677
- */
678
- static isAbortSignal(value) {
679
- return this.getPrototypeString(value) === this.PROTOTYPE_TAGS.ABORT_SIGNAL;
680
- }
681
- /**
682
- * 检查 value 是否为可迭代对象 (Iterable)
683
- * @param value 待检查值
684
- * @returns 是否为 Iterable
685
- * @example
686
- * ```ts
687
- * TypeUtil.isIterable([1, 2]); // true
688
- * ```
689
- */
690
- static isIterable(value) {
691
- return !!value && typeof value[Symbol.iterator] === "function";
692
- }
693
- /**
694
- * 检查 value 是否为 Falsy 值 (false, 0, "", null, undefined, NaN, 0n)
695
- * - 处理非字符串形式的 falsy;字符串形式(`"null"`、`"0"` 等)请使用 `isFalsyLike`
696
- *
697
- * @param value 待检查值
698
- * @returns 是否为 Falsy
699
- * @example
700
- * ```ts
701
- * TypeUtil.isFalsy(0); // true
702
- * ```
703
- */
704
- static isFalsy(value) {
705
- return this.isNaN(value) || this.isNullish(value) || value === false || value === 0 || value === 0n || value === "";
706
- }
707
- /**
708
- * 检查 value 是否为 FalsyLike 值
709
- * - 包含字符串形式的 `"null"`、`"undefined"`、`"false"`、`"0"` 等
710
- *
711
- * @param value 待检查值
712
- * @returns 是否为 FalsyLike
713
- * @example
714
- * ```ts
715
- * TypeUtil.isFalsyLike("false"); // true
716
- * TypeUtil.isFalsyLike("hello"); // false
717
- * ```
718
- */
719
- static isFalsyLike(value) {
720
- if (this.isFalsy(value)) return true;
721
- return typeof value === "string" && (value === "null" || value === "undefined" || value === "NaN" || value === "false" || value === "0" || value === "-0" || value === "0n");
722
- }
723
- };
724
- //#endregion
725
- //#region src/string/stringUtil.ts
726
- /**
727
- * 字符串工具类
728
- */
729
- var StringUtil = class {
730
- static cast(candidate, checkNullish = true, trim = true) {
731
- if (checkNullish) {
732
- if (candidate === null || candidate === void 0) return "";
733
- if (typeof candidate === "string" && candidate.trim().length === 0) return "";
734
- }
735
- const result = String(candidate);
736
- return trim ? result.trim() : result;
737
- }
738
- /**
739
- * 从字符串中提取数字字符串
740
- * - 移除非数字字符,保留符号和小数点
741
- *
742
- * @param input 待处理字符串
743
- * @returns 提取出的数字字符串
744
- * @example
745
- * ```ts
746
- * StringUtil.toNumber("$1,234.56"); // "1234.56"
747
- * StringUtil.toNumber("abc-123"); // "-123"
748
- * ```
749
- */
750
- static toNumber(input) {
751
- if (!TypeUtil.isString(input, true)) return "0";
752
- const cleaned = input.replace(/[^0-9.-]/g, "");
753
- if (!cleaned) return "0";
754
- let isDecimal = false;
755
- let signCount = 0;
756
- let firstIndex = -1;
757
- const stringList = cleaned.split("").map((s, i) => {
758
- if (s === ".") {
759
- if (isDecimal) return "";
760
- isDecimal = true;
761
- return ".";
762
- }
763
- if (s === "-") {
764
- firstIndex === -1 && signCount++;
765
- return "";
766
- }
767
- firstIndex === -1 && (firstIndex = i);
768
- return s;
769
- });
770
- const sign = signCount % 2 === 1 ? "-" : "";
771
- if (firstIndex === -1) return sign + "0";
772
- let result = stringList.join("");
773
- if (result.startsWith(".")) result = "0" + result;
774
- if (result.endsWith(".")) result = result.slice(0, -1);
775
- return sign + result;
776
- }
777
- static toLowerCase(input) {
778
- if (!TypeUtil.isString(input, true)) return "";
779
- return input.toLowerCase();
780
- }
781
- static toUpperCase(input) {
782
- if (!TypeUtil.isString(input, true)) return "";
783
- return input.toUpperCase();
784
- }
785
- /**
786
- * 调整大小写
787
- * - 每个单词(`\S+`)独立处理
788
- * - 包含非西欧字母字符(如 `.`、`,`、`'`、`-`)时,该词不处理
789
- * - 纯字母且全大写时,不处理
790
- * - 纯字母且非全大写时:`caseType` 为 `"lower"` 则首字母小写,`"upper"` 则首字母大写,其余字符保留
791
- * - ⚠️ 缺省 `caseType` 时不产生任何转换(no-op),需显式传 `"lower"` / `"upper"`
792
- *
793
- * @param input 待处理字符串
794
- * @param caseType 大小写类型(缺省时无操作)
795
- * @returns 处理后的字符串
796
- * @example
797
- * ```ts
798
- * // 重载 1: lower
799
- * StringUtil.toInitialCase("Hello World", "lower"); // "hello world"
800
- *
801
- * // 重载 2: upper
802
- * StringUtil.toInitialCase("hello world", "upper"); // "Hello World"
803
- *
804
- * // 缺省 caseType → no-op
805
- * StringUtil.toInitialCase("Hello"); // "Hello"
806
- * ```
807
- */
808
- static toInitialCase(input, caseType) {
809
- if (!TypeUtil.isString(input, true)) return "";
810
- return input.replace(/\S+/g, (word) => {
811
- if (/[^a-zA-Z\u00C0-\u017F]/.test(word)) return word;
812
- if (word === word.toLocaleUpperCase()) return word;
813
- if (caseType === "lower" && word[0]) return word[0].toLocaleLowerCase() + word.slice(1);
814
- if (caseType === "upper" && word[0]) return word[0].toLocaleUpperCase() + word.slice(1);
815
- return word;
816
- });
817
- }
818
- /**
819
- * 将路径转换为 POSIX 风格
820
- * - 统一使用正斜杠 (/)
821
- * - 可选移除 Windows 盘符 (如 C:)
822
- * - 可选移除开头的斜杠
823
- * - 规范化连续斜杠为单个斜杠
824
- *
825
- * @param input 待处理字符串
826
- * @param removeLeadingSlash 是否移除开头斜杠,默认为 `false`。如果移除了盘符,路径通常会以 / 开头,此参数可控制是否保留该 /
827
- * @returns 转换后的路径,如果输入无效则返回空字符串
828
- *
829
- * @example
830
- * ```ts
831
- * StringUtil.toPosix("C:\\Windows\\System32"); // 默认: "/Windows/System32" (移除了 C: 并标准化)
832
- *
833
- * StringUtil.toPosix("C:\\Windows\\System32", true); // 移除开头斜杠: "Windows/System32"
834
- *
835
- * StringUtil.toPosix("\\\\server\\share\\file.txt"); // UNC 路径: "/server/share/file.txt"
836
- *
837
- * StringUtil.toPosix("folder\\subfolder\\file.txt"); // 相对路径: "folder/subfolder/file.txt"
838
- * ```
839
- */
840
- static toPosix(input, removeLeadingSlash = false) {
841
- if (!TypeUtil.isString(input, true)) return "";
842
- let normalized = input.replace(/^[A-Za-z]:([\\/])?/, (_, separator) => {
843
- return separator ? "/" : "";
844
- });
845
- normalized = normalized.replaceAll("\\", "/");
846
- normalized = normalized.replaceAll(/\/+/g, "/");
847
- if (removeLeadingSlash && normalized.startsWith("/")) normalized = normalized.substring(1);
848
- return normalized;
849
- }
850
- static toJson(input, fallback) {
851
- if (!TypeUtil.isString(input, true)) return fallback;
852
- try {
853
- return JSON.parse(input);
854
- } catch (error) {
855
- return fallback;
856
- }
857
- }
858
- static toValues(input, valueType = "number", splitSymbol = ",") {
859
- if (!TypeUtil.isString(input, true)) return [];
860
- try {
861
- const values = input.split(splitSymbol);
862
- if (valueType === "number") return values.map((d) => Number(d));
863
- return values;
864
- } catch (error) {
865
- return [];
866
- }
867
- }
868
- /**
869
- * 从字符串中裁切掉所有的前缀和后缀字符
870
- *
871
- * @param input 待处理字符串
872
- * @param charsToTrim 裁切字符,默认为 `" "`
873
- * @returns 裁切后的字符串
874
- * @example
875
- * ```ts
876
- * StringUtil.trim(" hello "); // "hello"
877
- * StringUtil.trim("__hello__", "_"); // "hello"
878
- * ```
879
- */
880
- static trim(input, charsToTrim = " ") {
881
- if (!TypeUtil.isString(input, true)) return "";
882
- const toTrim = charsToTrim.replace(/[\W]{1}/g, "\\$&");
883
- const regex = new RegExp(`^[${toTrim}]+|[${toTrim}]+$`, "g");
884
- return input.replace(regex, "");
885
- }
886
- /**
887
- * 截取字符串
888
- * - 支持自定义省略符,不会截断在汉字中间(因为JS字符串本身按字符处理)
889
- *
890
- * @param input 待处理字符串
891
- * @param maxLength 最大长度 (包含省略符)
892
- * @param ellipsis 省略符,默认为 `...`
893
- * @returns 截取后的字符串
894
- * @example
895
- * ```ts
896
- * StringUtil.truncate("hello world", 8); // "hello..."
897
- * ```
898
- */
899
- static truncate(input, maxLength, ellipsis = "...") {
900
- if (!TypeUtil.isString(input, true)) return "";
901
- const codePoints = Array.from(input);
902
- if (!TypeUtil.isInteger(maxLength) || maxLength < 0) return input;
903
- if (codePoints.length <= maxLength) return input;
904
- const availableLength = maxLength - ellipsis.length;
905
- if (availableLength <= 0) return "";
906
- return codePoints.slice(0, availableLength).join("") + ellipsis;
907
- }
908
- /**
909
- * 字符串模板替换
910
- * - 使用对象的属性值替换字符串中的 {{key}} 模板
911
- *
912
- * @param input 待处理字符串
913
- * @param template 模板对象
914
- * @param regex 模板匹配正则 (默认: `\{\{(.+?)\}\}`)
915
- * @returns 替换后的字符串
916
- * @example
917
- * ```ts
918
- * StringUtil.template("Hello {{name}}", { name: "World" }); // "Hello World"
919
- * ```
920
- */
921
- static template(input, template, regex = /\{\{(.+?)\}\}/g) {
922
- if (!TypeUtil.isString(input, true)) return "";
923
- regex.lastIndex = 0;
924
- const executor = regex.global ? regex : new RegExp(regex.source, `${regex.flags}g`);
925
- let result = "";
926
- let from = 0;
927
- let match;
928
- while (match = executor.exec(input)) {
929
- const replacement = template[match[1]];
930
- const valueToInsert = replacement === null || replacement === void 0 ? match[0] : replacement;
931
- result += input.slice(from, match.index) + valueToInsert;
932
- from = executor.lastIndex;
933
- }
934
- return result + input.slice(from);
935
- }
936
- /**
937
- * 字符串替换
938
- * - 替换第一个匹配项
939
- *
940
- * @param input 待处理字符串
941
- * @param search 匹配项
942
- * @param replacement 替换项
943
- * @returns 替换后的字符串
944
- * @example
945
- * ```ts
946
- * StringUtil.replace("hello world", "world", "context"); // "hello context"
947
- * ```
948
- */
949
- static replace(input, search, replacement) {
950
- if (!TypeUtil.isString(input, true)) return "";
951
- return input.replace(search, replacement);
952
- }
953
- };
954
- //#endregion
955
- //#region src/math/mathUtil.ts
956
- /**
957
- * 数学工具类
958
- * - 基于 [`mathjs`](https://mathjs.org)
959
- */
960
- var MathUtil = class MathUtil {
961
- /**
962
- * 将任意类型的值转换为 `math.bignumber`
963
- *
964
- * @param mathJsInstance mathJs 实例
965
- * @param value 任意类型的值
966
- * @param fallback 回退值
967
- * @returns 转换后的 BigNumber
968
- * @example
969
- * ```ts
970
- * import { create, all } from "mathjs";
971
- * const math = create(all);
972
- * MathUtil.toBignumber(math, "0.1");
973
- * ```
974
- */
975
- static toBignumber(mathJsInstance, value, fallback) {
976
- const errorValue = fallback ?? mathJsInstance.bignumber(0);
977
- if (TypeUtil.isFalsyLike(value) || TypeUtil.isInfinityLike(value)) return errorValue;
978
- try {
979
- return mathJsInstance.bignumber(StringUtil.toNumber(`${value}`));
980
- } catch (error) {
981
- return errorValue;
982
- }
983
- }
984
- static toDecimal(mathJsInstance, value, precision, isFormat = true) {
985
- const bigNumber = MathUtil.toBignumber(mathJsInstance, value);
986
- return isFormat ? mathJsInstance.format(bigNumber, {
987
- notation: "fixed",
988
- precision
989
- }) : bigNumber;
990
- }
991
- /**
992
- * 数学表达式求值
993
- *
994
- * ⚠️ 安全警告:mathjs `evaluate` 存在已知的沙箱逃逸向量,**仅限可信输入**(硬编码表达式或受控参数),
995
- * 切勿直接用于用户提供的表达式。
996
- *
997
- * @param mathJsInstance mathJs 实例
998
- * @param expr 表达式
999
- * @param scope 键值映射
1000
- * @returns 计算结果的字符串表示
1001
- * @example
1002
- * ```ts
1003
- * MathUtil.toEvaluate(math, "a + b", { a: 1, b: 2 }); // "3"
1004
- * ```
1005
- */
1006
- static toEvaluate(mathJsInstance, expr, scope) {
1007
- const evaluateValue = `${mathJsInstance.evaluate(expr, scope || {})}`;
1008
- return mathJsInstance.format(MathUtil.toBignumber(mathJsInstance, evaluateValue), { notation: "fixed" });
1009
- }
1010
- };
1011
- //#endregion
1012
- Object.defineProperty(exports, "MathUtil", {
1013
- enumerable: true,
1014
- get: function() {
1015
- return MathUtil;
1016
- }
1017
- });
1018
- Object.defineProperty(exports, "StringUtil", {
1019
- enumerable: true,
1020
- get: function() {
1021
- return StringUtil;
1022
- }
1023
- });
1024
- Object.defineProperty(exports, "TypeUtil", {
1025
- enumerable: true,
1026
- get: function() {
1027
- return TypeUtil;
1028
- }
1029
- });