@fast-china/utils 2.0.0 → 2.0.2
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/CHANGELOG.md +27 -0
- package/Fast.png +0 -0
- package/README.md +27 -29
- package/README.zh.md +27 -29
- package/dist/base64/index.mjs.map +1 -1
- package/dist/crypto/index.d.mts +265 -96
- package/dist/crypto/index.mjs +563 -261
- package/dist/crypto/index.mjs.map +1 -1
- package/dist/identity/index.d.mts +5 -5
- package/dist/identity/index.mjs +2 -2
- package/dist/identity/index.mjs.map +1 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.global.min.js +3 -0
- package/dist/index.global.min.js.map +1 -0
- package/dist/index.mjs +2 -2
- package/dist/storage/index.d.mts +11 -8
- package/dist/storage/index.mjs +21 -18
- package/dist/storage/index.mjs.map +1 -1
- package/dist/vue/emits.mjs.map +1 -1
- package/dist/vue/index.d.mts +2 -2
- package/dist/vue/install.d.mts +11 -29
- package/dist/vue/install.mjs +25 -25
- package/dist/vue/install.mjs.map +1 -1
- package/dist/vue/props.mjs.map +1 -1
- package/dist/vue/render.d.mts +2 -3
- package/dist/vue/render.mjs +3 -9
- package/dist/vue/render.mjs.map +1 -1
- package/docs/API.md +30 -14
- package/docs/API.zh-CN.md +30 -14
- package/docs/DEVELOPMENT_RELEASE.zh-CN.md +4 -4
- package/docs/RUNTIME_CONTRACT.md +10 -10
- package/package.json +9 -11
- package/dist/array/index.d.mts.map +0 -1
- package/dist/async/index.d.mts.map +0 -1
- package/dist/base64/index.d.mts.map +0 -1
- package/dist/color/index.d.mts.map +0 -1
- package/dist/crypto/index.d.mts.map +0 -1
- package/dist/date/index.d.mts.map +0 -1
- package/dist/dom/style.d.mts.map +0 -1
- package/dist/env/index.d.mts.map +0 -1
- package/dist/identity/index.d.mts.map +0 -1
- package/dist/logger/index.d.mts.map +0 -1
- package/dist/number/index.d.mts.map +0 -1
- package/dist/object/index.d.mts.map +0 -1
- package/dist/storage/index.d.mts.map +0 -1
- package/dist/string/index.d.mts.map +0 -1
- package/dist/vue/emits.d.mts.map +0 -1
- package/dist/vue/expose.d.mts.map +0 -1
- package/dist/vue/func.d.mts.map +0 -1
- package/dist/vue/install.d.mts.map +0 -1
- package/dist/vue/props.d.mts.map +0 -1
- package/dist/vue/render.d.mts.map +0 -1
- package/dist/vue/slots.d.mts.map +0 -1
- package/dist/vue/with.d.mts.map +0 -1
- package/src/array/index.ts +0 -173
- package/src/async/index.ts +0 -475
- package/src/base64/index.ts +0 -374
- package/src/color/index.ts +0 -208
- package/src/crypto/index.ts +0 -670
- package/src/date/index.ts +0 -451
- package/src/dom/index.ts +0 -6
- package/src/dom/style.ts +0 -92
- package/src/env/index.ts +0 -169
- package/src/identity/index.ts +0 -144
- package/src/index.ts +0 -20
- package/src/internal/text.ts +0 -46
- package/src/logger/index.ts +0 -219
- package/src/number/index.ts +0 -235
- package/src/object/index.ts +0 -160
- package/src/storage/index.ts +0 -524
- package/src/string/index.ts +0 -328
- package/src/vue/emits.ts +0 -71
- package/src/vue/expose.ts +0 -11
- package/src/vue/func.ts +0 -17
- package/src/vue/index.ts +0 -13
- package/src/vue/install.ts +0 -185
- package/src/vue/props.ts +0 -39
- package/src/vue/render.ts +0 -41
- package/src/vue/slots.ts +0 -23
- package/src/vue/with.ts +0 -10
package/src/string/index.ts
DELETED
|
@@ -1,328 +0,0 @@
|
|
|
1
|
-
const defaultRandomAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
2
|
-
const defaultStringLocale = "en-US";
|
|
3
|
-
const maximumRandomStringLength = 1_000_000;
|
|
4
|
-
const maximumRandomValuesPerBatch = 16_384;
|
|
5
|
-
const uuidV4Pattern = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iu;
|
|
6
|
-
|
|
7
|
-
/** 字符串随机 API 需要的 Web Crypto 最小能力。 */
|
|
8
|
-
type RuntimeStringCrypto = Partial<Pick<Crypto, "getRandomValues" | "randomUUID">>;
|
|
9
|
-
|
|
10
|
-
/** 字素分割需要的可选 Intl 能力。 */
|
|
11
|
-
interface RuntimeStringIntl {
|
|
12
|
-
/** 可选 Segmenter 构造器;缺失时字素 API 使用内部兼容路径。 */
|
|
13
|
-
Segmenter?: typeof Intl.Segmenter;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/** 字符串工具延迟访问的平台全局对象最小视图。 */
|
|
17
|
-
interface RuntimeStringGlobals {
|
|
18
|
-
/** 安全随机字符串与 UUID 所需的可选 Web Crypto 能力。 */
|
|
19
|
-
crypto?: RuntimeStringCrypto;
|
|
20
|
-
/** 字素分割所需的可选 Intl 能力。 */
|
|
21
|
-
Intl?: RuntimeStringIntl;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const runtimeGlobals = globalThis as unknown as RuntimeStringGlobals;
|
|
25
|
-
|
|
26
|
-
/** 查询字符串解析结果;重复键保留为数组,不存在的键读取为 `undefined`。 */
|
|
27
|
-
export type ParsedQueryParameters = Record<string, string | string[] | undefined>;
|
|
28
|
-
|
|
29
|
-
/** 大小写与字素分割可接受的显式语言;省略时固定使用 `en-US` 以保持输出稳定。 */
|
|
30
|
-
export type StringLocale = string | readonly string[] | undefined;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* 获取字符串随机 API 所需的 Web Crypto 能力。
|
|
34
|
-
*
|
|
35
|
-
* @returns 具有 `getRandomValues` 的当前 Crypto 对象。
|
|
36
|
-
* @throws `Error` 当平台没有安全随机能力;绝不回退到 `Math.random()`。
|
|
37
|
-
*/
|
|
38
|
-
const requireWebCrypto = (): Crypto => {
|
|
39
|
-
const crypto = runtimeGlobals.crypto;
|
|
40
|
-
if (typeof crypto?.getRandomValues !== "function") {
|
|
41
|
-
throw new Error("Web Crypto random generation is unavailable in the current runtime.");
|
|
42
|
-
}
|
|
43
|
-
return crypto as Crypto;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* 从随机字节创建 UUID v4。
|
|
48
|
-
*
|
|
49
|
-
* @param bytes - 长度至少为 16 的随机字节;Version 与 Variant 位会被原地修改。
|
|
50
|
-
* @returns 小写、带连字符的 RFC 4122 UUID v4。
|
|
51
|
-
*/
|
|
52
|
-
const createUuidV4FromBytes = (bytes: Uint8Array): string => {
|
|
53
|
-
bytes[6] = ((bytes[6] ?? 0) & 15) | 64;
|
|
54
|
-
bytes[8] = ((bytes[8] ?? 0) & 63) | 128;
|
|
55
|
-
const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
56
|
-
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* 按用户可见字素切分文本。
|
|
61
|
-
*
|
|
62
|
-
* @param value - 待切分字符串。
|
|
63
|
-
* @param locale - Segmenter 使用的显式语言;省略时使用固定默认值。
|
|
64
|
-
* @returns 保留组合 Emoji、变音符号和连接序列的字素数组。
|
|
65
|
-
* @throws `Error` 当平台缺少 `Intl.Segmenter`。
|
|
66
|
-
*/
|
|
67
|
-
const splitGraphemes = (value: string, locale: StringLocale): string[] => {
|
|
68
|
-
const Segmenter = runtimeGlobals.Intl?.Segmenter;
|
|
69
|
-
if (typeof Segmenter !== "function") {
|
|
70
|
-
throw new Error("Intl.Segmenter is unavailable in the current runtime.");
|
|
71
|
-
}
|
|
72
|
-
const segmenter = new Segmenter(locale ?? defaultStringLocale, { granularity: "grapheme" });
|
|
73
|
-
return Array.from(segmenter.segment(value), ({ segment }) => segment);
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* 重复执行 URI 组件解码,直到值稳定或达到深度上限。
|
|
78
|
-
*
|
|
79
|
-
* @param value - 不包含 URI 路径语义的编码组件。
|
|
80
|
-
* @param maxDepth - 最大解码次数,默认 `10`。
|
|
81
|
-
* @returns 解码稳定或达到上限后的组件文本。
|
|
82
|
-
* @throws `URIError` 当任一层包含非法百分号序列;深度非法时抛出 `RangeError`。
|
|
83
|
-
*/
|
|
84
|
-
export function decodeURIComponentRepeatedly(value: string, maxDepth = 10): string {
|
|
85
|
-
if (!Number.isSafeInteger(maxDepth) || maxDepth < 0) throw new RangeError("maxDepth must be a non-negative safe integer.");
|
|
86
|
-
let decoded = value;
|
|
87
|
-
for (let index = 0; index < maxDepth; index += 1) {
|
|
88
|
-
const next = decodeURIComponent(decoded);
|
|
89
|
-
if (next === decoded) break;
|
|
90
|
-
decoded = next;
|
|
91
|
-
}
|
|
92
|
-
return decoded;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* 解析带 `://` 的绝对 URL、`?query` 或纯查询字符串。
|
|
97
|
-
*
|
|
98
|
-
* @remarks 纯查询字符串值中的未编码 `?` 会作为值内容保留;片段标识及其后内容被忽略。
|
|
99
|
-
* @param input - 完整 URL、带前导问号或不带前导问号的查询文本。
|
|
100
|
-
* @returns 重复键对应字符串数组,空值保留为空字符串。
|
|
101
|
-
*/
|
|
102
|
-
export function parseQueryString(input: string): ParsedQueryParameters {
|
|
103
|
-
const fragmentStart = input.indexOf("#");
|
|
104
|
-
const withoutFragment = fragmentStart < 0 ? input : input.slice(0, fragmentStart);
|
|
105
|
-
const isAbsoluteUrl = /^[a-z][a-z\d+.-]*:\/\//iu.test(withoutFragment);
|
|
106
|
-
const queryStart = withoutFragment.indexOf("?");
|
|
107
|
-
if (isAbsoluteUrl && queryStart < 0) return {};
|
|
108
|
-
const query = isAbsoluteUrl ? withoutFragment.slice(queryStart + 1) : withoutFragment.replace(/^\?/u, "");
|
|
109
|
-
const result: ParsedQueryParameters = {};
|
|
110
|
-
for (const [key, value] of new URLSearchParams(query)) {
|
|
111
|
-
const existing = Object.hasOwn(result, key) ? result[key] : undefined;
|
|
112
|
-
if (existing === undefined) {
|
|
113
|
-
// defineProperty 让 `__proto__` 成为普通自有键,不触发 Object.prototype Setter。
|
|
114
|
-
Object.defineProperty(result, key, { configurable: true, enumerable: true, value, writable: true });
|
|
115
|
-
} else if (Array.isArray(existing)) existing.push(value);
|
|
116
|
-
else Object.defineProperty(result, key, { configurable: true, enumerable: true, value: [existing, value], writable: true });
|
|
117
|
-
}
|
|
118
|
-
return result;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* 判断文本是否为任意合法 JSON 值,包括标量与 `null`。
|
|
123
|
-
*
|
|
124
|
-
* @param value - 待解析文本;纯空白不视为 JSON。
|
|
125
|
-
* @returns `JSON.parse` 能完整解析时返回 `true`。
|
|
126
|
-
*/
|
|
127
|
-
export function isValidJson(value: string): boolean {
|
|
128
|
-
if (value.trim().length === 0) return false;
|
|
129
|
-
try {
|
|
130
|
-
JSON.parse(value);
|
|
131
|
-
return true;
|
|
132
|
-
} catch {
|
|
133
|
-
return false;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* 按大小写边界、连字符、下划线与空白切分单词。
|
|
139
|
-
*
|
|
140
|
-
* @example `XMLHttp_request` 返回 `["XML", "Http", "request"]`。
|
|
141
|
-
* @param value - 待拆分文本。
|
|
142
|
-
* @returns 删除空项、保持输入顺序的单词数组。
|
|
143
|
-
*/
|
|
144
|
-
export function splitWords(value: string): string[] {
|
|
145
|
-
return value
|
|
146
|
-
.trim()
|
|
147
|
-
.replace(/(\p{Ll}|\p{N})(\p{Lu})/gu, "$1 $2")
|
|
148
|
-
.replace(/(\p{Lu})(\p{Lu}\p{Ll})/gu, "$1 $2")
|
|
149
|
-
.split(/[\s_-]+/u)
|
|
150
|
-
.filter((part) => part.length > 0);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* 将首个 Unicode 码点转为大写。
|
|
155
|
-
*
|
|
156
|
-
* @param value - 输入文本;空字符串保持为空。
|
|
157
|
-
* @param locale - 显式语言,默认固定为 `en-US`。
|
|
158
|
-
* @returns 首个 Unicode 码点转换后的文本。
|
|
159
|
-
*/
|
|
160
|
-
export function upperFirst(value: string, locale?: StringLocale): string {
|
|
161
|
-
const characters = Array.from(value);
|
|
162
|
-
const first = characters.shift();
|
|
163
|
-
return first === undefined ? "" : first.toLocaleUpperCase(locale ?? defaultStringLocale) + characters.join("");
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* 将首个 Unicode 码点转为小写。
|
|
168
|
-
*
|
|
169
|
-
* @param value - 输入文本;空字符串保持为空。
|
|
170
|
-
* @param locale - 显式语言,默认固定为 `en-US`。
|
|
171
|
-
* @returns 首个 Unicode 码点转换后的文本。
|
|
172
|
-
*/
|
|
173
|
-
export function lowerFirst(value: string, locale?: StringLocale): string {
|
|
174
|
-
const characters = Array.from(value);
|
|
175
|
-
const first = characters.shift();
|
|
176
|
-
return first === undefined ? "" : first.toLocaleLowerCase(locale ?? defaultStringLocale) + characters.join("");
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* 将文本转换为 camelCase。
|
|
181
|
-
*
|
|
182
|
-
* @param value - 由大小写、连字符、下划线或空白分隔的文本。
|
|
183
|
-
* @param locale - 大小写转换使用的语言,默认固定为 `en-US`。
|
|
184
|
-
* @returns camelCase 文本。
|
|
185
|
-
*/
|
|
186
|
-
export function camelCase(value: string, locale?: StringLocale): string {
|
|
187
|
-
return splitWords(value)
|
|
188
|
-
.map((part, index) => {
|
|
189
|
-
const normalized = part.toLocaleLowerCase(locale ?? defaultStringLocale);
|
|
190
|
-
return index === 0 ? normalized : upperFirst(normalized, locale);
|
|
191
|
-
})
|
|
192
|
-
.join("");
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* 将文本转换为 PascalCase。
|
|
197
|
-
*
|
|
198
|
-
* @param value - 参数语义与 {@link camelCase} 一致。
|
|
199
|
-
* @param locale - 大小写转换使用的显式语言。
|
|
200
|
-
* @returns PascalCase 文本。
|
|
201
|
-
*/
|
|
202
|
-
export function pascalCase(value: string, locale?: StringLocale): string {
|
|
203
|
-
return upperFirst(camelCase(value, locale), locale);
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* 将文本转换为 kebab-case。
|
|
208
|
-
*
|
|
209
|
-
* @param value - 参数语义与 {@link camelCase} 一致。
|
|
210
|
-
* @param locale - 大小写转换使用的显式语言。
|
|
211
|
-
* @returns kebab-case 文本。
|
|
212
|
-
*/
|
|
213
|
-
export function kebabCase(value: string, locale?: StringLocale): string {
|
|
214
|
-
return splitWords(value)
|
|
215
|
-
.map((part) => part.toLocaleLowerCase(locale ?? defaultStringLocale))
|
|
216
|
-
.join("-");
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* 按 Unicode 字素簇截断文本,避免拆开 emoji、组合音标或代理对。
|
|
221
|
-
*
|
|
222
|
-
* @param value - 输入文本。
|
|
223
|
-
* @param maxLength - 保留的最大字素簇数量。
|
|
224
|
-
* @param suffix - 被截断时追加的文本,默认单字符省略号 `…`;不计入上限。
|
|
225
|
-
* @param locale - 字素分割语言,默认固定为 `en-US`。
|
|
226
|
-
* @returns 未超限时返回原字符串,否则返回截断内容与后缀。
|
|
227
|
-
* @throws `RangeError` 当 `maxLength` 不是非负安全整数或 Locale 无效;缺少
|
|
228
|
-
* `Intl.Segmenter` 时抛出 `Error`。
|
|
229
|
-
*/
|
|
230
|
-
export function truncateGraphemes(value: string, maxLength: number, suffix = "…", locale?: StringLocale): string {
|
|
231
|
-
if (!Number.isSafeInteger(maxLength) || maxLength < 0) throw new RangeError("maxLength must be a non-negative safe integer.");
|
|
232
|
-
const segments = splitGraphemes(value, locale);
|
|
233
|
-
return segments.length > maxLength ? segments.slice(0, maxLength).join("") + suffix : value;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* 使用无偏 Web Crypto 随机数生成字符串。
|
|
238
|
-
*
|
|
239
|
-
* @param length - 字符数量,必须是 0 至 1,000,000 的安全整数。
|
|
240
|
-
* @param alphabet - 不得为空、包含重复字符或超过 2^32 个 Unicode 码点。
|
|
241
|
-
* @returns 由 `alphabet` 中 Unicode 码点组成的随机文本。
|
|
242
|
-
* @throws `RangeError` 当长度或字母表非法;缺少 Web Crypto 时抛出 `Error`。
|
|
243
|
-
*/
|
|
244
|
-
export function secureRandomString(length: number, alphabet: string = defaultRandomAlphabet): string {
|
|
245
|
-
if (!Number.isSafeInteger(length) || length < 0 || length > maximumRandomStringLength) {
|
|
246
|
-
throw new RangeError(`length must be a safe integer from 0 through ${maximumRandomStringLength}.`);
|
|
247
|
-
}
|
|
248
|
-
const characters = Array.from(alphabet);
|
|
249
|
-
if (characters.length === 0) throw new RangeError("alphabet cannot be empty.");
|
|
250
|
-
if (new Set(characters).size !== characters.length) throw new RangeError("alphabet cannot contain duplicate characters.");
|
|
251
|
-
if (characters.length > 0x1_0000_0000) throw new RangeError("alphabet cannot contain more than 2^32 characters.");
|
|
252
|
-
if (length === 0) return "";
|
|
253
|
-
|
|
254
|
-
const crypto = requireWebCrypto();
|
|
255
|
-
const uint32Range = 0x1_0000_0000;
|
|
256
|
-
// 丢弃不能平均映射到字母表的尾部区间,避免 `%` 造成前部字符概率偏高。
|
|
257
|
-
const acceptanceLimit = Math.floor(uint32Range / characters.length) * characters.length;
|
|
258
|
-
const result: string[] = [];
|
|
259
|
-
while (result.length < length) {
|
|
260
|
-
const remaining = length - result.length;
|
|
261
|
-
// 分批请求可控制临时内存,并避开 Web Crypto 单次随机数组大小限制。
|
|
262
|
-
const samples = crypto.getRandomValues(new Uint32Array(Math.min(remaining, maximumRandomValuesPerBatch)));
|
|
263
|
-
for (const sample of samples) {
|
|
264
|
-
if (sample >= acceptanceLimit) continue;
|
|
265
|
-
const character = characters[sample % characters.length];
|
|
266
|
-
if (character === undefined) continue;
|
|
267
|
-
result.push(character);
|
|
268
|
-
if (result.length === length) break;
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
return result.join("");
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
/**
|
|
275
|
-
* 使用 Web Crypto 生成 RFC 4122 version 4 UUID。
|
|
276
|
-
*
|
|
277
|
-
* @returns 小写、带连字符的 UUID v4。
|
|
278
|
-
* @throws 缺少 Web Crypto 时抛出 `Error`。
|
|
279
|
-
*/
|
|
280
|
-
export function generateUuidV4(): string {
|
|
281
|
-
const crypto = requireWebCrypto();
|
|
282
|
-
if (typeof crypto.randomUUID === "function") return crypto.randomUUID();
|
|
283
|
-
return createUuidV4FromBytes(crypto.getRandomValues(new Uint8Array(16)));
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
/**
|
|
287
|
-
* 判断字符串是否为 RFC 4122 version 4 UUID。
|
|
288
|
-
*
|
|
289
|
-
* @param value - 待验证文本;十六进制字母大小写均可。
|
|
290
|
-
* @returns 版本位与 Variant 位均正确时返回 `true`。
|
|
291
|
-
*/
|
|
292
|
-
export function isUuidV4(value: string): boolean {
|
|
293
|
-
return uuidV4Pattern.test(value);
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* 转义 HTML 文本上下文中的五个特殊字符。
|
|
298
|
-
*
|
|
299
|
-
* @remarks 这不是 HTML 清洗器,不能让不可信文本安全进入 URL、CSS、脚本或属性名上下文。
|
|
300
|
-
* @param value - 将作为 HTML 文本节点内容的字符串。
|
|
301
|
-
* @returns 转义 `&`、`<`、`>`、双引号与单引号后的文本。
|
|
302
|
-
*/
|
|
303
|
-
export function escapeHtml(value: string): string {
|
|
304
|
-
return value.replace(/[&<>"']/gu, (character) => {
|
|
305
|
-
switch (character) {
|
|
306
|
-
case "&":
|
|
307
|
-
return "&";
|
|
308
|
-
case "<":
|
|
309
|
-
return "<";
|
|
310
|
-
case ">":
|
|
311
|
-
return ">";
|
|
312
|
-
case '"':
|
|
313
|
-
return """;
|
|
314
|
-
default:
|
|
315
|
-
return "'";
|
|
316
|
-
}
|
|
317
|
-
});
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
/**
|
|
321
|
-
* 把连续 Unicode 空白折叠为单个空格并删除两端空白。
|
|
322
|
-
*
|
|
323
|
-
* @param value - 输入文本。
|
|
324
|
-
* @returns 规范化后的文本;全空白输入返回空字符串。
|
|
325
|
-
*/
|
|
326
|
-
export function normalizeWhitespace(value: string): string {
|
|
327
|
-
return value.trim().replace(/\s+/gu, " ");
|
|
328
|
-
}
|
package/src/vue/emits.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { computed } from "vue";
|
|
2
|
-
|
|
3
|
-
import type { ComputedRef } from "vue";
|
|
4
|
-
|
|
5
|
-
/** Vue Emits 对象中允许的校验器形状。 */
|
|
6
|
-
type EmitValidator = ((...arguments_: never[]) => unknown) | null;
|
|
7
|
-
/** 事件名到可选参数校验器的内部映射。 */
|
|
8
|
-
type EmitsOptions = Record<string, EmitValidator>;
|
|
9
|
-
/** 从校验器中提取事件参数;无校验器时保留未知参数。 */
|
|
10
|
-
type EventArguments<Validator> = Validator extends (...arguments_: infer Arguments) => unknown ? Arguments : unknown[];
|
|
11
|
-
/** 在类型层递归把 kebab-case 事件名转换为 PascalCase。 */
|
|
12
|
-
type PascalEventName<Value extends string> = Value extends `${infer Head}-${infer Tail}`
|
|
13
|
-
? `${Capitalize<Head>}${PascalEventName<Tail>}`
|
|
14
|
-
: Capitalize<Value>;
|
|
15
|
-
|
|
16
|
-
/** 把事件配置映射为 Vue `onXxx` 属性。 */
|
|
17
|
-
export type EmitHandlers<Emits extends EmitsOptions> = {
|
|
18
|
-
[Name in keyof Emits as Name extends string ? `on${PascalEventName<Name>}` : never]: (...arguments_: EventArguments<Emits[Name]>) => void;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* 把事件名转换为 Vue Handler Prop 名称。
|
|
23
|
-
*
|
|
24
|
-
* @param eventName - Emits 对象中的原始事件名,可使用 kebab-case。
|
|
25
|
-
* @returns `onPascalCase` 形式的属性名。
|
|
26
|
-
* @throws `TypeError` 当事件名包含空片段或无法生成有效 Handler 名称。
|
|
27
|
-
*/
|
|
28
|
-
const toHandlerName = (eventName: string): string => {
|
|
29
|
-
return `on${eventName
|
|
30
|
-
.split("-")
|
|
31
|
-
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
32
|
-
.join("")}`;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* 构建响应式 Vue 事件处理器。
|
|
37
|
-
*
|
|
38
|
-
* @param emits - Vue emits 配置对象。
|
|
39
|
-
* @param emit - `setup` 上下文提供的 emit 函数。
|
|
40
|
-
* @param ignoredEvents - 不需要向子组件透传的事件名。
|
|
41
|
-
* @returns 随配置重新计算的事件处理器对象。
|
|
42
|
-
*/
|
|
43
|
-
export function useEmits<Emits extends EmitsOptions>(
|
|
44
|
-
emits: Emits,
|
|
45
|
-
emit: (...arguments_: never[]) => unknown,
|
|
46
|
-
ignoredEvents: readonly (keyof Emits)[] = []
|
|
47
|
-
): ComputedRef<Partial<EmitHandlers<Emits>>> {
|
|
48
|
-
const ignored = new Set<PropertyKey>(ignoredEvents);
|
|
49
|
-
const emitEvent = emit as unknown as (eventName: string, ...arguments_: unknown[]) => void;
|
|
50
|
-
return computed<Partial<EmitHandlers<Emits>>>(() => {
|
|
51
|
-
const handlers = {} as Partial<EmitHandlers<Emits>>;
|
|
52
|
-
const handlerNames = new Set<string>();
|
|
53
|
-
for (const eventName of Object.keys(emits)) {
|
|
54
|
-
if (ignored.has(eventName)) continue;
|
|
55
|
-
if (eventName.length === 0 || /\s/u.test(eventName)) throw new TypeError(`Invalid Vue event name: "${eventName}".`);
|
|
56
|
-
const handlerName = toHandlerName(eventName);
|
|
57
|
-
if (handlerNames.has(handlerName)) {
|
|
58
|
-
throw new TypeError(`Vue events map to the same handler property: "${handlerName}".`);
|
|
59
|
-
}
|
|
60
|
-
handlerNames.add(handlerName);
|
|
61
|
-
Object.defineProperty(handlers, handlerName, {
|
|
62
|
-
enumerable: true,
|
|
63
|
-
value: (...arguments_: unknown[]): void => {
|
|
64
|
-
emitEvent(eventName, ...arguments_);
|
|
65
|
-
},
|
|
66
|
-
writable: true,
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
return handlers;
|
|
70
|
-
});
|
|
71
|
-
}
|
package/src/vue/expose.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 同时暴露组件实例能力并返回同一个对象,便于 `setup` 返回状态供 Vue Devtools 查看。
|
|
3
|
-
*
|
|
4
|
-
* @param expose - `setup` 上下文提供的 expose 函数。
|
|
5
|
-
* @param exposed - 需要暴露的状态和方法。
|
|
6
|
-
* @returns 原始 exposed 对象。
|
|
7
|
-
*/
|
|
8
|
-
export function useExpose<Exposed extends object>(expose: (exposed?: Exposed) => void, exposed: Exposed): Exposed {
|
|
9
|
-
expose(exposed);
|
|
10
|
-
return exposed;
|
|
11
|
-
}
|
package/src/vue/func.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/** 可同步或异步返回结果的函数。 */
|
|
2
|
-
export type AwaitableFunction<Arguments extends readonly unknown[], Result> = (...arguments_: Arguments) => Result | PromiseLike<Result>;
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* 统一执行同步或异步函数,异常保持原样向调用方传播。
|
|
6
|
-
*
|
|
7
|
-
* @param function_ - 可选的待执行函数。
|
|
8
|
-
* @param arguments_ - 原样传入函数的参数。
|
|
9
|
-
* @returns 函数结果;未传函数时返回 `undefined`。
|
|
10
|
-
*/
|
|
11
|
-
export async function callOptionalFunction<Arguments extends readonly unknown[], Result>(
|
|
12
|
-
function_: AwaitableFunction<Arguments, Result> | null | undefined,
|
|
13
|
-
...arguments_: Arguments
|
|
14
|
-
): Promise<Awaited<Result> | undefined> {
|
|
15
|
-
if (function_ === null || function_ === undefined) return undefined;
|
|
16
|
-
return await function_(...arguments_);
|
|
17
|
-
}
|
package/src/vue/index.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Vue 2.7/3 helpers re-exported by the package root entry.
|
|
3
|
-
*
|
|
4
|
-
* @packageDocumentation
|
|
5
|
-
*/
|
|
6
|
-
export * from "./emits.js";
|
|
7
|
-
export * from "./expose.js";
|
|
8
|
-
export * from "./func.js";
|
|
9
|
-
export * from "./install.js";
|
|
10
|
-
export * from "./props.js";
|
|
11
|
-
export * from "./render.js";
|
|
12
|
-
export * from "./slots.js";
|
|
13
|
-
export * from "./with.js";
|
package/src/vue/install.ts
DELETED
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
/** Vue 2.7 构造器与 Vue 3 App 共有的组件、指令注册能力。 */
|
|
2
|
-
export interface VueRegistrationTarget {
|
|
3
|
-
/**
|
|
4
|
-
* 读取或注册全局组件。
|
|
5
|
-
*
|
|
6
|
-
* @param name - 全局组件名。
|
|
7
|
-
* @param component - 注册时传入的组件;省略时读取现有组件。
|
|
8
|
-
* @returns Vue 平台返回的现有组件、注册结果或目标自身。
|
|
9
|
-
*/
|
|
10
|
-
component: (name: string, component?: unknown) => unknown;
|
|
11
|
-
/**
|
|
12
|
-
* 读取或注册全局指令。
|
|
13
|
-
*
|
|
14
|
-
* @param name - 不带 `v-` 的全局指令名。
|
|
15
|
-
* @param directive - 注册时传入的指令;省略时读取现有指令。
|
|
16
|
-
* @returns Vue 平台返回的现有指令、注册结果或目标自身。
|
|
17
|
-
*/
|
|
18
|
-
directive: (name: string, directive?: unknown) => unknown;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/** Vue 组件对象、函数组件或指令对象可接受的最小结构类型。 */
|
|
22
|
-
export type VueInstallValue = object | ((...arguments_: never[]) => unknown);
|
|
23
|
-
|
|
24
|
-
/** 为 Vue 组件或指令附加供 Vue 2.7 `Vue.use()` 与 Vue 3 `app.use()` 调用的安装能力。 */
|
|
25
|
-
export type Installable<Value> = Value & {
|
|
26
|
-
/**
|
|
27
|
-
* 把当前组件或指令安装到 Vue 2.7 构造器或 Vue 3 App。
|
|
28
|
-
* @param target - Vue 2.7 构造器或 Vue 3 App 实例。
|
|
29
|
-
*/
|
|
30
|
-
install: (target: unknown) => void;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
/** TSX 组件安装类型;与 {@link Installable} 保持同一运行时契约。 */
|
|
34
|
-
export type TSXWithInstall<Value> = Installable<Value>;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* 校验 Vue 插件安装目标。
|
|
38
|
-
*
|
|
39
|
-
* @param value - Vue 2.7 构造器或 Vue 3 App 实例。
|
|
40
|
-
* @returns 只包含组件和指令注册能力的结构化目标。
|
|
41
|
-
* @throws `TypeError` 当目标缺少 `component` 或 `directive` 方法。
|
|
42
|
-
*/
|
|
43
|
-
const assertTarget = (value: unknown): VueRegistrationTarget => {
|
|
44
|
-
if ((typeof value !== "object" && typeof value !== "function") || value === null) {
|
|
45
|
-
throw new TypeError("Vue plugin installation requires a Vue constructor or App.");
|
|
46
|
-
}
|
|
47
|
-
const target = value as Partial<VueRegistrationTarget>;
|
|
48
|
-
if (typeof target.component !== "function" || typeof target.directive !== "function") {
|
|
49
|
-
throw new TypeError("Vue plugin installation requires component() and directive() registration methods.");
|
|
50
|
-
}
|
|
51
|
-
return target as VueRegistrationTarget;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* 提取组件的全局注册名称。
|
|
56
|
-
*
|
|
57
|
-
* @param component - 待注册组件。
|
|
58
|
-
* @returns 去除外围空白后的显式名称。
|
|
59
|
-
* @throws `TypeError` 当组件没有非空字符串名称。
|
|
60
|
-
*/
|
|
61
|
-
const getComponentName = (component: VueInstallValue): string => {
|
|
62
|
-
const name = (component as { name?: unknown }).name;
|
|
63
|
-
if (typeof name !== "string" || name.length === 0 || /\s/u.test(name)) {
|
|
64
|
-
throw new TypeError("Installable Vue components must expose a non-empty name without whitespace.");
|
|
65
|
-
}
|
|
66
|
-
return name;
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
/** 预检完成、可以无失败注册的组件动作。 */
|
|
70
|
-
interface ComponentRegistration {
|
|
71
|
-
/** 已校验的组件引用。 */
|
|
72
|
-
component: VueInstallValue;
|
|
73
|
-
/** 已校验的组件名称。 */
|
|
74
|
-
name: string;
|
|
75
|
-
/** 目标中是否已经注册了完全相同的组件引用。 */
|
|
76
|
-
registered: boolean;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* 预检单个组件注册。
|
|
81
|
-
*
|
|
82
|
-
* @remarks 先读取同名组件并完成冲突判断,再返回延迟执行动作;调用方可以在所有组件预检通过后统一提交。
|
|
83
|
-
* @param target - 已校验的 Vue 注册目标。
|
|
84
|
-
* @param component - 待注册组件。
|
|
85
|
-
* @returns 已校验的组件引用、名称和目标中是否已经存在同一引用。
|
|
86
|
-
* @throws `Error` 当同名位置已由其他组件占用。
|
|
87
|
-
*/
|
|
88
|
-
const prepareComponentRegistration = (target: VueRegistrationTarget, component: VueInstallValue): ComponentRegistration => {
|
|
89
|
-
const name = getComponentName(component);
|
|
90
|
-
const existing = target.component(name);
|
|
91
|
-
if (existing !== undefined && existing !== component) {
|
|
92
|
-
throw new Error(`Vue component name "${name}" is already registered by another component.`);
|
|
93
|
-
}
|
|
94
|
-
return { component, name, registered: existing === component };
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* 为主组件附加 Vue 2.7 `Vue.use()` 与 Vue 3 `app.use()` 安装能力。
|
|
99
|
-
*
|
|
100
|
-
* @remarks 函数会直接为 `main` 定义附属组件属性和 `install`。所有组件名称、附属属性
|
|
101
|
-
* 冲突会在修改 `main` 前完成校验;安装到 App 时也会先预检全部全局名称,再统一注册。
|
|
102
|
-
* @param main - 具有非空 `name` 的组件。
|
|
103
|
-
* @param extras - 同时注册并以可枚举属性挂到主组件的附属组件映射。
|
|
104
|
-
* @returns 原始 `main` 引用,并附加类型化的 `install` 与 `extras` 属性。
|
|
105
|
-
* @throws `TypeError` 当组件缺少合法名称、已有 `install`、附属键或名称发生冲突。
|
|
106
|
-
* @throws `Error` 当安装目标中同名位置已经注册其他组件。
|
|
107
|
-
*/
|
|
108
|
-
export function withInstall<Main extends VueInstallValue, Extras extends Record<string, VueInstallValue> = Record<never, never>>(
|
|
109
|
-
main: Main,
|
|
110
|
-
extras?: Extras
|
|
111
|
-
): Installable<Main> & Extras {
|
|
112
|
-
const componentNames = new Set([getComponentName(main)]);
|
|
113
|
-
if ("install" in Object(main)) throw new TypeError("The Vue component already defines an install property.");
|
|
114
|
-
const extraEntries = Object.entries(extras ?? {});
|
|
115
|
-
if (extras !== undefined && Object.getOwnPropertySymbols(extras).some((key) => Object.prototype.propertyIsEnumerable.call(extras, key))) {
|
|
116
|
-
throw new TypeError("Vue component extras must use string property names.");
|
|
117
|
-
}
|
|
118
|
-
for (const [key, component] of extraEntries) {
|
|
119
|
-
const componentName = getComponentName(component);
|
|
120
|
-
if (componentNames.has(componentName)) throw new TypeError(`Vue component name "${componentName}" is registered more than once.`);
|
|
121
|
-
componentNames.add(componentName);
|
|
122
|
-
if (key === "install" || key in Object(main)) {
|
|
123
|
-
throw new TypeError(`Vue component extra "${key}" would overwrite a property on the main component.`);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
const installable = main as Installable<Main> & Extras;
|
|
127
|
-
for (const [key, component] of extraEntries) {
|
|
128
|
-
Object.defineProperty(installable, key, { configurable: true, enumerable: true, value: component, writable: true });
|
|
129
|
-
}
|
|
130
|
-
installable.install = (value: unknown): void => {
|
|
131
|
-
const target = assertTarget(value);
|
|
132
|
-
// 先完成全部冲突检查,再统一注册,避免安装到一半留下部分全局组件。
|
|
133
|
-
const registrations = [main, ...extraEntries.map(([, component]) => component)].map((component) =>
|
|
134
|
-
prepareComponentRegistration(target, component)
|
|
135
|
-
);
|
|
136
|
-
for (const registration of registrations) {
|
|
137
|
-
if (!registration.registered) target.component(registration.name, registration.component);
|
|
138
|
-
}
|
|
139
|
-
};
|
|
140
|
-
return installable;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* 为不需要单独注册的附属组件附加空安装函数。
|
|
145
|
-
*
|
|
146
|
-
* @remarks 适用于只能作为主组件附属属性使用、但仍需满足 Vue Plugin 类型的组件。
|
|
147
|
-
* 函数直接修改并返回传入组件,不会向 Vue 全局组件表注册内容。
|
|
148
|
-
* @param component - 尚未定义或继承 `install` 属性的组件。
|
|
149
|
-
* @returns 原组件引用及无副作用的 `install` 方法。
|
|
150
|
-
* @throws `TypeError` 当组件自身或原型链已经存在 `install`。
|
|
151
|
-
*/
|
|
152
|
-
export function withNoopInstall<Value extends VueInstallValue>(component: Value): TSXWithInstall<Value> {
|
|
153
|
-
if ("install" in Object(component)) throw new TypeError("The Vue component already defines an install property.");
|
|
154
|
-
const installable = component as TSXWithInstall<Value>;
|
|
155
|
-
installable.install = (): void => undefined;
|
|
156
|
-
return installable;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* 为 Vue 2.7/3 指令附加插件安装能力。
|
|
161
|
-
*
|
|
162
|
-
* @remarks 函数直接修改并返回指令。安装时重复注册同一引用保持幂等,不会覆盖同名的
|
|
163
|
-
* 其他指令。名称只传给 `directive()`,不得包含 `v-` 前缀。
|
|
164
|
-
* @param directive - 尚未定义或继承 `install` 属性的 Vue 指令对象。
|
|
165
|
-
* @param name - 非空、无空白且不以 `v-` 开头的全局指令名。
|
|
166
|
-
* @returns 原指令引用及 Vue Plugin `install` 方法。
|
|
167
|
-
* @throws `TypeError` 当名称非法、指令已有 `install`,或安装目标无效。
|
|
168
|
-
* @throws `Error` 当安装目标中同名位置已经注册其他指令。
|
|
169
|
-
*/
|
|
170
|
-
export function withInstallDirective<Value extends VueInstallValue>(directive: Value, name: string): Installable<Value> {
|
|
171
|
-
if (name.length === 0 || /\s/u.test(name) || name.startsWith("v-")) {
|
|
172
|
-
throw new TypeError("Installable Vue directives require a name without whitespace or a v- prefix.");
|
|
173
|
-
}
|
|
174
|
-
if ("install" in Object(directive)) throw new TypeError("The Vue directive already defines an install property.");
|
|
175
|
-
const installable = directive as Installable<Value>;
|
|
176
|
-
installable.install = (value: unknown): void => {
|
|
177
|
-
const target = assertTarget(value);
|
|
178
|
-
const existing = target.directive(name);
|
|
179
|
-
if (existing !== undefined && existing !== directive) {
|
|
180
|
-
throw new Error(`Vue directive name "${name}" is already registered by another directive.`);
|
|
181
|
-
}
|
|
182
|
-
if (existing !== directive) target.directive(name, directive);
|
|
183
|
-
};
|
|
184
|
-
return installable;
|
|
185
|
-
}
|
package/src/vue/props.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { computed } from "vue";
|
|
2
|
-
|
|
3
|
-
import type { ComputedRef, PropType } from "vue";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* 为 Vue 运行时 Props 构造器附加泛型类型。
|
|
7
|
-
*
|
|
8
|
-
* @remarks 该函数只帮助 TypeScript 建模,不验证运行时值与 `Value` 一致;调用方仍应
|
|
9
|
-
* 传入 Vue 支持的构造器或构造器数组。
|
|
10
|
-
* @param runtimeType - Vue 支持的运行时构造器或构造器数组。
|
|
11
|
-
* @returns 同一引用,仅在类型层收窄为 `PropType<Value>`。
|
|
12
|
-
*/
|
|
13
|
-
export function definePropType<Value>(runtimeType: unknown): PropType<Value> {
|
|
14
|
-
return runtimeType as PropType<Value>;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* 构建需要透传给子组件的响应式 Props。
|
|
19
|
-
*
|
|
20
|
-
* @param props - Vue `setup` 接收的只读响应式 Props 对象。
|
|
21
|
-
* @param rawProps - 子组件的运行时 Props 配置。
|
|
22
|
-
* @param ignoredProps - 不需要透传的 Props 名称。
|
|
23
|
-
* @returns 只包含 `rawProps` 声明键且随 Props 更新的 ComputedRef。
|
|
24
|
-
*/
|
|
25
|
-
export function useProps<Props extends object, RawProps extends object>(
|
|
26
|
-
props: Props,
|
|
27
|
-
rawProps: RawProps,
|
|
28
|
-
ignoredProps: readonly (keyof RawProps)[] = []
|
|
29
|
-
): ComputedRef<Pick<Props, Extract<keyof Props, keyof RawProps>>> {
|
|
30
|
-
const ignored = new Set<PropertyKey>(ignoredProps);
|
|
31
|
-
return computed<Pick<Props, Extract<keyof Props, keyof RawProps>>>(() => {
|
|
32
|
-
const result = {} as Pick<Props, Extract<keyof Props, keyof RawProps>>;
|
|
33
|
-
for (const key of Reflect.ownKeys(rawProps)) {
|
|
34
|
-
if (ignored.has(key) || !Object.hasOwn(props, key)) continue;
|
|
35
|
-
Object.defineProperty(result, key, { configurable: true, enumerable: true, value: Reflect.get(props, key), writable: true });
|
|
36
|
-
}
|
|
37
|
-
return result;
|
|
38
|
-
});
|
|
39
|
-
}
|