@pawover/kit 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +123 -0
- package/README.md +145 -1
- package/entry/eslint-rules.cjs +1 -0
- package/entry/eslint-rules.d.cts +1 -0
- package/entry/eslint-rules.d.ts +2 -0
- package/entry/eslint-rules.js +2 -0
- package/entry/hooks-alova.cjs +1 -0
- package/entry/hooks-alova.d.cts +1 -0
- package/entry/hooks-alova.d.ts +1 -0
- package/entry/hooks-alova.js +1 -0
- package/entry/hooks-react.cjs +1 -0
- package/entry/hooks-react.d.cts +1 -0
- package/entry/hooks-react.d.ts +1 -0
- package/entry/hooks-react.js +1 -0
- package/entry/index.cjs +1 -0
- package/entry/index.d.cts +1 -0
- package/entry/index.d.ts +1 -0
- package/entry/index.js +1 -0
- package/entry/math.cjs +1 -0
- package/entry/math.d.cts +1 -0
- package/entry/math.d.ts +1 -0
- package/entry/math.js +1 -0
- package/entry/types-react.cjs +1 -0
- package/entry/types-react.d.cts +1 -0
- package/entry/types-react.d.ts +1 -0
- package/entry/types-react.js +1 -0
- package/entry/types.cjs +1 -0
- package/entry/types.d.cts +1 -0
- package/entry/types.d.ts +1 -0
- package/entry/types.js +1 -0
- package/entry/vite.cjs +1 -0
- package/entry/vite.d.cts +1 -0
- package/entry/vite.d.ts +1 -0
- package/entry/vite.js +1 -0
- package/entry/zod.cjs +1 -0
- package/entry/zod.d.cts +1 -0
- package/entry/zod.d.ts +1 -0
- package/entry/zod.js +1 -0
- package/package.json +81 -96
- package/packages/eslint-rules/dist/core.antfu.cjs +0 -78
- package/packages/eslint-rules/dist/core.antfu.d.cts +0 -1
- package/packages/eslint-rules/dist/core.antfu.d.ts +0 -74
- package/packages/eslint-rules/dist/core.antfu.js +0 -79
- package/packages/eslint-rules/dist/core.imports.cjs +0 -59
- package/packages/eslint-rules/dist/core.imports.d.cts +0 -1
- package/packages/eslint-rules/dist/core.imports.d.ts +0 -61
- package/packages/eslint-rules/dist/core.imports.js +0 -59
- package/packages/eslint-rules/dist/core.importsSort.cjs +0 -7
- package/packages/eslint-rules/dist/core.importsSort.d.cts +0 -1
- package/packages/eslint-rules/dist/core.importsSort.d.ts +0 -7
- package/packages/eslint-rules/dist/core.importsSort.js +0 -7
- package/packages/eslint-rules/dist/core.javascript.cjs +0 -1208
- package/packages/eslint-rules/dist/core.javascript.d.cts +0 -1
- package/packages/eslint-rules/dist/core.javascript.d.ts +0 -1216
- package/packages/eslint-rules/dist/core.javascript.js +0 -1208
- package/packages/eslint-rules/dist/core.react.cjs +0 -551
- package/packages/eslint-rules/dist/core.react.d.cts +0 -1
- package/packages/eslint-rules/dist/core.react.d.ts +0 -551
- package/packages/eslint-rules/dist/core.react.js +0 -551
- package/packages/eslint-rules/dist/core.reactHooks.cjs +0 -107
- package/packages/eslint-rules/dist/core.reactHooks.d.cts +0 -1
- package/packages/eslint-rules/dist/core.reactHooks.d.ts +0 -107
- package/packages/eslint-rules/dist/core.reactHooks.js +0 -107
- package/packages/eslint-rules/dist/core.stylistic.cjs +0 -608
- package/packages/eslint-rules/dist/core.stylistic.d.cts +0 -1
- package/packages/eslint-rules/dist/core.stylistic.d.ts +0 -595
- package/packages/eslint-rules/dist/core.stylistic.js +0 -608
- package/packages/eslint-rules/dist/core.typescript.cjs +0 -905
- package/packages/eslint-rules/dist/core.typescript.d.cts +0 -1
- package/packages/eslint-rules/dist/core.typescript.d.ts +0 -917
- package/packages/eslint-rules/dist/core.typescript.js +0 -906
- package/packages/eslint-rules/dist/core.vue.cjs +0 -312
- package/packages/eslint-rules/dist/core.vue.d.cts +0 -1
- package/packages/eslint-rules/dist/core.vue.d.ts +0 -293
- package/packages/eslint-rules/dist/core.vue.js +0 -313
- package/packages/eslint-rules/dist/index.cjs +0 -126
- package/packages/eslint-rules/dist/index.d.cts +0 -1
- package/packages/eslint-rules/dist/index.d.ts +0 -113
- package/packages/eslint-rules/dist/index.js +0 -126
- package/packages/eslint-rules/dist/preset-Bk6lxNP0.cjs +0 -107
- package/packages/eslint-rules/dist/preset-CQJST7pW.js +0 -102
- package/packages/hooks/dist/alova.cjs +0 -53
- package/packages/hooks/dist/alova.d.cts +0 -1
- package/packages/hooks/dist/alova.d.ts +0 -33
- package/packages/hooks/dist/alova.js +0 -50
- package/packages/hooks/dist/index.cjs +0 -0
- package/packages/hooks/dist/index.d.cts +0 -1
- package/packages/hooks/dist/index.d.ts +0 -1
- package/packages/hooks/dist/index.js +0 -0
- package/packages/hooks/dist/react.cjs +0 -169
- package/packages/hooks/dist/react.d.cts +0 -1
- package/packages/hooks/dist/react.d.ts +0 -95
- package/packages/hooks/dist/react.js +0 -163
- package/packages/types/dist/index.cjs +0 -0
- package/packages/types/dist/index.d.cts +0 -1
- package/packages/types/dist/index.d.ts +0 -271
- package/packages/types/dist/index.js +0 -0
- package/packages/types/dist/react.cjs +0 -0
- package/packages/types/dist/react.d.cts +0 -1
- package/packages/types/dist/react.d.ts +0 -64
- package/packages/types/dist/react.js +0 -0
- package/packages/utils/dist/index.cjs +0 -2300
- package/packages/utils/dist/index.d.cts +0 -1
- package/packages/utils/dist/index.d.ts +0 -2740
- package/packages/utils/dist/index.js +0 -2287
- package/packages/utils/dist/math-BznvO4qI.cjs +0 -1029
- package/packages/utils/dist/math-DRbCtLQH.js +0 -1012
- package/packages/utils/dist/math.cjs +0 -3
- package/packages/utils/dist/math.d.cts +0 -1
- package/packages/utils/dist/math.d.ts +0 -60
- package/packages/utils/dist/math.js +0 -2
- package/packages/utils/dist/vite.cjs +0 -44
- package/packages/utils/dist/vite.d.cts +0 -1
- package/packages/utils/dist/vite.d.ts +0 -28
- package/packages/utils/dist/vite.js +0 -43
- package/packages/zod/dist/index.cjs +0 -91
- package/packages/zod/dist/index.d.cts +0 -1
- package/packages/zod/dist/index.d.ts +0 -57
- package/packages/zod/dist/index.js +0 -61
- /package/{packages/hooks/dist/metadata.json → entry/hooks-metadata.json} +0 -0
- /package/{packages/utils/dist → entry}/metadata.json +0 -0
|
@@ -1,2287 +0,0 @@
|
|
|
1
|
-
import { n as StringUtil, r as TypeUtil, t as MathUtil } from "./math-DRbCtLQH.js";
|
|
2
|
-
//#region src/array/arrayUtil.ts
|
|
3
|
-
/**
|
|
4
|
-
* 数组工具类
|
|
5
|
-
*/
|
|
6
|
-
var ArrayUtil = class {
|
|
7
|
-
static cast(candidate, checkNullish = true) {
|
|
8
|
-
if (checkNullish && TypeUtil.isNullish(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
|
-
* // 重载 1: 按元素本身比较(自动去重)
|
|
72
|
-
* ArrayUtil.difference([1, 2, 3], [2, 3, 4]); // [1]
|
|
73
|
-
* ArrayUtil.difference([1, 1, 2], [2]); // [1],重复项会被去重
|
|
74
|
-
*
|
|
75
|
-
* // 重载 2: 按 match 结果比较(不去重,保留 initialList 原始重复项与顺序)
|
|
76
|
-
* ArrayUtil.difference([{ id: 1 }, { id: 2 }], [{ id: 2 }], (x) => x.id); // [{ id: 1 }]
|
|
77
|
-
* ArrayUtil.difference([{ id: 1 }, { id: 1 }], [{ id: 2 }], (x) => x.id); // [{ id: 1 }, { id: 1 }]
|
|
78
|
-
* ```
|
|
79
|
-
*/
|
|
80
|
-
static difference(initialList, diffList, match) {
|
|
81
|
-
if (!TypeUtil.isArray(initialList) && !TypeUtil.isArray(diffList)) return [];
|
|
82
|
-
if (!TypeUtil.isArray(initialList) || !initialList.length) return [];
|
|
83
|
-
if (!TypeUtil.isArray(diffList) || !diffList.length) return [...initialList];
|
|
84
|
-
if (!TypeUtil.isFunction(match)) {
|
|
85
|
-
const arraySet = new Set(diffList);
|
|
86
|
-
return Array.from(new Set(initialList.filter((item) => !arraySet.has(item))));
|
|
87
|
-
}
|
|
88
|
-
const map = /* @__PURE__ */ new Map();
|
|
89
|
-
diffList.forEach((item, index) => {
|
|
90
|
-
map.set(match(item, index), true);
|
|
91
|
-
});
|
|
92
|
-
return initialList.filter((item, index) => !map.get(match(item, index)));
|
|
93
|
-
}
|
|
94
|
-
static intersection(initialList, diffList, match) {
|
|
95
|
-
if (!TypeUtil.isArray(initialList) || !TypeUtil.isArray(diffList)) return [];
|
|
96
|
-
if (!initialList.length || !diffList.length) return [];
|
|
97
|
-
if (!TypeUtil.isFunction(match)) {
|
|
98
|
-
const diffSet = new Set(diffList);
|
|
99
|
-
return initialList.filter((item) => diffSet.has(item));
|
|
100
|
-
}
|
|
101
|
-
const diffKeys = new Set(diffList.map((item, index) => match(item, index)));
|
|
102
|
-
return initialList.filter((item, index) => diffKeys.has(match(item, index)));
|
|
103
|
-
}
|
|
104
|
-
static merge(initialList, mergeList, match) {
|
|
105
|
-
if (!TypeUtil.isArray(initialList)) return [];
|
|
106
|
-
if (!TypeUtil.isArray(mergeList)) return [...initialList];
|
|
107
|
-
if (!TypeUtil.isFunction(match)) return Array.from(/* @__PURE__ */ new Set([...initialList, ...mergeList]));
|
|
108
|
-
const keys = /* @__PURE__ */ new Map();
|
|
109
|
-
mergeList.forEach((item, index) => {
|
|
110
|
-
keys.set(match(item, index), item);
|
|
111
|
-
});
|
|
112
|
-
return initialList.map((prevItem, index) => {
|
|
113
|
-
const key = match(prevItem, index);
|
|
114
|
-
return keys.has(key) ? keys.get(key) : prevItem;
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
static pick(initialList, filter, mapper) {
|
|
118
|
-
if (!TypeUtil.isArray(initialList)) return [];
|
|
119
|
-
if (!TypeUtil.isFunction(filter)) return [...initialList];
|
|
120
|
-
const hasMapper = TypeUtil.isFunction(mapper);
|
|
121
|
-
return initialList.reduce((prev, curr, index) => {
|
|
122
|
-
if (!filter(curr, index)) return prev;
|
|
123
|
-
if (hasMapper) prev.push(mapper(curr, index));
|
|
124
|
-
else prev.push(curr);
|
|
125
|
-
return prev;
|
|
126
|
-
}, []);
|
|
127
|
-
}
|
|
128
|
-
static replace(initialList, newItem, match) {
|
|
129
|
-
if (!TypeUtil.isArray(initialList) || !initialList.length) return [];
|
|
130
|
-
if (!TypeUtil.isFunction(match)) return [...initialList];
|
|
131
|
-
for (let i = 0; i < initialList.length; i++) {
|
|
132
|
-
const item = initialList[i];
|
|
133
|
-
if (match(item, i)) return [
|
|
134
|
-
...initialList.slice(0, i),
|
|
135
|
-
newItem,
|
|
136
|
-
...initialList.slice(i + 1, initialList.length)
|
|
137
|
-
];
|
|
138
|
-
}
|
|
139
|
-
return [...initialList];
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* 数组项替换并移动
|
|
143
|
-
* - 在给定的数组中,替换并移动符合匹配函数结果的项目
|
|
144
|
-
* - 只替换和移动第一个匹配项
|
|
145
|
-
* - 未匹配时,根据 `position` 在指定位置插入 `newItem`
|
|
146
|
-
* - ⚠️ `position` 为负数或非正整数(如 `-1`、`2.5`)时不生效,静默回退为 `push`(追加到末尾)
|
|
147
|
-
*
|
|
148
|
-
* @param initialList 初始数组
|
|
149
|
-
* @param newItem 替换项
|
|
150
|
-
* @param match 匹配函数
|
|
151
|
-
* @param position 移动位置,可选 `start` | `end` | 索引位置, 默认为 `end`
|
|
152
|
-
* @returns
|
|
153
|
-
* @example
|
|
154
|
-
* ```ts
|
|
155
|
-
* ArrayUtil.replaceMove([1, 2, 3, 4], 5, (n) => n === 2, 0); // [5, 1, 3, 4]
|
|
156
|
-
* ArrayUtil.replaceMove([1, 2, 3, 4], 5, (n) => n === 2, 2); // [1, 3, 5, 4]
|
|
157
|
-
* ArrayUtil.replaceMove([1, 2, 3, 4], 5, (n) => n === 2, "start"); // [5, 1, 3, 4]
|
|
158
|
-
* ArrayUtil.replaceMove([1, 2, 3, 4], 5, (n) => n === 2); // [1, 3, 4, 5]
|
|
159
|
-
*
|
|
160
|
-
* // position 为负数 → 静默回退为 push
|
|
161
|
-
* ArrayUtil.replaceMove([1, 2, 3, 4], 5, (n) => n === 2, -1); // [1, 3, 4, 5]
|
|
162
|
-
* ```
|
|
163
|
-
*/
|
|
164
|
-
static replaceMove(initialList, newItem, match, position) {
|
|
165
|
-
if (!TypeUtil.isArray(initialList)) return [];
|
|
166
|
-
if (!initialList.length) return [newItem];
|
|
167
|
-
if (!TypeUtil.isFunction(match)) return [...initialList];
|
|
168
|
-
const result = [...initialList];
|
|
169
|
-
const matchIndex = initialList.findIndex(match);
|
|
170
|
-
if (matchIndex !== -1) result.splice(matchIndex, 1);
|
|
171
|
-
if (position === "start") result.unshift(newItem);
|
|
172
|
-
else if (position === 0 || TypeUtil.isPositiveInteger(position, false)) result.splice(Math.min(position, result.length), 0, newItem);
|
|
173
|
-
else result.push(newItem);
|
|
174
|
-
return result;
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* 数组切分
|
|
178
|
-
* - 将数组以指定的长度切分后,组合在高维数组中
|
|
179
|
-
*
|
|
180
|
-
* @param initialList 初始数组
|
|
181
|
-
* @param size 分割尺寸,默认 `10`
|
|
182
|
-
* @returns 切分后的二维数组
|
|
183
|
-
* @example
|
|
184
|
-
* ```ts
|
|
185
|
-
* ArrayUtil.split([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
|
|
186
|
-
* ```
|
|
187
|
-
*/
|
|
188
|
-
static split(initialList, size = 10) {
|
|
189
|
-
if (!TypeUtil.isArray(initialList)) return [];
|
|
190
|
-
if (!TypeUtil.isPositiveInteger(size, false)) return [];
|
|
191
|
-
const count = Math.ceil(initialList.length / size);
|
|
192
|
-
return Array.from({ length: count }).fill(null).map((_c, i) => {
|
|
193
|
-
return initialList.slice(i * size, i * size + size);
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* 数组分组过滤
|
|
198
|
-
* - 给定一个数组和一个条件,返回一个由两个数组组成的元组,其中第一个数组包含所有满足条件的项,第二个数组包含所有不满足条件的项
|
|
199
|
-
*
|
|
200
|
-
* @param initialList 初始数组
|
|
201
|
-
* @param match 条件匹配函数
|
|
202
|
-
* @returns [满足条件的项[], 不满足条件的项[]]
|
|
203
|
-
* @example
|
|
204
|
-
* ```ts
|
|
205
|
-
* ArrayUtil.fork([1, 2, 3, 4], (n) => n % 2 === 0); // [[2, 4], [1, 3]]
|
|
206
|
-
* ```
|
|
207
|
-
*/
|
|
208
|
-
static fork(initialList, match) {
|
|
209
|
-
const forked = [[], []];
|
|
210
|
-
if (TypeUtil.isArray(initialList)) initialList.forEach((item, index) => {
|
|
211
|
-
forked[match(item, index) ? 0 : 1].push(item);
|
|
212
|
-
});
|
|
213
|
-
return forked;
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* 数组解压
|
|
217
|
-
* - `ArrayUtil.zip` 的反向操作
|
|
218
|
-
* - 默认按最长数组补齐 `undefined`
|
|
219
|
-
*
|
|
220
|
-
* @param arrayList 压缩后的数组
|
|
221
|
-
* @param options 配置项(`truncate` 为 `true` 时按最短数组截断)
|
|
222
|
-
* @returns 解压后的二维数组
|
|
223
|
-
* @example
|
|
224
|
-
* ```ts
|
|
225
|
-
* ArrayUtil.unzip([[1, "a"], [2, "b"]]); // [[1, 2], ["a", "b"]]
|
|
226
|
-
*
|
|
227
|
-
* // 补齐语义
|
|
228
|
-
* ArrayUtil.unzip([[1, 2], [3]]); // [[1, 3], [2, undefined]]
|
|
229
|
-
*
|
|
230
|
-
* // 截断语义
|
|
231
|
-
* ArrayUtil.unzip([[1, 2], [3]], { truncate: true }); // [[1, 3]]
|
|
232
|
-
* ```
|
|
233
|
-
*/
|
|
234
|
-
static unzip(arrayList, options) {
|
|
235
|
-
if (!TypeUtil.isArray(arrayList) || !arrayList.length) return [];
|
|
236
|
-
const length = options?.truncate ? arrayList.reduce((min, arr) => Math.min(min, arr.length), Infinity) : arrayList.reduce((max, arr) => Math.max(max, arr.length), 0);
|
|
237
|
-
const out = new Array(length);
|
|
238
|
-
let index = 0;
|
|
239
|
-
const get = (array) => array[index];
|
|
240
|
-
for (; index < out.length; index++) out[index] = Array.from(arrayList, get);
|
|
241
|
-
return out;
|
|
242
|
-
}
|
|
243
|
-
static zip(...arraysAndOptions) {
|
|
244
|
-
const last = arraysAndOptions[arraysAndOptions.length - 1];
|
|
245
|
-
const options = last && !TypeUtil.isArray(last) ? last : void 0;
|
|
246
|
-
const arrays = options ? arraysAndOptions.slice(0, -1) : arraysAndOptions;
|
|
247
|
-
return this.unzip(arrays, options);
|
|
248
|
-
}
|
|
249
|
-
static zipToObject(keys, values) {
|
|
250
|
-
const result = {};
|
|
251
|
-
if (!TypeUtil.isArray(keys) || !keys.length) return result;
|
|
252
|
-
const getValue = TypeUtil.isFunction(values) ? values : TypeUtil.isArray(values) ? (_k, i) => values[i] : (_k, _i) => values;
|
|
253
|
-
return keys.reduce((acc, key, idx) => {
|
|
254
|
-
Object.defineProperty(acc, key, {
|
|
255
|
-
value: getValue(key, idx),
|
|
256
|
-
enumerable: true,
|
|
257
|
-
writable: true,
|
|
258
|
-
configurable: true
|
|
259
|
-
});
|
|
260
|
-
return acc;
|
|
261
|
-
}, result);
|
|
262
|
-
}
|
|
263
|
-
};
|
|
264
|
-
//#endregion
|
|
265
|
-
//#region src/currency/currencyUtil.ts
|
|
266
|
-
/**
|
|
267
|
-
* 货币工具类
|
|
268
|
-
* - 基于 [`Intl.NumberFormat`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat) 进行本地化格式化
|
|
269
|
-
* - 支持精确小数位处理(依赖 `mathjs`)
|
|
270
|
-
*/
|
|
271
|
-
var CurrencyUtil = class {
|
|
272
|
-
/**
|
|
273
|
-
* 货币代码到 Locale 的映射枚举
|
|
274
|
-
* - 键为 ISO 4217 货币代码,值为 BCP 47 语言标签
|
|
275
|
-
* - 用于 `Intl.NumberFormat` 的本地化数字格式化
|
|
276
|
-
* - 格式示例基于 `Intl.NumberFormat` 对 `1,234,567.89` 的输出
|
|
277
|
-
*
|
|
278
|
-
* @example
|
|
279
|
-
* ```ts
|
|
280
|
-
* import { CurrencyUtil } from "@pawover/kit/utils";
|
|
281
|
-
* // 获取人民币的格式化 locale
|
|
282
|
-
* const locale = CurrencyUtil.CURRENCY_ENUM.CNY; // "zh-CN"
|
|
283
|
-
*
|
|
284
|
-
* // 结合 Intl.NumberFormat 使用
|
|
285
|
-
* new Intl.NumberFormat(CurrencyUtil.CURRENCY_ENUM.USD, {
|
|
286
|
-
* style: "currency",
|
|
287
|
-
* currency: "USD",
|
|
288
|
-
* }).format(1234.56); // "$1,234.56"
|
|
289
|
-
* ```
|
|
290
|
-
*/
|
|
291
|
-
static CURRENCY_ENUM = {
|
|
292
|
-
/** 美元(美国及美元化国家,Locale: 美式英语) → 1,234,567.89 */
|
|
293
|
-
USD: "en-US",
|
|
294
|
-
/** 加拿大元(加拿大,Locale: 加拿大英语) → 1,234,567.89 */
|
|
295
|
-
CAD: "en-CA",
|
|
296
|
-
/** 墨西哥比索(墨西哥,Locale: 墨西哥西班牙语) → 1,234,567.89 */
|
|
297
|
-
MXN: "es-MX",
|
|
298
|
-
/** 巴西雷亚尔(巴西,Locale: 巴西葡萄牙语) → 1.234.567,89 */
|
|
299
|
-
BRL: "pt-BR",
|
|
300
|
-
/** 阿根廷比索(阿根廷,Locale: 阿根廷西班牙语) → 1.234.567,89 */
|
|
301
|
-
ARS: "es-AR",
|
|
302
|
-
/** 智利比索(智利,Locale: 智利西班牙语) → 1.234.567,89 */
|
|
303
|
-
CLP: "es-CL",
|
|
304
|
-
/** 秘鲁新索尔(秘鲁,Locale: 秘鲁西班牙语) → 1,234,567.89 */
|
|
305
|
-
PEN: "es-PE",
|
|
306
|
-
/** 哥伦比亚比索(哥伦比亚,Locale: 哥伦比亚西班牙语) → 1.234.567,89 */
|
|
307
|
-
COP: "es-CO",
|
|
308
|
-
/** 哥斯达黎加科朗(哥斯达黎加,Locale: 哥斯达黎加西班牙语) → 1 234 567,89(空格千分位) */
|
|
309
|
-
CRC: "es-CR",
|
|
310
|
-
/** 人民币(中国,Locale: 简体中文) → 1,234,567.89 */
|
|
311
|
-
CNY: "zh-CN",
|
|
312
|
-
/** 港元(中国香港,Locale: 繁体中文+香港) → 1,234,567.89 */
|
|
313
|
-
HKD: "zh-HK",
|
|
314
|
-
/** 澳门元(中国澳门,Locale: 繁体中文+澳门) → 1,234,567.89 */
|
|
315
|
-
MOP: "zh-MO",
|
|
316
|
-
/** 日元(日本,Locale: 日语) → 1,234,567.89 */
|
|
317
|
-
JPY: "ja-JP",
|
|
318
|
-
/** 韩元(韩国,Locale: 韩语) → 1,234,567.89 */
|
|
319
|
-
KRW: "ko-KR",
|
|
320
|
-
/** 新加坡元(新加坡,Locale: 英语+新加坡) → 1,234,567.89 */
|
|
321
|
-
SGD: "en-SG",
|
|
322
|
-
/** 泰铢(泰国,Locale: 泰语) → 1,234,567.89 */
|
|
323
|
-
THB: "th-TH",
|
|
324
|
-
/** 印度卢比(印度,Locale: 英语+印度) → 12,34,567.89(2,2,3 分组) */
|
|
325
|
-
INR: "en-IN",
|
|
326
|
-
/** 沙特里亚尔(沙特,Locale: 阿拉伯语+沙特) → ١٬٢٣٤٬٥٦٧٫٨٩(阿拉伯数字) */
|
|
327
|
-
SAR: "ar-SA",
|
|
328
|
-
/** 阿联酋迪拉姆(阿联酋,Locale: 阿拉伯语+阿联酋) → 1,234,567.89 */
|
|
329
|
-
AED: "ar-AE",
|
|
330
|
-
/** 印尼盾(印尼,Locale: 印尼语) → 1.234.567,89 */
|
|
331
|
-
IDR: "id-ID",
|
|
332
|
-
/** 马来西亚林吉特(马来西亚,Locale: 马来语) → 1,234,567.89 */
|
|
333
|
-
MYR: "ms-MY",
|
|
334
|
-
/** 越南盾(越南,Locale: 越南语) → 1.234.567,89 */
|
|
335
|
-
VND: "vi-VN",
|
|
336
|
-
/** 菲律宾比索(菲律宾,Locale: 英语+菲律宾) → 1,234,567.89 */
|
|
337
|
-
PHP: "en-PH",
|
|
338
|
-
/** 巴基斯坦卢比(巴基斯坦,Locale: 英语+巴基斯坦) → 1,234,567.89 */
|
|
339
|
-
PKR: "en-PK",
|
|
340
|
-
/** 新台币(中国台湾地区,Locale: 繁体中文+台湾) → 1,234,567.89 */
|
|
341
|
-
TWD: "zh-TW",
|
|
342
|
-
/** 欧元(德国,Locale: 德语+德国,代表欧元区) → 1.234.567,89 */
|
|
343
|
-
EUR: "de-DE",
|
|
344
|
-
/** 英镑(英国,Locale: 英式英语) → 1,234,567.89 */
|
|
345
|
-
GBP: "en-GB",
|
|
346
|
-
/** 瑞士法郎(瑞士,Locale: 德语+瑞士) → 1'234'567.89 */
|
|
347
|
-
CHF: "de-CH",
|
|
348
|
-
/** 瑞典克朗(瑞典,Locale: 瑞典语) → 1 234 567,89(空格千分位) */
|
|
349
|
-
SEK: "sv-SE",
|
|
350
|
-
/** 挪威克朗(挪威,Locale: 挪威语) → 1 234 567,89(空格千分位) */
|
|
351
|
-
NOK: "no-NO",
|
|
352
|
-
/** 丹麦克朗(丹麦,Locale: 丹麦语) → 1.234.567,89 */
|
|
353
|
-
DKK: "da-DK",
|
|
354
|
-
/** 波兰兹罗提(波兰,Locale: 波兰语) → 1 234 567,89(空格千分位) */
|
|
355
|
-
PLN: "pl-PL",
|
|
356
|
-
/** 捷克克朗(捷克,Locale: 捷克语) → 1 234 567,89(空格千分位) */
|
|
357
|
-
CZK: "cs-CZ",
|
|
358
|
-
/** 匈牙利福林(匈牙利,Locale: 匈牙利语) → 1 234 567,89(空格千分位) */
|
|
359
|
-
HUF: "hu-HU",
|
|
360
|
-
/** 俄罗斯卢布(俄罗斯,Locale: 俄语) → 1 234 567,89(空格千分位) */
|
|
361
|
-
RUB: "ru-RU",
|
|
362
|
-
/** 罗马尼亚列伊(罗马尼亚,Locale: 罗马尼亚语) → 1.234.567,89 */
|
|
363
|
-
RON: "ro-RO",
|
|
364
|
-
/** 乌克兰格里夫纳(乌克兰,Locale: 乌克兰语) → 1 234 567,89(空格千分位) */
|
|
365
|
-
UAH: "uk-UA",
|
|
366
|
-
/** 澳大利亚元(澳大利亚,Locale: 澳大利亚英语) → 1,234,567.89 */
|
|
367
|
-
AUD: "en-AU",
|
|
368
|
-
/** 新西兰元(新西兰,Locale: 新西兰英语) → 1,234,567.89 */
|
|
369
|
-
NZD: "en-NZ",
|
|
370
|
-
/** 南非兰特(南非,Locale: 英语+南非) → 1 234 567,89(空格千分位) */
|
|
371
|
-
ZAR: "en-ZA",
|
|
372
|
-
/** 埃及镑(埃及,Locale: 阿拉伯语+埃及) → ١٬٢٣٤٬٥٦٧٫٨٩(阿拉伯数字) */
|
|
373
|
-
EGP: "ar-EG",
|
|
374
|
-
/** 土耳其里拉(土耳其,Locale: 土耳其语) → 1.234.567,89 */
|
|
375
|
-
TRY: "tr-TR",
|
|
376
|
-
/** 以色列新谢克尔(以色列,Locale: 希伯来语) → 1,234,567.89 */
|
|
377
|
-
ILS: "he-IL",
|
|
378
|
-
/** 摩洛哥迪拉姆(摩洛哥,Locale: 阿拉伯语+摩洛哥) → 1.234.567,89 */
|
|
379
|
-
MAD: "ar-MA",
|
|
380
|
-
/** 科威特第纳尔(科威特,Locale: 阿拉伯语+科威特) → ١٬٢٣٤٬٥٦٧٫٨٩(阿拉伯数字) */
|
|
381
|
-
KWD: "ar-KW",
|
|
382
|
-
/** 卡塔尔里亚尔(卡塔尔,Locale: 阿拉伯语+卡塔尔) → ١٬٢٣٤٬٥٦٧٫٨٩(阿拉伯数字) */
|
|
383
|
-
QAR: "ar-QA",
|
|
384
|
-
/** 尼日利亚奈拉(尼日利亚,Locale: 英语+尼日利亚) → 1,234,567.89 */
|
|
385
|
-
NGN: "en-NG",
|
|
386
|
-
/** 太平洋法郎(法属波利尼西亚,Locale: 法语+太平洋) → 1[U+202F]234[U+202F]567,89(窄空格千分位) */
|
|
387
|
-
XPF: "fr-PF"
|
|
388
|
-
};
|
|
389
|
-
static currencyFormatter(value, options) {
|
|
390
|
-
if (TypeUtil.isNullish(value)) return null;
|
|
391
|
-
const { currencySign, currencySignPosition, locales, currencyFormatOptions } = options;
|
|
392
|
-
const numberValue = Number(value);
|
|
393
|
-
if (Number.isNaN(numberValue)) return null;
|
|
394
|
-
let formatedValue = numberValue.toLocaleString(locales, currencyFormatOptions).replace(currencySign, "").trim();
|
|
395
|
-
if (currencySignPosition === "start") formatedValue = `${currencySign} ${formatedValue}`;
|
|
396
|
-
if (currencySignPosition === "end") formatedValue = `${formatedValue} ${currencySign}`;
|
|
397
|
-
return formatedValue;
|
|
398
|
-
}
|
|
399
|
-
static toRealValue(mathJsInstance, value, precision, stringMode) {
|
|
400
|
-
if (TypeUtil.isNullish(value)) return null;
|
|
401
|
-
const precisionValue = MathUtil.toDecimal(mathJsInstance, value, precision);
|
|
402
|
-
return stringMode === false ? Number(precisionValue) : precisionValue;
|
|
403
|
-
}
|
|
404
|
-
};
|
|
405
|
-
//#endregion
|
|
406
|
-
//#region src/dateTime/dateTimeUtil.ts
|
|
407
|
-
/**
|
|
408
|
-
* 日期工具类
|
|
409
|
-
*/
|
|
410
|
-
var DateTimeUtil = class {
|
|
411
|
-
/**
|
|
412
|
-
* 每秒的毫秒数
|
|
413
|
-
* @example
|
|
414
|
-
* ```ts
|
|
415
|
-
* DateTimeUtil.MILLISECONDS_PER_SECOND; // 1000
|
|
416
|
-
* ```
|
|
417
|
-
*/
|
|
418
|
-
static MILLISECONDS_PER_SECOND = 1e3;
|
|
419
|
-
/**
|
|
420
|
-
* 每分钟的秒数
|
|
421
|
-
* @example
|
|
422
|
-
* ```ts
|
|
423
|
-
* DateTimeUtil.SECOND_PER_MINUTE; // 60
|
|
424
|
-
* ```
|
|
425
|
-
*/
|
|
426
|
-
static SECOND_PER_MINUTE = 60;
|
|
427
|
-
/**
|
|
428
|
-
* 每小时的分钟数
|
|
429
|
-
* @example
|
|
430
|
-
* ```ts
|
|
431
|
-
* DateTimeUtil.MINUTE_PER_HOUR; // 60
|
|
432
|
-
* ```
|
|
433
|
-
*/
|
|
434
|
-
static MINUTE_PER_HOUR = 60;
|
|
435
|
-
/**
|
|
436
|
-
* 每小时的秒数
|
|
437
|
-
* @example
|
|
438
|
-
* ```ts
|
|
439
|
-
* DateTimeUtil.SECOND_PER_HOUR; // 3600
|
|
440
|
-
* ```
|
|
441
|
-
*/
|
|
442
|
-
static SECOND_PER_HOUR = this.SECOND_PER_MINUTE ** 2;
|
|
443
|
-
/**
|
|
444
|
-
* 每天小时数
|
|
445
|
-
* @example
|
|
446
|
-
* ```ts
|
|
447
|
-
* DateTimeUtil.HOUR_PER_DAY; // 24
|
|
448
|
-
* ```
|
|
449
|
-
*/
|
|
450
|
-
static HOUR_PER_DAY = 24;
|
|
451
|
-
/**
|
|
452
|
-
* 每天秒数
|
|
453
|
-
* @example
|
|
454
|
-
* ```ts
|
|
455
|
-
* DateTimeUtil.SECOND_PER_DAY; // 86400
|
|
456
|
-
* ```
|
|
457
|
-
*/
|
|
458
|
-
static SECOND_PER_DAY = this.SECOND_PER_HOUR * this.HOUR_PER_DAY;
|
|
459
|
-
/**
|
|
460
|
-
* 每周天数
|
|
461
|
-
* @example
|
|
462
|
-
* ```ts
|
|
463
|
-
* DateTimeUtil.DAY_PER_WEEK; // 7
|
|
464
|
-
* ```
|
|
465
|
-
*/
|
|
466
|
-
static DAY_PER_WEEK = 7;
|
|
467
|
-
/**
|
|
468
|
-
* 每月天数
|
|
469
|
-
* @example
|
|
470
|
-
* ```ts
|
|
471
|
-
* DateTimeUtil.DAY_PER_MONTH; // 30
|
|
472
|
-
* ```
|
|
473
|
-
*/
|
|
474
|
-
static DAY_PER_MONTH = 30;
|
|
475
|
-
/**
|
|
476
|
-
* 每年天数
|
|
477
|
-
* @example
|
|
478
|
-
* ```ts
|
|
479
|
-
* DateTimeUtil.DAY_PER_YEAR; // 365
|
|
480
|
-
* ```
|
|
481
|
-
*/
|
|
482
|
-
static DAY_PER_YEAR = 365;
|
|
483
|
-
/**
|
|
484
|
-
* 每年月数
|
|
485
|
-
* @example
|
|
486
|
-
* ```ts
|
|
487
|
-
* DateTimeUtil.MONTH_PER_YEAR; // 12
|
|
488
|
-
* ```
|
|
489
|
-
*/
|
|
490
|
-
static MONTH_PER_YEAR = 12;
|
|
491
|
-
/**
|
|
492
|
-
* 每年平均周
|
|
493
|
-
* @example
|
|
494
|
-
* ```ts
|
|
495
|
-
* DateTimeUtil.WEEK_PER_YEAR; // 52
|
|
496
|
-
* ```
|
|
497
|
-
*/
|
|
498
|
-
static WEEK_PER_YEAR = 52;
|
|
499
|
-
/**
|
|
500
|
-
* 每月平均周
|
|
501
|
-
* @example
|
|
502
|
-
* ```ts
|
|
503
|
-
* DateTimeUtil.WEEK_PER_MONTH; // 4
|
|
504
|
-
* ```
|
|
505
|
-
*/
|
|
506
|
-
static WEEK_PER_MONTH = 4;
|
|
507
|
-
/**
|
|
508
|
-
* 常用时间格式模板集合
|
|
509
|
-
*
|
|
510
|
-
* @example
|
|
511
|
-
* ```ts
|
|
512
|
-
* DateTimeUtil.FORMAT.ISO_DATE; // "yyyy-MM-dd"
|
|
513
|
-
* DateTimeUtil.FORMAT.CN_DATE_TIME; // "yyyy年MM月dd日 HH时mm分ss秒"
|
|
514
|
-
* ```
|
|
515
|
-
*/
|
|
516
|
-
static FORMAT = {
|
|
517
|
-
ISO_DATE: "yyyy-MM-dd",
|
|
518
|
-
ISO_TIME: "HH:mm:ss",
|
|
519
|
-
ISO_DATE_TIME: "yyyy-MM-dd HH:mm:ss",
|
|
520
|
-
ISO_DATE_TIME_MS: "yyyy-MM-dd HH:mm:ss.SSS",
|
|
521
|
-
ISO_DATETIME_TZ: "yyyy-MM-dd'T'HH:mm:ssXXX",
|
|
522
|
-
ISO_DATETIME_TZ_MS: "yyyy-MM-dd'T'HH:mm:ss.SSSXXX",
|
|
523
|
-
US_DATE: "MM/dd/yyyy",
|
|
524
|
-
US_DATE_TIME: "MM/dd/yyyy HH:mm:ss",
|
|
525
|
-
US_DATE_SHORT_YEAR: "MM/dd/yy",
|
|
526
|
-
EU_DATE: "dd/MM/yyyy",
|
|
527
|
-
EU_DATE_TIME: "dd/MM/yyyy HH:mm:ss",
|
|
528
|
-
CN_DATE: "yyyy年MM月dd日",
|
|
529
|
-
CN_DATE_TIME: "yyyy年MM月dd日 HH时mm分ss秒",
|
|
530
|
-
CN_DATE_WEEKDAY: "yyyy年MM月dd日 EEE",
|
|
531
|
-
CN_WEEKDAY_FULL: "EEEE",
|
|
532
|
-
SHORT_DATE: "yy-MM-dd",
|
|
533
|
-
SHORT_DATE_SLASH: "yy/MM/dd",
|
|
534
|
-
MONTH_DAY: "MM-dd",
|
|
535
|
-
MONTH_DAY_CN: "MM月dd日",
|
|
536
|
-
DATE_WITH_WEEKDAY_SHORT: "yyyy-MM-dd (EEE)",
|
|
537
|
-
DATE_WITH_WEEKDAY_FULL: "yyyy-MM-dd (EEEE)",
|
|
538
|
-
TIME_24: "HH:mm:ss",
|
|
539
|
-
TIME_24_NO_SEC: "HH:mm",
|
|
540
|
-
TIME_12: "hh:mm:ss a",
|
|
541
|
-
TIME_12_NO_SEC: "hh:mm a",
|
|
542
|
-
TIMESTAMP: "yyyyMMddHHmmss",
|
|
543
|
-
TIMESTAMP_MS: "yyyyMMddHHmmssSSS",
|
|
544
|
-
RFC2822: "EEE, dd MMM yyyy HH:mm:ss xxx",
|
|
545
|
-
READABLE_DATE: "MMM dd, yyyy",
|
|
546
|
-
READABLE_DATE_TIME: "MMM dd, yyyy HH:mm",
|
|
547
|
-
COMPACT_DATETIME: "yyyyMMdd_HHmmss"
|
|
548
|
-
};
|
|
549
|
-
/**
|
|
550
|
-
* 获取当前时区信息
|
|
551
|
-
*
|
|
552
|
-
* @returns 时区信息对象 (UTC偏移和时区名称)
|
|
553
|
-
* @example
|
|
554
|
-
* ```ts
|
|
555
|
-
* DateTimeUtil.getTimeZone(); // { UTC: "UTC+8", timeZone: "Asia/Shanghai" }
|
|
556
|
-
* ```
|
|
557
|
-
*/
|
|
558
|
-
static getTimeZone() {
|
|
559
|
-
const offsetMinutes = 0 - (/* @__PURE__ */ new Date()).getTimezoneOffset();
|
|
560
|
-
const sign = offsetMinutes >= 0 ? "+" : "-";
|
|
561
|
-
const absMinutes = Math.abs(offsetMinutes);
|
|
562
|
-
const hours = Math.floor(absMinutes / this.MINUTE_PER_HOUR);
|
|
563
|
-
const minutes = absMinutes % this.MINUTE_PER_HOUR;
|
|
564
|
-
return {
|
|
565
|
-
UTC: `UTC${sign}${hours}${minutes ? `:${String(minutes).padStart(2, "0")}` : ""}`,
|
|
566
|
-
timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone
|
|
567
|
-
};
|
|
568
|
-
}
|
|
569
|
-
};
|
|
570
|
-
//#endregion
|
|
571
|
-
//#region src/env/envUtil.ts
|
|
572
|
-
/**
|
|
573
|
-
* 环境检查工具类
|
|
574
|
-
* - ⚠️ `isBrowser` / `isWebWorker` / `isReactNative` 基于静态字段判定,在**模块加载时**求值一次。
|
|
575
|
-
* SSR 场景下若在 Node 端 import(此时 `window` 未定义),结果会永久为 `false`,不会随运行时环境变化重算。
|
|
576
|
-
*/
|
|
577
|
-
var EnvUtil = class {
|
|
578
|
-
static _isBrowser = typeof window !== "undefined" && TypeUtil.isFunction(window?.document?.createElement);
|
|
579
|
-
static _isWebWorker = typeof window === "undefined" && typeof self !== "undefined" && "importScripts" in self;
|
|
580
|
-
static _isReactNative = typeof navigator !== "undefined" && navigator.product === "ReactNative";
|
|
581
|
-
/**
|
|
582
|
-
* 检测是否处于浏览器环境
|
|
583
|
-
*
|
|
584
|
-
* @returns 是否为浏览器环境
|
|
585
|
-
* @example
|
|
586
|
-
* ```ts
|
|
587
|
-
* EnvUtil.isBrowser(); // true: 浏览器, false: Node.js
|
|
588
|
-
* ```
|
|
589
|
-
*/
|
|
590
|
-
static isBrowser() {
|
|
591
|
-
return this._isBrowser;
|
|
592
|
-
}
|
|
593
|
-
/**
|
|
594
|
-
* 检测是否处于 Web Worker 环境
|
|
595
|
-
*
|
|
596
|
-
* @returns 是否为 Web Worker 环境
|
|
597
|
-
* @example
|
|
598
|
-
* ```ts
|
|
599
|
-
* EnvUtil.isWebWorker(); // true: Worker, false: 主线程/Node.js
|
|
600
|
-
* ```
|
|
601
|
-
*/
|
|
602
|
-
static isWebWorker() {
|
|
603
|
-
return this._isWebWorker;
|
|
604
|
-
}
|
|
605
|
-
/**
|
|
606
|
-
* 检测是否处于 React Native 环境
|
|
607
|
-
*
|
|
608
|
-
* @returns 是否为 React Native 环境
|
|
609
|
-
* @example
|
|
610
|
-
* ```ts
|
|
611
|
-
* EnvUtil.isReactNative(); // true: React Native, false: Web/Node.js
|
|
612
|
-
* ```
|
|
613
|
-
*/
|
|
614
|
-
static isReactNative() {
|
|
615
|
-
return this._isReactNative;
|
|
616
|
-
}
|
|
617
|
-
/**
|
|
618
|
-
* 检查是否在 iframe 环境中
|
|
619
|
-
*
|
|
620
|
-
* @returns 是否在 iframe 中
|
|
621
|
-
* @example
|
|
622
|
-
* ```ts
|
|
623
|
-
* EnvUtil.isIframe(); // true: 当前页面在 iframe 中
|
|
624
|
-
* ```
|
|
625
|
-
*/
|
|
626
|
-
static isIframe() {
|
|
627
|
-
if (typeof window === "undefined") return false;
|
|
628
|
-
try {
|
|
629
|
-
return window.top !== window.self;
|
|
630
|
-
} catch (error) {
|
|
631
|
-
if (error.name === "SecurityError") return true;
|
|
632
|
-
return false;
|
|
633
|
-
}
|
|
634
|
-
}
|
|
635
|
-
/**
|
|
636
|
-
* 检测当前设备是否为桌面设备
|
|
637
|
-
*
|
|
638
|
-
* @param minWidth - 桌面设备最小宽度(默认 1200px)
|
|
639
|
-
* @param minScreenSize - 桌面设备最小屏幕尺寸(默认 10英寸)
|
|
640
|
-
* @param dpi - 标准 DPI 基准(默认 160)
|
|
641
|
-
* @returns 是否为桌面设备
|
|
642
|
-
* @example
|
|
643
|
-
* ```ts
|
|
644
|
-
* // 假设 window.innerWidth = 1920
|
|
645
|
-
* EnvUtil.isDesktop(); // true
|
|
646
|
-
*
|
|
647
|
-
* // 自定义阈值
|
|
648
|
-
* EnvUtil.isDesktop(1440, 13); // 更严格的桌面检测
|
|
649
|
-
* ```
|
|
650
|
-
*/
|
|
651
|
-
static isDesktop(minWidth = 1200, minScreenSize = 10, dpi = 160) {
|
|
652
|
-
if (typeof window === "undefined" || !TypeUtil.isPositiveInteger(minWidth) || !TypeUtil.isPositiveInteger(minScreenSize)) return false;
|
|
653
|
-
if (window.innerWidth < minWidth) return false;
|
|
654
|
-
try {
|
|
655
|
-
const widthPx = window.screen.width;
|
|
656
|
-
const heightPx = window.screen.height;
|
|
657
|
-
const DPI = dpi * (window.devicePixelRatio || 1);
|
|
658
|
-
const widthInch = widthPx / DPI;
|
|
659
|
-
const heightInch = heightPx / DPI;
|
|
660
|
-
return Math.sqrt(widthInch ** 2 + heightInch ** 2) >= minScreenSize;
|
|
661
|
-
} catch {
|
|
662
|
-
return true;
|
|
663
|
-
}
|
|
664
|
-
}
|
|
665
|
-
/**
|
|
666
|
-
* 检测当前设备是否为 Windows 桌面设备
|
|
667
|
-
*
|
|
668
|
-
* @param minWidth - 桌面设备最小宽度(默认 1200px)
|
|
669
|
-
* @param minScreenSize - 桌面设备最小屏幕尺寸(默认 10英寸)
|
|
670
|
-
* @param dpi - 标准 DPI 基准(默认 160)
|
|
671
|
-
* @returns 是否为 Windows 桌面设备
|
|
672
|
-
* @example
|
|
673
|
-
* ```ts
|
|
674
|
-
* // UA contains Windows
|
|
675
|
-
* EnvUtil.isWindowsDesktop(); // true
|
|
676
|
-
* ```
|
|
677
|
-
*/
|
|
678
|
-
static isWindowsDesktop(minWidth = 1200, minScreenSize = 10, dpi = 160) {
|
|
679
|
-
if (typeof navigator === "undefined" || !navigator.userAgent) return false;
|
|
680
|
-
return /Windows/i.test(navigator.userAgent) && this.isDesktop(minWidth, minScreenSize, dpi);
|
|
681
|
-
}
|
|
682
|
-
/**
|
|
683
|
-
* 检测当前设备是否为 macOS 桌面设备
|
|
684
|
-
*
|
|
685
|
-
* @param minWidth - 桌面设备最小宽度(默认 1200px)
|
|
686
|
-
* @param minScreenSize - 桌面设备最小屏幕尺寸(默认 10英寸)
|
|
687
|
-
* @param dpi - 标准 DPI 基准(默认 160)
|
|
688
|
-
* @returns 是否为 macOS 桌面设备
|
|
689
|
-
* @example
|
|
690
|
-
* ```ts
|
|
691
|
-
* // UA contains Macintosh
|
|
692
|
-
* EnvUtil.isMacOSDesktop(); // true
|
|
693
|
-
* ```
|
|
694
|
-
*/
|
|
695
|
-
static isMacOSDesktop(minWidth = 1200, minScreenSize = 10, dpi = 160) {
|
|
696
|
-
if (typeof navigator === "undefined" || !navigator.userAgent) return false;
|
|
697
|
-
return /Macintosh/i.test(navigator.userAgent) && this.isDesktop(minWidth, minScreenSize, dpi);
|
|
698
|
-
}
|
|
699
|
-
/**
|
|
700
|
-
* 检测当前设备是否为移动设备
|
|
701
|
-
*
|
|
702
|
-
* @param maxWidth - 移动设备最大宽度(默认 768px)
|
|
703
|
-
* @param dpi - 标准 DPI 基准(默认 160)
|
|
704
|
-
* @returns 是否为移动设备
|
|
705
|
-
* @example
|
|
706
|
-
* ```ts
|
|
707
|
-
* // 假设 window.innerWidth = 500
|
|
708
|
-
* EnvUtil.isMobile(); // true
|
|
709
|
-
* ```
|
|
710
|
-
*/
|
|
711
|
-
static isMobile(maxWidth = 768, dpi = 160) {
|
|
712
|
-
if (typeof window === "undefined" || !TypeUtil.isPositiveInteger(maxWidth)) return false;
|
|
713
|
-
if (window.innerWidth >= maxWidth) return false;
|
|
714
|
-
try {
|
|
715
|
-
const widthPx = window.screen.width;
|
|
716
|
-
const heightPx = window.screen.height;
|
|
717
|
-
const DPI = dpi * (window.devicePixelRatio || 1);
|
|
718
|
-
const widthInch = widthPx / DPI;
|
|
719
|
-
const heightInch = heightPx / DPI;
|
|
720
|
-
return Math.sqrt(widthInch ** 2 + heightInch ** 2) < 7;
|
|
721
|
-
} catch {
|
|
722
|
-
return true;
|
|
723
|
-
}
|
|
724
|
-
}
|
|
725
|
-
/**
|
|
726
|
-
* 检测当前设备是否为IOS移动设备
|
|
727
|
-
*
|
|
728
|
-
* @param maxWidth - 移动设备最大宽度(默认 768px)
|
|
729
|
-
* @param dpi - 标准 DPI 基准(默认 160)
|
|
730
|
-
* @returns 是否为 iOS 移动设备 (iPhone/iPod)
|
|
731
|
-
* @example
|
|
732
|
-
* ```ts
|
|
733
|
-
* // UA contains iPhone
|
|
734
|
-
* EnvUtil.isIOSMobile(); // true
|
|
735
|
-
* ```
|
|
736
|
-
*/
|
|
737
|
-
static isIOSMobile(maxWidth = 768, dpi = 160) {
|
|
738
|
-
if (typeof navigator === "undefined" || !navigator.userAgent) return false;
|
|
739
|
-
return /iPhone|iPad|iPod/i.test(navigator.userAgent) && this.isMobile(maxWidth, dpi);
|
|
740
|
-
}
|
|
741
|
-
/**
|
|
742
|
-
* 检测当前设备是否为平板
|
|
743
|
-
*
|
|
744
|
-
* @param minWidth - 平板最小宽度(默认 768px)
|
|
745
|
-
* @param maxWidth - 平板最大宽度(默认 1200px)
|
|
746
|
-
* @param dpi - 标准 DPI 基准(默认 160)
|
|
747
|
-
* @returns 是否为平板设备
|
|
748
|
-
* - 宽度命中 `[minWidth, maxWidth]` 区间,或 CSS/DPI 折算尺寸落在 `[7, 13)` 英寸(排除 DPR=1 的 1920×1080 桌面)
|
|
749
|
-
* @example
|
|
750
|
-
* ```ts
|
|
751
|
-
* // 假设 window.innerWidth = 1000
|
|
752
|
-
* EnvUtil.isTablet(); // true
|
|
753
|
-
* ```
|
|
754
|
-
*/
|
|
755
|
-
static isTablet(minWidth = 768, maxWidth = 1200, dpi = 160) {
|
|
756
|
-
if (typeof window === "undefined" || !TypeUtil.isPositiveInteger(minWidth) || !TypeUtil.isPositiveInteger(maxWidth)) return false;
|
|
757
|
-
const width = window.innerWidth;
|
|
758
|
-
const isWithinWidthRange = width >= minWidth && width <= maxWidth;
|
|
759
|
-
try {
|
|
760
|
-
const widthPx = window.screen.width;
|
|
761
|
-
const heightPx = window.screen.height;
|
|
762
|
-
const DPI = dpi * (window.devicePixelRatio || 1);
|
|
763
|
-
const widthInch = widthPx / DPI;
|
|
764
|
-
const heightInch = heightPx / DPI;
|
|
765
|
-
const screenInches = Math.sqrt(widthInch ** 2 + heightInch ** 2);
|
|
766
|
-
return isWithinWidthRange || screenInches >= 7 && screenInches < 13;
|
|
767
|
-
} catch {
|
|
768
|
-
return isWithinWidthRange;
|
|
769
|
-
}
|
|
770
|
-
}
|
|
771
|
-
};
|
|
772
|
-
//#endregion
|
|
773
|
-
//#region src/function/functionUtil.ts
|
|
774
|
-
/**
|
|
775
|
-
* 函数工具类
|
|
776
|
-
*/
|
|
777
|
-
var FunctionUtil = class {
|
|
778
|
-
/**
|
|
779
|
-
*将 Promise 转换为 `[err, result]` 格式,方便 async/await 错误处理
|
|
780
|
-
*
|
|
781
|
-
* @param promise 待处理的 Promise
|
|
782
|
-
* @param errorExt 附加到 error 对象的扩展信息(注意:如果原 error 是 Error 实例,扩展属性可能会覆盖或无法正确合并非枚举属性)
|
|
783
|
-
* @returns `[err, null]` 或 `[null, data]`
|
|
784
|
-
* @example
|
|
785
|
-
* ```ts
|
|
786
|
-
* const [err, data] = await FunctionUtil.to(someAsyncFunc());
|
|
787
|
-
* ```
|
|
788
|
-
*/
|
|
789
|
-
static to(promise, errorExt) {
|
|
790
|
-
return promise.then((data) => [null, data]).catch((err) => {
|
|
791
|
-
if (errorExt) {
|
|
792
|
-
const parsedError = {
|
|
793
|
-
name: "",
|
|
794
|
-
message: "",
|
|
795
|
-
stack: ""
|
|
796
|
-
};
|
|
797
|
-
if (err instanceof Error) {
|
|
798
|
-
parsedError.message = err.message;
|
|
799
|
-
parsedError.name = err.name;
|
|
800
|
-
parsedError.stack = err.stack;
|
|
801
|
-
Object.getOwnPropertyNames(err).forEach((key) => {
|
|
802
|
-
if (!(key in parsedError)) parsedError[key] = err[key];
|
|
803
|
-
});
|
|
804
|
-
} else {
|
|
805
|
-
Object.assign(parsedError, err);
|
|
806
|
-
if (!parsedError.message) parsedError.message = String(err);
|
|
807
|
-
}
|
|
808
|
-
Object.assign(parsedError, errorExt);
|
|
809
|
-
return [parsedError, void 0];
|
|
810
|
-
}
|
|
811
|
-
return [err ? err : /* @__PURE__ */ new Error("defaultError"), void 0];
|
|
812
|
-
});
|
|
813
|
-
}
|
|
814
|
-
/**
|
|
815
|
-
* 将 Arguments 对象转换为数组
|
|
816
|
-
*
|
|
817
|
-
* ⚠️ 注意:TypeScript 官方推荐使用 rest parameters (...args) 替代 arguments
|
|
818
|
-
* 本函数仅用于处理遗留代码或特殊场景(如装饰器中需保留 this 绑定)
|
|
819
|
-
*
|
|
820
|
-
* @param args Arguments 对象(必须为类数组对象)
|
|
821
|
-
* @param start 起始索引(可选,默认为 0)
|
|
822
|
-
* @returns 转换后的数组,元素类型为 T
|
|
823
|
-
*
|
|
824
|
-
* @throws TypeError 如果 args 为 null 或 undefined
|
|
825
|
-
*
|
|
826
|
-
* @example
|
|
827
|
-
* ```ts
|
|
828
|
-
* // 遗留代码场景
|
|
829
|
-
* function legacyFn(a: number, b: string) {
|
|
830
|
-
* const argsArray = FunctionUtil.toArgs(arguments);
|
|
831
|
-
* // argsArray: unknown[]
|
|
832
|
-
* }
|
|
833
|
-
*
|
|
834
|
-
* // 现代替代方案(推荐)
|
|
835
|
-
* function modernFn(a: number, b: string, ...rest: unknown[]) {
|
|
836
|
-
* // rest 已经是数组,无需 toArgs
|
|
837
|
-
* }
|
|
838
|
-
*
|
|
839
|
-
* // 参数截取
|
|
840
|
-
* function skipFirst(...args: unknown[]) {
|
|
841
|
-
* const rest = FunctionUtil.toArgs(arguments, 1);
|
|
842
|
-
* // rest: unknown[],跳过第一个参数
|
|
843
|
-
* }
|
|
844
|
-
* ```
|
|
845
|
-
*/
|
|
846
|
-
static toArgs(args, start) {
|
|
847
|
-
if (args === null || args === void 0) throw new TypeError(`function [toArgs] Expected parameter [args] to be a arguments object, got ${typeof args}`);
|
|
848
|
-
return Array.prototype.slice.call(args, start);
|
|
849
|
-
}
|
|
850
|
-
/**
|
|
851
|
-
* 将同步或异步函数统一包装为 Promise
|
|
852
|
-
* - 自动捕获同步异常
|
|
853
|
-
*
|
|
854
|
-
* @param fn 返回值可为同步值或 Promise 的函数
|
|
855
|
-
* @returns 标准化的 Promise
|
|
856
|
-
*
|
|
857
|
-
* @example
|
|
858
|
-
* ```ts
|
|
859
|
-
* // 同步函数
|
|
860
|
-
* FunctionUtil.toPromise(() => 42).then(v => console.log(v)); // 42
|
|
861
|
-
*
|
|
862
|
-
* // 异步函数
|
|
863
|
-
* FunctionUtil.toPromise(async () => await fetchData()).then(data => ...);
|
|
864
|
-
*
|
|
865
|
-
* // 异常处理
|
|
866
|
-
* FunctionUtil.toPromise(() => { throw new Error('fail'); }).catch(err => console.error(err)); // 捕获同步异常
|
|
867
|
-
* ```
|
|
868
|
-
*/
|
|
869
|
-
static toPromise(fn) {
|
|
870
|
-
try {
|
|
871
|
-
return Promise.resolve(fn());
|
|
872
|
-
} catch (error) {
|
|
873
|
-
return Promise.reject(error);
|
|
874
|
-
}
|
|
875
|
-
}
|
|
876
|
-
};
|
|
877
|
-
//#endregion
|
|
878
|
-
//#region src/mime/mimeUtil.ts
|
|
879
|
-
/**
|
|
880
|
-
* MIME 工具类
|
|
881
|
-
*/
|
|
882
|
-
var MimeUtil = class {
|
|
883
|
-
/**
|
|
884
|
-
* 文件类型 MIME 常量
|
|
885
|
-
* - 每个类型对应具体的文件扩展名
|
|
886
|
-
*/
|
|
887
|
-
static FILE_MIME = {
|
|
888
|
-
/** 普通文本文件(.txt) */
|
|
889
|
-
TEXT: "text/plain",
|
|
890
|
-
/** 超文本标记语言文档(.html/.htm) */
|
|
891
|
-
HTML: "text/html",
|
|
892
|
-
/** 层叠样式表文件(.css) */
|
|
893
|
-
CSS: "text/css",
|
|
894
|
-
/** 逗号分隔值文件/表格数据(.csv) */
|
|
895
|
-
CSV: "text/csv",
|
|
896
|
-
/** 制表符分隔值文件(.tsv) */
|
|
897
|
-
TSV: "text/tab-separated-values",
|
|
898
|
-
/** XML 文档(.xml) */
|
|
899
|
-
XML: "application/xml",
|
|
900
|
-
/** XML 文档/兼容值 */
|
|
901
|
-
XML_LEGACY: "text/xml",
|
|
902
|
-
/** XHTML 文档(.xhtml/.xht) */
|
|
903
|
-
XHTML: "application/xhtml+xml",
|
|
904
|
-
/** JavaScript 文件(.js) */
|
|
905
|
-
JS: "text/javascript",
|
|
906
|
-
/** TypeScript 文件(.ts) */
|
|
907
|
-
TS: "text/typescript",
|
|
908
|
-
/** Python 文件(.py) */
|
|
909
|
-
PY: "text/x-python",
|
|
910
|
-
/** Shell 脚本 (.sh) */
|
|
911
|
-
SH: "text/x-sh",
|
|
912
|
-
/** C 语言源文件(.c) */
|
|
913
|
-
C: "text/x-c",
|
|
914
|
-
/** C++ 源文件(.cpp/.cc/.cxx) */
|
|
915
|
-
CPP: "text/x-c++",
|
|
916
|
-
/** C# 源文件(.cs) */
|
|
917
|
-
CSHARP: "text/x-csharp",
|
|
918
|
-
/** Java 源文件(.java) */
|
|
919
|
-
JAVA: "text/x-java",
|
|
920
|
-
/** Go 源文件(.go) */
|
|
921
|
-
GO: "text/x-go",
|
|
922
|
-
/** Rust 源文件(.rs) */
|
|
923
|
-
RUST: "text/x-rust",
|
|
924
|
-
/** PHP 文件(.php) */
|
|
925
|
-
PHP: "text/x-php",
|
|
926
|
-
/** Ruby 文件(.rb) */
|
|
927
|
-
RUBY: "text/x-ruby",
|
|
928
|
-
/** Swift 源文件(.swift) */
|
|
929
|
-
SWIFT: "text/x-swift",
|
|
930
|
-
/** YAML 文档(.yaml/.yml) */
|
|
931
|
-
YAML: "application/yaml",
|
|
932
|
-
/** YAML 文档/兼容值 */
|
|
933
|
-
YAML_LEGACY: "text/vnd.yaml",
|
|
934
|
-
/** TOML 文档(.toml) */
|
|
935
|
-
TOML: "application/toml",
|
|
936
|
-
/** TOML 文档/兼容值 */
|
|
937
|
-
TOML_LEGACY: "text/x-toml",
|
|
938
|
-
/** SQL 脚本(.sql) */
|
|
939
|
-
SQL: "application/sql",
|
|
940
|
-
/** SQL 脚本/兼容值 */
|
|
941
|
-
SQL_LEGACY: "text/x-sql",
|
|
942
|
-
/** Markdown 格式文档(.md/.markdown) */
|
|
943
|
-
MARKDOWN: "text/markdown",
|
|
944
|
-
/** 富文本格式文档(.rtf) */
|
|
945
|
-
RTF: "application/rtf",
|
|
946
|
-
/** iCalendar 日历格式(.ics) */
|
|
947
|
-
CALENDAR: "text/calendar",
|
|
948
|
-
/** JPEG 图像(.jpg/.jpeg) */
|
|
949
|
-
JPEG: "image/jpeg",
|
|
950
|
-
/** JPG 图像(JPEG 别名,.jpg) */
|
|
951
|
-
JPG: "image/jpeg",
|
|
952
|
-
/** PNG 图像/无损压缩,支持透明(.png) */
|
|
953
|
-
PNG: "image/png",
|
|
954
|
-
/** GIF 图像/支持动画(.gif) */
|
|
955
|
-
GIF: "image/gif",
|
|
956
|
-
/** Windows 位图(.bmp) */
|
|
957
|
-
BMP: "image/bmp",
|
|
958
|
-
/** SVG 向量图形(.svg) */
|
|
959
|
-
SVG: "image/svg+xml",
|
|
960
|
-
/** APNG 动态图像(.apng) */
|
|
961
|
-
APNG: "image/apng",
|
|
962
|
-
/** AVIF 图像/高效压缩(.avif) */
|
|
963
|
-
AVIF: "image/avif",
|
|
964
|
-
/** 图标文件格式(.ico) */
|
|
965
|
-
ICO: "image/vnd.microsoft.icon",
|
|
966
|
-
/** 图标文件格式/兼容值(.ico) */
|
|
967
|
-
ICO_LEGACY: "image/x-icon",
|
|
968
|
-
/** WebP 图像/高效压缩(.webp) */
|
|
969
|
-
WEBP: "image/webp",
|
|
970
|
-
/** TIFF 图像(.tif/.tiff) */
|
|
971
|
-
TIFF: "image/tiff",
|
|
972
|
-
/** HEIC 图像/高效编码(.heic) */
|
|
973
|
-
HEIC: "image/heic",
|
|
974
|
-
/** HEIF 图像/高效编码(.heif) */
|
|
975
|
-
HEIF: "image/heif",
|
|
976
|
-
/** Adobe Photoshop 文件(.psd) */
|
|
977
|
-
PSD: "image/vnd.adobe.photoshop",
|
|
978
|
-
/** MP3 音频(.mp3) */
|
|
979
|
-
MP3: "audio/mpeg",
|
|
980
|
-
/** AAC 音频(.aac) */
|
|
981
|
-
AAC: "audio/aac",
|
|
982
|
-
/** MIDI 音乐文件(.mid/.midi) */
|
|
983
|
-
MIDI: "audio/midi",
|
|
984
|
-
/** OGG 音频(.oga) */
|
|
985
|
-
OGG_AUDIO: "audio/ogg",
|
|
986
|
-
/** Opus 音频(.opus) */
|
|
987
|
-
OPUS: "audio/opus",
|
|
988
|
-
/** FLAC 无损音频(.flac) */
|
|
989
|
-
FLAC: "audio/flac",
|
|
990
|
-
/** WAV 音频(.wav) */
|
|
991
|
-
WAV: "audio/wav",
|
|
992
|
-
/** WebM 音频(.weba) */
|
|
993
|
-
WEBM_AUDIO: "audio/webm",
|
|
994
|
-
/** RealAudio 音频(.ra/.ram) */
|
|
995
|
-
REAL_AUDIO: "audio/x-pn-realaudio",
|
|
996
|
-
/** MP4 视频(.mp4) */
|
|
997
|
-
MP4: "video/mp4",
|
|
998
|
-
/** MPEG 视频(.mpeg/.mpg) */
|
|
999
|
-
MPEG: "video/mpeg",
|
|
1000
|
-
/** OGG 视频(.ogv) */
|
|
1001
|
-
OGG_VIDEO: "video/ogg",
|
|
1002
|
-
/** AVI 视频(.avi) */
|
|
1003
|
-
AVI: "video/x-msvideo",
|
|
1004
|
-
/** 3GPP 视频(.3gp) */
|
|
1005
|
-
THREE_GPP: "video/3gpp",
|
|
1006
|
-
/** 3GPP2 视频(.3g2) */
|
|
1007
|
-
THREE_GPP2: "video/3gpp2",
|
|
1008
|
-
/** WebM 视频(.webm) */
|
|
1009
|
-
WEBM: "video/webm",
|
|
1010
|
-
/** Matroska 视频(.mkv) */
|
|
1011
|
-
MKV: "video/x-matroska",
|
|
1012
|
-
/** Matroska 音频(.mka) */
|
|
1013
|
-
MKA: "audio/x-matroska",
|
|
1014
|
-
/** QuickTime 视频(.mov) */
|
|
1015
|
-
QUICKTIME: "video/quicktime",
|
|
1016
|
-
/** PDF 文档(.pdf) */
|
|
1017
|
-
PDF: "application/pdf",
|
|
1018
|
-
/** Word 97-2003 文档(.doc) */
|
|
1019
|
-
DOC: "application/msword",
|
|
1020
|
-
/** Word 2007+ 文档(.docx) */
|
|
1021
|
-
DOCX: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
1022
|
-
/** Excel 2007+ 工作簿(.xlsx) */
|
|
1023
|
-
XLSX: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
1024
|
-
/** 启用宏的Excel工作簿(.xlsm) */
|
|
1025
|
-
XLSM: "application/vnd.ms-excel.sheet.macroEnabled.12",
|
|
1026
|
-
/** Excel模板文件(.xltx) */
|
|
1027
|
-
XLTX: "application/vnd.openxmlformats-officedocument.spreadsheetml.template",
|
|
1028
|
-
/** PowerPoint 2007+ 演示文稿(.pptx) */
|
|
1029
|
-
PPTX: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
1030
|
-
/** PowerPoint 97-2003 演示文稿(.ppt) */
|
|
1031
|
-
PPT: "application/vnd.ms-powerpoint",
|
|
1032
|
-
/** OpenDocument 文本文档(.odt) */
|
|
1033
|
-
ODT: "application/vnd.oasis.opendocument.text",
|
|
1034
|
-
/** OpenDocument 表格文档(.ods) */
|
|
1035
|
-
ODS: "application/vnd.oasis.opendocument.spreadsheet",
|
|
1036
|
-
/** OpenDocument 演示文稿(.odp) */
|
|
1037
|
-
ODP: "application/vnd.oasis.opendocument.presentation",
|
|
1038
|
-
/** EPUB 电子书(.epub) */
|
|
1039
|
-
EPUB: "application/epub+zip",
|
|
1040
|
-
/** Kindle 电子书(.azw) */
|
|
1041
|
-
AZW: "application/vnd.amazon.ebook",
|
|
1042
|
-
/** ZIP 压缩文件(.zip) */
|
|
1043
|
-
ZIP: "application/zip",
|
|
1044
|
-
/** GZIP 压缩文件(.gz) */
|
|
1045
|
-
GZIP: "application/gzip",
|
|
1046
|
-
/** TAR 归档文件(.tar) */
|
|
1047
|
-
TAR: "application/x-tar",
|
|
1048
|
-
/** BZip 归档(.bz) */
|
|
1049
|
-
BZIP: "application/x-bzip",
|
|
1050
|
-
/** BZip2 归档(.bz2) */
|
|
1051
|
-
BZIP2: "application/x-bzip2",
|
|
1052
|
-
/** 7-Zip 压缩文件(.7z) */
|
|
1053
|
-
SEVEN_Z: "application/x-7z-compressed",
|
|
1054
|
-
/** RAR 压缩文件(.rar) */
|
|
1055
|
-
RAR: "application/vnd.rar",
|
|
1056
|
-
/** XZ 压缩文件(.xz) */
|
|
1057
|
-
XZ: "application/x-xz",
|
|
1058
|
-
/** Zstandard 压缩文件(.zst) */
|
|
1059
|
-
ZSTD: "application/zstd",
|
|
1060
|
-
/** ISO 光盘镜像(.iso) */
|
|
1061
|
-
ISO9660_IMAGE: "application/x-iso9660-image",
|
|
1062
|
-
/** JSON 数据格式(.json) */
|
|
1063
|
-
JSON: "application/json",
|
|
1064
|
-
/** JSON-LD 格式(.jsonld) */
|
|
1065
|
-
LD_JSON: "application/ld+json",
|
|
1066
|
-
/** Web App Manifest(.webmanifest) */
|
|
1067
|
-
MANIFEST: "application/manifest+json",
|
|
1068
|
-
/** Java 归档文件(.jar) */
|
|
1069
|
-
JAR: "application/java-archive",
|
|
1070
|
-
/** WebAssembly 二进制指令格式(.wasm) */
|
|
1071
|
-
WASM: "application/wasm",
|
|
1072
|
-
/** MS 嵌入式 OpenType 字体(.eot) */
|
|
1073
|
-
EOT: "application/vnd.ms-fontobject",
|
|
1074
|
-
/** OpenType 字体(.otf) */
|
|
1075
|
-
OTF: "font/otf",
|
|
1076
|
-
/** WOFF 字体(.woff) */
|
|
1077
|
-
WOFF: "font/woff",
|
|
1078
|
-
/** WOFF2 字体(.woff2) */
|
|
1079
|
-
WOFF2: "font/woff2",
|
|
1080
|
-
/** TrueType 字体(.ttf) */
|
|
1081
|
-
TTF: "font/ttf",
|
|
1082
|
-
/** Excel 97-2003 工作簿(.xls) */
|
|
1083
|
-
XLS: "application/vnd.ms-excel",
|
|
1084
|
-
/** Microsoft XPS 文档(.xps) */
|
|
1085
|
-
XPS: "application/vnd.ms-xpsdocument",
|
|
1086
|
-
/** Word 启用宏文档(.docm) */
|
|
1087
|
-
DOCM: "application/vnd.ms-word.document.macroEnabled.12"
|
|
1088
|
-
};
|
|
1089
|
-
/**
|
|
1090
|
-
* 协议/内容类型 MIME 常量
|
|
1091
|
-
* - 用于 HTTP 请求/响应内容协商,无对应文件扩展名
|
|
1092
|
-
*/
|
|
1093
|
-
static PROTOCOL_MIME = {
|
|
1094
|
-
/** 通用二进制数据流 */
|
|
1095
|
-
OCTET_STREAM: "application/octet-stream",
|
|
1096
|
-
/** URL 编码表单 */
|
|
1097
|
-
FORM_URLENCODED: "application/x-www-form-urlencoded",
|
|
1098
|
-
/** multipart 表单 */
|
|
1099
|
-
FORM_DATA: "multipart/form-data",
|
|
1100
|
-
/** Server-Sent Events 数据流 */
|
|
1101
|
-
EVENT_STREAM: "text/event-stream",
|
|
1102
|
-
/** 问题详情 JSON(RFC 9457) */
|
|
1103
|
-
PROBLEM_JSON: "application/problem+json",
|
|
1104
|
-
/** JSON Patch(RFC 6902) */
|
|
1105
|
-
JSON_PATCH: "application/json-patch+json",
|
|
1106
|
-
/** JSON Merge Patch(RFC 7386) */
|
|
1107
|
-
MERGE_PATCH_JSON: "application/merge-patch+json"
|
|
1108
|
-
};
|
|
1109
|
-
/**
|
|
1110
|
-
* 根据文件后缀名获取对应的标准 MIME 类型(含历史兼容值)
|
|
1111
|
-
* - 支持带 `.` 或不带 `.` 的后缀名,不区分大小写
|
|
1112
|
-
* - 元组第一项始终为 IANA 官方标准 MIME,后续项为历史兼容值
|
|
1113
|
-
* - 仅查询文件类型 MIME,不包含无后缀对应的协议类型
|
|
1114
|
-
*
|
|
1115
|
-
* @param extension 文件后缀名(如 `".png"` / `"png"` / `".PNG"`)
|
|
1116
|
-
* @returns 标准 MIME + 兼容值的元组;如无匹配则返回 `undefined`
|
|
1117
|
-
* @example
|
|
1118
|
-
* ```ts
|
|
1119
|
-
* MimeUtil.fromExtension(".png"); // ["image/png"]
|
|
1120
|
-
* MimeUtil.fromExtension("ico"); // ["image/vnd.microsoft.icon", "image/x-icon"]
|
|
1121
|
-
* MimeUtil.fromExtension(".xml"); // ["application/xml", "text/xml"]
|
|
1122
|
-
* MimeUtil.fromExtension(".xyz"); // undefined
|
|
1123
|
-
* ```
|
|
1124
|
-
*/
|
|
1125
|
-
static fromExtension(extension) {
|
|
1126
|
-
const ext = StringUtil.cast(extension).toLowerCase();
|
|
1127
|
-
const key = ext.startsWith(".") ? ext : `.${ext}`;
|
|
1128
|
-
return EXT_TO_MIME[key];
|
|
1129
|
-
}
|
|
1130
|
-
/**
|
|
1131
|
-
* 根据 MIME 类型获取对应的文件后缀名列表
|
|
1132
|
-
* - 一个 MIME 类型可能对应多个后缀名(如 `text/html` → `.html` / `.htm`)
|
|
1133
|
-
* - 兼容值和标准值映射到相同的后缀(如 `image/x-icon` 和 `image/vnd.microsoft.icon` 均返回 `[".ico"]`)
|
|
1134
|
-
* - 仅查询文件类型 MIME,协议类型无对应后缀
|
|
1135
|
-
*
|
|
1136
|
-
* @param mime MIME 类型字符串(如 `"image/png"` / `"IMAGE/PNG"`)
|
|
1137
|
-
* @returns 文件后缀名列表;如无匹配则返回 `undefined`
|
|
1138
|
-
* @example
|
|
1139
|
-
* ```ts
|
|
1140
|
-
* MimeUtil.toExtension("IMAGE/PNG"); // [".png"]
|
|
1141
|
-
* MimeUtil.toExtension("text/html"); // [".html", ".htm"]
|
|
1142
|
-
* MimeUtil.toExtension("image/jpeg"); // [".jpg", ".jpeg"]
|
|
1143
|
-
* MimeUtil.toExtension("application/octet-stream"); // undefined
|
|
1144
|
-
* ```
|
|
1145
|
-
*/
|
|
1146
|
-
static toExtension(mime) {
|
|
1147
|
-
const m = StringUtil.cast(mime).toLowerCase();
|
|
1148
|
-
return MIME_TO_EXT.get(m);
|
|
1149
|
-
}
|
|
1150
|
-
};
|
|
1151
|
-
const EXT_TO_MIME = {
|
|
1152
|
-
".txt": [MimeUtil.FILE_MIME.TEXT],
|
|
1153
|
-
".html": [MimeUtil.FILE_MIME.HTML],
|
|
1154
|
-
".htm": [MimeUtil.FILE_MIME.HTML],
|
|
1155
|
-
".css": [MimeUtil.FILE_MIME.CSS],
|
|
1156
|
-
".csv": [MimeUtil.FILE_MIME.CSV],
|
|
1157
|
-
".tsv": [MimeUtil.FILE_MIME.TSV],
|
|
1158
|
-
".xml": [MimeUtil.FILE_MIME.XML, MimeUtil.FILE_MIME.XML_LEGACY],
|
|
1159
|
-
".xhtml": [MimeUtil.FILE_MIME.XHTML],
|
|
1160
|
-
".xht": [MimeUtil.FILE_MIME.XHTML],
|
|
1161
|
-
".js": [MimeUtil.FILE_MIME.JS],
|
|
1162
|
-
".ts": [MimeUtil.FILE_MIME.TS],
|
|
1163
|
-
".py": [MimeUtil.FILE_MIME.PY],
|
|
1164
|
-
".sh": [MimeUtil.FILE_MIME.SH],
|
|
1165
|
-
".c": [MimeUtil.FILE_MIME.C],
|
|
1166
|
-
".cpp": [MimeUtil.FILE_MIME.CPP],
|
|
1167
|
-
".cc": [MimeUtil.FILE_MIME.CPP],
|
|
1168
|
-
".cxx": [MimeUtil.FILE_MIME.CPP],
|
|
1169
|
-
".cs": [MimeUtil.FILE_MIME.CSHARP],
|
|
1170
|
-
".java": [MimeUtil.FILE_MIME.JAVA],
|
|
1171
|
-
".go": [MimeUtil.FILE_MIME.GO],
|
|
1172
|
-
".rs": [MimeUtil.FILE_MIME.RUST],
|
|
1173
|
-
".php": [MimeUtil.FILE_MIME.PHP],
|
|
1174
|
-
".rb": [MimeUtil.FILE_MIME.RUBY],
|
|
1175
|
-
".swift": [MimeUtil.FILE_MIME.SWIFT],
|
|
1176
|
-
".yaml": [MimeUtil.FILE_MIME.YAML, MimeUtil.FILE_MIME.YAML_LEGACY],
|
|
1177
|
-
".yml": [MimeUtil.FILE_MIME.YAML, MimeUtil.FILE_MIME.YAML_LEGACY],
|
|
1178
|
-
".toml": [MimeUtil.FILE_MIME.TOML, MimeUtil.FILE_MIME.TOML_LEGACY],
|
|
1179
|
-
".sql": [MimeUtil.FILE_MIME.SQL, MimeUtil.FILE_MIME.SQL_LEGACY],
|
|
1180
|
-
".md": [MimeUtil.FILE_MIME.MARKDOWN],
|
|
1181
|
-
".markdown": [MimeUtil.FILE_MIME.MARKDOWN],
|
|
1182
|
-
".rtf": [MimeUtil.FILE_MIME.RTF],
|
|
1183
|
-
".ics": [MimeUtil.FILE_MIME.CALENDAR],
|
|
1184
|
-
".jpg": [MimeUtil.FILE_MIME.JPEG],
|
|
1185
|
-
".jpeg": [MimeUtil.FILE_MIME.JPEG],
|
|
1186
|
-
".png": [MimeUtil.FILE_MIME.PNG],
|
|
1187
|
-
".gif": [MimeUtil.FILE_MIME.GIF],
|
|
1188
|
-
".bmp": [MimeUtil.FILE_MIME.BMP],
|
|
1189
|
-
".svg": [MimeUtil.FILE_MIME.SVG],
|
|
1190
|
-
".apng": [MimeUtil.FILE_MIME.APNG],
|
|
1191
|
-
".avif": [MimeUtil.FILE_MIME.AVIF],
|
|
1192
|
-
".ico": [MimeUtil.FILE_MIME.ICO, MimeUtil.FILE_MIME.ICO_LEGACY],
|
|
1193
|
-
".webp": [MimeUtil.FILE_MIME.WEBP],
|
|
1194
|
-
".tif": [MimeUtil.FILE_MIME.TIFF],
|
|
1195
|
-
".tiff": [MimeUtil.FILE_MIME.TIFF],
|
|
1196
|
-
".heic": [MimeUtil.FILE_MIME.HEIC],
|
|
1197
|
-
".heif": [MimeUtil.FILE_MIME.HEIF],
|
|
1198
|
-
".psd": [MimeUtil.FILE_MIME.PSD],
|
|
1199
|
-
".mp3": [MimeUtil.FILE_MIME.MP3],
|
|
1200
|
-
".aac": [MimeUtil.FILE_MIME.AAC],
|
|
1201
|
-
".mid": [MimeUtil.FILE_MIME.MIDI],
|
|
1202
|
-
".midi": [MimeUtil.FILE_MIME.MIDI],
|
|
1203
|
-
".oga": [MimeUtil.FILE_MIME.OGG_AUDIO],
|
|
1204
|
-
".opus": [MimeUtil.FILE_MIME.OPUS],
|
|
1205
|
-
".flac": [MimeUtil.FILE_MIME.FLAC],
|
|
1206
|
-
".wav": [MimeUtil.FILE_MIME.WAV],
|
|
1207
|
-
".weba": [MimeUtil.FILE_MIME.WEBM_AUDIO],
|
|
1208
|
-
".ra": [MimeUtil.FILE_MIME.REAL_AUDIO],
|
|
1209
|
-
".ram": [MimeUtil.FILE_MIME.REAL_AUDIO],
|
|
1210
|
-
".mp4": [MimeUtil.FILE_MIME.MP4],
|
|
1211
|
-
".mpeg": [MimeUtil.FILE_MIME.MPEG],
|
|
1212
|
-
".mpg": [MimeUtil.FILE_MIME.MPEG],
|
|
1213
|
-
".ogv": [MimeUtil.FILE_MIME.OGG_VIDEO],
|
|
1214
|
-
".avi": [MimeUtil.FILE_MIME.AVI],
|
|
1215
|
-
".3gp": [MimeUtil.FILE_MIME.THREE_GPP],
|
|
1216
|
-
".3g2": [MimeUtil.FILE_MIME.THREE_GPP2],
|
|
1217
|
-
".webm": [MimeUtil.FILE_MIME.WEBM],
|
|
1218
|
-
".mkv": [MimeUtil.FILE_MIME.MKV],
|
|
1219
|
-
".mka": [MimeUtil.FILE_MIME.MKA],
|
|
1220
|
-
".mov": [MimeUtil.FILE_MIME.QUICKTIME],
|
|
1221
|
-
".pdf": [MimeUtil.FILE_MIME.PDF],
|
|
1222
|
-
".doc": [MimeUtil.FILE_MIME.DOC],
|
|
1223
|
-
".docx": [MimeUtil.FILE_MIME.DOCX],
|
|
1224
|
-
".xlsx": [MimeUtil.FILE_MIME.XLSX],
|
|
1225
|
-
".xlsm": [MimeUtil.FILE_MIME.XLSM],
|
|
1226
|
-
".xltx": [MimeUtil.FILE_MIME.XLTX],
|
|
1227
|
-
".pptx": [MimeUtil.FILE_MIME.PPTX],
|
|
1228
|
-
".ppt": [MimeUtil.FILE_MIME.PPT],
|
|
1229
|
-
".odt": [MimeUtil.FILE_MIME.ODT],
|
|
1230
|
-
".ods": [MimeUtil.FILE_MIME.ODS],
|
|
1231
|
-
".odp": [MimeUtil.FILE_MIME.ODP],
|
|
1232
|
-
".epub": [MimeUtil.FILE_MIME.EPUB],
|
|
1233
|
-
".azw": [MimeUtil.FILE_MIME.AZW],
|
|
1234
|
-
".zip": [MimeUtil.FILE_MIME.ZIP],
|
|
1235
|
-
".gz": [MimeUtil.FILE_MIME.GZIP],
|
|
1236
|
-
".tar": [MimeUtil.FILE_MIME.TAR],
|
|
1237
|
-
".bz": [MimeUtil.FILE_MIME.BZIP],
|
|
1238
|
-
".bz2": [MimeUtil.FILE_MIME.BZIP2],
|
|
1239
|
-
".7z": [MimeUtil.FILE_MIME.SEVEN_Z],
|
|
1240
|
-
".rar": [MimeUtil.FILE_MIME.RAR],
|
|
1241
|
-
".xz": [MimeUtil.FILE_MIME.XZ],
|
|
1242
|
-
".zst": [MimeUtil.FILE_MIME.ZSTD],
|
|
1243
|
-
".iso": [MimeUtil.FILE_MIME.ISO9660_IMAGE],
|
|
1244
|
-
".json": [MimeUtil.FILE_MIME.JSON],
|
|
1245
|
-
".jsonld": [MimeUtil.FILE_MIME.LD_JSON],
|
|
1246
|
-
".webmanifest": [MimeUtil.FILE_MIME.MANIFEST],
|
|
1247
|
-
".jar": [MimeUtil.FILE_MIME.JAR],
|
|
1248
|
-
".wasm": [MimeUtil.FILE_MIME.WASM],
|
|
1249
|
-
".eot": [MimeUtil.FILE_MIME.EOT],
|
|
1250
|
-
".otf": [MimeUtil.FILE_MIME.OTF],
|
|
1251
|
-
".woff": [MimeUtil.FILE_MIME.WOFF],
|
|
1252
|
-
".woff2": [MimeUtil.FILE_MIME.WOFF2],
|
|
1253
|
-
".ttf": [MimeUtil.FILE_MIME.TTF],
|
|
1254
|
-
".xls": [MimeUtil.FILE_MIME.XLS],
|
|
1255
|
-
".xps": [MimeUtil.FILE_MIME.XPS],
|
|
1256
|
-
".docm": [MimeUtil.FILE_MIME.DOCM]
|
|
1257
|
-
};
|
|
1258
|
-
const MIME_TO_EXT = (() => {
|
|
1259
|
-
const map = /* @__PURE__ */ new Map();
|
|
1260
|
-
for (const [ext, mimes] of Object.entries(EXT_TO_MIME)) for (const mime of mimes) {
|
|
1261
|
-
const exts = map.get(mime);
|
|
1262
|
-
if (exts) {
|
|
1263
|
-
if (!exts.includes(ext)) exts.push(ext);
|
|
1264
|
-
} else map.set(mime, [ext]);
|
|
1265
|
-
}
|
|
1266
|
-
return map;
|
|
1267
|
-
})();
|
|
1268
|
-
//#endregion
|
|
1269
|
-
//#region src/number/numberUtil.ts
|
|
1270
|
-
/**
|
|
1271
|
-
* 数字工具类
|
|
1272
|
-
*/
|
|
1273
|
-
var NumberUtil = class {
|
|
1274
|
-
/**
|
|
1275
|
-
* 数字区间检查函数
|
|
1276
|
-
*
|
|
1277
|
-
* @param input 待检查数字
|
|
1278
|
-
* @param interval 由两个数字组成的元组 [left, right]
|
|
1279
|
-
* @param includeLeft 是否包含左边界(默认 true)
|
|
1280
|
-
* @param includeRight 是否包含右边界(默认 false)
|
|
1281
|
-
* @returns 是否在区间内
|
|
1282
|
-
* @example
|
|
1283
|
-
* ```ts
|
|
1284
|
-
* NumberUtil.within(5, [1, 10]); // true
|
|
1285
|
-
* NumberUtil.within(1, [1, 10], false); // false
|
|
1286
|
-
* ```
|
|
1287
|
-
*/
|
|
1288
|
-
static within(input, interval, includeLeft = true, includeRight = false) {
|
|
1289
|
-
if (!TypeUtil.isNumber(input) || TypeUtil.isInfinity(input)) throw new Error("function [within] Expected parameter [input] to be a finite number.");
|
|
1290
|
-
if (!TypeUtil.isArray(interval) || interval.length !== 2) throw new Error("function [within] Expected parameter [interval] to be a tuple with 2 numbers.");
|
|
1291
|
-
const [left, right] = interval;
|
|
1292
|
-
if (left > right) throw new Error(`Invalid interval: left (${left}) must be <= right (${right}).`);
|
|
1293
|
-
if (includeLeft && includeRight) return input >= left && input <= right;
|
|
1294
|
-
else if (includeLeft) return input >= left && input < right;
|
|
1295
|
-
else if (includeRight) return input > left && input <= right;
|
|
1296
|
-
else return input > left && input < right;
|
|
1297
|
-
}
|
|
1298
|
-
};
|
|
1299
|
-
//#endregion
|
|
1300
|
-
//#region src/object/objectUtil.ts
|
|
1301
|
-
/**
|
|
1302
|
-
* 对象工具类
|
|
1303
|
-
*/
|
|
1304
|
-
var ObjectUtil = class {
|
|
1305
|
-
static keys(value) {
|
|
1306
|
-
return Object.keys(value);
|
|
1307
|
-
}
|
|
1308
|
-
static values(value) {
|
|
1309
|
-
return Object.values(value);
|
|
1310
|
-
}
|
|
1311
|
-
static entries(value) {
|
|
1312
|
-
return Object.entries(value);
|
|
1313
|
-
}
|
|
1314
|
-
/**
|
|
1315
|
-
* 映射对象条目
|
|
1316
|
-
* - 将对象的键值对映射为新的键值对
|
|
1317
|
-
*
|
|
1318
|
-
* @param plainObject 对象
|
|
1319
|
-
* @param toEntry 映射函数
|
|
1320
|
-
* @returns 映射后的新对象
|
|
1321
|
-
* @example
|
|
1322
|
-
* ```ts
|
|
1323
|
-
* const obj = { a: 1, b: 2 };
|
|
1324
|
-
*
|
|
1325
|
-
* ObjectUtil.entriesMap(obj, (k, v) => [k, v * 2]); // { a: 2, b: 4 }
|
|
1326
|
-
*
|
|
1327
|
-
* ObjectUtil.entriesMap(obj, (k, v) => [`prefix_${String(k)}`, `${v}x`]); // { prefix_a: "1x", prefix_b: "2x" }
|
|
1328
|
-
* ```
|
|
1329
|
-
*/
|
|
1330
|
-
static entriesMap(plainObject, toEntry) {
|
|
1331
|
-
const defaultResult = {};
|
|
1332
|
-
if (!TypeUtil.isPlainObject(plainObject)) return defaultResult;
|
|
1333
|
-
return this.entries(plainObject).reduce((acc, [key, value]) => {
|
|
1334
|
-
const [newKey, newValue] = toEntry(key, value);
|
|
1335
|
-
Object.defineProperty(acc, newKey, {
|
|
1336
|
-
value: newValue,
|
|
1337
|
-
enumerable: true,
|
|
1338
|
-
writable: true,
|
|
1339
|
-
configurable: true
|
|
1340
|
-
});
|
|
1341
|
-
return acc;
|
|
1342
|
-
}, defaultResult);
|
|
1343
|
-
}
|
|
1344
|
-
static pick(obj, keys) {
|
|
1345
|
-
const result = {};
|
|
1346
|
-
if (!TypeUtil.isPlainObject(obj)) return result;
|
|
1347
|
-
if (!TypeUtil.isArray(keys)) return obj;
|
|
1348
|
-
return keys.reduce((acc, key) => {
|
|
1349
|
-
if (key in obj) Object.defineProperty(acc, key, {
|
|
1350
|
-
value: obj[key],
|
|
1351
|
-
enumerable: true,
|
|
1352
|
-
writable: true,
|
|
1353
|
-
configurable: true
|
|
1354
|
-
});
|
|
1355
|
-
return acc;
|
|
1356
|
-
}, result);
|
|
1357
|
-
}
|
|
1358
|
-
static omit(obj, keys) {
|
|
1359
|
-
const result = {};
|
|
1360
|
-
if (!TypeUtil.isPlainObject(obj)) return result;
|
|
1361
|
-
if (!TypeUtil.isArray(keys)) return obj;
|
|
1362
|
-
const keysToOmit = new Set(keys);
|
|
1363
|
-
return Object.keys(obj).reduce((acc, key) => {
|
|
1364
|
-
if (!keysToOmit.has(key)) Object.defineProperty(acc, key, {
|
|
1365
|
-
value: obj[key],
|
|
1366
|
-
enumerable: true,
|
|
1367
|
-
writable: true,
|
|
1368
|
-
configurable: true
|
|
1369
|
-
});
|
|
1370
|
-
return acc;
|
|
1371
|
-
}, result);
|
|
1372
|
-
}
|
|
1373
|
-
static invert(obj) {
|
|
1374
|
-
const result = {};
|
|
1375
|
-
if (!TypeUtil.isPlainObject(obj)) return result;
|
|
1376
|
-
for (const [k, v] of this.entries(obj)) if (TypeUtil.isString(v) || TypeUtil.isNumber(v) || TypeUtil.isSymbol(v)) result[v] = k;
|
|
1377
|
-
return result;
|
|
1378
|
-
}
|
|
1379
|
-
static crush(obj) {
|
|
1380
|
-
if (!obj) return {};
|
|
1381
|
-
function crushReducer(crushed, value, path) {
|
|
1382
|
-
if (TypeUtil.isPlainObject(value) || TypeUtil.isArray(value)) for (const [prop, propValue] of Object.entries(value)) crushReducer(crushed, propValue, path ? `${path}.${prop}` : prop);
|
|
1383
|
-
else crushed[path] = value;
|
|
1384
|
-
return crushed;
|
|
1385
|
-
}
|
|
1386
|
-
return crushReducer({}, obj, "");
|
|
1387
|
-
}
|
|
1388
|
-
static enumKeys(enumeration) {
|
|
1389
|
-
const [isEnum, isBidirectionalEnum] = TypeUtil.isEnumeration(enumeration);
|
|
1390
|
-
if (!isEnum) throw Error("function [enumKeys] expected parameter to be a enum, and requires at least one member");
|
|
1391
|
-
const keys = this.keys(enumeration);
|
|
1392
|
-
if (isBidirectionalEnum) return keys.splice(keys.length / 2, keys.length / 2);
|
|
1393
|
-
return keys;
|
|
1394
|
-
}
|
|
1395
|
-
static enumValues(enumeration) {
|
|
1396
|
-
const [isEnum, isBidirectionalEnum] = TypeUtil.isEnumeration(enumeration);
|
|
1397
|
-
if (!isEnum) throw Error("function [enumValues] expected parameter to be a enum, and requires at least one member");
|
|
1398
|
-
const values = this.values(enumeration);
|
|
1399
|
-
if (isBidirectionalEnum) return values.splice(values.length / 2, values.length / 2);
|
|
1400
|
-
return values;
|
|
1401
|
-
}
|
|
1402
|
-
static enumEntries(enumeration) {
|
|
1403
|
-
const [isEnum, isBidirectionalEnum] = TypeUtil.isEnumeration(enumeration);
|
|
1404
|
-
if (!isEnum) throw Error("function [enumEntries] expected parameter to be a enum, and requires at least one member");
|
|
1405
|
-
const entries = this.entries(enumeration);
|
|
1406
|
-
if (isBidirectionalEnum) return entries.splice(entries.length / 2, entries.length / 2);
|
|
1407
|
-
return entries;
|
|
1408
|
-
}
|
|
1409
|
-
};
|
|
1410
|
-
//#endregion
|
|
1411
|
-
//#region src/theme/themeUtil.ts
|
|
1412
|
-
/**
|
|
1413
|
-
* 主题工具类
|
|
1414
|
-
*/
|
|
1415
|
-
var ThemeUtil = class {
|
|
1416
|
-
/**
|
|
1417
|
-
* 固定主题类型(仅亮色/暗色)
|
|
1418
|
-
*
|
|
1419
|
-
* @example
|
|
1420
|
-
* ```ts
|
|
1421
|
-
* ThemeUtil.THEME.LIGHT; // "light"
|
|
1422
|
-
* ThemeUtil.THEME.DARK; // "dark"
|
|
1423
|
-
* ```
|
|
1424
|
-
*/
|
|
1425
|
-
static THEME = {
|
|
1426
|
-
LIGHT: "light",
|
|
1427
|
-
DARK: "dark"
|
|
1428
|
-
};
|
|
1429
|
-
/**
|
|
1430
|
-
* 主题模式(支持跟随系统)
|
|
1431
|
-
*
|
|
1432
|
-
* @example
|
|
1433
|
-
* ```ts
|
|
1434
|
-
* ThemeUtil.THEME_MODE.SYSTEM; // "system"
|
|
1435
|
-
* ThemeUtil.THEME_MODE.DARK; // "dark"
|
|
1436
|
-
* ```
|
|
1437
|
-
*/
|
|
1438
|
-
static THEME_MODE = {
|
|
1439
|
-
LIGHT: "light",
|
|
1440
|
-
DARK: "dark",
|
|
1441
|
-
SYSTEM: "system"
|
|
1442
|
-
};
|
|
1443
|
-
};
|
|
1444
|
-
//#endregion
|
|
1445
|
-
//#region src/tree/utils.ts
|
|
1446
|
-
function getFinalChildrenKey(tree, meta, options) {
|
|
1447
|
-
if (TypeUtil.isFunction(options.getChildrenKey)) {
|
|
1448
|
-
const dynamicChildrenKey = options.getChildrenKey(tree, meta);
|
|
1449
|
-
if (dynamicChildrenKey && dynamicChildrenKey !== null) return dynamicChildrenKey;
|
|
1450
|
-
}
|
|
1451
|
-
return options.childrenKey;
|
|
1452
|
-
}
|
|
1453
|
-
//#endregion
|
|
1454
|
-
//#region src/tree/filter.ts
|
|
1455
|
-
function preImpl$3(row, callback, options) {
|
|
1456
|
-
if (!callback(row, options)) return;
|
|
1457
|
-
const finalChildrenKey = getFinalChildrenKey(row, options, options);
|
|
1458
|
-
const children = row[finalChildrenKey];
|
|
1459
|
-
let newChildren;
|
|
1460
|
-
if (TypeUtil.isArray(children)) {
|
|
1461
|
-
const nextLevelOptions = {
|
|
1462
|
-
...options,
|
|
1463
|
-
parents: [...options.parents, row],
|
|
1464
|
-
depth: options.depth + 1
|
|
1465
|
-
};
|
|
1466
|
-
newChildren = children.map((c) => preImpl$3(c, callback, nextLevelOptions)).filter((c) => !!c);
|
|
1467
|
-
}
|
|
1468
|
-
return {
|
|
1469
|
-
...row,
|
|
1470
|
-
[finalChildrenKey]: newChildren
|
|
1471
|
-
};
|
|
1472
|
-
}
|
|
1473
|
-
function postImpl$3(row, callback, options) {
|
|
1474
|
-
const finalChildrenKey = getFinalChildrenKey(row, options, options);
|
|
1475
|
-
const children = row[finalChildrenKey];
|
|
1476
|
-
let newChildren;
|
|
1477
|
-
if (TypeUtil.isArray(children)) {
|
|
1478
|
-
const nextLevelOptions = {
|
|
1479
|
-
...options,
|
|
1480
|
-
parents: [...options.parents, row],
|
|
1481
|
-
depth: options.depth + 1
|
|
1482
|
-
};
|
|
1483
|
-
newChildren = children.map((c) => postImpl$3(c, callback, nextLevelOptions)).filter((c) => !!c);
|
|
1484
|
-
}
|
|
1485
|
-
if (!callback(row, options)) return;
|
|
1486
|
-
return {
|
|
1487
|
-
...row,
|
|
1488
|
-
[finalChildrenKey]: newChildren
|
|
1489
|
-
};
|
|
1490
|
-
}
|
|
1491
|
-
function breadthImpl$3(row, callback, options) {
|
|
1492
|
-
const queue = [{
|
|
1493
|
-
queueRow: row,
|
|
1494
|
-
queueOptions: options
|
|
1495
|
-
}];
|
|
1496
|
-
const resultCache = /* @__PURE__ */ new WeakMap();
|
|
1497
|
-
const newNodeCache = /* @__PURE__ */ new WeakMap();
|
|
1498
|
-
const childrenKeyCache = /* @__PURE__ */ new WeakMap();
|
|
1499
|
-
let result;
|
|
1500
|
-
const runQueue = () => {
|
|
1501
|
-
if (queue.length === 0) return result;
|
|
1502
|
-
const { queueRow, queueOptions } = queue.shift();
|
|
1503
|
-
const finalChildrenKey = getFinalChildrenKey(queueRow, queueOptions, queueOptions);
|
|
1504
|
-
const children = queueRow[finalChildrenKey];
|
|
1505
|
-
if (TypeUtil.isArray(children)) {
|
|
1506
|
-
const nextLevelOptions = {
|
|
1507
|
-
...queueOptions,
|
|
1508
|
-
parents: [...queueOptions.parents, queueRow],
|
|
1509
|
-
depth: queueOptions.depth + 1
|
|
1510
|
-
};
|
|
1511
|
-
const subQueueItems = children.map((queueRow) => ({
|
|
1512
|
-
queueRow,
|
|
1513
|
-
queueOptions: nextLevelOptions
|
|
1514
|
-
}));
|
|
1515
|
-
queue.push(...subQueueItems);
|
|
1516
|
-
}
|
|
1517
|
-
const parent = ArrayUtil.last(queueOptions.parents);
|
|
1518
|
-
const isTopNode = queueOptions.depth === 0;
|
|
1519
|
-
const parentResult = parent && resultCache.get(parent);
|
|
1520
|
-
if (!isTopNode && !parentResult) return runQueue();
|
|
1521
|
-
const callbackResult = callback(queueRow, queueOptions);
|
|
1522
|
-
if (isTopNode && !callbackResult) return;
|
|
1523
|
-
const newNode = {
|
|
1524
|
-
...queueRow,
|
|
1525
|
-
[finalChildrenKey]: void 0
|
|
1526
|
-
};
|
|
1527
|
-
if (isTopNode) result = newNode;
|
|
1528
|
-
resultCache.set(queueRow, callbackResult);
|
|
1529
|
-
newNodeCache.set(queueRow, newNode);
|
|
1530
|
-
childrenKeyCache.set(queueRow, finalChildrenKey);
|
|
1531
|
-
if (callbackResult && parent) {
|
|
1532
|
-
const parentNewNode = newNodeCache.get(parent);
|
|
1533
|
-
const parentChildrenKey = childrenKeyCache.get(parent);
|
|
1534
|
-
if (parentNewNode && parentChildrenKey) {
|
|
1535
|
-
if (!parentNewNode[parentChildrenKey]) parentNewNode[parentChildrenKey] = [];
|
|
1536
|
-
parentNewNode[parentChildrenKey].push(newNode);
|
|
1537
|
-
}
|
|
1538
|
-
}
|
|
1539
|
-
return runQueue();
|
|
1540
|
-
};
|
|
1541
|
-
return runQueue();
|
|
1542
|
-
}
|
|
1543
|
-
const treeFilterStrategies = {
|
|
1544
|
-
pre: preImpl$3,
|
|
1545
|
-
post: postImpl$3,
|
|
1546
|
-
breadth: breadthImpl$3
|
|
1547
|
-
};
|
|
1548
|
-
//#endregion
|
|
1549
|
-
//#region src/tree/find.ts
|
|
1550
|
-
function preImpl$2(row, callback, options) {
|
|
1551
|
-
if (callback(row, options)) return row;
|
|
1552
|
-
const children = row[getFinalChildrenKey(row, options, options)];
|
|
1553
|
-
if (TypeUtil.isArray(children)) for (const child of children) {
|
|
1554
|
-
const result = preImpl$2(child, callback, {
|
|
1555
|
-
...options,
|
|
1556
|
-
parents: [...options.parents, row],
|
|
1557
|
-
depth: options.depth + 1
|
|
1558
|
-
});
|
|
1559
|
-
if (result) return result;
|
|
1560
|
-
}
|
|
1561
|
-
}
|
|
1562
|
-
function postImpl$2(row, callback, options) {
|
|
1563
|
-
const children = row[getFinalChildrenKey(row, options, options)];
|
|
1564
|
-
if (TypeUtil.isArray(children)) for (const child of children) {
|
|
1565
|
-
const result = postImpl$2(child, callback, {
|
|
1566
|
-
...options,
|
|
1567
|
-
parents: [...options.parents, row],
|
|
1568
|
-
depth: options.depth + 1
|
|
1569
|
-
});
|
|
1570
|
-
if (result) return result;
|
|
1571
|
-
}
|
|
1572
|
-
if (callback(row, options)) return row;
|
|
1573
|
-
}
|
|
1574
|
-
function breadthImpl$2(row, callback, options) {
|
|
1575
|
-
const queue = [{
|
|
1576
|
-
queueRow: row,
|
|
1577
|
-
queueOptions: options
|
|
1578
|
-
}];
|
|
1579
|
-
const runQueue = () => {
|
|
1580
|
-
if (queue.length === 0) return;
|
|
1581
|
-
const { queueRow, queueOptions } = queue.shift();
|
|
1582
|
-
const children = queueRow[getFinalChildrenKey(queueRow, queueOptions, queueOptions)];
|
|
1583
|
-
if (TypeUtil.isArray(children)) {
|
|
1584
|
-
const nextLevelOptions = {
|
|
1585
|
-
...queueOptions,
|
|
1586
|
-
parents: [...queueOptions.parents, queueRow],
|
|
1587
|
-
depth: queueOptions.depth + 1
|
|
1588
|
-
};
|
|
1589
|
-
const subQueueItems = children.map((queueRow) => ({
|
|
1590
|
-
queueRow,
|
|
1591
|
-
queueOptions: nextLevelOptions
|
|
1592
|
-
}));
|
|
1593
|
-
queue.push(...subQueueItems);
|
|
1594
|
-
}
|
|
1595
|
-
if (callback(queueRow, queueOptions)) return queueRow;
|
|
1596
|
-
return runQueue();
|
|
1597
|
-
};
|
|
1598
|
-
return runQueue();
|
|
1599
|
-
}
|
|
1600
|
-
const treeFindStrategies = {
|
|
1601
|
-
pre: preImpl$2,
|
|
1602
|
-
post: postImpl$2,
|
|
1603
|
-
breadth: breadthImpl$2
|
|
1604
|
-
};
|
|
1605
|
-
//#endregion
|
|
1606
|
-
//#region src/tree/forEach.ts
|
|
1607
|
-
function preImpl$1(row, callback, options) {
|
|
1608
|
-
callback(row, options);
|
|
1609
|
-
const children = row[getFinalChildrenKey(row, options, options)];
|
|
1610
|
-
if (TypeUtil.isArray(children)) {
|
|
1611
|
-
const nextLevelOptions = {
|
|
1612
|
-
...options,
|
|
1613
|
-
parents: [...options.parents, row],
|
|
1614
|
-
depth: options.depth + 1
|
|
1615
|
-
};
|
|
1616
|
-
for (const child of children) preImpl$1(child, callback, nextLevelOptions);
|
|
1617
|
-
}
|
|
1618
|
-
}
|
|
1619
|
-
function postImpl$1(row, callback, options) {
|
|
1620
|
-
const children = row[getFinalChildrenKey(row, options, options)];
|
|
1621
|
-
if (TypeUtil.isArray(children)) {
|
|
1622
|
-
const nextLevelOptions = {
|
|
1623
|
-
...options,
|
|
1624
|
-
parents: [...options.parents, row],
|
|
1625
|
-
depth: options.depth + 1
|
|
1626
|
-
};
|
|
1627
|
-
for (const child of children) postImpl$1(child, callback, nextLevelOptions);
|
|
1628
|
-
}
|
|
1629
|
-
callback(row, options);
|
|
1630
|
-
}
|
|
1631
|
-
function breadthImpl$1(row, callback, options) {
|
|
1632
|
-
const queue = [{
|
|
1633
|
-
queueRow: row,
|
|
1634
|
-
queueOptions: options
|
|
1635
|
-
}];
|
|
1636
|
-
const runQueue = () => {
|
|
1637
|
-
if (queue.length === 0) return;
|
|
1638
|
-
const { queueRow, queueOptions } = queue.shift();
|
|
1639
|
-
const children = queueRow[getFinalChildrenKey(queueRow, queueOptions, queueOptions)];
|
|
1640
|
-
if (TypeUtil.isArray(children)) {
|
|
1641
|
-
const nextLevelOptions = {
|
|
1642
|
-
...queueOptions,
|
|
1643
|
-
parents: [...queueOptions.parents, queueRow],
|
|
1644
|
-
depth: queueOptions.depth + 1
|
|
1645
|
-
};
|
|
1646
|
-
const subQueueItems = children.map((queueRow) => ({
|
|
1647
|
-
queueRow,
|
|
1648
|
-
queueOptions: nextLevelOptions
|
|
1649
|
-
}));
|
|
1650
|
-
queue.push(...subQueueItems);
|
|
1651
|
-
}
|
|
1652
|
-
callback(queueRow, queueOptions);
|
|
1653
|
-
runQueue();
|
|
1654
|
-
};
|
|
1655
|
-
runQueue();
|
|
1656
|
-
}
|
|
1657
|
-
const treeForEachStrategies = {
|
|
1658
|
-
pre: preImpl$1,
|
|
1659
|
-
post: postImpl$1,
|
|
1660
|
-
breadth: breadthImpl$1
|
|
1661
|
-
};
|
|
1662
|
-
//#endregion
|
|
1663
|
-
//#region src/tree/map.ts
|
|
1664
|
-
function preImpl(row, callback, options) {
|
|
1665
|
-
const finalChildrenKey = getFinalChildrenKey(row, options, options);
|
|
1666
|
-
const result = callback(row, options);
|
|
1667
|
-
const children = row[finalChildrenKey];
|
|
1668
|
-
let newChildren;
|
|
1669
|
-
if (TypeUtil.isArray(children)) {
|
|
1670
|
-
const nextLevelOptions = {
|
|
1671
|
-
...options,
|
|
1672
|
-
parents: [...options.parents, row],
|
|
1673
|
-
depth: options.depth + 1
|
|
1674
|
-
};
|
|
1675
|
-
newChildren = children.map((c) => preImpl(c, callback, nextLevelOptions));
|
|
1676
|
-
}
|
|
1677
|
-
return {
|
|
1678
|
-
...result,
|
|
1679
|
-
[finalChildrenKey]: newChildren
|
|
1680
|
-
};
|
|
1681
|
-
}
|
|
1682
|
-
function postImpl(row, callback, options) {
|
|
1683
|
-
const finalChildrenKey = getFinalChildrenKey(row, options, options);
|
|
1684
|
-
const children = row[finalChildrenKey];
|
|
1685
|
-
let newChildren;
|
|
1686
|
-
if (TypeUtil.isArray(children)) {
|
|
1687
|
-
const nextLevelOptions = {
|
|
1688
|
-
...options,
|
|
1689
|
-
parents: [...options.parents, row],
|
|
1690
|
-
depth: options.depth + 1
|
|
1691
|
-
};
|
|
1692
|
-
newChildren = children.map((c) => postImpl(c, callback, nextLevelOptions));
|
|
1693
|
-
}
|
|
1694
|
-
return {
|
|
1695
|
-
...callback(row, options),
|
|
1696
|
-
[finalChildrenKey]: newChildren
|
|
1697
|
-
};
|
|
1698
|
-
}
|
|
1699
|
-
function breadthImpl(row, callback, options) {
|
|
1700
|
-
const queue = [{
|
|
1701
|
-
queueRow: row,
|
|
1702
|
-
queueOptions: options
|
|
1703
|
-
}];
|
|
1704
|
-
const cache = /* @__PURE__ */ new WeakMap();
|
|
1705
|
-
const childrenKeyCache = /* @__PURE__ */ new WeakMap();
|
|
1706
|
-
let result;
|
|
1707
|
-
const runQueue = () => {
|
|
1708
|
-
if (queue.length === 0) return result;
|
|
1709
|
-
const { queueRow, queueOptions } = queue.shift();
|
|
1710
|
-
const finalChildrenKey = getFinalChildrenKey(queueRow, queueOptions, queueOptions);
|
|
1711
|
-
const children = queueRow[finalChildrenKey];
|
|
1712
|
-
if (TypeUtil.isArray(children)) {
|
|
1713
|
-
const nextLevelOptions = {
|
|
1714
|
-
...queueOptions,
|
|
1715
|
-
parents: [...queueOptions.parents, queueRow],
|
|
1716
|
-
depth: queueOptions.depth + 1
|
|
1717
|
-
};
|
|
1718
|
-
const subQueueItems = children.map((queueRow) => ({
|
|
1719
|
-
queueRow,
|
|
1720
|
-
queueOptions: nextLevelOptions
|
|
1721
|
-
}));
|
|
1722
|
-
queue.push(...subQueueItems);
|
|
1723
|
-
}
|
|
1724
|
-
const res = callback(queueRow, queueOptions);
|
|
1725
|
-
cache.set(queueRow, res);
|
|
1726
|
-
childrenKeyCache.set(queueRow, finalChildrenKey);
|
|
1727
|
-
const parent = ArrayUtil.last(queueOptions.parents);
|
|
1728
|
-
if (parent) {
|
|
1729
|
-
const newParent = cache.get(parent);
|
|
1730
|
-
const parentChildrenKey = childrenKeyCache.get(parent);
|
|
1731
|
-
if (newParent && parentChildrenKey) if (newParent[parentChildrenKey]) newParent[parentChildrenKey].push(res);
|
|
1732
|
-
else newParent[parentChildrenKey] = [res];
|
|
1733
|
-
}
|
|
1734
|
-
if (queueOptions.depth === 0) result = res;
|
|
1735
|
-
return runQueue();
|
|
1736
|
-
};
|
|
1737
|
-
return runQueue();
|
|
1738
|
-
}
|
|
1739
|
-
const treeMapStrategies = {
|
|
1740
|
-
pre: preImpl,
|
|
1741
|
-
post: postImpl,
|
|
1742
|
-
breadth: breadthImpl
|
|
1743
|
-
};
|
|
1744
|
-
//#endregion
|
|
1745
|
-
//#region src/tree/treeUtil.ts
|
|
1746
|
-
/**
|
|
1747
|
-
* 树结构工具类
|
|
1748
|
-
*
|
|
1749
|
-
* 引用策略约定:
|
|
1750
|
-
* - 转换类方法(`rowsToTree` / `treeToRows` / `filter` / `map`):不突变输入;输出的每个节点均为**新对象引用**(来源节点的浅拷贝,仅自有可枚举属性;非枚举属性、原型链、getter 及深层嵌套对象不保证)。
|
|
1751
|
-
* - 查询类方法(`find` / `forEach`):按查询语义直接使用**原对象引用**。
|
|
1752
|
-
*/
|
|
1753
|
-
var TreeUtil = class {
|
|
1754
|
-
/**
|
|
1755
|
-
* 行结构 转 树结构
|
|
1756
|
-
* - 将平铺的数组转换为树形结构
|
|
1757
|
-
* - 返回的树结构与输入行无共享节点(新对象引用),输入行不会被突变
|
|
1758
|
-
* - 重复 id 的行只取首次出现;仅叶子/缺失父节点的 id 会作为根节点,且每个根节点只输出一次
|
|
1759
|
-
*
|
|
1760
|
-
* @param rows 行数据数组
|
|
1761
|
-
* @param options 配置项
|
|
1762
|
-
* @returns 树结构数组(所有节点均包含 children 数组)
|
|
1763
|
-
* @example
|
|
1764
|
-
* ```ts
|
|
1765
|
-
* const rows = [
|
|
1766
|
-
* { id: 1, parentId: null },
|
|
1767
|
-
* { id: 2, parentId: 1 },
|
|
1768
|
-
* ];
|
|
1769
|
-
* TreeUtil.rowsToTree(rows); // [{ id: 1, parentId: null, children: [{ id: 2, parentId: 1, children: [] }] }]
|
|
1770
|
-
* ```
|
|
1771
|
-
*/
|
|
1772
|
-
static rowsToTree(rows, options) {
|
|
1773
|
-
const { parentIdKey = "parentId", rowKey = "id", childrenKey = "children" } = options || {};
|
|
1774
|
-
const result = [];
|
|
1775
|
-
const map = /* @__PURE__ */ new Map();
|
|
1776
|
-
const processedIds = /* @__PURE__ */ new Set();
|
|
1777
|
-
for (const row of rows) {
|
|
1778
|
-
const id = row[rowKey];
|
|
1779
|
-
if (!map.get(id)) map.set(id, { ...row });
|
|
1780
|
-
}
|
|
1781
|
-
for (const row of rows) {
|
|
1782
|
-
const id = row[rowKey];
|
|
1783
|
-
if (processedIds.has(id)) continue;
|
|
1784
|
-
processedIds.add(id);
|
|
1785
|
-
const parentId = row[parentIdKey];
|
|
1786
|
-
const node = map.get(id);
|
|
1787
|
-
if (!node) continue;
|
|
1788
|
-
if (TypeUtil.isNullish(parentId) || parentId === id || !map.has(parentId)) {
|
|
1789
|
-
result.push(node);
|
|
1790
|
-
continue;
|
|
1791
|
-
}
|
|
1792
|
-
const parent = map.get(parentId);
|
|
1793
|
-
const siblings = parent[childrenKey];
|
|
1794
|
-
if (TypeUtil.isNullish(siblings)) parent[childrenKey] = [node];
|
|
1795
|
-
else if (Array.isArray(siblings)) siblings.push(node);
|
|
1796
|
-
else {
|
|
1797
|
-
const message = `The key "${childrenKey.toString()}" in parent item is not an array.`;
|
|
1798
|
-
throw new Error(message);
|
|
1799
|
-
}
|
|
1800
|
-
}
|
|
1801
|
-
for (const root of result) this.forEach(root, (node) => {
|
|
1802
|
-
const record = node;
|
|
1803
|
-
const children = record[childrenKey];
|
|
1804
|
-
if (TypeUtil.isNullish(children)) record[childrenKey] = [];
|
|
1805
|
-
else if (!Array.isArray(children)) {
|
|
1806
|
-
const message = `The key "${childrenKey.toString()}" in parent item is not an array.`;
|
|
1807
|
-
throw new Error(message);
|
|
1808
|
-
}
|
|
1809
|
-
}, { childrenKey });
|
|
1810
|
-
return result;
|
|
1811
|
-
}
|
|
1812
|
-
/**
|
|
1813
|
-
* 树结构 转 行结构
|
|
1814
|
-
* - 将树形结构扁平化为数组
|
|
1815
|
-
*
|
|
1816
|
-
* @param tree 树结构数据 (单个节点或节点数组)
|
|
1817
|
-
* @param options 配置项
|
|
1818
|
-
* @returns 扁平化后的数组
|
|
1819
|
-
* @example
|
|
1820
|
-
* ```ts
|
|
1821
|
-
* const tree = [{ id: 1, children: [{ id: 2 }] }];
|
|
1822
|
-
* TreeUtil.treeToRows(tree); // [{ id: 1, children: undefined }, { id: 2, children: undefined }]
|
|
1823
|
-
* ```
|
|
1824
|
-
*/
|
|
1825
|
-
static treeToRows(tree, options = {}) {
|
|
1826
|
-
const { childrenKey = "children" } = options;
|
|
1827
|
-
const result = [];
|
|
1828
|
-
if (!tree) return result;
|
|
1829
|
-
this.forEach(tree, (t) => result.push({
|
|
1830
|
-
...t,
|
|
1831
|
-
[childrenKey]: void 0
|
|
1832
|
-
}), options);
|
|
1833
|
-
return result;
|
|
1834
|
-
}
|
|
1835
|
-
/**
|
|
1836
|
-
* 遍历树节点
|
|
1837
|
-
*
|
|
1838
|
-
* @param tree 树结构数据
|
|
1839
|
-
* @param callback 回调函数
|
|
1840
|
-
* @param options 配置项
|
|
1841
|
-
* @example
|
|
1842
|
-
* ```ts
|
|
1843
|
-
* const tree = [{ id: 1, children: [{ id: 2 }] }];
|
|
1844
|
-
* const ids: number[] = [];
|
|
1845
|
-
* TreeUtil.forEach(tree, (node) => ids.push(node.id)); // ids: [1, 2] (pre-order default)
|
|
1846
|
-
* ```
|
|
1847
|
-
*/
|
|
1848
|
-
static forEach(tree, callback, options = {}) {
|
|
1849
|
-
const { childrenKey = "children", strategy = "pre", getChildrenKey } = options;
|
|
1850
|
-
const traversalMethod = treeForEachStrategies[strategy];
|
|
1851
|
-
const innerOptions = {
|
|
1852
|
-
childrenKey,
|
|
1853
|
-
depth: 0,
|
|
1854
|
-
parents: [],
|
|
1855
|
-
getChildrenKey
|
|
1856
|
-
};
|
|
1857
|
-
if (TypeUtil.isArray(tree)) for (const row of tree) traversalMethod(row, callback, innerOptions);
|
|
1858
|
-
else traversalMethod(tree, callback, innerOptions);
|
|
1859
|
-
}
|
|
1860
|
-
/**
|
|
1861
|
-
* 查找树节点
|
|
1862
|
-
* - 返回第一个回调返回 true 的节点
|
|
1863
|
-
*
|
|
1864
|
-
* @param tree 树结构数据
|
|
1865
|
-
* @param callback 回调函数
|
|
1866
|
-
* @param options 配置项
|
|
1867
|
-
* @returns 找到的节点,未找到则返回 undefined
|
|
1868
|
-
* @example
|
|
1869
|
-
* ```ts
|
|
1870
|
-
* const tree = [{ id: 1, children: [{ id: 2 }] }];
|
|
1871
|
-
* TreeUtil.find(tree, (node) => node.id === 2); // { id: 2, ... }
|
|
1872
|
-
* ```
|
|
1873
|
-
*/
|
|
1874
|
-
static find(tree, callback, options = {}) {
|
|
1875
|
-
const { childrenKey = "children", strategy = "pre", getChildrenKey } = options;
|
|
1876
|
-
const traversalMethod = treeFindStrategies[strategy];
|
|
1877
|
-
const innerOptions = {
|
|
1878
|
-
childrenKey,
|
|
1879
|
-
depth: 0,
|
|
1880
|
-
parents: [],
|
|
1881
|
-
getChildrenKey
|
|
1882
|
-
};
|
|
1883
|
-
if (TypeUtil.isArray(tree)) {
|
|
1884
|
-
for (const row of tree) {
|
|
1885
|
-
const result = traversalMethod(row, callback, innerOptions);
|
|
1886
|
-
if (result) return result;
|
|
1887
|
-
}
|
|
1888
|
-
return;
|
|
1889
|
-
}
|
|
1890
|
-
return traversalMethod(tree, callback, innerOptions);
|
|
1891
|
-
}
|
|
1892
|
-
static filter(tree, callback, options = {}) {
|
|
1893
|
-
const { childrenKey = "children", strategy = "pre", getChildrenKey } = options;
|
|
1894
|
-
const traversalMethod = treeFilterStrategies[strategy];
|
|
1895
|
-
const innerOptions = {
|
|
1896
|
-
childrenKey,
|
|
1897
|
-
depth: 0,
|
|
1898
|
-
parents: [],
|
|
1899
|
-
getChildrenKey
|
|
1900
|
-
};
|
|
1901
|
-
return TypeUtil.isArray(tree) ? tree.map((row) => traversalMethod(row, callback, innerOptions)).filter((t) => !!t) : traversalMethod(tree, callback, innerOptions) || [];
|
|
1902
|
-
}
|
|
1903
|
-
static map(tree, callback, options = {}) {
|
|
1904
|
-
const { childrenKey = "children", strategy = "pre", getChildrenKey } = options;
|
|
1905
|
-
const traversalMethod = treeMapStrategies[strategy];
|
|
1906
|
-
const innerOptions = {
|
|
1907
|
-
childrenKey,
|
|
1908
|
-
depth: 0,
|
|
1909
|
-
parents: [],
|
|
1910
|
-
getChildrenKey
|
|
1911
|
-
};
|
|
1912
|
-
return TypeUtil.isArray(tree) ? tree.map((row) => traversalMethod(row, callback, innerOptions)) : traversalMethod(tree, callback, innerOptions);
|
|
1913
|
-
}
|
|
1914
|
-
};
|
|
1915
|
-
//#endregion
|
|
1916
|
-
//#region src/validate/validateUtil.ts
|
|
1917
|
-
/**
|
|
1918
|
-
* 验证工具类
|
|
1919
|
-
*/
|
|
1920
|
-
var ValidateUtil = class {
|
|
1921
|
-
static _phone = /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/;
|
|
1922
|
-
/**
|
|
1923
|
-
* 验证是否为手机号码
|
|
1924
|
-
* @example
|
|
1925
|
-
* ```ts
|
|
1926
|
-
* ValidateUtil.isPhone("13800138000"); // true
|
|
1927
|
-
* ```
|
|
1928
|
-
*/
|
|
1929
|
-
static isPhone(input) {
|
|
1930
|
-
return this._phone.test(input.toString());
|
|
1931
|
-
}
|
|
1932
|
-
static _telephone = /^(((0\d{2,3})-)?((\d{7,8})|(400\d{7})|(800\d{7}))(-(\d{1,4}))?)$/;
|
|
1933
|
-
/**
|
|
1934
|
-
* 验证是否为固定电话
|
|
1935
|
-
* @example
|
|
1936
|
-
* ```ts
|
|
1937
|
-
* ValidateUtil.isTelephone("010-12345678"); // true
|
|
1938
|
-
* ```
|
|
1939
|
-
*/
|
|
1940
|
-
static isTelephone(input) {
|
|
1941
|
-
return this._telephone.test(input.toString());
|
|
1942
|
-
}
|
|
1943
|
-
static _IMEI = /^\d{15,17}$/;
|
|
1944
|
-
/**
|
|
1945
|
-
* 验证是否为移动设备识别码
|
|
1946
|
-
* @example
|
|
1947
|
-
* ```ts
|
|
1948
|
-
* ValidateUtil.isIMEI("490154203237518"); // true
|
|
1949
|
-
* ```
|
|
1950
|
-
*/
|
|
1951
|
-
static isIMEI(input) {
|
|
1952
|
-
return this._IMEI.test(input.toString());
|
|
1953
|
-
}
|
|
1954
|
-
static _email = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\])|(([a-z\-0-9]+\.)+[a-z]{2,}))$/i;
|
|
1955
|
-
/**
|
|
1956
|
-
* 验证是否为电子邮箱
|
|
1957
|
-
* @example
|
|
1958
|
-
* ```ts
|
|
1959
|
-
* ValidateUtil.isEmail("dev@example.com"); // true
|
|
1960
|
-
* ```
|
|
1961
|
-
*/
|
|
1962
|
-
static isEmail(input) {
|
|
1963
|
-
return this._email.test(input.toString());
|
|
1964
|
-
}
|
|
1965
|
-
static _link = /^(https?:\/\/)?(([\w-]+(\.[\w-]+)*\.[a-z]{2,6})|((\d{1,3}\.){3}\d{1,3}))(:\d+)?(\/\S*)?$/i;
|
|
1966
|
-
/**
|
|
1967
|
-
* 验证是否为 http(s) 链接
|
|
1968
|
-
* @example
|
|
1969
|
-
* ```ts
|
|
1970
|
-
* ValidateUtil.isHttpLink("https://example.com/path"); // true
|
|
1971
|
-
* ```
|
|
1972
|
-
*/
|
|
1973
|
-
static isHttpLink(input) {
|
|
1974
|
-
return this._link.test(input.toString());
|
|
1975
|
-
}
|
|
1976
|
-
static _portLink = /^(https?:\/\/)?[\w-]+(\.[\w-]+)+:\d{1,5}\/?$/i;
|
|
1977
|
-
/**
|
|
1978
|
-
* 验证是否为端口号链接
|
|
1979
|
-
* @example
|
|
1980
|
-
* ```ts
|
|
1981
|
-
* ValidateUtil.isPortLink("http://example.com:8080"); // true
|
|
1982
|
-
* ```
|
|
1983
|
-
*/
|
|
1984
|
-
static isPortLink(input) {
|
|
1985
|
-
return this._portLink.test(input.toString());
|
|
1986
|
-
}
|
|
1987
|
-
static _thunderLink = /^thunderx?:\/\/[a-zA-Z\d]+=$/i;
|
|
1988
|
-
/**
|
|
1989
|
-
* 验证是否为迅雷链接
|
|
1990
|
-
* @example
|
|
1991
|
-
* ```ts
|
|
1992
|
-
* ValidateUtil.isThunderLink("thunder://QUFodHRwOi8vZXhhbXBsZS5jb20vZmlsZQ=="); // true
|
|
1993
|
-
* ```
|
|
1994
|
-
*/
|
|
1995
|
-
static isThunderLink(input) {
|
|
1996
|
-
return this._thunderLink.test(input.toString());
|
|
1997
|
-
}
|
|
1998
|
-
static _uscc = /^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/;
|
|
1999
|
-
/**
|
|
2000
|
-
* 验证是否为统一社会信用代码(USCC / USCI / USCCS)
|
|
2001
|
-
* - 固定 18 位:1 位登记管理部门码 + 1 位机构类别码 + 6 位行政区划码 + 9 位主体标识码 + 1 位校验码
|
|
2002
|
-
* - 字符集:数字 0-9 + 大写英文字母(排除 I、O、Z、S、V,防视觉混淆)
|
|
2003
|
-
* - 第 1-2 位允许字母(如登记管理部门码 `A`,代表"其他"),第 3-8 位行政区划码为纯数字
|
|
2004
|
-
*
|
|
2005
|
-
* @param input 待校验字符串
|
|
2006
|
-
* @returns 是否为合法格式的统一社会信用代码
|
|
2007
|
-
* @example
|
|
2008
|
-
* ```ts
|
|
2009
|
-
* ValidateUtil.isUSCC("91350100M000100Y43"); // true
|
|
2010
|
-
* ValidateUtil.isUSCC("A1350100M000100Y43"); // true (A 开头"其他"部门码)
|
|
2011
|
-
* ```
|
|
2012
|
-
*/
|
|
2013
|
-
static isUSCC(input) {
|
|
2014
|
-
return this._uscc.test(input.toString());
|
|
2015
|
-
}
|
|
2016
|
-
/**
|
|
2017
|
-
* 验证是否为统一社会信用代码(同 `isUSCC`)
|
|
2018
|
-
* - USCC / USCI / USCCS 均指统一社会信用代码,固定 18 位
|
|
2019
|
-
* - 15 位旧税务登记号在 2015 年"三证合一"前使用,现已作废,视为无效
|
|
2020
|
-
*
|
|
2021
|
-
* @param input 待校验字符串
|
|
2022
|
-
* @returns 是否为合法代码
|
|
2023
|
-
* @example
|
|
2024
|
-
* ```ts
|
|
2025
|
-
* ValidateUtil.isUSCCS("91350100M000100Y43"); // true (18位)
|
|
2026
|
-
* ValidateUtil.isUSCCS("91350100M000100"); // false (15位旧号,已作废)
|
|
2027
|
-
* ```
|
|
2028
|
-
*/
|
|
2029
|
-
static isUSCCS(input) {
|
|
2030
|
-
return this.isUSCC(input);
|
|
2031
|
-
}
|
|
2032
|
-
static _dirPathWindows = /^[a-z]:\\(?:\w+\\?)*$/i;
|
|
2033
|
-
/**
|
|
2034
|
-
* 验证是否为 Windows 系统文件夹路径
|
|
2035
|
-
* @example
|
|
2036
|
-
* ```ts
|
|
2037
|
-
* ValidateUtil.isDirPathWindows("C:\\Users\\pawover\\"); // true
|
|
2038
|
-
* ```
|
|
2039
|
-
*/
|
|
2040
|
-
static isDirPathWindows(input) {
|
|
2041
|
-
return this._dirPathWindows.test(input.toString());
|
|
2042
|
-
}
|
|
2043
|
-
static _filePathWindows = /^[a-z]:\\(?:\w+\\)*\w+\.\w+$/i;
|
|
2044
|
-
/**
|
|
2045
|
-
* 验证是否为 Windows 系统文件路径
|
|
2046
|
-
* @example
|
|
2047
|
-
* ```ts
|
|
2048
|
-
* ValidateUtil.isFilePathWindows("C:\\Users\\pawover\\a.txt"); // true
|
|
2049
|
-
* ```
|
|
2050
|
-
*/
|
|
2051
|
-
static isFilePathWindows(input) {
|
|
2052
|
-
return this._filePathWindows.test(input.toString());
|
|
2053
|
-
}
|
|
2054
|
-
static _dirPathLinux = /^\/(?:[^\\/\s]+\/)*$/;
|
|
2055
|
-
/**
|
|
2056
|
-
* 验证是否为 Linux 系统文件夹路径
|
|
2057
|
-
* @example
|
|
2058
|
-
* ```ts
|
|
2059
|
-
* ValidateUtil.isDirPathLinux("/usr/local/"); // true
|
|
2060
|
-
* ```
|
|
2061
|
-
*/
|
|
2062
|
-
static isDirPathLinux(input) {
|
|
2063
|
-
return this._dirPathLinux.test(input.toString());
|
|
2064
|
-
}
|
|
2065
|
-
static _filePathLinux = /^(\/$|\/(?:[^\\/\s]+\/)*[^\\/\s]+$)/;
|
|
2066
|
-
/**
|
|
2067
|
-
* 验证是否为 Linux 系统文件路径
|
|
2068
|
-
* @example
|
|
2069
|
-
* ```ts
|
|
2070
|
-
* ValidateUtil.isFilePathLinux("/usr/local/bin/node"); // true
|
|
2071
|
-
* ```
|
|
2072
|
-
*/
|
|
2073
|
-
static isFilePathLinux(input) {
|
|
2074
|
-
return this._filePathLinux.test(input.toString());
|
|
2075
|
-
}
|
|
2076
|
-
static _EVCarNumber = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z](([DF]((?![IO])[a-zA-Z0-9](?![IO]))\d{4})|(\d{5}[DF]))$/;
|
|
2077
|
-
/**
|
|
2078
|
-
* 验证是否为新能源车牌号
|
|
2079
|
-
* @example
|
|
2080
|
-
* ```ts
|
|
2081
|
-
* ValidateUtil.isEVCarNumber("粤AD12345"); // true
|
|
2082
|
-
* ```
|
|
2083
|
-
*/
|
|
2084
|
-
static isEVCarNumber(input) {
|
|
2085
|
-
return this._EVCarNumber.test(input.toString());
|
|
2086
|
-
}
|
|
2087
|
-
static _GVCarNumber = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$/;
|
|
2088
|
-
/**
|
|
2089
|
-
* 验证是否为燃油车车牌号
|
|
2090
|
-
* @example
|
|
2091
|
-
* ```ts
|
|
2092
|
-
* ValidateUtil.isGVCarNumber("粤B12345"); // true
|
|
2093
|
-
* ```
|
|
2094
|
-
*/
|
|
2095
|
-
static isGVCarNumber(input) {
|
|
2096
|
-
return this._GVCarNumber.test(input.toString());
|
|
2097
|
-
}
|
|
2098
|
-
static _chineseName = /^[一-龢][一·-龢]*$/;
|
|
2099
|
-
/**
|
|
2100
|
-
* 验证是否为中文姓名
|
|
2101
|
-
* @example
|
|
2102
|
-
* ```ts
|
|
2103
|
-
* ValidateUtil.isChineseName("张三"); // true
|
|
2104
|
-
* ```
|
|
2105
|
-
*/
|
|
2106
|
-
static isChineseName(input) {
|
|
2107
|
-
return this._chineseName.test(input.toString());
|
|
2108
|
-
}
|
|
2109
|
-
static _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;
|
|
2110
|
-
/**
|
|
2111
|
-
* 验证是否为中国身份证号
|
|
2112
|
-
* - ⚠️ 仅校验 18 位格式(含生日合法性),**不验证第 18 位校验位**,伪造码可通过校验
|
|
2113
|
-
* @example
|
|
2114
|
-
* ```ts
|
|
2115
|
-
* ValidateUtil.isChineseID("11010519491231002X"); // true
|
|
2116
|
-
* ```
|
|
2117
|
-
*/
|
|
2118
|
-
static isChineseID(input) {
|
|
2119
|
-
return this._chineseId.test(input.toString());
|
|
2120
|
-
}
|
|
2121
|
-
static _chineseProvince = /^(?:安徽|澳门|北京|重庆|福建|甘肃|广东|广西|贵州|海南|河北|河南|黑龙江|湖北|湖南|吉林|江苏|江西|辽宁|内蒙古|宁夏|青海|山东|山西|陕西|上海|四川|台湾|天津|西藏|香港|新疆|云南|浙江)$/;
|
|
2122
|
-
/**
|
|
2123
|
-
* 验证是否为中国省份
|
|
2124
|
-
* @example
|
|
2125
|
-
* ```ts
|
|
2126
|
-
* ValidateUtil.isChineseProvince("浙江"); // true
|
|
2127
|
-
* ```
|
|
2128
|
-
*/
|
|
2129
|
-
static isChineseProvince(input) {
|
|
2130
|
-
return this._chineseProvince.test(input.toString());
|
|
2131
|
-
}
|
|
2132
|
-
static _chineseNation = /^(?:汉族|蒙古族|回族|藏族|维吾尔族|苗族|彝族|壮族|布依族|朝鲜族|满族|侗族|瑶族|白族|土家族|哈尼族|哈萨克族|傣族|黎族|傈僳族|佤族|畲族|高山族|拉祜族|水族|东乡族|纳西族|景颇族|柯尔克孜族|土族|达斡尔族|仫佬族|羌族|布朗族|撒拉族|毛南族|仡佬族|锡伯族|阿昌族|普米族|塔吉克族|怒族|乌孜别克族|俄罗斯族|鄂温克族|德昂族|保安族|裕固族|京族|塔塔尔族|独龙族|鄂伦春族|赫哲族|门巴族|珞巴族|基诺族|其它未识别民族|外国人入中国籍)$/;
|
|
2133
|
-
/**
|
|
2134
|
-
* 验证是否为中华民族
|
|
2135
|
-
* @example
|
|
2136
|
-
* ```ts
|
|
2137
|
-
* ValidateUtil.isChineseNation("汉族"); // true
|
|
2138
|
-
* ```
|
|
2139
|
-
*/
|
|
2140
|
-
static isChineseNation(input) {
|
|
2141
|
-
return this._chineseNation.test(input.toString());
|
|
2142
|
-
}
|
|
2143
|
-
static _letter = /^[a-z]+$/i;
|
|
2144
|
-
/**
|
|
2145
|
-
* 验证是否只包含字母
|
|
2146
|
-
* @example
|
|
2147
|
-
* ```ts
|
|
2148
|
-
* ValidateUtil.isLetter("abcDEF"); // true
|
|
2149
|
-
* ```
|
|
2150
|
-
*/
|
|
2151
|
-
static isLetter(input) {
|
|
2152
|
-
return this._letter.test(input.toString());
|
|
2153
|
-
}
|
|
2154
|
-
static _letterLowercase = /^[a-z]+$/;
|
|
2155
|
-
/**
|
|
2156
|
-
* 验证是否只包含小写字母
|
|
2157
|
-
* @example
|
|
2158
|
-
* ```ts
|
|
2159
|
-
* ValidateUtil.isLetterLowercase("abc"); // true
|
|
2160
|
-
* ```
|
|
2161
|
-
*/
|
|
2162
|
-
static isLetterLowercase(input) {
|
|
2163
|
-
return this._letterLowercase.test(input.toString());
|
|
2164
|
-
}
|
|
2165
|
-
static _letterUppercase = /^[A-Z]+$/;
|
|
2166
|
-
/**
|
|
2167
|
-
* 验证是否只包含大写字母
|
|
2168
|
-
* @example
|
|
2169
|
-
* ```ts
|
|
2170
|
-
* ValidateUtil.isLetterUppercase("ABC"); // true
|
|
2171
|
-
* ```
|
|
2172
|
-
*/
|
|
2173
|
-
static isLetterUppercase(input) {
|
|
2174
|
-
return this._letterUppercase.test(input.toString());
|
|
2175
|
-
}
|
|
2176
|
-
static _letterOmit = /^[^A-Z]*$/i;
|
|
2177
|
-
/**
|
|
2178
|
-
* 验证是否不包含字母
|
|
2179
|
-
* @example
|
|
2180
|
-
* ```ts
|
|
2181
|
-
* ValidateUtil.isLetterOmit("123_-"); // true
|
|
2182
|
-
* ```
|
|
2183
|
-
*/
|
|
2184
|
-
static isLetterOmit(input) {
|
|
2185
|
-
return this._letterOmit.test(input.toString());
|
|
2186
|
-
}
|
|
2187
|
-
static _LetterAndNumber = /^[A-Z0-9]+$/i;
|
|
2188
|
-
/**
|
|
2189
|
-
* 验证是否为数字和字母组合
|
|
2190
|
-
* @example
|
|
2191
|
-
* ```ts
|
|
2192
|
-
* ValidateUtil.isLetterAndNumber("A1B2"); // true
|
|
2193
|
-
* ```
|
|
2194
|
-
*/
|
|
2195
|
-
static isLetterAndNumber(input) {
|
|
2196
|
-
return this._LetterAndNumber.test(input.toString());
|
|
2197
|
-
}
|
|
2198
|
-
static _signedFloat = /^[+-]?(\d+(\.\d+)?|\.\d+)$/;
|
|
2199
|
-
/**
|
|
2200
|
-
* 验证是否为有符号浮点数
|
|
2201
|
-
* @example
|
|
2202
|
-
* ```ts
|
|
2203
|
-
* ValidateUtil.isSignedFloat("-12.34"); // true
|
|
2204
|
-
* ```
|
|
2205
|
-
*/
|
|
2206
|
-
static isSignedFloat(input) {
|
|
2207
|
-
return this._signedFloat.test(input.toString());
|
|
2208
|
-
}
|
|
2209
|
-
static _unsignedFloat = /^\+?(\d+(\.\d+)?|\.\d+)$/;
|
|
2210
|
-
/**
|
|
2211
|
-
* 验证是否为无符号浮点数
|
|
2212
|
-
* @example
|
|
2213
|
-
* ```ts
|
|
2214
|
-
* ValidateUtil.isUnsignedFloat("12.34"); // true
|
|
2215
|
-
* ```
|
|
2216
|
-
*/
|
|
2217
|
-
static isUnsignedFloat(input) {
|
|
2218
|
-
return this._unsignedFloat.test(input.toString());
|
|
2219
|
-
}
|
|
2220
|
-
static _signedInteger = /^[+-]?\d+$/;
|
|
2221
|
-
/**
|
|
2222
|
-
* 验证是否为有符号整数
|
|
2223
|
-
* @example
|
|
2224
|
-
* ```ts
|
|
2225
|
-
* ValidateUtil.isSignedInteger("-12"); // true
|
|
2226
|
-
* ```
|
|
2227
|
-
*/
|
|
2228
|
-
static isSignedInteger(input) {
|
|
2229
|
-
return this._signedInteger.test(input.toString());
|
|
2230
|
-
}
|
|
2231
|
-
static _unsignedInteger = /^\+?\d+$/;
|
|
2232
|
-
/**
|
|
2233
|
-
* 验证是否为无符号整数
|
|
2234
|
-
* @example
|
|
2235
|
-
* ```ts
|
|
2236
|
-
* ValidateUtil.isUnsignedInteger("12"); // true
|
|
2237
|
-
* ```
|
|
2238
|
-
*/
|
|
2239
|
-
static isUnsignedInteger(input) {
|
|
2240
|
-
return this._unsignedInteger.test(input.toString());
|
|
2241
|
-
}
|
|
2242
|
-
static _spaceInclude = /\s/;
|
|
2243
|
-
/**
|
|
2244
|
-
* 验证是否包含空格
|
|
2245
|
-
* @example
|
|
2246
|
-
* ```ts
|
|
2247
|
-
* ValidateUtil.isSpaceInclude("a b"); // true
|
|
2248
|
-
* ```
|
|
2249
|
-
*/
|
|
2250
|
-
static isSpaceInclude(input) {
|
|
2251
|
-
return this._spaceInclude.test(input.toString());
|
|
2252
|
-
}
|
|
2253
|
-
static _spaceStart = /^\s/;
|
|
2254
|
-
/**
|
|
2255
|
-
* 验证是否以空格开头
|
|
2256
|
-
* @example
|
|
2257
|
-
* ```ts
|
|
2258
|
-
* ValidateUtil.isSpaceStart(" abc"); // true
|
|
2259
|
-
* ```
|
|
2260
|
-
*/
|
|
2261
|
-
static isSpaceStart(input) {
|
|
2262
|
-
return this._spaceStart.test(input.toString());
|
|
2263
|
-
}
|
|
2264
|
-
static _spaceEnd = /\s$/;
|
|
2265
|
-
/**
|
|
2266
|
-
* 验证是否以空格结尾
|
|
2267
|
-
* @example
|
|
2268
|
-
* ```ts
|
|
2269
|
-
* ValidateUtil.isSpaceEnd("abc "); // true
|
|
2270
|
-
* ```
|
|
2271
|
-
*/
|
|
2272
|
-
static isSpaceEnd(input) {
|
|
2273
|
-
return this._spaceEnd.test(input.toString());
|
|
2274
|
-
}
|
|
2275
|
-
/**
|
|
2276
|
-
* 验证是否以空格开头或结尾
|
|
2277
|
-
* @example
|
|
2278
|
-
* ```ts
|
|
2279
|
-
* ValidateUtil.isSpaceStartOrEnd(" abc"); // true
|
|
2280
|
-
* ```
|
|
2281
|
-
*/
|
|
2282
|
-
static isSpaceStartOrEnd(input) {
|
|
2283
|
-
return this.isSpaceStart(input) || this.isSpaceEnd(input);
|
|
2284
|
-
}
|
|
2285
|
-
};
|
|
2286
|
-
//#endregion
|
|
2287
|
-
export { ArrayUtil, CurrencyUtil, DateTimeUtil, EnvUtil, FunctionUtil, MimeUtil, NumberUtil, ObjectUtil, StringUtil, ThemeUtil, TreeUtil, TypeUtil, ValidateUtil };
|