@pawover/kit 0.0.0-beta.45 → 0.0.0-beta.50
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 +52 -82
- package/{dist/hooks-alova.d.ts → packages/hooks/dist/alova.d.ts} +4 -5
- package/{dist/hooks-alova.js → packages/hooks/dist/alova.js} +3 -5
- 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/{dist/hooks-react.d.ts → packages/hooks/dist/react.d.ts} +76 -72
- package/packages/hooks/dist/react.js +4419 -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-CESQdidv.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 -2
- package/dist/enums.js +0 -145
- package/dist/enums.js.map +0 -1
- package/dist/except-6l9Qdmn1.d.ts +0 -986
- package/dist/except-6l9Qdmn1.d.ts.map +0 -1
- package/dist/hooks-alova.d.ts.map +0 -1
- package/dist/hooks-alova.js.map +0 -1
- package/dist/hooks-react.d.ts.map +0 -1
- package/dist/hooks-react.js +0 -166
- package/dist/hooks-react.js.map +0 -1
- package/dist/index-DsR_kNCf.d.ts +0 -18
- package/dist/index-DsR_kNCf.d.ts.map +0 -1
- package/dist/index-JKtXbRi8.d.ts +0 -149
- package/dist/index-JKtXbRi8.d.ts.map +0 -1
- package/dist/index.d.ts +0 -3736
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -2
- package/dist/patches-fetchEventSource.d.ts +0 -87
- package/dist/patches-fetchEventSource.d.ts.map +0 -1
- package/dist/patches-fetchEventSource.js +0 -200
- package/dist/patches-fetchEventSource.js.map +0 -1
- package/dist/utils-B7AhGrZp.js +0 -2042
- package/dist/utils-B7AhGrZp.js.map +0 -1
- package/dist/value-of-Dz22arsm.d.ts +0 -26
- package/dist/value-of-Dz22arsm.d.ts.map +0 -1
- package/dist/vite.d.ts +0 -12
- 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 -112
- package/dist/zod.d.ts.map +0 -1
- package/dist/zod.js +0 -112
- package/dist/zod.js.map +0 -1
- package/metadata.json +0 -184
|
@@ -0,0 +1,1527 @@
|
|
|
1
|
+
import { n as TypeUtil, r as _defineProperty, t as StringUtil } from "./string-CESQdidv.js";
|
|
2
|
+
//#region src/array/arrayUtil.ts
|
|
3
|
+
/**
|
|
4
|
+
* 数组工具类
|
|
5
|
+
*/
|
|
6
|
+
var ArrayUtil = class {
|
|
7
|
+
static cast(candidate, checkEmpty = true) {
|
|
8
|
+
if (checkEmpty && (TypeUtil.isUndefined(candidate) || TypeUtil.isNull(candidate))) return [];
|
|
9
|
+
return TypeUtil.isArray(candidate) ? [...candidate] : [candidate];
|
|
10
|
+
}
|
|
11
|
+
static first(initialList, fallback) {
|
|
12
|
+
if (!TypeUtil.isArray(initialList) || initialList.length === 0) return fallback;
|
|
13
|
+
return initialList[0];
|
|
14
|
+
}
|
|
15
|
+
static last(initialList, fallback) {
|
|
16
|
+
if (!TypeUtil.isArray(initialList) || initialList.length === 0) return fallback;
|
|
17
|
+
return initialList[initialList.length - 1];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 数组竞选
|
|
21
|
+
* - 返回在匹配函数的比较条件中获胜的最终项目,适用于更复杂的最小值/最大值计算
|
|
22
|
+
*
|
|
23
|
+
* @param initialList 数组
|
|
24
|
+
* @param match 匹配函数
|
|
25
|
+
* @returns 获胜的元素,如果数组为空或参数无效则返回 `null`
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* const list = [1, 10, 5];
|
|
29
|
+
* ArrayUtil.compete(list, (a, b) => (a > b ? a : b)); // 10
|
|
30
|
+
* ArrayUtil.compete(list, (a, b) => (a < b ? a : b)); // 1
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
static compete(initialList, match) {
|
|
34
|
+
if (!TypeUtil.isArray(initialList) || initialList.length === 0 || !TypeUtil.isFunction(match)) return null;
|
|
35
|
+
return initialList.reduce(match);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 统计数组的项目出现次数
|
|
39
|
+
* - 通过给定的标识符匹配函数,返回一个对象,其中键是回调函数返回的 key 值,每个值是一个整数,表示该 key 出现的次数
|
|
40
|
+
*
|
|
41
|
+
* @param initialList 初始数组
|
|
42
|
+
* @param match 匹配函数
|
|
43
|
+
* @returns 统计对象
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* const list = ["a", "b", "a", "c"];
|
|
47
|
+
* ArrayUtil.count(list, (x) => x); // { a: 2, b: 1, c: 1 }
|
|
48
|
+
*
|
|
49
|
+
* const users = [{ id: 1, group: "A" }, { id: 2, group: "B" }, { id: 3, group: "A" }];
|
|
50
|
+
* ArrayUtil.count(users, (u) => u.group); // { A: 2, B: 1 }
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
static count(initialList, match) {
|
|
54
|
+
if (!TypeUtil.isArray(initialList) || !TypeUtil.isFunction(match)) return {};
|
|
55
|
+
return initialList.reduce((prev, curr, index) => {
|
|
56
|
+
const id = match(curr, index).toString();
|
|
57
|
+
prev[id] = (prev[id] ?? 0) + 1;
|
|
58
|
+
return prev;
|
|
59
|
+
}, {});
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* 获取数组差集
|
|
63
|
+
* - 返回在 `initialList` 中存在,但在 `diffList` 中不存在的元素
|
|
64
|
+
*
|
|
65
|
+
* @param initialList 初始数组
|
|
66
|
+
* @param diffList 对比数组
|
|
67
|
+
* @param match 匹配函数
|
|
68
|
+
* @returns 差集数组
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* ArrayUtil.difference([1, 2, 3], [2, 3, 4]); // [1]
|
|
72
|
+
* ArrayUtil.difference([{ id: 1 }, { id: 2 }], [{ id: 2 }], (x) => x.id); // [{ id: 1 }]
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
static difference(initialList, diffList, match) {
|
|
76
|
+
if (!TypeUtil.isArray(initialList) && !TypeUtil.isArray(diffList)) return [];
|
|
77
|
+
if (!TypeUtil.isArray(initialList) || !initialList.length) return [];
|
|
78
|
+
if (!TypeUtil.isArray(diffList) || !diffList.length) return [...initialList];
|
|
79
|
+
if (!TypeUtil.isFunction(match)) {
|
|
80
|
+
const arraySet = new Set(diffList);
|
|
81
|
+
return Array.from(new Set(initialList.filter((item) => !arraySet.has(item))));
|
|
82
|
+
}
|
|
83
|
+
const map = /* @__PURE__ */ new Map();
|
|
84
|
+
diffList.forEach((item, index) => {
|
|
85
|
+
map.set(match(item, index), true);
|
|
86
|
+
});
|
|
87
|
+
return initialList.filter((item, index) => !map.get(match(item, index)));
|
|
88
|
+
}
|
|
89
|
+
static intersection(initialList, diffList, match) {
|
|
90
|
+
if (!TypeUtil.isArray(initialList) || !TypeUtil.isArray(diffList)) return [];
|
|
91
|
+
if (!initialList.length || !diffList.length) return [];
|
|
92
|
+
if (!TypeUtil.isFunction(match)) {
|
|
93
|
+
const diffSet = new Set(diffList);
|
|
94
|
+
return initialList.filter((item) => diffSet.has(item));
|
|
95
|
+
}
|
|
96
|
+
const diffKeys = new Set(diffList.map((item, index) => match(item, index)));
|
|
97
|
+
return initialList.filter((item, index) => diffKeys.has(match(item, index)));
|
|
98
|
+
}
|
|
99
|
+
static merge(initialList, mergeList, match) {
|
|
100
|
+
if (!TypeUtil.isArray(initialList)) return [];
|
|
101
|
+
if (!TypeUtil.isArray(mergeList)) return [...initialList];
|
|
102
|
+
if (!TypeUtil.isFunction(match)) return Array.from(new Set([...initialList, ...mergeList]));
|
|
103
|
+
const keys = /* @__PURE__ */ new Map();
|
|
104
|
+
mergeList.forEach((item, index) => {
|
|
105
|
+
keys.set(match(item, index), item);
|
|
106
|
+
});
|
|
107
|
+
return initialList.map((prevItem, index) => {
|
|
108
|
+
const key = match(prevItem, index);
|
|
109
|
+
return keys.has(key) ? keys.get(key) : prevItem;
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
static pick(initialList, filter, mapper) {
|
|
113
|
+
if (!TypeUtil.isArray(initialList)) return [];
|
|
114
|
+
if (!TypeUtil.isFunction(filter)) return [...initialList];
|
|
115
|
+
const hasMapper = TypeUtil.isFunction(mapper);
|
|
116
|
+
return initialList.reduce((prev, curr, index) => {
|
|
117
|
+
if (!filter(curr, index)) return prev;
|
|
118
|
+
if (hasMapper) prev.push(mapper(curr, index));
|
|
119
|
+
else prev.push(curr);
|
|
120
|
+
return prev;
|
|
121
|
+
}, []);
|
|
122
|
+
}
|
|
123
|
+
static replace(initialList, newItem, match) {
|
|
124
|
+
if (!TypeUtil.isArray(initialList) || !initialList.length) return [];
|
|
125
|
+
if (!TypeUtil.isFunction(match)) return [...initialList];
|
|
126
|
+
for (let i = 0; i < initialList.length; i++) {
|
|
127
|
+
const item = initialList[i];
|
|
128
|
+
if (match(item, i)) return [
|
|
129
|
+
...initialList.slice(0, i),
|
|
130
|
+
newItem,
|
|
131
|
+
...initialList.slice(i + 1, initialList.length)
|
|
132
|
+
];
|
|
133
|
+
}
|
|
134
|
+
return [...initialList];
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* 数组项替换并移动
|
|
138
|
+
* - 在给定的数组中,替换并移动符合匹配函数结果的项目
|
|
139
|
+
* - 只替换和移动第一个匹配项
|
|
140
|
+
* - 未匹配时,根据 `position` 在指定位置插入 `newItem`
|
|
141
|
+
*
|
|
142
|
+
* @param initialList 初始数组
|
|
143
|
+
* @param newItem 替换项
|
|
144
|
+
* @param match 匹配函数
|
|
145
|
+
* @param position 移动位置,可选 `start` | `end` | 索引位置, 默认为 `end`
|
|
146
|
+
* @returns
|
|
147
|
+
* @example
|
|
148
|
+
* ```ts
|
|
149
|
+
* ArrayUtil.replaceMove([1, 2, 3, 4], 5, (n) => n === 2, 0); // [5, 1, 3, 4]
|
|
150
|
+
* ArrayUtil.replaceMove([1, 2, 3, 4], 5, (n) => n === 2, 2); // [1, 3, 5, 4]
|
|
151
|
+
* ArrayUtil.replaceMove([1, 2, 3, 4], 5, (n) => n === 2, "start"); // [5, 1, 3, 4]
|
|
152
|
+
* ArrayUtil.replaceMove([1, 2, 3, 4], 5, (n) => n === 2); // [1, 3, 4, 5]
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
static replaceMove(initialList, newItem, match, position) {
|
|
156
|
+
if (!TypeUtil.isArray(initialList)) return [];
|
|
157
|
+
if (!initialList.length) return [newItem];
|
|
158
|
+
if (!TypeUtil.isFunction(match)) return [...initialList];
|
|
159
|
+
const result = [...initialList];
|
|
160
|
+
const matchIndex = initialList.findIndex(match);
|
|
161
|
+
if (matchIndex !== -1) result.splice(matchIndex, 1);
|
|
162
|
+
if (position === "start") result.unshift(newItem);
|
|
163
|
+
else if (position === 0 || TypeUtil.isPositiveInteger(position, false)) result.splice(Math.min(position, result.length), 0, newItem);
|
|
164
|
+
else result.push(newItem);
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* 数组切分
|
|
169
|
+
* - 将数组以指定的长度切分后,组合在高维数组中
|
|
170
|
+
*
|
|
171
|
+
* @param initialList 初始数组
|
|
172
|
+
* @param size 分割尺寸,默认 `10`
|
|
173
|
+
* @returns 切分后的二维数组
|
|
174
|
+
* @example
|
|
175
|
+
* ```ts
|
|
176
|
+
* ArrayUtil.split([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
static split(initialList, size = 10) {
|
|
180
|
+
if (!TypeUtil.isArray(initialList)) return [];
|
|
181
|
+
if (!TypeUtil.isPositiveInteger(size, false)) return [];
|
|
182
|
+
const count = Math.ceil(initialList.length / size);
|
|
183
|
+
return Array.from({ length: count }).fill(null).map((_c, i) => {
|
|
184
|
+
return initialList.slice(i * size, i * size + size);
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* 数组分组过滤
|
|
189
|
+
* - 给定一个数组和一个条件,返回一个由两个数组组成的元组,其中第一个数组包含所有满足条件的项,第二个数组包含所有不满足条件的项
|
|
190
|
+
*
|
|
191
|
+
* @param initialList 初始数组
|
|
192
|
+
* @param match 条件匹配函数
|
|
193
|
+
* @returns [满足条件的项[], 不满足条件的项[]]
|
|
194
|
+
* @example
|
|
195
|
+
* ```ts
|
|
196
|
+
* ArrayUtil.fork([1, 2, 3, 4], (n) => n % 2 === 0); // [[2, 4], [1, 3]]
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
static fork(initialList, match) {
|
|
200
|
+
const forked = [[], []];
|
|
201
|
+
if (TypeUtil.isArray(initialList)) initialList.forEach((item, index) => {
|
|
202
|
+
forked[match(item, index) ? 0 : 1].push(item);
|
|
203
|
+
});
|
|
204
|
+
return forked;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* 数组解压
|
|
208
|
+
* - `ArrayUtil.zip` 的反向操作
|
|
209
|
+
*
|
|
210
|
+
* @param arrayList 压缩后的数组
|
|
211
|
+
* @returns 解压后的二维数组
|
|
212
|
+
* @example
|
|
213
|
+
* ```ts
|
|
214
|
+
* ArrayUtil.unzip([[1, "a"], [2, "b"]]); // [[1, 2], ["a", "b"]]
|
|
215
|
+
* ```
|
|
216
|
+
*/
|
|
217
|
+
static unzip(arrayList) {
|
|
218
|
+
if (!TypeUtil.isArray(arrayList) || !arrayList.length) return [];
|
|
219
|
+
const out = new Array(arrayList.reduce((max, arr) => Math.max(max, arr.length), 0));
|
|
220
|
+
let index = 0;
|
|
221
|
+
const get = (array) => array[index];
|
|
222
|
+
for (; index < out.length; index++) out[index] = Array.from(arrayList, get);
|
|
223
|
+
return out;
|
|
224
|
+
}
|
|
225
|
+
static zip(...arrays) {
|
|
226
|
+
return this.unzip(arrays);
|
|
227
|
+
}
|
|
228
|
+
static zipToObject(keys, values) {
|
|
229
|
+
const result = {};
|
|
230
|
+
if (!TypeUtil.isArray(keys) || !keys.length) return result;
|
|
231
|
+
const getValue = TypeUtil.isFunction(values) ? values : TypeUtil.isArray(values) ? (_k, i) => values[i] : (_k, _i) => values;
|
|
232
|
+
return keys.reduce((acc, key, idx) => {
|
|
233
|
+
acc[key] = getValue(key, idx);
|
|
234
|
+
return acc;
|
|
235
|
+
}, result);
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
//#endregion
|
|
239
|
+
//#region src/dateTime/dateTimeUtil.ts
|
|
240
|
+
var _DateTimeUtil;
|
|
241
|
+
/**
|
|
242
|
+
* 日期工具类
|
|
243
|
+
*/
|
|
244
|
+
var DateTimeUtil = class {
|
|
245
|
+
/**
|
|
246
|
+
* 获取当前时区信息
|
|
247
|
+
*
|
|
248
|
+
* @returns 时区信息对象 (UTC偏移和时区名称)
|
|
249
|
+
* @example
|
|
250
|
+
* ```ts
|
|
251
|
+
* DateTimeUtil.getTimeZone(); // { UTC: "UTC+8", timeZone: "Asia/Shanghai" }
|
|
252
|
+
* ```
|
|
253
|
+
*/
|
|
254
|
+
static getTimeZone() {
|
|
255
|
+
const hour = 0 - (/* @__PURE__ */ new Date()).getTimezoneOffset() / this.MINUTE_PER_HOUR;
|
|
256
|
+
return {
|
|
257
|
+
UTC: "UTC" + (hour >= 0 ? "+" + hour : hour),
|
|
258
|
+
timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
_DateTimeUtil = DateTimeUtil;
|
|
263
|
+
_defineProperty(DateTimeUtil, "MILLISECONDS_PER_SECOND", 1e3);
|
|
264
|
+
_defineProperty(DateTimeUtil, "SECOND_PER_MINUTE", 60);
|
|
265
|
+
_defineProperty(DateTimeUtil, "MINUTE_PER_HOUR", 60);
|
|
266
|
+
_defineProperty(DateTimeUtil, "SECOND_PER_HOUR", _DateTimeUtil.SECOND_PER_MINUTE ** 2);
|
|
267
|
+
_defineProperty(DateTimeUtil, "HOUR_PER_DAY", 24);
|
|
268
|
+
_defineProperty(DateTimeUtil, "SECOND_PER_DAY", _DateTimeUtil.SECOND_PER_HOUR * _DateTimeUtil.HOUR_PER_DAY);
|
|
269
|
+
_defineProperty(DateTimeUtil, "DAY_PER_WEEK", 7);
|
|
270
|
+
_defineProperty(DateTimeUtil, "DAY_PER_MONTH", 30);
|
|
271
|
+
_defineProperty(DateTimeUtil, "DAY_PER_YEAR", 365);
|
|
272
|
+
_defineProperty(DateTimeUtil, "MONTH_PER_YEAR", 12);
|
|
273
|
+
_defineProperty(DateTimeUtil, "WEEK_PER_YEAR", 52);
|
|
274
|
+
_defineProperty(DateTimeUtil, "WEEK_PER_MONTH", 4);
|
|
275
|
+
_defineProperty(DateTimeUtil, "FORMAT", {
|
|
276
|
+
ISO_DATE: "yyyy-MM-dd",
|
|
277
|
+
ISO_TIME: "HH:mm:ss",
|
|
278
|
+
ISO_DATE_TIME: "yyyy-MM-dd HH:mm:ss",
|
|
279
|
+
ISO_DATE_TIME_MS: "yyyy-MM-dd HH:mm:ss.SSS",
|
|
280
|
+
ISO_DATETIME_TZ: "yyyy-MM-dd'T'HH:mm:ssXXX",
|
|
281
|
+
ISO_DATETIME_TZ_MS: "yyyy-MM-dd'T'HH:mm:ss.SSSXXX",
|
|
282
|
+
US_DATE: "MM/dd/yyyy",
|
|
283
|
+
US_DATE_TIME: "MM/dd/yyyy HH:mm:ss",
|
|
284
|
+
US_DATE_SHORT_YEAR: "MM/dd/yy",
|
|
285
|
+
EU_DATE: "dd/MM/yyyy",
|
|
286
|
+
EU_DATE_TIME: "dd/MM/yyyy HH:mm:ss",
|
|
287
|
+
CN_DATE: "yyyy年MM月dd日",
|
|
288
|
+
CN_DATE_TIME: "yyyy年MM月dd日 HH时mm分ss秒",
|
|
289
|
+
CN_DATE_WEEKDAY: "yyyy年MM月dd日 EEE",
|
|
290
|
+
CN_WEEKDAY_FULL: "EEEE",
|
|
291
|
+
SHORT_DATE: "yy-MM-dd",
|
|
292
|
+
SHORT_DATE_SLASH: "yy/MM/dd",
|
|
293
|
+
MONTH_DAY: "MM-dd",
|
|
294
|
+
MONTH_DAY_CN: "MM月dd日",
|
|
295
|
+
DATE_WITH_WEEKDAY_SHORT: "yyyy-MM-dd (EEE)",
|
|
296
|
+
DATE_WITH_WEEKDAY_FULL: "yyyy-MM-dd (EEEE)",
|
|
297
|
+
TIME_24: "HH:mm:ss",
|
|
298
|
+
TIME_24_NO_SEC: "HH:mm",
|
|
299
|
+
TIME_12: "hh:mm:ss a",
|
|
300
|
+
TIME_12_NO_SEC: "hh:mm a",
|
|
301
|
+
TIMESTAMP: "yyyyMMddHHmmss",
|
|
302
|
+
TIMESTAMP_MS: "yyyyMMddHHmmssSSS",
|
|
303
|
+
RFC2822: "EEE, dd MMM yyyy HH:mm:ss xxx",
|
|
304
|
+
READABLE_DATE: "MMM dd, yyyy",
|
|
305
|
+
READABLE_DATE_TIME: "MMM dd, yyyy HH:mm",
|
|
306
|
+
COMPACT_DATETIME: "yyyyMMdd_HHmmss"
|
|
307
|
+
});
|
|
308
|
+
//#endregion
|
|
309
|
+
//#region src/env/envUtil.ts
|
|
310
|
+
/**
|
|
311
|
+
* 环境检查工具类
|
|
312
|
+
*/
|
|
313
|
+
var EnvUtil = class {
|
|
314
|
+
static isBrowser() {
|
|
315
|
+
return this._isBrowser;
|
|
316
|
+
}
|
|
317
|
+
static isWebWorker() {
|
|
318
|
+
return this._isWebWorker;
|
|
319
|
+
}
|
|
320
|
+
static isReactNative() {
|
|
321
|
+
return this._isReactNative;
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* 检查是否在 iframe 环境中
|
|
325
|
+
*/
|
|
326
|
+
static isIframe() {
|
|
327
|
+
if (typeof window === "undefined") return false;
|
|
328
|
+
try {
|
|
329
|
+
return window.top !== window.self;
|
|
330
|
+
} catch (error) {
|
|
331
|
+
if (error.name === "SecurityError") return true;
|
|
332
|
+
return false;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* 检测当前设备是否为桌面设备
|
|
337
|
+
*
|
|
338
|
+
* @param minWidth - 桌面设备最小宽度(默认 1200px)
|
|
339
|
+
* @param minScreenSize - 桌面设备最小屏幕尺寸(默认 10英寸)
|
|
340
|
+
* @param dpi - 标准 DPI 基准(默认 160)
|
|
341
|
+
* @returns 是否为桌面设备
|
|
342
|
+
* @example
|
|
343
|
+
* ```ts
|
|
344
|
+
* // 假设 window.innerWidth = 1920
|
|
345
|
+
* EnvUtil.isDesktop(); // true
|
|
346
|
+
*
|
|
347
|
+
* // 自定义阈值
|
|
348
|
+
* EnvUtil.isDesktop(1440, 13); // 更严格的桌面检测
|
|
349
|
+
* ```
|
|
350
|
+
*/
|
|
351
|
+
static isDesktop(minWidth = 1200, minScreenSize = 10, dpi = 160) {
|
|
352
|
+
if (typeof window === "undefined" || !TypeUtil.isPositiveInteger(minWidth) || !TypeUtil.isPositiveInteger(minScreenSize)) return false;
|
|
353
|
+
if (window.innerWidth < minWidth) return false;
|
|
354
|
+
try {
|
|
355
|
+
const widthPx = window.screen.width;
|
|
356
|
+
const heightPx = window.screen.height;
|
|
357
|
+
const DPI = dpi * (window.devicePixelRatio || 1);
|
|
358
|
+
const widthInch = widthPx / DPI;
|
|
359
|
+
const heightInch = heightPx / DPI;
|
|
360
|
+
return Math.sqrt(widthInch ** 2 + heightInch ** 2) >= minScreenSize;
|
|
361
|
+
} catch {
|
|
362
|
+
return true;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* 检测当前设备是否为 Windows 桌面设备
|
|
367
|
+
*
|
|
368
|
+
* @param minWidth - 桌面设备最小宽度(默认 1200px)
|
|
369
|
+
* @param minScreenSize - 桌面设备最小屏幕尺寸(默认 10英寸)
|
|
370
|
+
* @param dpi - 标准 DPI 基准(默认 160)
|
|
371
|
+
* @returns 是否为 Windows 桌面设备
|
|
372
|
+
* @example
|
|
373
|
+
* ```ts
|
|
374
|
+
* // UA contains Windows
|
|
375
|
+
* EnvUtil.isWindowsDesktop(); // true
|
|
376
|
+
* ```
|
|
377
|
+
*/
|
|
378
|
+
static isWindowsDesktop(minWidth = 1200, minScreenSize = 10, dpi = 160) {
|
|
379
|
+
if (typeof navigator === "undefined" || !navigator.userAgent) return false;
|
|
380
|
+
return /Windows/i.test(navigator.userAgent) && this.isDesktop(minWidth, minScreenSize, dpi);
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* 检测当前设备是否为 macOS 桌面设备
|
|
384
|
+
*
|
|
385
|
+
* @param minWidth - 桌面设备最小宽度(默认 1200px)
|
|
386
|
+
* @param minScreenSize - 桌面设备最小屏幕尺寸(默认 10英寸)
|
|
387
|
+
* @param dpi - 标准 DPI 基准(默认 160)
|
|
388
|
+
* @returns 是否为 macOS 桌面设备
|
|
389
|
+
* @example
|
|
390
|
+
* ```ts
|
|
391
|
+
* // UA contains Macintosh
|
|
392
|
+
* EnvUtil.isMacOSDesktop(); // true
|
|
393
|
+
* ```
|
|
394
|
+
*/
|
|
395
|
+
static isMacOSDesktop(minWidth = 1200, minScreenSize = 10, dpi = 160) {
|
|
396
|
+
if (typeof navigator === "undefined" || !navigator.userAgent) return false;
|
|
397
|
+
return /Macintosh/i.test(navigator.userAgent) && this.isDesktop(minWidth, minScreenSize, dpi);
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* 检测当前设备是否为移动设备
|
|
401
|
+
*
|
|
402
|
+
* @param maxWidth - 移动设备最大宽度(默认 768px)
|
|
403
|
+
* @param dpi - 标准 DPI 基准(默认 160)
|
|
404
|
+
* @returns 是否为移动设备
|
|
405
|
+
* @example
|
|
406
|
+
* ```ts
|
|
407
|
+
* // 假设 window.innerWidth = 500
|
|
408
|
+
* EnvUtil.isMobile(); // true
|
|
409
|
+
* ```
|
|
410
|
+
*/
|
|
411
|
+
static isMobile(maxWidth = 768, dpi = 160) {
|
|
412
|
+
if (typeof window === "undefined" || !TypeUtil.isPositiveInteger(maxWidth)) return false;
|
|
413
|
+
if (window.innerWidth >= maxWidth) return false;
|
|
414
|
+
try {
|
|
415
|
+
const widthPx = window.screen.width;
|
|
416
|
+
const heightPx = window.screen.height;
|
|
417
|
+
const DPI = dpi * (window.devicePixelRatio || 1);
|
|
418
|
+
const widthInch = widthPx / DPI;
|
|
419
|
+
const heightInch = heightPx / DPI;
|
|
420
|
+
return Math.sqrt(widthInch ** 2 + heightInch ** 2) < 7;
|
|
421
|
+
} catch {
|
|
422
|
+
return true;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* 检测当前设备是否为IOS移动设备
|
|
427
|
+
*
|
|
428
|
+
* @param maxWidth - 移动设备最大宽度(默认 768px)
|
|
429
|
+
* @param dpi - 标准 DPI 基准(默认 160)
|
|
430
|
+
* @returns 是否为 iOS 移动设备 (iPhone/iPod)
|
|
431
|
+
* @example
|
|
432
|
+
* ```ts
|
|
433
|
+
* // UA contains iPhone
|
|
434
|
+
* EnvUtil.isIOSMobile(); // true
|
|
435
|
+
* ```
|
|
436
|
+
*/
|
|
437
|
+
static isIOSMobile(maxWidth = 768, dpi = 160) {
|
|
438
|
+
if (typeof navigator === "undefined" || !navigator.userAgent) return false;
|
|
439
|
+
return /iPhone|iPad|iPod/i.test(navigator.userAgent) && this.isMobile(maxWidth, dpi);
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* 检测当前设备是否为平板
|
|
443
|
+
*
|
|
444
|
+
* @param minWidth - 平板最小宽度(默认 768px)
|
|
445
|
+
* @param maxWidth - 平板最大宽度(默认 1200px)
|
|
446
|
+
* @param dpi - 标准 DPI 基准(默认 160)
|
|
447
|
+
* @returns 是否为平板设备
|
|
448
|
+
* @example
|
|
449
|
+
* ```ts
|
|
450
|
+
* // 假设 window.innerWidth = 1000
|
|
451
|
+
* EnvUtil.isTablet(); // true
|
|
452
|
+
* ```
|
|
453
|
+
*/
|
|
454
|
+
static isTablet(minWidth = 768, maxWidth = 1200, dpi = 160) {
|
|
455
|
+
if (typeof window === "undefined" || !TypeUtil.isPositiveInteger(minWidth) || !TypeUtil.isPositiveInteger(maxWidth)) return false;
|
|
456
|
+
const width = window.innerWidth;
|
|
457
|
+
const isWithinWidthRange = width >= minWidth && width <= maxWidth;
|
|
458
|
+
try {
|
|
459
|
+
const widthPx = window.screen.width;
|
|
460
|
+
const heightPx = window.screen.height;
|
|
461
|
+
const DPI = dpi * (window.devicePixelRatio || 1);
|
|
462
|
+
const widthInch = widthPx / DPI;
|
|
463
|
+
const heightInch = heightPx / DPI;
|
|
464
|
+
const screenInches = Math.sqrt(widthInch ** 2 + heightInch ** 2);
|
|
465
|
+
return isWithinWidthRange || screenInches >= 7;
|
|
466
|
+
} catch {
|
|
467
|
+
return isWithinWidthRange;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
_defineProperty(EnvUtil, "_isBrowser", typeof window !== "undefined" && TypeUtil.isFunction(window?.document?.createElement));
|
|
472
|
+
_defineProperty(EnvUtil, "_isWebWorker", typeof window === "undefined" && typeof self !== "undefined" && "importScripts" in self);
|
|
473
|
+
_defineProperty(EnvUtil, "_isReactNative", typeof navigator !== "undefined" && navigator.product === "ReactNative");
|
|
474
|
+
//#endregion
|
|
475
|
+
//#region src/function/functionUtil.ts
|
|
476
|
+
/**
|
|
477
|
+
* 函数工具类
|
|
478
|
+
*/
|
|
479
|
+
var FunctionUtil = class {
|
|
480
|
+
/**
|
|
481
|
+
*将 Promise 转换为 `[err, result]` 格式,方便 async/await 错误处理
|
|
482
|
+
*
|
|
483
|
+
* @param promise 待处理的 Promise
|
|
484
|
+
* @param errorExt 附加到 error 对象的扩展信息(注意:如果原 error 是 Error 实例,扩展属性可能会覆盖或无法正确合并非枚举属性)
|
|
485
|
+
* @returns `[err, null]` 或 `[null, data]`
|
|
486
|
+
* @example
|
|
487
|
+
* ```ts
|
|
488
|
+
* const [err, data] = await FunctionUtil.to(someAsyncFunc());
|
|
489
|
+
* ```
|
|
490
|
+
*/
|
|
491
|
+
static to(promise, errorExt) {
|
|
492
|
+
return promise.then((data) => [null, data]).catch((err) => {
|
|
493
|
+
if (errorExt) {
|
|
494
|
+
const parsedError = {
|
|
495
|
+
name: "",
|
|
496
|
+
message: "",
|
|
497
|
+
stack: ""
|
|
498
|
+
};
|
|
499
|
+
if (err instanceof Error) {
|
|
500
|
+
parsedError.message = err.message;
|
|
501
|
+
parsedError.name = err.name;
|
|
502
|
+
parsedError.stack = err.stack;
|
|
503
|
+
Object.getOwnPropertyNames(err).forEach((key) => {
|
|
504
|
+
if (!(key in parsedError)) parsedError[key] = err[key];
|
|
505
|
+
});
|
|
506
|
+
} else Object.assign(parsedError, err);
|
|
507
|
+
Object.assign(parsedError, errorExt);
|
|
508
|
+
return [parsedError, void 0];
|
|
509
|
+
}
|
|
510
|
+
return [err ? err : /* @__PURE__ */ new Error("defaultError"), void 0];
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* 将 Arguments 对象转换为数组
|
|
515
|
+
*
|
|
516
|
+
* ⚠️ 注意:TypeScript 官方推荐使用 rest parameters (...args) 替代 arguments
|
|
517
|
+
* 本函数仅用于处理遗留代码或特殊场景(如装饰器中需保留 this 绑定)
|
|
518
|
+
*
|
|
519
|
+
* @param args Arguments 对象(必须为类数组对象)
|
|
520
|
+
* @param start 起始索引(可选,默认为 0)
|
|
521
|
+
* @returns 转换后的数组,元素类型为 T
|
|
522
|
+
*
|
|
523
|
+
* @throws TypeError 如果 args 为 null 或 undefined
|
|
524
|
+
*
|
|
525
|
+
* @example
|
|
526
|
+
* // 遗留代码场景
|
|
527
|
+
* function legacyFn(a: number, b: string) {
|
|
528
|
+
* const argsArray = FunctionUtil.toArgs(arguments);
|
|
529
|
+
* // argsArray: unknown[]
|
|
530
|
+
* }
|
|
531
|
+
*
|
|
532
|
+
* // 现代替代方案(推荐)
|
|
533
|
+
* function modernFn(a: number, b: string, ...rest: unknown[]) {
|
|
534
|
+
* // rest 已经是数组,无需 toArgs
|
|
535
|
+
* }
|
|
536
|
+
*
|
|
537
|
+
* // 参数截取
|
|
538
|
+
* function skipFirst(...args: unknown[]) {
|
|
539
|
+
* const rest = FunctionUtil.toArgs(arguments, 1);
|
|
540
|
+
* // rest: unknown[],跳过第一个参数
|
|
541
|
+
* }
|
|
542
|
+
*/
|
|
543
|
+
static toArgs(args, start) {
|
|
544
|
+
if (args === null) throw new TypeError(`function [toArgs] Expected parameter [args] to be a arguments object, got ${typeof args}`);
|
|
545
|
+
const array = Array.from(args);
|
|
546
|
+
return TypeUtil.isNumber(start) ? array.slice(start) : array;
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* 将同步或异步函数统一包装为 Promise
|
|
550
|
+
* - 自动捕获同步异常
|
|
551
|
+
*
|
|
552
|
+
* @param fn 返回值可为同步值或 Promise 的函数
|
|
553
|
+
* @returns 标准化的 Promise
|
|
554
|
+
*
|
|
555
|
+
* @example
|
|
556
|
+
* // 同步函数
|
|
557
|
+
* FunctionUtil.toPromise(() => 42).then(v => console.log(v)); // 42
|
|
558
|
+
*
|
|
559
|
+
* // 异步函数
|
|
560
|
+
* FunctionUtil.toPromise(async () => await fetchData()).then(data => ...);
|
|
561
|
+
*
|
|
562
|
+
* // 异常处理
|
|
563
|
+
* FunctionUtil.toPromise(() => { throw new Error('fail'); }).catch(err => console.error(err)); // 捕获同步异常
|
|
564
|
+
*/
|
|
565
|
+
static toPromise(fn) {
|
|
566
|
+
try {
|
|
567
|
+
return Promise.resolve(fn());
|
|
568
|
+
} catch (error) {
|
|
569
|
+
return Promise.reject(error);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
//#endregion
|
|
574
|
+
//#region src/mime/mimeUtil.ts
|
|
575
|
+
/**
|
|
576
|
+
* MIME 工具类
|
|
577
|
+
*/
|
|
578
|
+
var MimeUtil = class {};
|
|
579
|
+
_defineProperty(MimeUtil, "MIME", {
|
|
580
|
+
/** 普通文本文件 */
|
|
581
|
+
TEXT: "text/plain",
|
|
582
|
+
/** 超文本标记语言文档 */
|
|
583
|
+
HTML: "text/html",
|
|
584
|
+
/** 层叠样式表文件 */
|
|
585
|
+
CSS: "text/css",
|
|
586
|
+
/** 逗号分隔值文件(表格数据) */
|
|
587
|
+
CSV: "text/csv",
|
|
588
|
+
/** 制表符分隔值文件 */
|
|
589
|
+
TSV: "text/tab-separated-values",
|
|
590
|
+
/** XML 文档 */
|
|
591
|
+
XML: "text/xml",
|
|
592
|
+
/** XHTML 文档(XML 严格格式的 HTML) */
|
|
593
|
+
XHTML: "application/xhtml+xml",
|
|
594
|
+
/** JavaScript 脚本文件(标准推荐) */
|
|
595
|
+
JS: "text/javascript",
|
|
596
|
+
/** Markdown 格式文档 */
|
|
597
|
+
MARKDOWN: "text/markdown",
|
|
598
|
+
/** 富文本格式文档(.rtf) */
|
|
599
|
+
RTF: "application/rtf",
|
|
600
|
+
/** iCalendar 日历格式(.ics) */
|
|
601
|
+
CALENDAR: "text/calendar",
|
|
602
|
+
/** JPEG 图像(.jpg/.jpeg) */
|
|
603
|
+
JPEG: "image/jpeg",
|
|
604
|
+
/** PNG 图像(无损压缩,支持透明) */
|
|
605
|
+
PNG: "image/png",
|
|
606
|
+
/** GIF 图像(支持动画) */
|
|
607
|
+
GIF: "image/gif",
|
|
608
|
+
/** Windows 位图(.bmp) */
|
|
609
|
+
BMP: "image/bmp",
|
|
610
|
+
/** SVG 向量图形(.svg) */
|
|
611
|
+
SVG: "image/svg+xml",
|
|
612
|
+
/** APNG 动态图像(.apng) */
|
|
613
|
+
APNG: "image/apng",
|
|
614
|
+
/** AVIF 图像(高效压缩) */
|
|
615
|
+
AVIF: "image/avif",
|
|
616
|
+
/** 图标文件格式(.ico) */
|
|
617
|
+
ICO: "image/vnd.microsoft.icon",
|
|
618
|
+
/** WebP 图像(高效压缩) */
|
|
619
|
+
WEBP: "image/webp",
|
|
620
|
+
/** MP3 音频(.mp3) */
|
|
621
|
+
MP3: "audio/mpeg",
|
|
622
|
+
/** AAC 音频(.aac) */
|
|
623
|
+
AAC: "audio/aac",
|
|
624
|
+
/** MIDI 音乐文件(.mid/.midi) */
|
|
625
|
+
MIDI: "audio/midi",
|
|
626
|
+
/** OGG 音频(.oga) */
|
|
627
|
+
OGG_AUDIO: "audio/ogg",
|
|
628
|
+
/** Opus 音频(.opus) */
|
|
629
|
+
OPUS: "audio/opus",
|
|
630
|
+
/** WAV 音频(.wav) */
|
|
631
|
+
WAV: "audio/wav",
|
|
632
|
+
/** RealAudio 音频(.ra/.ram) */
|
|
633
|
+
REAL_AUDIO: "audio/x-pn-realaudio",
|
|
634
|
+
/** MP4 视频(.mp4) */
|
|
635
|
+
MP4: "video/mp4",
|
|
636
|
+
/** MPEG 视频(.mpeg/.mpg) */
|
|
637
|
+
MPEG: "video/mpeg",
|
|
638
|
+
/** OGG 视频(.ogv) */
|
|
639
|
+
OGG_VIDEO: "video/ogg",
|
|
640
|
+
/** AVI 视频(.avi) */
|
|
641
|
+
AVI: "video/x-msvideo",
|
|
642
|
+
/** 3GPP 视频(.3gp) */
|
|
643
|
+
THREE_GPP: "video/3gpp",
|
|
644
|
+
/** 3GPP2 视频(.3g2) */
|
|
645
|
+
THREE_GPP2: "video/3gpp2",
|
|
646
|
+
/** WebM 视频(.webm) */
|
|
647
|
+
WEBM: "video/webm",
|
|
648
|
+
/** PDF 文档 */
|
|
649
|
+
PDF: "application/pdf",
|
|
650
|
+
/** Word 97-2003 文档(.doc) */
|
|
651
|
+
DOC: "application/msword",
|
|
652
|
+
/** Word 2007+ 文档(.docx) */
|
|
653
|
+
DOCX: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
654
|
+
/** Excel 2007+ 工作簿(.xlsx) */
|
|
655
|
+
XLSX: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
656
|
+
/** 启用宏的Excel工作簿(.xlsm) */
|
|
657
|
+
XLSM: "application/vnd.ms-excel.sheet.macroEnabled.12",
|
|
658
|
+
/** Excel模板文件(.xltx) */
|
|
659
|
+
XLTX: "application/vnd.openxmlformats-officedocument.spreadsheetml.template",
|
|
660
|
+
/** PowerPoint 2007+ 演示文稿(.pptx) */
|
|
661
|
+
PPTX: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
662
|
+
/** PowerPoint 97-2003 演示文稿(.ppt) */
|
|
663
|
+
PPT: "application/vnd.ms-powerpoint",
|
|
664
|
+
/** OpenDocument 文本文档(.odt) */
|
|
665
|
+
ODT: "application/vnd.oasis.opendocument.text",
|
|
666
|
+
/** OpenDocument 表格文档(.ods) */
|
|
667
|
+
ODS: "application/vnd.oasis.opendocument.spreadsheet",
|
|
668
|
+
/** OpenDocument 演示文稿(.odp) */
|
|
669
|
+
ODP: "application/vnd.oasis.opendocument.presentation",
|
|
670
|
+
/** EPUB 电子书(.epub) */
|
|
671
|
+
EPUB: "application/epub+zip",
|
|
672
|
+
/** Kindle 电子书(.azw) */
|
|
673
|
+
AZW: "application/vnd.amazon.ebook",
|
|
674
|
+
/** ZIP 压缩文件(.zip) */
|
|
675
|
+
ZIP: "application/zip",
|
|
676
|
+
/** GZIP 压缩文件(.gz) */
|
|
677
|
+
GZIP: "application/gzip",
|
|
678
|
+
/** GZIP 压缩文件(旧格式) */
|
|
679
|
+
X_GZIP: "application/x-gzip",
|
|
680
|
+
/** TAR 归档文件(.tar) */
|
|
681
|
+
TAR: "application/x-tar",
|
|
682
|
+
/** BZip 归档(.bz) */
|
|
683
|
+
BZIP: "application/x-bzip",
|
|
684
|
+
/** BZip2 归档(.bz2) */
|
|
685
|
+
BZIP2: "application/x-bzip2",
|
|
686
|
+
/** 7-Zip 压缩文件(.7z) */
|
|
687
|
+
SEVEN_Z: "application/x-7z-compressed",
|
|
688
|
+
/** 通用二进制数据(默认类型) */
|
|
689
|
+
OCTET_STREAM: "application/octet-stream",
|
|
690
|
+
/** JSON 数据格式(.json) */
|
|
691
|
+
JSON: "application/json",
|
|
692
|
+
/** JSON-LD 格式(.jsonld) */
|
|
693
|
+
LD_JSON: "application/ld+json",
|
|
694
|
+
/** Java 归档文件(.jar) */
|
|
695
|
+
JAR: "application/java-archive",
|
|
696
|
+
/** MS 嵌入式 OpenType 字体(.eot) */
|
|
697
|
+
EOT: "application/vnd.ms-fontobject",
|
|
698
|
+
/** OpenType 字体(.otf) */
|
|
699
|
+
OTF: "font/otf",
|
|
700
|
+
/** Excel 97-2003 工作簿(.xls) */
|
|
701
|
+
XLS: "application/vnd.ms-excel",
|
|
702
|
+
/** Microsoft XPS 文档(.xps) */
|
|
703
|
+
XPS: "application/vnd.ms-xpsdocument",
|
|
704
|
+
/** Word 启用宏文档(.docm) */
|
|
705
|
+
DOCM: "application/vnd.ms-word.document.macroEnabled.12"
|
|
706
|
+
});
|
|
707
|
+
//#endregion
|
|
708
|
+
//#region src/number/numberUtil.ts
|
|
709
|
+
/**
|
|
710
|
+
* 数字工具类
|
|
711
|
+
*/
|
|
712
|
+
var NumberUtil = class {
|
|
713
|
+
/**
|
|
714
|
+
* 数字区间检查函数
|
|
715
|
+
*
|
|
716
|
+
* @param input 待检查数字
|
|
717
|
+
* @param interval 由两个数字组成的元组 [left, right]
|
|
718
|
+
* @param includeLeft 是否包含左边界(默认 true)
|
|
719
|
+
* @param includeRight 是否包含右边界(默认 false)
|
|
720
|
+
* @returns 是否在区间内
|
|
721
|
+
* @example
|
|
722
|
+
* ```ts
|
|
723
|
+
* NumberUtil.within(5, [1, 10]); // true
|
|
724
|
+
* NumberUtil.within(1, [1, 10], false); // false
|
|
725
|
+
* ```
|
|
726
|
+
*/
|
|
727
|
+
static within(input, interval, includeLeft = true, includeRight = false) {
|
|
728
|
+
if (!TypeUtil.isNumber(input) || TypeUtil.isInfinity(input)) throw new Error("function [within] Expected parameter [input] to be a finite number.");
|
|
729
|
+
if (!TypeUtil.isArray(interval) || interval.length !== 2) throw new Error("function [within] Expected parameter [interval] to be a tuple with 2 numbers.");
|
|
730
|
+
const [left, right] = interval;
|
|
731
|
+
if (left > right) throw new Error(`Invalid interval: left (${left}) must be <= right (${right}).`);
|
|
732
|
+
if (includeLeft && includeRight) return input >= left && input <= right;
|
|
733
|
+
else if (includeLeft) return input >= left && input < right;
|
|
734
|
+
else if (includeRight) return input > left && input <= right;
|
|
735
|
+
else return input > left && input < right;
|
|
736
|
+
}
|
|
737
|
+
};
|
|
738
|
+
//#endregion
|
|
739
|
+
//#region src/object/objectUtil.ts
|
|
740
|
+
/**
|
|
741
|
+
* 对象工具类
|
|
742
|
+
*/
|
|
743
|
+
var ObjectUtil = class {
|
|
744
|
+
static keys(value) {
|
|
745
|
+
return Object.keys(value);
|
|
746
|
+
}
|
|
747
|
+
static values(value) {
|
|
748
|
+
return Object.values(value);
|
|
749
|
+
}
|
|
750
|
+
static entries(value) {
|
|
751
|
+
return Object.entries(value);
|
|
752
|
+
}
|
|
753
|
+
/**
|
|
754
|
+
* 映射对象条目
|
|
755
|
+
* - 将对象的键值对映射为新的键值对
|
|
756
|
+
*
|
|
757
|
+
* @param plainObject 对象
|
|
758
|
+
* @param toEntry 映射函数
|
|
759
|
+
* @returns 映射后的新对象
|
|
760
|
+
* @example
|
|
761
|
+
* ```ts
|
|
762
|
+
* const obj = { a: 1, b: 2 };
|
|
763
|
+
* ObjectUtil.entriesMap(obj, (k, v) => [k, v * 2]); // { a: 2, b: 4 }
|
|
764
|
+
* ```
|
|
765
|
+
*/
|
|
766
|
+
static entriesMap(plainObject, toEntry) {
|
|
767
|
+
const defaultResult = {};
|
|
768
|
+
if (!TypeUtil.isObject(plainObject)) return defaultResult;
|
|
769
|
+
return this.entries(plainObject).reduce((acc, [key, value]) => {
|
|
770
|
+
const [newKey, newValue] = toEntry(key, value);
|
|
771
|
+
acc[newKey] = newValue;
|
|
772
|
+
return acc;
|
|
773
|
+
}, defaultResult);
|
|
774
|
+
}
|
|
775
|
+
static pick(obj, keys) {
|
|
776
|
+
const result = {};
|
|
777
|
+
if (!TypeUtil.isObject(obj)) return result;
|
|
778
|
+
if (!TypeUtil.isArray(keys)) return obj;
|
|
779
|
+
return keys.reduce((acc, key) => {
|
|
780
|
+
if (key in obj) acc[key] = obj[key];
|
|
781
|
+
return acc;
|
|
782
|
+
}, result);
|
|
783
|
+
}
|
|
784
|
+
static omit(obj, keys) {
|
|
785
|
+
const result = {};
|
|
786
|
+
if (!TypeUtil.isObject(obj)) return result;
|
|
787
|
+
if (!TypeUtil.isArray(keys)) return obj;
|
|
788
|
+
const keysToOmit = new Set(keys);
|
|
789
|
+
return Object.keys(obj).reduce((acc, key) => {
|
|
790
|
+
if (!keysToOmit.has(key)) acc[key] = obj[key];
|
|
791
|
+
return acc;
|
|
792
|
+
}, result);
|
|
793
|
+
}
|
|
794
|
+
static invert(obj) {
|
|
795
|
+
const result = {};
|
|
796
|
+
if (!TypeUtil.isObject(obj)) return result;
|
|
797
|
+
for (const [k, v] of this.entries(obj)) if (TypeUtil.isString(v) || TypeUtil.isNumber(v) || TypeUtil.isSymbol(v)) result[v] = k;
|
|
798
|
+
return result;
|
|
799
|
+
}
|
|
800
|
+
static crush(obj) {
|
|
801
|
+
if (!obj) return {};
|
|
802
|
+
function crushReducer(crushed, value, path) {
|
|
803
|
+
if (TypeUtil.isObject(value) || TypeUtil.isArray(value)) for (const [prop, propValue] of Object.entries(value)) crushReducer(crushed, propValue, path ? `${path}.${prop}` : prop);
|
|
804
|
+
else crushed[path] = value;
|
|
805
|
+
return crushed;
|
|
806
|
+
}
|
|
807
|
+
return crushReducer({}, obj, "");
|
|
808
|
+
}
|
|
809
|
+
static enumKeys(enumeration) {
|
|
810
|
+
const [isEnum, isBidirectionalEnum] = TypeUtil.isEnumeration(enumeration);
|
|
811
|
+
if (!isEnum) throw Error("function [enumKeys] expected parameter to be a enum, and requires at least one member");
|
|
812
|
+
const keys = this.keys(enumeration);
|
|
813
|
+
if (isBidirectionalEnum) return keys.splice(keys.length / 2, keys.length / 2);
|
|
814
|
+
return keys;
|
|
815
|
+
}
|
|
816
|
+
static enumValues(enumeration) {
|
|
817
|
+
const [isEnum, isBidirectionalEnum] = TypeUtil.isEnumeration(enumeration);
|
|
818
|
+
if (!isEnum) throw Error("function [enumValues] expected parameter to be a enum, and requires at least one member");
|
|
819
|
+
const values = this.values(enumeration);
|
|
820
|
+
if (isBidirectionalEnum) return values.splice(values.length / 2, values.length / 2);
|
|
821
|
+
return values;
|
|
822
|
+
}
|
|
823
|
+
static enumEntries(enumeration) {
|
|
824
|
+
const [isEnum, isBidirectionalEnum] = TypeUtil.isEnumeration(enumeration);
|
|
825
|
+
if (!isEnum) throw Error("function [enumEntries] expected parameter to be a enum, and requires at least one member");
|
|
826
|
+
const entries = this.entries(enumeration);
|
|
827
|
+
if (isBidirectionalEnum) return entries.splice(entries.length / 2, entries.length / 2);
|
|
828
|
+
return entries;
|
|
829
|
+
}
|
|
830
|
+
};
|
|
831
|
+
//#endregion
|
|
832
|
+
//#region src/theme/themeUtil.ts
|
|
833
|
+
/**
|
|
834
|
+
* 主题工具类
|
|
835
|
+
*/
|
|
836
|
+
var ThemeUtil = class {};
|
|
837
|
+
_defineProperty(ThemeUtil, "THEME", {
|
|
838
|
+
LIGHT: "light",
|
|
839
|
+
DARK: "dark"
|
|
840
|
+
});
|
|
841
|
+
_defineProperty(ThemeUtil, "THEME_MODE", {
|
|
842
|
+
LIGHT: "light",
|
|
843
|
+
DARK: "dark",
|
|
844
|
+
SYSTEM: "system"
|
|
845
|
+
});
|
|
846
|
+
//#endregion
|
|
847
|
+
//#region src/tree/utils.ts
|
|
848
|
+
function getFinalChildrenKey(tree, meta, options) {
|
|
849
|
+
if (TypeUtil.isFunction(options.getChildrenKey)) {
|
|
850
|
+
const dynamicChildrenKey = options.getChildrenKey(tree, meta);
|
|
851
|
+
if (dynamicChildrenKey && dynamicChildrenKey !== null) return dynamicChildrenKey;
|
|
852
|
+
}
|
|
853
|
+
return options.childrenKey;
|
|
854
|
+
}
|
|
855
|
+
//#endregion
|
|
856
|
+
//#region src/tree/filter.ts
|
|
857
|
+
function preImpl$3(row, callback, options) {
|
|
858
|
+
if (!callback(row, options)) return;
|
|
859
|
+
const finalChildrenKey = getFinalChildrenKey(row, options, options);
|
|
860
|
+
const children = row[finalChildrenKey];
|
|
861
|
+
let newChildren;
|
|
862
|
+
if (TypeUtil.isArray(children)) {
|
|
863
|
+
const nextLevelOptions = {
|
|
864
|
+
...options,
|
|
865
|
+
parents: [...options.parents, row],
|
|
866
|
+
depth: options.depth + 1
|
|
867
|
+
};
|
|
868
|
+
newChildren = children.map((c) => preImpl$3(c, callback, nextLevelOptions)).filter((c) => !!c);
|
|
869
|
+
}
|
|
870
|
+
return {
|
|
871
|
+
...row,
|
|
872
|
+
[finalChildrenKey]: newChildren
|
|
873
|
+
};
|
|
874
|
+
}
|
|
875
|
+
function postImpl$3(row, callback, options) {
|
|
876
|
+
const finalChildrenKey = getFinalChildrenKey(row, options, options);
|
|
877
|
+
const children = row[finalChildrenKey];
|
|
878
|
+
let newChildren;
|
|
879
|
+
if (TypeUtil.isArray(children)) {
|
|
880
|
+
const nextLevelOptions = {
|
|
881
|
+
...options,
|
|
882
|
+
parents: [...options.parents, row],
|
|
883
|
+
depth: options.depth + 1
|
|
884
|
+
};
|
|
885
|
+
newChildren = children.map((c) => preImpl$3(c, callback, nextLevelOptions)).filter((c) => !!c);
|
|
886
|
+
}
|
|
887
|
+
if (!callback(row, options)) return;
|
|
888
|
+
return {
|
|
889
|
+
...row,
|
|
890
|
+
[finalChildrenKey]: newChildren
|
|
891
|
+
};
|
|
892
|
+
}
|
|
893
|
+
function breadthImpl$3(row, callback, options) {
|
|
894
|
+
const queue = [{
|
|
895
|
+
queueRow: row,
|
|
896
|
+
queueOptions: options
|
|
897
|
+
}];
|
|
898
|
+
const resultCache = /* @__PURE__ */ new WeakMap();
|
|
899
|
+
const newNodeCache = /* @__PURE__ */ new WeakMap();
|
|
900
|
+
const childrenKeyCache = /* @__PURE__ */ new WeakMap();
|
|
901
|
+
let result;
|
|
902
|
+
const runQueue = () => {
|
|
903
|
+
if (queue.length === 0) return result;
|
|
904
|
+
const { queueRow, queueOptions } = queue.shift();
|
|
905
|
+
const finalChildrenKey = getFinalChildrenKey(queueRow, queueOptions, queueOptions);
|
|
906
|
+
const children = queueRow[finalChildrenKey];
|
|
907
|
+
if (TypeUtil.isArray(children)) {
|
|
908
|
+
const nextLevelOptions = {
|
|
909
|
+
...queueOptions,
|
|
910
|
+
parents: [...queueOptions.parents, queueRow],
|
|
911
|
+
depth: queueOptions.depth + 1
|
|
912
|
+
};
|
|
913
|
+
const subQueueItems = children.map((queueRow) => ({
|
|
914
|
+
queueRow,
|
|
915
|
+
queueOptions: nextLevelOptions
|
|
916
|
+
}));
|
|
917
|
+
queue.push(...subQueueItems);
|
|
918
|
+
}
|
|
919
|
+
const parent = ArrayUtil.last(queueOptions.parents);
|
|
920
|
+
const isTopNode = queueOptions.depth === 0;
|
|
921
|
+
const parentResult = parent && resultCache.get(parent);
|
|
922
|
+
if (!isTopNode && !parentResult) return runQueue();
|
|
923
|
+
const callbackResult = callback(queueRow, queueOptions);
|
|
924
|
+
if (isTopNode && !callbackResult) return;
|
|
925
|
+
const newNode = {
|
|
926
|
+
...queueRow,
|
|
927
|
+
[finalChildrenKey]: void 0
|
|
928
|
+
};
|
|
929
|
+
if (isTopNode) result = newNode;
|
|
930
|
+
resultCache.set(queueRow, callbackResult);
|
|
931
|
+
newNodeCache.set(queueRow, newNode);
|
|
932
|
+
childrenKeyCache.set(queueRow, finalChildrenKey);
|
|
933
|
+
if (callbackResult && parent) {
|
|
934
|
+
const parentNewNode = newNodeCache.get(parent);
|
|
935
|
+
const parentChildrenKey = childrenKeyCache.get(parent);
|
|
936
|
+
if (parentNewNode && parentChildrenKey) {
|
|
937
|
+
if (!parentNewNode[parentChildrenKey]) parentNewNode[parentChildrenKey] = [];
|
|
938
|
+
parentNewNode[parentChildrenKey].push(newNode);
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
return runQueue();
|
|
942
|
+
};
|
|
943
|
+
return runQueue();
|
|
944
|
+
}
|
|
945
|
+
const treeFilterStrategies = {
|
|
946
|
+
pre: preImpl$3,
|
|
947
|
+
post: postImpl$3,
|
|
948
|
+
breadth: breadthImpl$3
|
|
949
|
+
};
|
|
950
|
+
//#endregion
|
|
951
|
+
//#region src/tree/find.ts
|
|
952
|
+
function preImpl$2(row, callback, options) {
|
|
953
|
+
if (callback(row, options)) return row;
|
|
954
|
+
const children = row[getFinalChildrenKey(row, options, options)];
|
|
955
|
+
if (TypeUtil.isArray(children)) for (const child of children) {
|
|
956
|
+
const result = preImpl$2(child, callback, {
|
|
957
|
+
...options,
|
|
958
|
+
parents: [...options.parents, row],
|
|
959
|
+
depth: options.depth + 1
|
|
960
|
+
});
|
|
961
|
+
if (result) return result;
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
function postImpl$2(row, callback, options) {
|
|
965
|
+
const children = row[getFinalChildrenKey(row, options, options)];
|
|
966
|
+
if (TypeUtil.isArray(children)) for (const child of children) {
|
|
967
|
+
const result = postImpl$2(child, callback, {
|
|
968
|
+
...options,
|
|
969
|
+
parents: [...options.parents, row],
|
|
970
|
+
depth: options.depth + 1
|
|
971
|
+
});
|
|
972
|
+
if (result) return result;
|
|
973
|
+
}
|
|
974
|
+
if (callback(row, options)) return row;
|
|
975
|
+
}
|
|
976
|
+
function breadthImpl$2(row, callback, options) {
|
|
977
|
+
const queue = [{
|
|
978
|
+
queueRow: row,
|
|
979
|
+
queueOptions: options
|
|
980
|
+
}];
|
|
981
|
+
const runQueue = () => {
|
|
982
|
+
if (queue.length === 0) return;
|
|
983
|
+
const { queueRow, queueOptions } = queue.shift();
|
|
984
|
+
const children = queueRow[getFinalChildrenKey(queueRow, queueOptions, queueOptions)];
|
|
985
|
+
if (TypeUtil.isArray(children)) {
|
|
986
|
+
const nextLevelOptions = {
|
|
987
|
+
...queueOptions,
|
|
988
|
+
parents: [...queueOptions.parents, queueRow],
|
|
989
|
+
depth: queueOptions.depth + 1
|
|
990
|
+
};
|
|
991
|
+
const subQueueItems = children.map((queueRow) => ({
|
|
992
|
+
queueRow,
|
|
993
|
+
queueOptions: nextLevelOptions
|
|
994
|
+
}));
|
|
995
|
+
queue.push(...subQueueItems);
|
|
996
|
+
}
|
|
997
|
+
if (callback(queueRow, queueOptions)) return queueRow;
|
|
998
|
+
return runQueue();
|
|
999
|
+
};
|
|
1000
|
+
return runQueue();
|
|
1001
|
+
}
|
|
1002
|
+
const treeFindStrategies = {
|
|
1003
|
+
pre: preImpl$2,
|
|
1004
|
+
post: postImpl$2,
|
|
1005
|
+
breadth: breadthImpl$2
|
|
1006
|
+
};
|
|
1007
|
+
//#endregion
|
|
1008
|
+
//#region src/tree/forEach.ts
|
|
1009
|
+
function preImpl$1(row, callback, options) {
|
|
1010
|
+
callback(row, options);
|
|
1011
|
+
const children = row[getFinalChildrenKey(row, options, options)];
|
|
1012
|
+
if (TypeUtil.isArray(children)) {
|
|
1013
|
+
const nextLevelOptions = {
|
|
1014
|
+
...options,
|
|
1015
|
+
parents: [...options.parents, row],
|
|
1016
|
+
depth: options.depth + 1
|
|
1017
|
+
};
|
|
1018
|
+
for (const child of children) preImpl$1(child, callback, nextLevelOptions);
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
function postImpl$1(row, callback, options) {
|
|
1022
|
+
const children = row[getFinalChildrenKey(row, options, options)];
|
|
1023
|
+
if (TypeUtil.isArray(children)) {
|
|
1024
|
+
const nextLevelOptions = {
|
|
1025
|
+
...options,
|
|
1026
|
+
parents: [...options.parents, row],
|
|
1027
|
+
depth: options.depth + 1
|
|
1028
|
+
};
|
|
1029
|
+
for (const child of children) postImpl$1(child, callback, nextLevelOptions);
|
|
1030
|
+
}
|
|
1031
|
+
callback(row, options);
|
|
1032
|
+
}
|
|
1033
|
+
function breadthImpl$1(row, callback, options) {
|
|
1034
|
+
const queue = [{
|
|
1035
|
+
queueRow: row,
|
|
1036
|
+
queueOptions: options
|
|
1037
|
+
}];
|
|
1038
|
+
const runQueue = () => {
|
|
1039
|
+
if (queue.length === 0) return;
|
|
1040
|
+
const { queueRow, queueOptions } = queue.shift();
|
|
1041
|
+
const children = queueRow[getFinalChildrenKey(queueRow, queueOptions, queueOptions)];
|
|
1042
|
+
if (TypeUtil.isArray(children)) {
|
|
1043
|
+
const nextLevelOptions = {
|
|
1044
|
+
...queueOptions,
|
|
1045
|
+
parents: [...queueOptions.parents, queueRow],
|
|
1046
|
+
depth: queueOptions.depth + 1
|
|
1047
|
+
};
|
|
1048
|
+
const subQueueItems = children.map((queueRow) => ({
|
|
1049
|
+
queueRow,
|
|
1050
|
+
queueOptions: nextLevelOptions
|
|
1051
|
+
}));
|
|
1052
|
+
queue.push(...subQueueItems);
|
|
1053
|
+
}
|
|
1054
|
+
callback(queueRow, queueOptions);
|
|
1055
|
+
runQueue();
|
|
1056
|
+
};
|
|
1057
|
+
runQueue();
|
|
1058
|
+
}
|
|
1059
|
+
const treeForEachStrategies = {
|
|
1060
|
+
pre: preImpl$1,
|
|
1061
|
+
post: postImpl$1,
|
|
1062
|
+
breadth: breadthImpl$1
|
|
1063
|
+
};
|
|
1064
|
+
//#endregion
|
|
1065
|
+
//#region src/tree/map.ts
|
|
1066
|
+
function preImpl(row, callback, options) {
|
|
1067
|
+
const finalChildrenKey = getFinalChildrenKey(row, options, options);
|
|
1068
|
+
const result = callback(row, options);
|
|
1069
|
+
const children = row[finalChildrenKey];
|
|
1070
|
+
let newChildren;
|
|
1071
|
+
if (TypeUtil.isArray(children)) {
|
|
1072
|
+
const nextLevelOptions = {
|
|
1073
|
+
...options,
|
|
1074
|
+
parents: [...options.parents, row],
|
|
1075
|
+
depth: options.depth + 1
|
|
1076
|
+
};
|
|
1077
|
+
newChildren = children.map((c) => preImpl(c, callback, nextLevelOptions));
|
|
1078
|
+
}
|
|
1079
|
+
return {
|
|
1080
|
+
...result,
|
|
1081
|
+
[finalChildrenKey]: newChildren
|
|
1082
|
+
};
|
|
1083
|
+
}
|
|
1084
|
+
function postImpl(row, callback, options) {
|
|
1085
|
+
const finalChildrenKey = getFinalChildrenKey(row, options, options);
|
|
1086
|
+
const children = row[finalChildrenKey];
|
|
1087
|
+
let newChildren;
|
|
1088
|
+
if (TypeUtil.isArray(children)) {
|
|
1089
|
+
const nextLevelOptions = {
|
|
1090
|
+
...options,
|
|
1091
|
+
parents: [...options.parents, row],
|
|
1092
|
+
depth: options.depth + 1
|
|
1093
|
+
};
|
|
1094
|
+
newChildren = children.map((c) => postImpl(c, callback, nextLevelOptions));
|
|
1095
|
+
}
|
|
1096
|
+
return {
|
|
1097
|
+
...callback(row, options),
|
|
1098
|
+
[finalChildrenKey]: newChildren
|
|
1099
|
+
};
|
|
1100
|
+
}
|
|
1101
|
+
function breadthImpl(row, callback, options) {
|
|
1102
|
+
const queue = [{
|
|
1103
|
+
queueRow: row,
|
|
1104
|
+
queueOptions: options
|
|
1105
|
+
}];
|
|
1106
|
+
const cache = /* @__PURE__ */ new WeakMap();
|
|
1107
|
+
const childrenKeyCache = /* @__PURE__ */ new WeakMap();
|
|
1108
|
+
let result;
|
|
1109
|
+
const runQueue = () => {
|
|
1110
|
+
if (queue.length === 0) return result;
|
|
1111
|
+
const { queueRow, queueOptions } = queue.shift();
|
|
1112
|
+
const finalChildrenKey = getFinalChildrenKey(queueRow, queueOptions, queueOptions);
|
|
1113
|
+
const children = queueRow[finalChildrenKey];
|
|
1114
|
+
if (TypeUtil.isArray(children)) {
|
|
1115
|
+
const nextLevelOptions = {
|
|
1116
|
+
...queueOptions,
|
|
1117
|
+
parents: [...queueOptions.parents, queueRow],
|
|
1118
|
+
depth: queueOptions.depth + 1
|
|
1119
|
+
};
|
|
1120
|
+
const subQueueItems = children.map((queueRow) => ({
|
|
1121
|
+
queueRow,
|
|
1122
|
+
queueOptions: nextLevelOptions
|
|
1123
|
+
}));
|
|
1124
|
+
queue.push(...subQueueItems);
|
|
1125
|
+
}
|
|
1126
|
+
const res = callback(queueRow, queueOptions);
|
|
1127
|
+
cache.set(queueRow, res);
|
|
1128
|
+
childrenKeyCache.set(queueRow, finalChildrenKey);
|
|
1129
|
+
const parent = ArrayUtil.last(queueOptions.parents);
|
|
1130
|
+
if (parent) {
|
|
1131
|
+
const newParent = cache.get(parent);
|
|
1132
|
+
const parentChildrenKey = childrenKeyCache.get(parent);
|
|
1133
|
+
if (newParent && parentChildrenKey) if (newParent[parentChildrenKey]) newParent[parentChildrenKey].push(res);
|
|
1134
|
+
else newParent[parentChildrenKey] = [res];
|
|
1135
|
+
}
|
|
1136
|
+
if (queueOptions.depth === 0) result = res;
|
|
1137
|
+
return runQueue();
|
|
1138
|
+
};
|
|
1139
|
+
return runQueue();
|
|
1140
|
+
}
|
|
1141
|
+
const treeMapStrategies = {
|
|
1142
|
+
pre: preImpl,
|
|
1143
|
+
post: postImpl,
|
|
1144
|
+
breadth: breadthImpl
|
|
1145
|
+
};
|
|
1146
|
+
//#endregion
|
|
1147
|
+
//#region src/tree/treeUtil.ts
|
|
1148
|
+
/**
|
|
1149
|
+
* 树结构工具类
|
|
1150
|
+
*/
|
|
1151
|
+
var TreeUtil = class {
|
|
1152
|
+
/**
|
|
1153
|
+
* 行结构 转 树结构
|
|
1154
|
+
* - 将平铺的数组转换为树形结构
|
|
1155
|
+
*
|
|
1156
|
+
* @param rows 行数据数组
|
|
1157
|
+
* @param options 配置项
|
|
1158
|
+
* @returns 树结构数组
|
|
1159
|
+
* @example
|
|
1160
|
+
* ```ts
|
|
1161
|
+
* const rows = [
|
|
1162
|
+
* { id: 1, parentId: null },
|
|
1163
|
+
* { id: 2, parentId: 1 },
|
|
1164
|
+
* ];
|
|
1165
|
+
* TreeUtil.rowsToTree(rows); // [{ id: 1, parentId: null, children: [{ id: 2, parentId: 1 }] }]
|
|
1166
|
+
* ```
|
|
1167
|
+
*/
|
|
1168
|
+
static rowsToTree(rows, options) {
|
|
1169
|
+
const { parentIdKey = "parentId", rowKey = "id", childrenKey = "children" } = options || {};
|
|
1170
|
+
const result = [];
|
|
1171
|
+
const map = /* @__PURE__ */ new Map();
|
|
1172
|
+
for (const row of rows) {
|
|
1173
|
+
const id = row[rowKey];
|
|
1174
|
+
if (!map.get(id)) map.set(id, row);
|
|
1175
|
+
}
|
|
1176
|
+
for (const row of rows) {
|
|
1177
|
+
const parentId = row[parentIdKey];
|
|
1178
|
+
const parent = map.get(parentId);
|
|
1179
|
+
if (!parent || !parentId) {
|
|
1180
|
+
result.push(row);
|
|
1181
|
+
continue;
|
|
1182
|
+
}
|
|
1183
|
+
const siblings = parent[childrenKey];
|
|
1184
|
+
if (TypeUtil.isNull(siblings) || TypeUtil.isUndefined(siblings)) parent[childrenKey] = [row];
|
|
1185
|
+
else if (Array.isArray(siblings)) siblings.push(row);
|
|
1186
|
+
else {
|
|
1187
|
+
const message = `The key "${childrenKey.toString()}" in parent item is not an array.`;
|
|
1188
|
+
throw new Error(message);
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
return result;
|
|
1192
|
+
}
|
|
1193
|
+
/**
|
|
1194
|
+
* 树结构 转 行结构
|
|
1195
|
+
* - 将树形结构扁平化为数组
|
|
1196
|
+
*
|
|
1197
|
+
* @param tree 树结构数据 (单个节点或节点数组)
|
|
1198
|
+
* @param options 配置项
|
|
1199
|
+
* @returns 扁平化后的数组
|
|
1200
|
+
* @example
|
|
1201
|
+
* ```ts
|
|
1202
|
+
* const tree = [{ id: 1, children: [{ id: 2 }] }];
|
|
1203
|
+
* TreeUtil.treeToRows(tree); // [{ id: 1, children: undefined }, { id: 2, children: undefined }]
|
|
1204
|
+
* ```
|
|
1205
|
+
*/
|
|
1206
|
+
static treeToRows(tree, options = {}) {
|
|
1207
|
+
const { childrenKey = "children" } = options;
|
|
1208
|
+
const result = [];
|
|
1209
|
+
if (!tree) return result;
|
|
1210
|
+
this.forEach(tree, (t) => result.push({
|
|
1211
|
+
...t,
|
|
1212
|
+
[childrenKey]: void 0
|
|
1213
|
+
}), options);
|
|
1214
|
+
return result;
|
|
1215
|
+
}
|
|
1216
|
+
/**
|
|
1217
|
+
* 遍历树节点
|
|
1218
|
+
*
|
|
1219
|
+
* @param tree 树结构数据
|
|
1220
|
+
* @param callback 回调函数
|
|
1221
|
+
* @param options 配置项
|
|
1222
|
+
* @example
|
|
1223
|
+
* ```ts
|
|
1224
|
+
* const tree = [{ id: 1, children: [{ id: 2 }] }];
|
|
1225
|
+
* const ids: number[] = [];
|
|
1226
|
+
* TreeUtil.forEach(tree, (node) => ids.push(node.id)); // ids: [1, 2] (pre-order default)
|
|
1227
|
+
* ```
|
|
1228
|
+
*/
|
|
1229
|
+
static forEach(tree, callback, options = {}) {
|
|
1230
|
+
const { childrenKey = "children", strategy = "pre", getChildrenKey } = options;
|
|
1231
|
+
const traversalMethod = treeForEachStrategies[strategy];
|
|
1232
|
+
const innerOptions = {
|
|
1233
|
+
childrenKey,
|
|
1234
|
+
depth: 0,
|
|
1235
|
+
parents: [],
|
|
1236
|
+
getChildrenKey
|
|
1237
|
+
};
|
|
1238
|
+
if (TypeUtil.isArray(tree)) for (const row of tree) traversalMethod(row, callback, innerOptions);
|
|
1239
|
+
else traversalMethod(tree, callback, innerOptions);
|
|
1240
|
+
}
|
|
1241
|
+
/**
|
|
1242
|
+
* 查找树节点
|
|
1243
|
+
* - 返回第一个回调返回 true 的节点
|
|
1244
|
+
*
|
|
1245
|
+
* @param tree 树结构数据
|
|
1246
|
+
* @param callback 回调函数
|
|
1247
|
+
* @param options 配置项
|
|
1248
|
+
* @returns 找到的节点,未找到则返回 undefined
|
|
1249
|
+
* @example
|
|
1250
|
+
* ```ts
|
|
1251
|
+
* const tree = [{ id: 1, children: [{ id: 2 }] }];
|
|
1252
|
+
* TreeUtil.find(tree, (node) => node.id === 2); // { id: 2, ... }
|
|
1253
|
+
* ```
|
|
1254
|
+
*/
|
|
1255
|
+
static find(tree, callback, options = {}) {
|
|
1256
|
+
const { childrenKey = "children", strategy = "pre", getChildrenKey } = options;
|
|
1257
|
+
const traversalMethod = treeFindStrategies[strategy];
|
|
1258
|
+
const innerOptions = {
|
|
1259
|
+
childrenKey,
|
|
1260
|
+
depth: 0,
|
|
1261
|
+
parents: [],
|
|
1262
|
+
getChildrenKey
|
|
1263
|
+
};
|
|
1264
|
+
if (TypeUtil.isArray(tree)) {
|
|
1265
|
+
for (const row of tree) {
|
|
1266
|
+
const result = traversalMethod(row, callback, innerOptions);
|
|
1267
|
+
if (result) return result;
|
|
1268
|
+
}
|
|
1269
|
+
return;
|
|
1270
|
+
}
|
|
1271
|
+
return traversalMethod(tree, callback, innerOptions);
|
|
1272
|
+
}
|
|
1273
|
+
static filter(tree, callback, options = {}) {
|
|
1274
|
+
const { childrenKey = "children", strategy = "pre", getChildrenKey } = options;
|
|
1275
|
+
const traversalMethod = treeFilterStrategies[strategy];
|
|
1276
|
+
const innerOptions = {
|
|
1277
|
+
childrenKey,
|
|
1278
|
+
depth: 0,
|
|
1279
|
+
parents: [],
|
|
1280
|
+
getChildrenKey
|
|
1281
|
+
};
|
|
1282
|
+
return TypeUtil.isArray(tree) ? tree.map((row) => traversalMethod(row, callback, innerOptions)).filter((t) => !!t) : traversalMethod(tree, callback, innerOptions) || [];
|
|
1283
|
+
}
|
|
1284
|
+
static map(tree, callback, options = {}) {
|
|
1285
|
+
const { childrenKey = "children", strategy = "pre", getChildrenKey } = options;
|
|
1286
|
+
const traversalMethod = treeMapStrategies[strategy];
|
|
1287
|
+
const innerOptions = {
|
|
1288
|
+
childrenKey,
|
|
1289
|
+
depth: 0,
|
|
1290
|
+
parents: [],
|
|
1291
|
+
getChildrenKey
|
|
1292
|
+
};
|
|
1293
|
+
return TypeUtil.isArray(tree) ? tree.map((row) => traversalMethod(row, callback, innerOptions)) : traversalMethod(tree, callback, innerOptions);
|
|
1294
|
+
}
|
|
1295
|
+
};
|
|
1296
|
+
//#endregion
|
|
1297
|
+
//#region src/validate/validateUtil.ts
|
|
1298
|
+
/**
|
|
1299
|
+
* 验证工具类
|
|
1300
|
+
*/
|
|
1301
|
+
var ValidateUtil = class {
|
|
1302
|
+
/**
|
|
1303
|
+
* 验证是否为手机号码
|
|
1304
|
+
*/
|
|
1305
|
+
static isPhone(input) {
|
|
1306
|
+
return this._phone.test(input.toString());
|
|
1307
|
+
}
|
|
1308
|
+
/**
|
|
1309
|
+
* 验证是否为固定电话
|
|
1310
|
+
*/
|
|
1311
|
+
static isTelephone(input) {
|
|
1312
|
+
return this._telephone.test(input.toString());
|
|
1313
|
+
}
|
|
1314
|
+
/**
|
|
1315
|
+
* 验证是否为移动设备识别码
|
|
1316
|
+
*/
|
|
1317
|
+
static isIMEI(input) {
|
|
1318
|
+
return this._IMEI.test(input.toString());
|
|
1319
|
+
}
|
|
1320
|
+
/**
|
|
1321
|
+
* 验证是否为电子邮箱
|
|
1322
|
+
*/
|
|
1323
|
+
static isEmail(input) {
|
|
1324
|
+
return this._email.test(input.toString());
|
|
1325
|
+
}
|
|
1326
|
+
/**
|
|
1327
|
+
* 验证是否为 http(s) 链接
|
|
1328
|
+
*/
|
|
1329
|
+
static isHttpLink(input) {
|
|
1330
|
+
return this._link.test(input.toString());
|
|
1331
|
+
}
|
|
1332
|
+
/**
|
|
1333
|
+
* 验证是否为端口号链接
|
|
1334
|
+
*/
|
|
1335
|
+
static isPortLink(input) {
|
|
1336
|
+
return this._portLink.test(input.toString());
|
|
1337
|
+
}
|
|
1338
|
+
/**
|
|
1339
|
+
* 验证是否为迅雷链接
|
|
1340
|
+
*/
|
|
1341
|
+
static isThunderLink(input) {
|
|
1342
|
+
return this._thunderLink.test(input.toString());
|
|
1343
|
+
}
|
|
1344
|
+
/**
|
|
1345
|
+
* 验证是否为统一社会信用代码
|
|
1346
|
+
*/
|
|
1347
|
+
static isUSCC(input) {
|
|
1348
|
+
return this._uscc.test(input.toString());
|
|
1349
|
+
}
|
|
1350
|
+
/**
|
|
1351
|
+
* 验证是否为统一社会信用代码 - 15位/18位/20位数字/字母
|
|
1352
|
+
*/
|
|
1353
|
+
static isUSCCS(input) {
|
|
1354
|
+
return this._usccs.test(input.toString());
|
|
1355
|
+
}
|
|
1356
|
+
/**
|
|
1357
|
+
* 验证是否为 Windows 系统文件夹路径
|
|
1358
|
+
*/
|
|
1359
|
+
static isDirPathWindows(input) {
|
|
1360
|
+
return this._dirPathWindows.test(input.toString());
|
|
1361
|
+
}
|
|
1362
|
+
/**
|
|
1363
|
+
* 验证是否为 Windows 系统文件路径
|
|
1364
|
+
*/
|
|
1365
|
+
static isFilePathWindows(input) {
|
|
1366
|
+
return this._filePathWindows.test(input.toString());
|
|
1367
|
+
}
|
|
1368
|
+
/**
|
|
1369
|
+
* 验证是否为 Linux 系统文件夹路径
|
|
1370
|
+
*/
|
|
1371
|
+
static isDirPathLinux(input) {
|
|
1372
|
+
return this._dirPathLinux.test(input.toString());
|
|
1373
|
+
}
|
|
1374
|
+
/**
|
|
1375
|
+
* 验证是否为 Linux 系统文件路径
|
|
1376
|
+
*/
|
|
1377
|
+
static isFilePathLinux(input) {
|
|
1378
|
+
return this._filePathLinux.test(input.toString());
|
|
1379
|
+
}
|
|
1380
|
+
/**
|
|
1381
|
+
* 验证是否为新能源车牌号
|
|
1382
|
+
*/
|
|
1383
|
+
static isEVCarNumber(input) {
|
|
1384
|
+
return this._EVCarNumber.test(input.toString());
|
|
1385
|
+
}
|
|
1386
|
+
/**
|
|
1387
|
+
* 验证是否为燃油车车牌号
|
|
1388
|
+
*/
|
|
1389
|
+
static isGVCarNumber(input) {
|
|
1390
|
+
return this._GVCarNumber.test(input.toString());
|
|
1391
|
+
}
|
|
1392
|
+
/**
|
|
1393
|
+
* 验证是否为中文姓名
|
|
1394
|
+
*/
|
|
1395
|
+
static isChineseName(input) {
|
|
1396
|
+
return this._chineseName.test(input.toString());
|
|
1397
|
+
}
|
|
1398
|
+
/**
|
|
1399
|
+
* 验证是否为中国身份证号
|
|
1400
|
+
*/
|
|
1401
|
+
static isChineseID(input) {
|
|
1402
|
+
return this._chineseId.test(input.toString());
|
|
1403
|
+
}
|
|
1404
|
+
/**
|
|
1405
|
+
* 验证是否为中国省份
|
|
1406
|
+
*/
|
|
1407
|
+
static isChineseProvince(input) {
|
|
1408
|
+
return this._chineseProvince.test(input.toString());
|
|
1409
|
+
}
|
|
1410
|
+
/**
|
|
1411
|
+
* 验证是否为中华民族
|
|
1412
|
+
*/
|
|
1413
|
+
static isChineseNation(input) {
|
|
1414
|
+
return this._chineseNation.test(input.toString());
|
|
1415
|
+
}
|
|
1416
|
+
/**
|
|
1417
|
+
* 验证是否只包含字母
|
|
1418
|
+
*/
|
|
1419
|
+
static isLetter(input) {
|
|
1420
|
+
return this._letter.test(input.toString());
|
|
1421
|
+
}
|
|
1422
|
+
/**
|
|
1423
|
+
* 验证是否只包含小写字母
|
|
1424
|
+
*/
|
|
1425
|
+
static isLetterLowercase(input) {
|
|
1426
|
+
return this._letterLowercase.test(input.toString());
|
|
1427
|
+
}
|
|
1428
|
+
/**
|
|
1429
|
+
* 验证是否只包含大写字母
|
|
1430
|
+
*/
|
|
1431
|
+
static isLetterUppercase(input) {
|
|
1432
|
+
return this._letterUppercase.test(input.toString());
|
|
1433
|
+
}
|
|
1434
|
+
/**
|
|
1435
|
+
* 验证是否不包含字母
|
|
1436
|
+
*/
|
|
1437
|
+
static isLetterOmit(input) {
|
|
1438
|
+
return this._letterOmit.test(input.toString());
|
|
1439
|
+
}
|
|
1440
|
+
/**
|
|
1441
|
+
* 验证是否为数字和字母组合
|
|
1442
|
+
*/
|
|
1443
|
+
static isLetterAndNumber(input) {
|
|
1444
|
+
return this._LetterAndNumber.test(input.toString());
|
|
1445
|
+
}
|
|
1446
|
+
/**
|
|
1447
|
+
* 验证是否为有符号浮点数
|
|
1448
|
+
*/
|
|
1449
|
+
static isSignedFloat(input) {
|
|
1450
|
+
return this._signedFloat.test(input.toString());
|
|
1451
|
+
}
|
|
1452
|
+
/**
|
|
1453
|
+
* 验证是否为无符号浮点数
|
|
1454
|
+
*/
|
|
1455
|
+
static isUnsignedFloat(input) {
|
|
1456
|
+
return this._unsignedFloat.test(input.toString());
|
|
1457
|
+
}
|
|
1458
|
+
/**
|
|
1459
|
+
* 验证是否为有符号整数
|
|
1460
|
+
*/
|
|
1461
|
+
static isSignedInteger(input) {
|
|
1462
|
+
return this._signedInteger.test(input.toString());
|
|
1463
|
+
}
|
|
1464
|
+
/**
|
|
1465
|
+
* 验证是否为无符号整数
|
|
1466
|
+
*/
|
|
1467
|
+
static isUnsignedInteger(input) {
|
|
1468
|
+
return this._unsignedInteger.test(input.toString());
|
|
1469
|
+
}
|
|
1470
|
+
/**
|
|
1471
|
+
* 验证是否包含空格
|
|
1472
|
+
*/
|
|
1473
|
+
static isSpaceInclude(input) {
|
|
1474
|
+
return this._spaceInclude.test(input.toString());
|
|
1475
|
+
}
|
|
1476
|
+
/**
|
|
1477
|
+
* 验证是否以空格开头
|
|
1478
|
+
*/
|
|
1479
|
+
static isSpaceStart(input) {
|
|
1480
|
+
return this._spaceStart.test(input.toString());
|
|
1481
|
+
}
|
|
1482
|
+
/**
|
|
1483
|
+
* 验证是否以空格结尾
|
|
1484
|
+
*/
|
|
1485
|
+
static isSpaceEnd(input) {
|
|
1486
|
+
return this._spaceEnd.test(input.toString());
|
|
1487
|
+
}
|
|
1488
|
+
/**
|
|
1489
|
+
* 验证是否以空格开头或结尾
|
|
1490
|
+
*/
|
|
1491
|
+
static isSpaceStartOrEnd(input) {
|
|
1492
|
+
return this.isSpaceStart(input) || this.isSpaceEnd(input);
|
|
1493
|
+
}
|
|
1494
|
+
};
|
|
1495
|
+
_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}$/);
|
|
1496
|
+
_defineProperty(ValidateUtil, "_telephone", /^(((0\d{2,3})-)?((\d{7,8})|(400\d{7})|(800\d{7}))(-(\d{1,4}))?)$/);
|
|
1497
|
+
_defineProperty(ValidateUtil, "_IMEI", /^\d{15,17}$/);
|
|
1498
|
+
_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);
|
|
1499
|
+
_defineProperty(ValidateUtil, "_link", /^(https?:\/\/)?(([\w-]+(\.[\w-]+)*\.[a-z]{2,6})|((\d{1,3}\.){3}\d{1,3}))(:\d+)?(\/\S*)?$/i);
|
|
1500
|
+
_defineProperty(ValidateUtil, "_portLink", /^(https?:\/\/)?[\w-]+(\.[\w-]+)+:\d{1,5}\/?$/i);
|
|
1501
|
+
_defineProperty(ValidateUtil, "_thunderLink", /^thunderx?:\/\/[a-zA-Z\d]+=$/i);
|
|
1502
|
+
_defineProperty(ValidateUtil, "_uscc", /^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/);
|
|
1503
|
+
_defineProperty(ValidateUtil, "_usccs", /^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/);
|
|
1504
|
+
_defineProperty(ValidateUtil, "_dirPathWindows", /^[a-z]:\\(?:\w+\\?)*$/i);
|
|
1505
|
+
_defineProperty(ValidateUtil, "_filePathWindows", /^[a-z]:\\(?:\w+\\)*\w+\.\w+$/i);
|
|
1506
|
+
_defineProperty(ValidateUtil, "_dirPathLinux", /^\/(?:[^\\/\s]+\/)*$/);
|
|
1507
|
+
_defineProperty(ValidateUtil, "_filePathLinux", /^(\/$|\/(?:[^\\/\s]+\/)*[^\\/\s]+$)/);
|
|
1508
|
+
_defineProperty(ValidateUtil, "_EVCarNumber", /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z](([DF]((?![IO])[a-zA-Z0-9](?![IO]))\d{4})|(\d{5}[DF]))$/);
|
|
1509
|
+
_defineProperty(ValidateUtil, "_GVCarNumber", /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$/);
|
|
1510
|
+
_defineProperty(ValidateUtil, "_chineseName", /^[一-龢][一·-龢]*$/);
|
|
1511
|
+
_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);
|
|
1512
|
+
_defineProperty(ValidateUtil, "_chineseProvince", /^安徽|澳门|北京|重庆|福建|甘肃|广东|广西|贵州|海南|河北|河南|黑龙江|湖北|湖南|吉林|江苏|江西|辽宁|内蒙古|宁夏|青海|山东|山西|陕西|上海|四川|台湾|天津|西藏|香港|新疆|云南|浙江$/);
|
|
1513
|
+
_defineProperty(ValidateUtil, "_chineseNation", /^汉族|蒙古族|回族|藏族|维吾尔族|苗族|彝族|壮族|布依族|朝鲜族|满族|侗族|瑶族|白族|土家族|哈尼族|哈萨克族|傣族|黎族|傈僳族|佤族|畲族|高山族|拉祜族|水族|东乡族|纳西族|景颇族|柯尔克孜族|土族|达斡尔族|仫佬族|羌族|布朗族|撒拉族|毛南族|仡佬族|锡伯族|阿昌族|普米族|塔吉克族|怒族|乌孜别克族|俄罗斯族|鄂温克族|德昂族|保安族|裕固族|京族|塔塔尔族|独龙族|鄂伦春族|赫哲族|门巴族|珞巴族|基诺族|其它未识别民族|外国人入中国籍$/);
|
|
1514
|
+
_defineProperty(ValidateUtil, "_letter", /^[a-z]+$/i);
|
|
1515
|
+
_defineProperty(ValidateUtil, "_letterLowercase", /^[a-z]+$/);
|
|
1516
|
+
_defineProperty(ValidateUtil, "_letterUppercase", /^[A-Z]+$/);
|
|
1517
|
+
_defineProperty(ValidateUtil, "_letterOmit", /^[^A-Z]*$/i);
|
|
1518
|
+
_defineProperty(ValidateUtil, "_LetterAndNumber", /^[A-Z0-9]+$/i);
|
|
1519
|
+
_defineProperty(ValidateUtil, "_signedFloat", /^[+-]?(\d+(\.\d+)?|\.\d+)$/);
|
|
1520
|
+
_defineProperty(ValidateUtil, "_unsignedFloat", /^\+?(\d+(\.\d+)?|\.\d+)$/);
|
|
1521
|
+
_defineProperty(ValidateUtil, "_signedInteger", /^[+-]?\d+$/);
|
|
1522
|
+
_defineProperty(ValidateUtil, "_unsignedInteger", /^\+?\d+$/);
|
|
1523
|
+
_defineProperty(ValidateUtil, "_spaceInclude", /\s/);
|
|
1524
|
+
_defineProperty(ValidateUtil, "_spaceStart", /^\s/);
|
|
1525
|
+
_defineProperty(ValidateUtil, "_spaceEnd", /\s$/);
|
|
1526
|
+
//#endregion
|
|
1527
|
+
export { ArrayUtil, DateTimeUtil, EnvUtil, FunctionUtil, MimeUtil, NumberUtil, ObjectUtil, StringUtil, ThemeUtil, TreeUtil, TypeUtil, ValidateUtil };
|