@pawover/kit 0.0.0-beta.9 → 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.
Files changed (47) hide show
  1. package/package.json +89 -62
  2. package/packages/hooks/dist/alova.d.ts +31 -0
  3. package/packages/hooks/dist/alova.js +64 -0
  4. package/packages/hooks/dist/index.d.ts +1 -0
  5. package/packages/hooks/dist/index.js +0 -0
  6. package/packages/hooks/dist/metadata.json +16 -0
  7. package/packages/hooks/dist/react.d.ts +164 -0
  8. package/packages/hooks/dist/react.js +2112 -0
  9. package/packages/utils/dist/index.d.ts +4293 -0
  10. package/packages/utils/dist/index.js +1527 -0
  11. package/packages/utils/dist/math.d.ts +54 -0
  12. package/packages/utils/dist/math.js +56 -0
  13. package/packages/utils/dist/metadata.json +14 -0
  14. package/packages/utils/dist/string-DCWqoW4P.js +793 -0
  15. package/packages/utils/dist/vite.d.ts +16 -0
  16. package/packages/utils/dist/vite.js +26 -0
  17. package/packages/zod/dist/index.d.ts +58 -0
  18. package/packages/zod/dist/index.js +61 -0
  19. package/dist/enums.d.ts +0 -25
  20. package/dist/enums.d.ts.map +0 -1
  21. package/dist/enums.js +0 -25
  22. package/dist/enums.js.map +0 -1
  23. package/dist/hooks-alova.d.ts +0 -23
  24. package/dist/hooks-alova.d.ts.map +0 -1
  25. package/dist/hooks-alova.js +0 -39
  26. package/dist/hooks-alova.js.map +0 -1
  27. package/dist/hooks-react.d.ts +0 -95
  28. package/dist/hooks-react.d.ts.map +0 -1
  29. package/dist/hooks-react.js +0 -328
  30. package/dist/hooks-react.js.map +0 -1
  31. package/dist/index.d.ts +0 -3726
  32. package/dist/index.d.ts.map +0 -1
  33. package/dist/index.js +0 -1469
  34. package/dist/index.js.map +0 -1
  35. package/dist/patches-fetchEventSource.d.ts +0 -815
  36. package/dist/patches-fetchEventSource.d.ts.map +0 -1
  37. package/dist/patches-fetchEventSource.js +0 -316
  38. package/dist/patches-fetchEventSource.js.map +0 -1
  39. package/dist/vite.d.ts +0 -13
  40. package/dist/vite.d.ts.map +0 -1
  41. package/dist/vite.js +0 -23
  42. package/dist/vite.js.map +0 -1
  43. package/dist/zod.d.ts +0 -109
  44. package/dist/zod.d.ts.map +0 -1
  45. package/dist/zod.js +0 -143
  46. package/dist/zod.js.map +0 -1
  47. package/metadata.json +0 -167
package/dist/index.js DELETED
@@ -1,1469 +0,0 @@
1
- //#region src/utils/typeof/types.ts
2
- const PROTOTYPE_TAGS = {
3
- abortSignal: "[object AbortSignal]",
4
- array: "[object Array]",
5
- asyncFunction: "[object AsyncFunction]",
6
- asyncGeneratorFunction: "[object AsyncGeneratorFunction]",
7
- bigInt: "[object BigInt]",
8
- blob: "[object Blob]",
9
- boolean: "[object Boolean]",
10
- date: "[object Date]",
11
- error: "[object Error]",
12
- file: "[object File]",
13
- function: "[object Function]",
14
- generatorFunction: "[object GeneratorFunction]",
15
- iframe: "[object HTMLIFrameElement]",
16
- map: "[object Map]",
17
- null: "[object Null]",
18
- number: "[object Number]",
19
- object: "[object Object]",
20
- promise: "[object Promise]",
21
- readableStream: "[object ReadableStream]",
22
- regExp: "[object RegExp]",
23
- set: "[object Set]",
24
- string: "[object String]",
25
- symbol: "[object Symbol]",
26
- undefined: "[object Undefined]",
27
- URLSearchParams: "[object URLSearchParams]",
28
- weakMap: "[object WeakMap]",
29
- weakSet: "[object WeakSet]",
30
- webSocket: "[object WebSocket]",
31
- window: "[object Window]"
32
- };
33
- const TYPED_ARRAY_TAGS = new Set([
34
- "[object Int8Array]",
35
- "[object Uint8Array]",
36
- "[object Uint8ClampedArray]",
37
- "[object Int16Array]",
38
- "[object Uint16Array]",
39
- "[object Int32Array]",
40
- "[object Uint32Array]",
41
- "[object Float32Array]",
42
- "[object Float64Array]",
43
- "[object BigInt64Array]",
44
- "[object BigUint64Array]"
45
- ]);
46
- function resolvePrototypeString(value) {
47
- return Object.prototype.toString.call(value);
48
- }
49
-
50
- //#endregion
51
- //#region src/utils/typeof/isAbortSignal.ts
52
- function isAbortSignal(value) {
53
- return resolvePrototypeString(value) === PROTOTYPE_TAGS.abortSignal;
54
- }
55
-
56
- //#endregion
57
- //#region src/utils/typeof/isArray.ts
58
- function isArray(value) {
59
- return Array.isArray(value);
60
- }
61
- function isTypedArray(value) {
62
- return typeof value === "object" && value !== null && TYPED_ARRAY_TAGS.has(resolvePrototypeString(value));
63
- }
64
-
65
- //#endregion
66
- //#region src/utils/typeof/isBigInt.ts
67
- function isBigInt(value) {
68
- return typeof value === "bigint";
69
- }
70
-
71
- //#endregion
72
- //#region src/utils/typeof/isBlob.ts
73
- function isBlob(value) {
74
- if (typeof window === "undefined") return false;
75
- return resolvePrototypeString(value) === PROTOTYPE_TAGS.blob;
76
- }
77
- function isFile(value) {
78
- if (typeof window === "undefined") return false;
79
- return resolvePrototypeString(value) === PROTOTYPE_TAGS.file;
80
- }
81
-
82
- //#endregion
83
- //#region src/utils/typeof/isBoolean.ts
84
- function isBoolean(value) {
85
- return typeof value === "boolean";
86
- }
87
-
88
- //#endregion
89
- //#region src/utils/typeof/isClass.ts
90
- function isConstructable(fn) {
91
- try {
92
- Reflect.construct(fn, []);
93
- return true;
94
- } catch {
95
- return false;
96
- }
97
- }
98
- function isClass(value) {
99
- return isFunction(value) && !isAsyncFunction(value) && Function.prototype.toString.call(value).startsWith("class ") && isConstructable(value) && value.prototype !== void 0;
100
- }
101
-
102
- //#endregion
103
- //#region src/utils/typeof/isObject.ts
104
- /**
105
- * 判断是否为对象类型
106
- * - 可选是否检查原型为 `Object.prototype`,防止原型链污染
107
- *
108
- * @param value 待检查值
109
- * @param prototypeCheck 是否进行原型检查,默认 `true`
110
- */
111
- function isObject(value, prototypeCheck = true) {
112
- const check = resolvePrototypeString(value) === PROTOTYPE_TAGS.object;
113
- return prototypeCheck ? check && Object.getPrototypeOf(value) === Object.prototype : check;
114
- }
115
-
116
- //#endregion
117
- //#region src/utils/typeof/isDate.ts
118
- function isDate(value) {
119
- if (!isObject(value)) return false;
120
- try {
121
- return resolvePrototypeString(value) === PROTOTYPE_TAGS.date && typeof value["getTime"] === "function";
122
- } catch (error) {
123
- return false;
124
- }
125
- }
126
-
127
- //#endregion
128
- //#region src/utils/typeof/isEnumeration.ts
129
- /**
130
- * 判断一个值是否为有效的枚举对象
131
- * - 枚举不能为空
132
- * - 枚举所有的值必须是 string 或 number
133
- *
134
- * @param obj
135
- * @returns
136
- */
137
- function isEnumeration(obj) {
138
- if (!isObject(obj)) return [false, false];
139
- const keys = Object.keys(obj);
140
- if (!keys.length) return [false, false];
141
- const values = Object.values(obj);
142
- if (!values.every((v) => typeof v === "string" || typeof v === "number")) return [false, false];
143
- return [true, keys.every((k) => values.some((v) => v.toString() === k))];
144
- }
145
-
146
- //#endregion
147
- //#region src/utils/typeof/isEqual.ts
148
- /**
149
- * 检查给定的值是否相等
150
- * @reference https://github.com/radashi-org/radashi/blob/main/src/typed/isEqual.ts
151
- *
152
- * @param {T} x
153
- * @param {T} y
154
- */
155
- function isEqual(x, y) {
156
- if (Object.is(x, y)) return true;
157
- if (isDate(x) && isDate(y)) return x.getTime() === y.getTime();
158
- if (isRegExp(x) && isRegExp(y)) return x.toString() === y.toString();
159
- if (typeof x !== "object" || x === null || typeof y !== "object" || y === null) return false;
160
- const keysX = Reflect.ownKeys(x);
161
- const keysY = Reflect.ownKeys(y);
162
- if (keysX.length !== keysY.length) return false;
163
- for (const key of keysX) {
164
- if (!Reflect.has(y, key)) return false;
165
- if (!isEqual(x[key], y[key])) return false;
166
- }
167
- return true;
168
- }
169
-
170
- //#endregion
171
- //#region src/utils/typeof/isError.ts
172
- function isError(value) {
173
- return value instanceof Error || resolvePrototypeString(value) === PROTOTYPE_TAGS.error;
174
- }
175
-
176
- //#endregion
177
- //#region src/utils/typeof/isFalsy.ts
178
- function isFalsy(value) {
179
- if (isNaN(value) || isNull(value) || isUndefined(value)) return true;
180
- return value === false || value === 0 || value === 0n || value === "";
181
- }
182
- function isFalsyLike(value) {
183
- if (isFalsy(value)) return true;
184
- return typeof value === "string" && (value === "null" || value === "undefined" || value === "NaN" || value === "false" || value === "0" || value === "0n");
185
- }
186
-
187
- //#endregion
188
- //#region src/utils/typeof/isFunction.ts
189
- function isFunction(value) {
190
- return typeof value === "function";
191
- }
192
- function isAsyncFunction(value) {
193
- return isFunction(value) && resolvePrototypeString(value) === PROTOTYPE_TAGS.asyncFunction;
194
- }
195
- function isGeneratorFunction(value) {
196
- return isFunction(value) && resolvePrototypeString(value) === PROTOTYPE_TAGS.generatorFunction;
197
- }
198
- function isAsyncGeneratorFunction(value) {
199
- return isFunction(value) && resolvePrototypeString(value) === PROTOTYPE_TAGS.asyncGeneratorFunction;
200
- }
201
-
202
- //#endregion
203
- //#region src/utils/typeof/isIterable.ts
204
- function isIterable(value) {
205
- return isObject(value) && Symbol.iterator in value;
206
- }
207
-
208
- //#endregion
209
- //#region src/utils/typeof/isMap.ts
210
- function isMap(value) {
211
- return resolvePrototypeString(value) === PROTOTYPE_TAGS.map;
212
- }
213
- function isWeakMap(value) {
214
- return resolvePrototypeString(value) === PROTOTYPE_TAGS.weakMap;
215
- }
216
-
217
- //#endregion
218
- //#region src/utils/typeof/isNull.ts
219
- function isNull(value) {
220
- return value === null;
221
- }
222
-
223
- //#endregion
224
- //#region src/utils/typeof/isNumber.ts
225
- /**
226
- * 检查 value 是否为 number 类型
227
- *
228
- * @param value 待检查值
229
- * @param checkNaN 是否排除 `NaN`,默认为 `true`
230
- */
231
- function isNumber(value, checkNaN = true) {
232
- return typeof value === "number" && (!checkNaN || !isNaN(value));
233
- }
234
- /**
235
- * 检查 value 是否为 NaN
236
- *
237
- * @param value 待检查值
238
- */
239
- function isNaN(value) {
240
- return Number.isNaN(value);
241
- }
242
- /**
243
- * 检查 value 是否为整数
244
- *
245
- * @param value 待检查值
246
- * @param safeCheck 是否附加安全数检查
247
- */
248
- function isInteger(value, safeCheck = true) {
249
- const check = Number.isInteger(value);
250
- return safeCheck ? check && Number.isSafeInteger(value) : check;
251
- }
252
- /**
253
- * 检查 value 是否为正整数
254
- * - 此函数中 `0` 不被视为正整数
255
- *
256
- * @param value 待检查值
257
- * @param safeCheck 是否附加安全数检查
258
- */
259
- function isPositiveInteger(value, safeCheck = true) {
260
- return isInteger(value, safeCheck) && value > 0;
261
- }
262
- /**
263
- * 检查 value 是否为负整数
264
- * - 此函数中 `0` 不被视为负整数
265
- *
266
- * @param value 待检查值
267
- * @param safeCheck 是否附加安全数检查
268
- */
269
- function isNegativeInteger(value, safeCheck = true) {
270
- return isInteger(value, safeCheck) && value < 0;
271
- }
272
- /**
273
- * 检查 value 是否为 Infinity
274
- * - 排除 `NaN`
275
- *
276
- * @param value 待检查值
277
- */
278
- function isInfinity(value) {
279
- return isNumber(value) && (Number.POSITIVE_INFINITY === value || Number.NEGATIVE_INFINITY === value);
280
- }
281
- /**
282
- * 检查 value 是否类似 Infinity
283
- * - 排除 `NaN`
284
- *
285
- * @param value 待检查值
286
- */
287
- function isInfinityLike(value) {
288
- const check = isInfinity(value);
289
- if (check) return check;
290
- if (typeof value === "string") {
291
- const v = value.trim().toLowerCase();
292
- return v === "infinity" || v === "-infinity" || v === "+infinity";
293
- }
294
- return false;
295
- }
296
-
297
- //#endregion
298
- //#region src/utils/typeof/isPromise.ts
299
- function isPromise(value) {
300
- return resolvePrototypeString(value) === PROTOTYPE_TAGS.promise;
301
- }
302
- function isPromiseLike(value) {
303
- return isPromise(value) || isObject(value) && isFunction(value["then"]);
304
- }
305
-
306
- //#endregion
307
- //#region src/utils/typeof/isReadableStream.ts
308
- /**
309
- * Checks if a value is a WHATWG ReadableStream instance.
310
- *
311
- * - Uses `Object.prototype.toString` where supported (modern browsers, Node.js ≥18).
312
- * - Falls back to duck-typing in older environments.
313
- * - Resistant to basic forgery, but not 100% secure in all polyfill scenarios.
314
- *
315
- * ⚠️ Note: In older Node.js (<18) or with non-compliant polyfills, this may return false positives or negatives.
316
- */
317
- function isReadableStream(value) {
318
- if (resolvePrototypeString(value) === PROTOTYPE_TAGS.readableStream) return true;
319
- return isObject(value) && isFunction(value["getReader"]) && isFunction(value["pipeThrough"]);
320
- }
321
-
322
- //#endregion
323
- //#region src/utils/typeof/isRegExp.ts
324
- function isRegExp(value) {
325
- if (!isObject(value)) return false;
326
- try {
327
- const regex = value;
328
- return resolvePrototypeString(value) === PROTOTYPE_TAGS.regExp && isString(regex.source) && isString(regex.flags) && isBoolean(regex.global) && isFunction(regex.test);
329
- } catch (error) {
330
- return false;
331
- }
332
- }
333
-
334
- //#endregion
335
- //#region src/utils/typeof/isSet.ts
336
- function isSet(value) {
337
- return resolvePrototypeString(value) === PROTOTYPE_TAGS.set;
338
- }
339
- function isWeakSet(value) {
340
- return resolvePrototypeString(value) === PROTOTYPE_TAGS.weakSet;
341
- }
342
-
343
- //#endregion
344
- //#region src/utils/typeof/isString.ts
345
- /**
346
- * 检查 value 是否为 string 类型
347
- *
348
- * @param value 待检查值
349
- * @param checkEmpty 是否排除空字符串
350
- */
351
- function isString(value, checkEmpty = false) {
352
- return typeof value === "string" && (!checkEmpty || !!value.length);
353
- }
354
-
355
- //#endregion
356
- //#region src/utils/typeof/isSymbol.ts
357
- function isSymbol(value) {
358
- return typeof value === "symbol";
359
- }
360
-
361
- //#endregion
362
- //#region src/utils/typeof/isUndefined.ts
363
- function isUndefined(value) {
364
- return typeof value === "undefined";
365
- }
366
-
367
- //#endregion
368
- //#region src/utils/typeof/isURLSearchParams.ts
369
- function isURLSearchParams(value) {
370
- return resolvePrototypeString(value) === PROTOTYPE_TAGS.URLSearchParams;
371
- }
372
-
373
- //#endregion
374
- //#region src/utils/typeof/isWebSocket.ts
375
- function isWebSocket(value) {
376
- return resolvePrototypeString(value) === PROTOTYPE_TAGS.webSocket;
377
- }
378
-
379
- //#endregion
380
- //#region src/utils/typeof/isWindow.ts
381
- function isWindow(value) {
382
- return resolvePrototypeString(value) === PROTOTYPE_TAGS.window;
383
- }
384
-
385
- //#endregion
386
- //#region src/utils/array/arrayCast.ts
387
- /**
388
- * 构造数组
389
- * @param candidate 待构造项
390
- * @param checkEmpty 是否检查 `undefined` 和 `null`
391
- */
392
- function arrayCast(candidate, checkEmpty = true) {
393
- if (checkEmpty && (isUndefined(candidate) || isNull(candidate))) return [];
394
- return isArray(candidate) ? [...candidate] : [candidate];
395
- }
396
-
397
- //#endregion
398
- //#region src/utils/array/arrayCompete.ts
399
- /**
400
- * 数组竞争
401
- * - 返回在匹配函数的比较条件中获胜的最终项目,适用于更复杂的最小值/最大值计算
402
- *
403
- * @param initialList 数组
404
- * @param match 匹配函数
405
- */
406
- function arrayCompete(initialList, match) {
407
- if (!isArray(initialList) || initialList.length === 0 || !isFunction(match)) return null;
408
- return initialList.reduce(match);
409
- }
410
-
411
- //#endregion
412
- //#region src/utils/array/arrayCounting.ts
413
- /**
414
- * 统计数组的项目出现次数
415
- * - 通过给定的标识符匹配函数,返回一个对象,其中键是回调函数返回的 key 值,每个值是一个整数,表示该 key 出现的次数
416
- *
417
- * @param initialList 初始数组
418
- * @param match 匹配函数
419
- */
420
- function arrayCounting(initialList, match) {
421
- if (!isArray(initialList) || !isFunction(match)) return {};
422
- return initialList.reduce((prev, curr) => {
423
- const id = match(curr).toString();
424
- prev[id] = (prev[id] ?? 0) + 1;
425
- return prev;
426
- }, {});
427
- }
428
-
429
- //#endregion
430
- //#region src/utils/array/arrayDifference.ts
431
- /**
432
- * 求数组差集
433
- *
434
- * @param initialList 初始数组
435
- * @param diffList 对比数组
436
- * @param match 匹配函数
437
- */
438
- function arrayDifference(initialList, diffList, match) {
439
- if (!isArray(initialList) && !isArray(diffList)) return [];
440
- if (!isArray(initialList) || !initialList.length) return [...diffList];
441
- if (!isArray(diffList) || !diffList.length) return [...initialList];
442
- if (!isFunction(match)) {
443
- const arraySet = new Set(diffList);
444
- return Array.from(new Set(initialList.filter((item) => !arraySet.has(item))));
445
- }
446
- const map = /* @__PURE__ */ new Map();
447
- diffList.forEach((item) => {
448
- map.set(match(item), true);
449
- });
450
- return initialList.filter((a) => !map.get(match(a)));
451
- }
452
-
453
- //#endregion
454
- //#region src/utils/array/arrayFirst.ts
455
- function arrayFirst(initialList, saveValue) {
456
- if (!isArray(initialList) || initialList.length === 0) return saveValue;
457
- return initialList[0];
458
- }
459
-
460
- //#endregion
461
- //#region src/utils/array/arrayFork.ts
462
- /**
463
- * 数组分组过滤
464
- * - 给定一个数组和一个条件,返回一个由两个数组组成的元组,其中第一个数组包含所有满足条件的项,第二个数组包含所有不满足条件的项
465
- *
466
- * @param initialList 初始数组
467
- * @param match 条件匹配函数
468
- */
469
- function arrayFork(initialList, match) {
470
- const forked = [[], []];
471
- if (isArray(initialList)) for (const item of initialList) forked[match(item) ? 0 : 1].push(item);
472
- return forked;
473
- }
474
-
475
- //#endregion
476
- //#region src/utils/array/arrayIntersection.ts
477
- /**
478
- * 求数组交集
479
- *
480
- * @param initialList 初始数组
481
- * @param diffList 对比数组
482
- * @param match 匹配函数
483
- */
484
- function arrayIntersection(initialList, diffList, match) {
485
- if (!isArray(initialList) && !isArray(diffList)) return [];
486
- if (!isArray(initialList) || !initialList.length) return [...diffList];
487
- if (!isArray(diffList) || !diffList.length) return [...initialList];
488
- if (!isFunction(match)) {
489
- const arraySet = new Set(diffList);
490
- return Array.from(new Set(initialList.filter((item) => arraySet.has(item))));
491
- }
492
- const map = /* @__PURE__ */ new Map();
493
- diffList.forEach((item) => {
494
- map.set(match(item), true);
495
- });
496
- return initialList.filter((a) => map.get(match(a)));
497
- }
498
-
499
- //#endregion
500
- //#region src/utils/array/arrayLast.ts
501
- function arrayLast(initialList, saveValue) {
502
- if (!isArray(initialList) || initialList.length === 0) return saveValue;
503
- return initialList[initialList.length - 1];
504
- }
505
-
506
- //#endregion
507
- //#region src/utils/array/arrayMerge.ts
508
- /**
509
- * 数组合并
510
- * - 通过给定的标识符匹配函数,用第二个数组中的匹配项替换第一个数组中匹配项的所有内容
511
- *
512
- * @param initialList 初始数组
513
- * @param mergeList 待合并数组
514
- * @param match 匹配函数
515
- */
516
- function arrayMerge(initialList, mergeList, match) {
517
- if (!isArray(initialList)) return [];
518
- if (!isArray(mergeList)) return [...initialList];
519
- if (!isFunction(match)) return Array.from(new Set([...initialList, ...mergeList]));
520
- const keys = /* @__PURE__ */ new Map();
521
- for (const item of mergeList) keys.set(match(item), item);
522
- return initialList.map((prevItem) => {
523
- const key = match(prevItem);
524
- return keys.has(key) ? keys.get(key) : prevItem;
525
- });
526
- }
527
-
528
- //#endregion
529
- //#region src/utils/array/arrayPick.ts
530
- /**
531
- * 数组选择
532
- * - 一次性应用 `filter` 和 `map` 操作
533
- *
534
- * @param initialList 初始数组
535
- * @param filter filter 函数
536
- * @param mapper map 函数
537
- */
538
- function arrayPick(initialList, filter, mapper) {
539
- if (!isArray(initialList)) return [];
540
- if (!isFunction(filter)) return [...initialList];
541
- const hasMapper = isFunction(mapper);
542
- return initialList.reduce((prev, curr, index) => {
543
- if (!filter(curr, index)) return prev;
544
- if (hasMapper) prev.push(mapper(curr, index));
545
- else prev.push(curr);
546
- return prev;
547
- }, []);
548
- }
549
-
550
- //#endregion
551
- //#region src/utils/array/arrayReplace.ts
552
- /**
553
- * 数组项替换
554
- * - 在给定的数组中,替换符合匹配函数结果的项目。只替换第一个匹配项。始终返回原始数组的副本。
555
- *
556
- * @param initialList 初始数组
557
- * @param newItem 替换项
558
- * @param match 匹配函数
559
- */
560
- function arrayReplace(initialList, newItem, match) {
561
- if (!initialList) return [];
562
- if (newItem === void 0 || !isFunction(match)) return [...initialList];
563
- for (let i = 0; i < initialList.length; i++) {
564
- const item = initialList[i];
565
- if (item !== void 0 && match(item, i)) return [
566
- ...initialList.slice(0, i),
567
- newItem,
568
- ...initialList.slice(i + 1, initialList.length)
569
- ];
570
- }
571
- return [...initialList];
572
- }
573
-
574
- //#endregion
575
- //#region src/utils/array/arraySplit.ts
576
- /**
577
- * 数组切分
578
- * - 将数组以指定的长度切分后,组合在高维数组中
579
- *
580
- * @param initialList 初始数组
581
- * @param size 分割尺寸,默认 `10`
582
- */
583
- function arraySplit(initialList, size = 10) {
584
- if (!isArray(initialList)) return [];
585
- const count = Math.ceil(initialList.length / size);
586
- return Array.from({ length: count }).fill(null).map((_c, i) => {
587
- return initialList.slice(i * size, i * size + size);
588
- });
589
- }
590
-
591
- //#endregion
592
- //#region src/utils/device/isMobile.ts
593
- /**
594
- * 检测当前设备是否为移动设备
595
- *
596
- * @param maxWidth - 移动设备最大宽度(默认 768px)
597
- * @param dpi - 标准 DPI 基准(默认 160)
598
- */
599
- function isMobile(maxWidth = 768, dpi = 160) {
600
- if (typeof window === "undefined" || !isPositiveInteger(maxWidth)) return false;
601
- return !isTablet(maxWidth, 1200, dpi);
602
- }
603
- /**
604
- * 检测当前设备是否为IOS移动设备
605
- *
606
- * @param maxWidth - 移动设备最大宽度(默认 768px)
607
- * @param dpi - 标准 DPI 基准(默认 160)
608
- */
609
- function isISOMobile(maxWidth = 768, dpi = 160) {
610
- if (typeof navigator === "undefined" || !navigator.userAgent || !isPositiveInteger(maxWidth)) return false;
611
- return /iPhone|iPad|iPod/i.test(navigator.userAgent) && !isTablet(maxWidth, 1200, dpi);
612
- }
613
-
614
- //#endregion
615
- //#region src/utils/device/isTablet.ts
616
- /**
617
- * 检测当前设备是否为平板
618
- *
619
- * @param minWidth - 平板最小宽度(默认 768px)
620
- * @param maxWidth - 平板最大宽度(默认 1200px)
621
- * @param dpi - 标准 DPI 基准(默认 160)
622
- */
623
- function isTablet(minWidth = 768, maxWidth = 1200, dpi = 160) {
624
- if (typeof window === "undefined" || !isPositiveInteger(minWidth) || !isPositiveInteger(maxWidth)) return false;
625
- const width = window.innerWidth;
626
- const isWithinWidthRange = width >= minWidth && width <= maxWidth;
627
- try {
628
- const widthPx = window.screen.width;
629
- const heightPx = window.screen.height;
630
- const DPI = dpi * (window.devicePixelRatio || 1);
631
- const widthInch = widthPx / DPI;
632
- const heightInch = heightPx / DPI;
633
- const screenInches = Math.sqrt(widthInch ** 2 + heightInch ** 2);
634
- return isWithinWidthRange || screenInches >= 7;
635
- } catch {
636
- return isWithinWidthRange;
637
- }
638
- }
639
-
640
- //#endregion
641
- //#region src/utils/function/to.ts
642
- /**
643
- * @param promise
644
- * @param errorExt 可以传递给err对象的其他信息
645
- */
646
- function to(promise, errorExt) {
647
- return promise.then((data) => [null, data]).catch((err) => {
648
- if (errorExt) return [{
649
- ...err,
650
- ...errorExt
651
- }, void 0];
652
- return [err ? err : /* @__PURE__ */ new Error("defaultError"), void 0];
653
- });
654
- }
655
-
656
- //#endregion
657
- //#region src/utils/string/stringToNumber.ts
658
- const R1$1 = /[^0-9.-]/g;
659
- /**
660
- * 从字符串中提取数字字符串
661
- *
662
- * @param input 待处理字符串
663
- */
664
- function stringToNumber(input) {
665
- if (!isString(input, true)) return "";
666
- const cleaned = input.replace(R1$1, "");
667
- let isDecimal = false;
668
- let signCount = 0;
669
- let firstIndex = -1;
670
- const stringList = cleaned.split("").map((s, i) => {
671
- if (s === ".") {
672
- if (isDecimal) return "";
673
- isDecimal = true;
674
- return ".";
675
- }
676
- if (s === "-") {
677
- firstIndex === -1 && signCount++;
678
- return "";
679
- }
680
- firstIndex === -1 && (firstIndex = i);
681
- return s;
682
- });
683
- const sign = signCount % 2 === 1 ? "-" : "";
684
- if (firstIndex === -1) return sign + "0";
685
- let result = stringList.join("");
686
- if (result.startsWith(".")) result = "0" + result;
687
- if (result.endsWith(".")) result = result.slice(0, -1);
688
- return sign + result;
689
- }
690
-
691
- //#endregion
692
- //#region src/utils/math/toMathBignumber.ts
693
- /**
694
- * 将任意类型的值转换为 `math.bignumber`
695
- *
696
- * @param mathJsInstance mathJs 实例
697
- * @param value 任意类型的值
698
- * @param saveValue 安全值
699
- */
700
- function toMathBignumber(mathJsInstance, value, saveValue) {
701
- const errorValue = saveValue ?? mathJsInstance.bignumber(0);
702
- if (isFalsyLike(value) || isInfinityLike(value)) return errorValue;
703
- try {
704
- return mathJsInstance.bignumber(stringToNumber(`${value}`));
705
- } catch (error) {
706
- return errorValue;
707
- }
708
- }
709
-
710
- //#endregion
711
- //#region src/utils/math/toMathDecimal.ts
712
- function toMathDecimal(mathJsInstance, value, precision, isFormat = true) {
713
- const bigNumber = toMathBignumber(mathJsInstance, value);
714
- return isFormat ? mathJsInstance.format(bigNumber, {
715
- notation: "fixed",
716
- precision
717
- }) : bigNumber;
718
- }
719
-
720
- //#endregion
721
- //#region src/utils/math/toMathEvaluate.ts
722
- /**
723
- * 数学表达式求值
724
- *
725
- * @param mathJsInstance mathJs 实例
726
- * @param expr 表达式
727
- * @param scope 键值映射
728
- */
729
- function toMathEvaluate(mathJsInstance, expr, scope) {
730
- const evaluateValue = `${mathJsInstance.evaluate(expr, scope || {})}`;
731
- return mathJsInstance.format(toMathBignumber(mathJsInstance, evaluateValue), { notation: "fixed" });
732
- }
733
-
734
- //#endregion
735
- //#region src/utils/object/cloneDeep.ts
736
- const DefaultCloningStrategy = {
737
- cloneMap(input, track, clone) {
738
- const output = track(/* @__PURE__ */ new Map());
739
- for (const [key, value] of input) output.set(key, clone(value));
740
- return output;
741
- },
742
- cloneSet(input, track, clone) {
743
- const output = track(/* @__PURE__ */ new Set());
744
- for (const value of input) output.add(clone(value));
745
- return output;
746
- },
747
- cloneArray(input, track, clone) {
748
- const output = track(new Array(input.length));
749
- input.forEach((value, index) => {
750
- output[index] = clone(value);
751
- });
752
- return output;
753
- },
754
- cloneObject(input, track, clone) {
755
- const output = track(Object.create(Object.getPrototypeOf(input)));
756
- for (const key of Reflect.ownKeys(input)) {
757
- const descriptor = Object.getOwnPropertyDescriptor(input, key);
758
- if ("value" in descriptor) descriptor.value = clone(descriptor.value);
759
- Object.defineProperty(output, key, descriptor);
760
- }
761
- return output;
762
- },
763
- cloneOther(input, track) {
764
- return track(input);
765
- }
766
- };
767
- /**
768
- * cloneDeep
769
- * @reference https://github.com/radashi-org/radashi/blob/main/src/object/cloneDeep.ts
770
- */
771
- function cloneDeep(root, customStrategy) {
772
- const strategy = {
773
- ...DefaultCloningStrategy,
774
- ...customStrategy
775
- };
776
- const tracked = /* @__PURE__ */ new Map();
777
- const track = (parent, newParent) => {
778
- tracked.set(parent, newParent);
779
- return newParent;
780
- };
781
- const clone = (value) => {
782
- return value && typeof value === "object" ? tracked.get(value) ?? cloneDeep$1(value, strategy) : value;
783
- };
784
- const cloneDeep$1 = (parent, strategy$1) => {
785
- const newParent = (isObject(parent) ? strategy$1.cloneObject : isArray(parent) ? strategy$1.cloneArray : isMap(parent) ? strategy$1.cloneMap : isSet(parent) ? strategy$1.cloneSet : strategy$1.cloneOther)(parent, track.bind(null, parent), clone);
786
- if (!newParent) return cloneDeep$1(parent, DefaultCloningStrategy);
787
- tracked.set(parent, newParent);
788
- return newParent;
789
- };
790
- return cloneDeep$1(root, strategy);
791
- }
792
-
793
- //#endregion
794
- //#region src/utils/object/enumEntries.ts
795
- function enumEntries(enumeration) {
796
- const [isEnum, isTwoWayEnum] = isEnumeration(enumeration);
797
- if (!isEnum) throw Error("function enumEntries expected parameter is a enum, and requires at least one member");
798
- const entries = objectEntries(enumeration);
799
- if (isTwoWayEnum) return entries.splice(entries.length / 2, entries.length / 2);
800
- return entries;
801
- }
802
-
803
- //#endregion
804
- //#region src/utils/object/enumKeys.ts
805
- function enumKeys(enumeration) {
806
- const [isEnum, isTwoWayEnum] = isEnumeration(enumeration);
807
- if (!isEnum) throw Error("function enumKeys expected parameter is a enum, and requires at least one member");
808
- const keys = objectKeys(enumeration);
809
- if (isTwoWayEnum) return keys.splice(keys.length / 2, keys.length / 2);
810
- return keys;
811
- }
812
-
813
- //#endregion
814
- //#region src/utils/object/enumValues.ts
815
- function enumValues(enumeration) {
816
- const [isEnum, isTwoWayEnum] = isEnumeration(enumeration);
817
- if (!isEnum) throw Error("function enumValues expected parameter is a enum, and requires at least one member");
818
- const values = objectValues(enumeration);
819
- if (isTwoWayEnum) return values.splice(values.length / 2, values.length / 2);
820
- return values;
821
- }
822
-
823
- //#endregion
824
- //#region src/utils/object/objectEntries.ts
825
- function objectEntries(value) {
826
- return Object.entries(value);
827
- }
828
-
829
- //#endregion
830
- //#region src/utils/object/mapEntries.ts
831
- function mapEntries(obj, toEntry) {
832
- const defaultResult = {};
833
- if (!obj) return defaultResult;
834
- return objectEntries(obj).reduce((acc, [key, value]) => {
835
- const [newKey, newValue] = toEntry(key, value);
836
- acc[newKey] = newValue;
837
- return acc;
838
- }, defaultResult);
839
- }
840
-
841
- //#endregion
842
- //#region src/utils/object/objectAssign.ts
843
- function objectAssign(initial, override) {
844
- if (!isObject(initial) || !isObject(override)) return initial ?? override ?? {};
845
- const proto = Object.getPrototypeOf(initial);
846
- const assigned = proto ? { ...initial } : Object.assign(Object.create(proto), initial);
847
- for (const key of Object.keys(override)) assigned[key] = isObject(initial[key]) && isObject(override[key]) ? objectAssign(initial[key], override[key]) : override[key];
848
- return assigned;
849
- }
850
-
851
- //#endregion
852
- //#region src/utils/object/objectCrush.ts
853
- function objectCrush(obj) {
854
- if (!obj) return {};
855
- function crushReducer(crushed, value, path) {
856
- if (isObject(value) || isArray(value)) for (const [prop, propValue] of Object.entries(value)) crushReducer(crushed, propValue, path ? `${path}.${prop}` : prop);
857
- else crushed[path] = value;
858
- return crushed;
859
- }
860
- return crushReducer({}, obj, "");
861
- }
862
-
863
- //#endregion
864
- //#region src/utils/object/objectInvert.ts
865
- function objectInvert(obj) {
866
- const result = {};
867
- if (!isObject(obj)) return result;
868
- for (const [k, v] of objectEntries(obj)) if (isString(v) || isNumber(v) || isSymbol(v)) result[v] = k;
869
- return result;
870
- }
871
-
872
- //#endregion
873
- //#region src/utils/object/objectKeys.ts
874
- function objectKeys(value) {
875
- return Object.keys(value);
876
- }
877
-
878
- //#endregion
879
- //#region src/utils/object/objectOmit.ts
880
- function objectOmit(obj, keys) {
881
- const result = {};
882
- if (!isObject(obj)) return result;
883
- if (!isArray(keys)) return obj;
884
- const keysToOmit = new Set(keys);
885
- return Object.keys(obj).reduce((acc, key) => {
886
- if (!keysToOmit.has(key)) acc[key] = obj[key];
887
- return acc;
888
- }, result);
889
- }
890
-
891
- //#endregion
892
- //#region src/utils/object/objectPick.ts
893
- function objectPick(obj, keys) {
894
- const result = {};
895
- if (!isObject(obj)) return result;
896
- if (!isArray(keys)) return obj;
897
- return keys.reduce((acc, key) => {
898
- if (key in obj) acc[key] = obj[key];
899
- return acc;
900
- }, result);
901
- }
902
-
903
- //#endregion
904
- //#region src/utils/object/objectValues.ts
905
- function objectValues(value) {
906
- return Object.values(value);
907
- }
908
-
909
- //#endregion
910
- //#region src/utils/string/stringInitialCase.ts
911
- const R1 = /\S+/g;
912
- const R2 = /[^a-zA-Z\u00C0-\u017F]/;
913
- /**
914
- * 字符串首字母大小写
915
- * - 包含非西欧字母字符时,不处理
916
- * - 纯字母且全大写时,不处理
917
- * - 纯字母且非全大写时,首字母小写,其余保留
918
- * - 纯字母且非全大写时,首字母大写,其余保留
919
- *
920
- * @param input 待处理字符串
921
- * @param caseType 大小写类型
922
- */
923
- function stringInitialCase(input, caseType) {
924
- if (!isString(input, true)) return "";
925
- return input.replace(R1, (word) => {
926
- if (R2.test(word)) return word;
927
- if (word === word.toLocaleUpperCase()) return word;
928
- if (caseType === "lower" && word[0]) return word[0].toLocaleLowerCase() + word.slice(1);
929
- if (caseType === "upper" && word[0]) return word[0].toLocaleUpperCase() + word.slice(1);
930
- return word;
931
- });
932
- }
933
-
934
- //#endregion
935
- //#region src/utils/string/stringReplace.ts
936
- /**
937
- * 字符串替换
938
- * - 替换第一个匹配项
939
- *
940
- * @param input 待处理字符串
941
- * @param search 匹配项
942
- * @param replacement 替换项
943
- */
944
- function stringReplace(input, search, replacement) {
945
- if (!isString(input, true)) return "";
946
- return input.replace(search, replacement);
947
- }
948
-
949
- //#endregion
950
- //#region src/utils/string/stringTemplate.ts
951
- /**
952
- * 字符串模板替换
953
- *
954
- * @param input 待处理字符串
955
- * @param template 模板对象
956
- * @param regex 模板匹配正则
957
- */
958
- function stringTemplate(input, template, regex = /\{\{(.+?)\}\}/g) {
959
- if (!isString(input, true)) return "";
960
- let result = "";
961
- let from = 0;
962
- let match;
963
- while (match = regex.exec(input)) {
964
- result += input.slice(from, match.index) + template[match[1]];
965
- from = regex.lastIndex;
966
- }
967
- return result + input.slice(from);
968
- }
969
-
970
- //#endregion
971
- //#region src/utils/string/stringToJson.ts
972
- /**
973
- * 处理 JSON 字符串
974
- *
975
- * @param input 待处理字符串
976
- * @param safeValue 安全值
977
- */
978
- function stringToJson(input, safeValue) {
979
- if (!isString(input, true)) return safeValue;
980
- try {
981
- return JSON.parse(input);
982
- } catch (error) {
983
- return safeValue;
984
- }
985
- }
986
-
987
- //#endregion
988
- //#region src/utils/string/stringToPosix.ts
989
- /**
990
- * 将路径转换为 POSIX 风格
991
- *
992
- * @param input 待处理字符串
993
- * @param removeLeadingSlash 是否移除开头斜杠,默认为 `false`
994
- */
995
- function stringToPosix(input, removeLeadingSlash = false) {
996
- if (!isString(input, true)) return "";
997
- let normalized = input.replace(/^[A-Za-z]:[\\/]?/, "").replace(/^[\\/]+/, "/");
998
- normalized = normalized.replace(/\\/g, "/");
999
- normalized = normalized.replace(/\/+/g, "/");
1000
- if (removeLeadingSlash && normalized.startsWith("/")) normalized = normalized.substring(1);
1001
- return normalized;
1002
- }
1003
-
1004
- //#endregion
1005
- //#region src/utils/string/stringToValues.ts
1006
- function stringToValues(input, valueType = "number", splitSymbol = ",") {
1007
- if (!isString(input, true)) return [];
1008
- try {
1009
- const values = input.split(splitSymbol);
1010
- if (valueType === "number") return values.map((d) => Number(d));
1011
- return values;
1012
- } catch (error) {
1013
- return [];
1014
- }
1015
- }
1016
-
1017
- //#endregion
1018
- //#region src/utils/string/stringTrim.ts
1019
- /**
1020
- * 从字符串中裁切掉所有的前缀和后缀字符
1021
- *
1022
- * @param input 待处理字符串
1023
- * @param charsToTrim 裁切字符,默认为 `" "`
1024
- */
1025
- function stringTrim(input, charsToTrim = " ") {
1026
- if (!isString(input, true)) return "";
1027
- const toTrim = charsToTrim.replace(/[\W]{1}/g, "\\$&");
1028
- const regex = new RegExp(`^[${toTrim}]+|[${toTrim}]+$`, "g");
1029
- return input.replace(regex, "");
1030
- }
1031
-
1032
- //#endregion
1033
- //#region src/utils/string/stringTruncate.ts
1034
- /**
1035
- * 截取字符串
1036
- * - 支持中英文混排,不会在汉字中间截断
1037
- *
1038
- * @param input 待处理字符串
1039
- * @param maxLength 最大长度
1040
- * @param ellipsis 省略符,默认为 `...`
1041
- */
1042
- function stringTruncate(input, maxLength, ellipsis = "...") {
1043
- if (!isString(input, true)) return "";
1044
- if (!isPositiveInteger(maxLength)) return input;
1045
- if (input.length <= maxLength) return input;
1046
- const truncated = input.slice(0, maxLength - ellipsis.length);
1047
- return truncated.length > 0 ? truncated + ellipsis : "";
1048
- }
1049
-
1050
- //#endregion
1051
- //#region src/utils/time/timeZone.ts
1052
- function getTimeZone() {
1053
- const hour = 0 - (/* @__PURE__ */ new Date()).getTimezoneOffset() / 60;
1054
- return {
1055
- UTC: "UTC" + (hour >= 0 ? "+" + hour : hour),
1056
- timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone
1057
- };
1058
- }
1059
-
1060
- //#endregion
1061
- //#region src/utils/tree/types.ts
1062
- function getFinalChildrenKey(tree, meta, options) {
1063
- if (isFunction(options.getChildrenKey)) {
1064
- const dynamicChildrenKey = options.getChildrenKey(tree, meta);
1065
- if (dynamicChildrenKey && dynamicChildrenKey !== null) return dynamicChildrenKey;
1066
- }
1067
- return options.childrenKey;
1068
- }
1069
-
1070
- //#endregion
1071
- //#region src/utils/tree/treeFilter.ts
1072
- function preImpl$3(row, callback, options) {
1073
- if (!callback(row, options)) return;
1074
- const finalChildrenKey = getFinalChildrenKey(row, options, options);
1075
- const children = row[finalChildrenKey];
1076
- let newChildren;
1077
- if (isArray(children)) {
1078
- const nextLevelOptions = {
1079
- ...options,
1080
- parents: [...options.parents, row],
1081
- depth: options.depth + 1
1082
- };
1083
- newChildren = children.map((c) => preImpl$3(c, callback, nextLevelOptions)).filter((c) => !!c);
1084
- }
1085
- return {
1086
- ...row,
1087
- [finalChildrenKey]: newChildren
1088
- };
1089
- }
1090
- function postImpl$3(row, callback, options) {
1091
- const finalChildrenKey = getFinalChildrenKey(row, options, options);
1092
- const children = row[finalChildrenKey];
1093
- let newChildren;
1094
- if (isArray(children)) {
1095
- const nextLevelOptions = {
1096
- ...options,
1097
- parents: [...options.parents, row],
1098
- depth: options.depth + 1
1099
- };
1100
- newChildren = children.map((c) => preImpl$3(c, callback, nextLevelOptions)).filter((c) => !!c);
1101
- }
1102
- if (!callback(row, options)) return;
1103
- return {
1104
- ...row,
1105
- [finalChildrenKey]: newChildren
1106
- };
1107
- }
1108
- function breadthImpl$3(row, callback, options) {
1109
- const queue = [{
1110
- queueRow: row,
1111
- queueOptions: options
1112
- }];
1113
- const resultCache = /* @__PURE__ */ new WeakMap();
1114
- const newNodeCache = /* @__PURE__ */ new WeakMap();
1115
- const childrenKeyCache = /* @__PURE__ */ new WeakMap();
1116
- let result;
1117
- const runQueue = () => {
1118
- if (queue.length === 0) return result;
1119
- const { queueRow, queueOptions } = queue.shift();
1120
- const finalChildrenKey = getFinalChildrenKey(queueRow, queueOptions, queueOptions);
1121
- const children = queueRow[finalChildrenKey];
1122
- if (isArray(children)) {
1123
- const nextLevelOptions = {
1124
- ...queueOptions,
1125
- parents: [...queueOptions.parents, queueRow],
1126
- depth: queueOptions.depth + 1
1127
- };
1128
- const subQueueItems = children.map((queueRow$1) => ({
1129
- queueRow: queueRow$1,
1130
- queueOptions: nextLevelOptions
1131
- }));
1132
- queue.push(...subQueueItems);
1133
- }
1134
- const parent = arrayLast(queueOptions.parents);
1135
- const isTopNode = queueOptions.depth === 0;
1136
- const parentResult = parent && resultCache.get(parent);
1137
- if (!isTopNode && !parentResult) return runQueue();
1138
- const callbackResult = callback(queueRow, queueOptions);
1139
- if (isTopNode && !callbackResult) return;
1140
- const newNode = {
1141
- ...queueRow,
1142
- [finalChildrenKey]: void 0
1143
- };
1144
- if (isTopNode) result = newNode;
1145
- resultCache.set(queueRow, callbackResult);
1146
- newNodeCache.set(queueRow, newNode);
1147
- childrenKeyCache.set(queueRow, finalChildrenKey);
1148
- if (callbackResult && parent) {
1149
- const parentNewNode = newNodeCache.get(parent);
1150
- const parentChildrenKey = childrenKeyCache.get(parent);
1151
- if (parentNewNode && parentChildrenKey) {
1152
- if (!parentNewNode[parentChildrenKey]) parentNewNode[parentChildrenKey] = [];
1153
- parentNewNode[parentChildrenKey].push(newNode);
1154
- }
1155
- }
1156
- return runQueue();
1157
- };
1158
- return runQueue();
1159
- }
1160
- const strategies$3 = {
1161
- pre: preImpl$3,
1162
- post: postImpl$3,
1163
- breadth: breadthImpl$3
1164
- };
1165
- function treeFilter(tree, callback, options = {}) {
1166
- const { childrenKey = "children", strategy = "pre", getChildrenKey } = options;
1167
- const traversalMethod = strategies$3[strategy];
1168
- const innerOptions = {
1169
- childrenKey,
1170
- depth: 0,
1171
- parents: [],
1172
- getChildrenKey
1173
- };
1174
- return isArray(tree) ? tree.map((row) => traversalMethod(row, callback, innerOptions)).filter((t) => !!t) : traversalMethod(tree, callback, innerOptions) || [];
1175
- }
1176
-
1177
- //#endregion
1178
- //#region src/utils/tree/treeFind.ts
1179
- const strategies$2 = {
1180
- pre: preImpl$2,
1181
- post: postImpl$2,
1182
- breadth: breadthImpl$2
1183
- };
1184
- function preImpl$2(row, callback, options) {
1185
- if (callback(row, options)) return row;
1186
- const children = row[getFinalChildrenKey(row, options, options)];
1187
- if (isArray(children)) for (const child of children) {
1188
- const result = preImpl$2(child, callback, {
1189
- ...options,
1190
- parents: [...options.parents, row],
1191
- depth: options.depth + 1
1192
- });
1193
- if (result) return result;
1194
- }
1195
- }
1196
- function postImpl$2(row, callback, options) {
1197
- const children = row[getFinalChildrenKey(row, options, options)];
1198
- if (isArray(children)) for (const child of children) {
1199
- const result = postImpl$2(child, callback, {
1200
- ...options,
1201
- parents: [...options.parents, row],
1202
- depth: options.depth + 1
1203
- });
1204
- if (result) return result;
1205
- }
1206
- if (callback(row, options)) return row;
1207
- }
1208
- function breadthImpl$2(row, callback, options) {
1209
- const queue = [{
1210
- queueRow: row,
1211
- queueOptions: options
1212
- }];
1213
- const runQueue = () => {
1214
- if (queue.length === 0) return;
1215
- const { queueRow, queueOptions } = queue.shift();
1216
- const children = queueRow[getFinalChildrenKey(queueRow, queueOptions, queueOptions)];
1217
- if (isArray(children)) {
1218
- const nextLevelOptions = {
1219
- ...queueOptions,
1220
- parents: [...queueOptions.parents, queueRow],
1221
- depth: queueOptions.depth + 1
1222
- };
1223
- const subQueueItems = children.map((queueRow$1) => ({
1224
- queueRow: queueRow$1,
1225
- queueOptions: nextLevelOptions
1226
- }));
1227
- queue.push(...subQueueItems);
1228
- }
1229
- if (callback(queueRow, queueOptions)) return queueRow;
1230
- return runQueue();
1231
- };
1232
- return runQueue();
1233
- }
1234
- /**
1235
- * 查找树节点,找到第一个返回非空值的节点
1236
- */
1237
- function treeFind(tree, callback, options = {}) {
1238
- const { childrenKey = "children", strategy = "pre", getChildrenKey } = options;
1239
- const traversalMethod = strategies$2[strategy];
1240
- const innerOptions = {
1241
- childrenKey,
1242
- depth: 0,
1243
- parents: [],
1244
- getChildrenKey
1245
- };
1246
- if (isArray(tree)) {
1247
- for (const row of tree) {
1248
- const result = traversalMethod(row, callback, innerOptions);
1249
- if (result) return result;
1250
- }
1251
- return;
1252
- }
1253
- return traversalMethod(tree, callback, innerOptions);
1254
- }
1255
-
1256
- //#endregion
1257
- //#region src/utils/tree/treeForEach.ts
1258
- const strategies$1 = {
1259
- pre: preImpl$1,
1260
- post: postImpl$1,
1261
- breadth: breadthImpl$1
1262
- };
1263
- function preImpl$1(row, callback, options) {
1264
- callback(row, options);
1265
- const children = row[getFinalChildrenKey(row, options, options)];
1266
- if (isArray(children)) {
1267
- const nextLevelOptions = {
1268
- ...options,
1269
- parents: [...options.parents, row],
1270
- depth: options.depth + 1
1271
- };
1272
- for (const child of children) preImpl$1(child, callback, nextLevelOptions);
1273
- }
1274
- }
1275
- function postImpl$1(row, callback, options) {
1276
- const children = row[getFinalChildrenKey(row, options, options)];
1277
- if (isArray(children)) {
1278
- const nextLevelOptions = {
1279
- ...options,
1280
- parents: [...options.parents, row],
1281
- depth: options.depth + 1
1282
- };
1283
- for (const child of children) postImpl$1(child, callback, nextLevelOptions);
1284
- }
1285
- callback(row, options);
1286
- }
1287
- function breadthImpl$1(row, callback, options) {
1288
- const queue = [{
1289
- queueRow: row,
1290
- queueOptions: options
1291
- }];
1292
- const runQueue = () => {
1293
- if (queue.length === 0) return;
1294
- const { queueRow, queueOptions } = queue.shift();
1295
- const children = queueRow[getFinalChildrenKey(queueRow, queueOptions, queueOptions)];
1296
- if (isArray(children)) {
1297
- const nextLevelOptions = {
1298
- ...queueOptions,
1299
- parents: [...queueOptions.parents, queueRow],
1300
- depth: queueOptions.depth + 1
1301
- };
1302
- const subQueueItems = children.map((queueRow$1) => ({
1303
- queueRow: queueRow$1,
1304
- queueOptions: nextLevelOptions
1305
- }));
1306
- queue.push(...subQueueItems);
1307
- }
1308
- callback(queueRow, queueOptions);
1309
- runQueue();
1310
- };
1311
- runQueue();
1312
- }
1313
- function treeForEach(tree, callback, options = {}) {
1314
- const { childrenKey = "children", strategy = "pre", getChildrenKey } = options;
1315
- const traversalMethod = strategies$1[strategy];
1316
- const innerOptions = {
1317
- childrenKey,
1318
- depth: 0,
1319
- parents: [],
1320
- getChildrenKey
1321
- };
1322
- if (isArray(tree)) for (const row of tree) traversalMethod(row, callback, innerOptions);
1323
- else traversalMethod(tree, callback, innerOptions);
1324
- }
1325
-
1326
- //#endregion
1327
- //#region src/utils/tree/treeMap.ts
1328
- const strategies = {
1329
- pre: preImpl,
1330
- post: postImpl,
1331
- breadth: breadthImpl
1332
- };
1333
- function preImpl(row, callback, options) {
1334
- const finalChildrenKey = getFinalChildrenKey(row, options, options);
1335
- const result = callback(row, options);
1336
- const children = row[finalChildrenKey];
1337
- let newChildren;
1338
- if (isArray(children)) {
1339
- const nextLevelOptions = {
1340
- ...options,
1341
- parents: [...options.parents, row],
1342
- depth: options.depth + 1
1343
- };
1344
- newChildren = children.map((c) => preImpl(c, callback, nextLevelOptions));
1345
- }
1346
- return {
1347
- ...result,
1348
- [finalChildrenKey]: newChildren
1349
- };
1350
- }
1351
- function postImpl(row, callback, options) {
1352
- const finalChildrenKey = getFinalChildrenKey(row, options, options);
1353
- const children = row[finalChildrenKey];
1354
- let newChildren;
1355
- if (isArray(children)) {
1356
- const nextLevelOptions = {
1357
- ...options,
1358
- parents: [...options.parents, row],
1359
- depth: options.depth + 1
1360
- };
1361
- newChildren = children.map((c) => postImpl(c, callback, nextLevelOptions));
1362
- }
1363
- return {
1364
- ...callback(row, options),
1365
- [finalChildrenKey]: newChildren
1366
- };
1367
- }
1368
- function breadthImpl(row, callback, options) {
1369
- const queue = [{
1370
- queueRow: row,
1371
- queueOptions: options
1372
- }];
1373
- const cache = /* @__PURE__ */ new WeakMap();
1374
- const childrenKeyCache = /* @__PURE__ */ new WeakMap();
1375
- let result;
1376
- const runQueue = () => {
1377
- if (queue.length === 0) return result;
1378
- const { queueRow, queueOptions } = queue.shift();
1379
- const finalChildrenKey = getFinalChildrenKey(queueRow, queueOptions, queueOptions);
1380
- const children = queueRow[finalChildrenKey];
1381
- if (isArray(children)) {
1382
- const nextLevelOptions = {
1383
- ...queueOptions,
1384
- parents: [...queueOptions.parents, queueRow],
1385
- depth: queueOptions.depth + 1
1386
- };
1387
- const subQueueItems = children.map((queueRow$1) => ({
1388
- queueRow: queueRow$1,
1389
- queueOptions: nextLevelOptions
1390
- }));
1391
- queue.push(...subQueueItems);
1392
- }
1393
- const res = callback(queueRow, queueOptions);
1394
- cache.set(queueRow, res);
1395
- childrenKeyCache.set(queueRow, finalChildrenKey);
1396
- const parent = arrayLast(queueOptions.parents);
1397
- if (parent) {
1398
- const newParent = cache.get(parent);
1399
- const parentChildrenKey = childrenKeyCache.get(parent);
1400
- if (newParent && parentChildrenKey) if (newParent[parentChildrenKey]) newParent[parentChildrenKey].push(res);
1401
- else newParent[parentChildrenKey] = [res];
1402
- }
1403
- if (queueOptions.depth === 0) result = res;
1404
- return runQueue();
1405
- };
1406
- return runQueue();
1407
- }
1408
- function treeMap(tree, callback, options = {}) {
1409
- const { childrenKey = "children", strategy = "pre", getChildrenKey } = options;
1410
- const traversalMethod = strategies[strategy];
1411
- const innerOptions = {
1412
- childrenKey,
1413
- depth: 0,
1414
- parents: [],
1415
- getChildrenKey
1416
- };
1417
- return isArray(tree) ? tree.map((row) => traversalMethod(row, callback, innerOptions)) : traversalMethod(tree, callback, innerOptions);
1418
- }
1419
-
1420
- //#endregion
1421
- //#region src/utils/tree/rowsToTree.ts
1422
- /**
1423
- * 行结构 转 树结构
1424
- */
1425
- function rowsToTree(rows, options) {
1426
- const { parentIdKey = "parentId", rowKey = "id", childrenKey = "children" } = options || {};
1427
- const result = [];
1428
- const map = /* @__PURE__ */ new Map();
1429
- for (const row of rows) {
1430
- const id = row[rowKey];
1431
- if (!map.get(id)) map.set(id, row);
1432
- }
1433
- for (const row of rows) {
1434
- const parentId = row[parentIdKey];
1435
- const parent = map.get(parentId);
1436
- if (!parent || !parentId) {
1437
- result.push(row);
1438
- continue;
1439
- }
1440
- const siblings = parent[childrenKey];
1441
- if (isNull(siblings) || isUndefined(siblings)) parent[childrenKey] = [row];
1442
- else if (Array.isArray(siblings)) siblings.push(row);
1443
- else {
1444
- const message = `The key "${childrenKey.toString()}" in parent item is not an array.`;
1445
- throw new Error(message);
1446
- }
1447
- }
1448
- return result;
1449
- }
1450
-
1451
- //#endregion
1452
- //#region src/utils/tree/treeToRows.ts
1453
- /**
1454
- * 树结构 转 行结构
1455
- */
1456
- function treeToRows(tree, options = {}) {
1457
- const { childrenKey = "children" } = options;
1458
- const result = [];
1459
- if (!tree) return result;
1460
- treeForEach(tree, (t) => result.push({
1461
- ...t,
1462
- [childrenKey]: void 0
1463
- }), options);
1464
- return result;
1465
- }
1466
-
1467
- //#endregion
1468
- export { arrayCast, arrayCompete, arrayCounting, arrayDifference, arrayFirst, arrayFork, arrayIntersection, arrayLast, arrayMerge, arrayPick, arrayReplace, arraySplit, cloneDeep, enumEntries, enumKeys, enumValues, getTimeZone, isAbortSignal, isArray, isAsyncFunction, isAsyncGeneratorFunction, isBigInt, isBlob, isBoolean, isClass, isDate, isEnumeration, isEqual, isError, isFalsy, isFalsyLike, isFile, isFunction, isGeneratorFunction, isISOMobile, isInfinity, isInfinityLike, isInteger, isIterable, isMap, isMobile, isNaN, isNegativeInteger, isNull, isNumber, isObject, isPositiveInteger, isPromise, isPromiseLike, isReadableStream, isRegExp, isSet, isString, isSymbol, isTablet, isTypedArray, isURLSearchParams, isUndefined, isWeakMap, isWeakSet, isWebSocket, isWindow, mapEntries, objectAssign, objectCrush, objectEntries, objectInvert, objectKeys, objectOmit, objectPick, objectValues, rowsToTree, stringInitialCase, stringReplace, stringTemplate, stringToJson, stringToNumber, stringToPosix, stringToValues, stringTrim, stringTruncate, to, toMathBignumber, toMathDecimal, toMathEvaluate, treeFilter, treeFind, treeForEach, treeMap, treeToRows };
1469
- //# sourceMappingURL=index.js.map