@giszhc/file-utils 0.0.4 → 0.0.6
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.md +483 -4
- package/dist/file-utils.js +2946 -280
- package/package.json +5 -1
- package/types/compress.d.ts +28 -0
- package/types/convert.d.ts +73 -0
- package/types/helpers.d.ts +271 -0
- package/types/index.d.ts +10 -41
- package/types/utils.d.ts +9 -9
- package/types/verify.d.ts +93 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@giszhc/file-utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -23,6 +23,10 @@
|
|
|
23
23
|
"download"
|
|
24
24
|
],
|
|
25
25
|
"homepage": "https://github.com/giszhc/file-utils#readme",
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"changedpi": "^1.0.4",
|
|
28
|
+
"jszip": "^3.10.1"
|
|
29
|
+
},
|
|
26
30
|
"devDependencies": {
|
|
27
31
|
"typescript": "~5.9.3",
|
|
28
32
|
"vite": "^7.2.4"
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 文件压缩相关方法
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* 文件列表转 ZIP 压缩包
|
|
6
|
+
*
|
|
7
|
+
* @param fileList - 文件列表数组
|
|
8
|
+
* @param filename - ZIP 文件名(不含扩展名)
|
|
9
|
+
* @returns Promise<File> - 返回 ZIP 压缩包 File 对象
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const files = [file1, file2, file3];
|
|
13
|
+
* const zipFile = await fileListToZip(files, 'my-files');
|
|
14
|
+
* console.log(zipFile.name); // "my-files.zip"
|
|
15
|
+
*/
|
|
16
|
+
export declare const fileListToZip: (fileList: File[], filename: string) => Promise<File>;
|
|
17
|
+
/**
|
|
18
|
+
* 下载文件列表的 ZIP 压缩包
|
|
19
|
+
*
|
|
20
|
+
* @param fileList - 文件列表数组
|
|
21
|
+
* @param filename - ZIP 文件名(不含扩展名)
|
|
22
|
+
* @returns Promise<void>
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* const files = [file1, file2, file3];
|
|
26
|
+
* await downloadFileListAsZip(files, 'my-files');
|
|
27
|
+
*/
|
|
28
|
+
export declare const downloadFileListAsZip: (fileList: File[], filename: string) => Promise<void>;
|
package/types/convert.d.ts
CHANGED
|
@@ -35,3 +35,76 @@ export declare function fileToBase64(file: File | Blob): Promise<string>;
|
|
|
35
35
|
* const blob = base64ToBlob(base64, 'image/png');
|
|
36
36
|
*/
|
|
37
37
|
export declare function base64ToBlob(base64: string, mimeType?: string): Blob;
|
|
38
|
+
/**
|
|
39
|
+
* 将 Blob 转换为指定文件名的 File 对象
|
|
40
|
+
*
|
|
41
|
+
* @param blob - Blob 对象
|
|
42
|
+
* @param filename - 文件名
|
|
43
|
+
* @returns File - 转换后的 File 对象
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* const blob = new Blob(['Hello'], { type: 'text/plain' });
|
|
47
|
+
* const file = blobToFileName(blob, 'hello.txt');
|
|
48
|
+
* console.log(file.name); // "hello.txt"
|
|
49
|
+
*/
|
|
50
|
+
export declare function blobToFile(blob: Blob, filename: string): File;
|
|
51
|
+
/**
|
|
52
|
+
* 将 Base64 编码转换为 File 对象
|
|
53
|
+
*
|
|
54
|
+
* @param base64 - Base64 编码的字符串(可以包含或不包含 data URL 前缀)
|
|
55
|
+
* @param filename - 文件名(可选),默认为 'file'
|
|
56
|
+
* @param mimeType - MIME 类型(可选),如果未指定且 base64 不包含前缀,默认为 'application/octet-stream'
|
|
57
|
+
* @returns File - 转换后的 File 对象
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* // 转换带前缀的 Base64
|
|
61
|
+
* const base64 = 'data:image/png;base64,iVBORw0KGgoAAAANS...';
|
|
62
|
+
* const file = base64ToFile(base64, 'image.png');
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* // 转换不带前缀的 Base64
|
|
66
|
+
* const base64 = 'iVBORw0KGgoAAAANS...';
|
|
67
|
+
* const file = base64ToFile(base64, 'image.png', 'image/png');
|
|
68
|
+
*/
|
|
69
|
+
export declare function base64ToFile(base64: string, filename?: string, mimeType?: string): File;
|
|
70
|
+
/**
|
|
71
|
+
* 将图片 URL 转换为 Base64 编码
|
|
72
|
+
* 使用 Canvas 将图片绘制后转换为 Base64
|
|
73
|
+
*
|
|
74
|
+
* @param imageUrl - 图片 URL 地址
|
|
75
|
+
* @param mimeType - 输出的 MIME 类型(可选),默认为 'image/png'
|
|
76
|
+
* @param quality - 图片质量(可选),范围 0.0 - 1.0,默认为 1.0
|
|
77
|
+
* @returns Promise<string> - Base64 编码的字符串(包含 data URL 前缀)
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* // 转换远程图片
|
|
81
|
+
* const base64 = await imageUrlToBase64('https://example.com/image.jpg');
|
|
82
|
+
* console.log(base64); // data:image/png;base64,iVBORw0KGgoAAAANS...
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* // 转换为 JPEG 格式,80% 质量
|
|
86
|
+
* const base64 = await imageUrlToBase64('https://example.com/image.png', 'image/jpeg', 0.8);
|
|
87
|
+
*/
|
|
88
|
+
export declare function imageUrlToBase64(imageUrl: string, mimeType?: string, quality?: number): Promise<string>;
|
|
89
|
+
/**
|
|
90
|
+
* fileChangedImageDPI 函数用于修改图片的分辨率 DPI
|
|
91
|
+
*
|
|
92
|
+
* @param source - 原图片,支持 Base64 字符串或 Blob 对象
|
|
93
|
+
* @param dpi - 需要设置的分辨率值,默认为 96
|
|
94
|
+
* @returns Promise<string | Blob> - 返回修改后的 DPI 图片(Base64 字符串或 Blob)
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* // 修改 Base64 图片的 DPI
|
|
98
|
+
* const base64 = 'data:image/png;base64,iVBORw0KGgoAAAANS...';
|
|
99
|
+
* const newBase64 = fileChangedImageDPI(base64, 300);
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* // 修改 Blob 图片的 DPI
|
|
103
|
+
* const blob = new Blob([imageData], { type: 'image/png' });
|
|
104
|
+
* const newBlob = await fileChangedImageDPI(blob, 300);
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* // 使用默认 DPI (96)
|
|
108
|
+
* const result = await fileChangedImageDPI(base64);
|
|
109
|
+
*/
|
|
110
|
+
export declare function fileChangedImageDPI(source: string | Blob, dpi?: number): Promise<string | Blob>;
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 通用辅助工具方法
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* 深拷贝方法
|
|
6
|
+
* 支持对象、数组、Date、RegExp 等类型的深拷贝
|
|
7
|
+
*
|
|
8
|
+
* @param target - 需要拷贝的目标对象
|
|
9
|
+
* @returns T - 拷贝后的新对象
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const obj = { a: 1, b: { c: 2 } };
|
|
13
|
+
* const copy = deepClone(obj);
|
|
14
|
+
* copy.b.c = 3;
|
|
15
|
+
* console.log(obj.b.c); // 2 (原对象不受影响)
|
|
16
|
+
*/
|
|
17
|
+
export declare function deepClone<T>(target: T): T;
|
|
18
|
+
/**
|
|
19
|
+
* 数字保留指定小数位数
|
|
20
|
+
*
|
|
21
|
+
* @param number - 需要处理的数字
|
|
22
|
+
* @param fractionDigits - 保留的小数位数,默认为 2
|
|
23
|
+
* @returns number - 处理后的数字
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* numberFixed(3.14159, 2) // 3.14
|
|
27
|
+
* numberFixed(10, 3) // 10
|
|
28
|
+
*/
|
|
29
|
+
export declare const numberFixed: (number: number, fractionDigits?: number) => number;
|
|
30
|
+
/**
|
|
31
|
+
* 解析 URL 参数为对象
|
|
32
|
+
*
|
|
33
|
+
* @param url - URL 字符串,默认为 location.href
|
|
34
|
+
* @returns Record<string, string> - 包含 URL 参数的对象
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* parseUrlParams('https://example.com?name=John&age=30')
|
|
38
|
+
* // { name: 'John', age: '30' }
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* parseUrlParams() // 解析当前页面 URL 参数
|
|
42
|
+
*/
|
|
43
|
+
export declare function parseUrlParams(url?: string): Record<string, string>;
|
|
44
|
+
/**
|
|
45
|
+
* 将对象转换为 FormData
|
|
46
|
+
*
|
|
47
|
+
* @param obj - 需要转换的对象
|
|
48
|
+
* @param formData - 可选的 FormData 实例,用于追加数据
|
|
49
|
+
* @returns FormData - 转换后的 FormData 对象
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* const obj = { name: 'John', age: 30 };
|
|
53
|
+
* const formData = objectToFormData(obj);
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* // 嵌套对象
|
|
57
|
+
* const obj = { user: { name: 'John', email: 'john@example.com' } };
|
|
58
|
+
* const formData = objectToFormData(obj);
|
|
59
|
+
*/
|
|
60
|
+
export declare function objectToFormData(obj: Record<string, any>, formData?: FormData): FormData;
|
|
61
|
+
/**
|
|
62
|
+
* 从给定对象中移除特定的键,返回新对象
|
|
63
|
+
*
|
|
64
|
+
* @param obj - 原始对象
|
|
65
|
+
* @param keys - 需要移除的键名数组
|
|
66
|
+
* @param reverse - 是否反向操作(即只保留指定的键),默认为 false
|
|
67
|
+
* @returns T - 移除指定键后的新对象
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* omitKeys({ a: 1, b: 2, c: 3 }, ['b', c]) // { a: 1 }
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* omitKeys({ a: 1, b: 2, c: 3 }, ['a'], true) // { a: 1 } (反向操作,只保留 a)
|
|
74
|
+
*/
|
|
75
|
+
export declare function omitKeys<T extends Record<string, any>>(obj: T, keys: (keyof T | string)[], reverse?: boolean): Partial<T>;
|
|
76
|
+
/**
|
|
77
|
+
* 生成 UUID
|
|
78
|
+
*
|
|
79
|
+
* @param removeHyphens - 是否移除横杠,默认为 false(保留横杠)
|
|
80
|
+
* @returns string - 生成的 UUID 字符串
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* generateUUID() // "550e8400-e29b-41d4-a716-446655440000"
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* generateUUID(true) // "550e8400e29b41d4a716446655440000" (无横杠)
|
|
87
|
+
*/
|
|
88
|
+
export declare function generateUUID(removeHyphens?: boolean): string;
|
|
89
|
+
/**
|
|
90
|
+
* 过滤对象中的空值
|
|
91
|
+
* 移除值为空字符串、null 或 undefined 的属性
|
|
92
|
+
*
|
|
93
|
+
* @param data - 需要过滤的对象
|
|
94
|
+
* @returns Record<string, any> - 过滤后的新对象
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* const data = { a: 1, b: '', c: null, d: undefined, e: 'test' };
|
|
98
|
+
* const filtered = filterEmptyValue(data); // { a: 1, e: 'test' }
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* const params = { name: 'John', age: null, email: '' };
|
|
102
|
+
* const validParams = filterEmptyValue(params); // { name: 'John' }
|
|
103
|
+
*/
|
|
104
|
+
export declare function filterEmptyValue(data: Record<string, any>): Record<string, any>;
|
|
105
|
+
/**
|
|
106
|
+
* 数组求和
|
|
107
|
+
*
|
|
108
|
+
* @param arr - 数字数组
|
|
109
|
+
* @returns number - 数组元素的总和
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* arraySum([1, 2, 3, 4, 5]) // 15
|
|
113
|
+
* arraySum([10, 20, 30]) // 60
|
|
114
|
+
*/
|
|
115
|
+
export declare function arraySum(arr: number[]): number;
|
|
116
|
+
/**
|
|
117
|
+
* 格式化数字金额,使用逗号分隔(千分位)
|
|
118
|
+
*
|
|
119
|
+
* @param amount - 需要格式化的数字金额
|
|
120
|
+
* @param decimals - 小数位数,默认为 2
|
|
121
|
+
* @returns string - 格式化后的字符串(如:1,234,567.89)
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* formatAmount(1234567.89) // "1,234,567.89"
|
|
125
|
+
* formatAmount(1000000, 0) // "1,000,000"
|
|
126
|
+
* formatAmount(999) // "999.00"
|
|
127
|
+
*/
|
|
128
|
+
export declare function formatAmount(amount: number, decimals?: number): string;
|
|
129
|
+
/**
|
|
130
|
+
* 将字符串中间部分替换为星号(*)
|
|
131
|
+
* 常用于隐藏电话号码、身份证号等敏感信息
|
|
132
|
+
*
|
|
133
|
+
* @param str - 需要处理的字符串
|
|
134
|
+
* @param start - 开始保留的字符数(从左侧计数)
|
|
135
|
+
* @param end - 结束保留的字符数(从右侧计数)
|
|
136
|
+
* @param maskChar - 用于替换的字符,默认为 '*'
|
|
137
|
+
* @returns string - 处理后的字符串
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* maskString('13800138000', 3, 4) // "138****8000"
|
|
141
|
+
* maskString('1234567890', 2, 2) // "12******90"
|
|
142
|
+
* maskString('abc', 1, 1) // "a*b"
|
|
143
|
+
*/
|
|
144
|
+
export declare function maskString(str: string, start?: number, end?: number, maskChar?: string): string;
|
|
145
|
+
/**
|
|
146
|
+
* 格式化手机号码,隐藏中间 4 位
|
|
147
|
+
*
|
|
148
|
+
* @param phone - 手机号码
|
|
149
|
+
* @param maskChar - 用于替换的字符,默认为 '*'
|
|
150
|
+
* @returns string - 格式化后的手机号码(如:138****8000)
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* formatPhone('13800138000') // "138****8000"
|
|
154
|
+
* formatPhone('13900139000', '#') // "139####9000"
|
|
155
|
+
*/
|
|
156
|
+
export declare function formatPhone(phone: string, maskChar?: string): string;
|
|
157
|
+
/**
|
|
158
|
+
* arrayStringFormatNumber 函数用于将字符串数组转换为数值数组
|
|
159
|
+
*
|
|
160
|
+
* @param arrayList - 字符串数组
|
|
161
|
+
* @returns number[] - 返回格式化后的数值数组
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* // 将字符串数字数组转换为数值数组
|
|
165
|
+
* const result = arrayStringFormatNumber(["0", "1", "2", "3", "4"]);
|
|
166
|
+
* console.log(result); // [0, 1, 2, 3, 4]
|
|
167
|
+
*
|
|
168
|
+
* @description
|
|
169
|
+
* 此方法将传入的字符串数组格式化为数值数组,并返回新的数组
|
|
170
|
+
*/
|
|
171
|
+
export declare function arrayStringFormatNumber(arrayList: string[]): number[];
|
|
172
|
+
/**
|
|
173
|
+
* arrayCustomSort 函数用于根据给定的 id 顺序对数据列表进行排序。
|
|
174
|
+
*
|
|
175
|
+
* @param ids - 包含 id 的数组,定义排序的顺序。
|
|
176
|
+
* @param dataList - 需要排序的数据列表。
|
|
177
|
+
* @param cbA - 一个回调函数,用于从数据列表的每个元素中提取 id,用于排序。
|
|
178
|
+
* @param cbB - 一个回调函数,用于从数据列表的每个元素中提取 id,用于排序。
|
|
179
|
+
* @returns void - 直接修改原数组,不返回值
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```typescript
|
|
183
|
+
* const ids = ['id1', 'id2', 'id3'];
|
|
184
|
+
* const dataList = [
|
|
185
|
+
* { id: 'id3', name: 'Layer 3' },
|
|
186
|
+
* { id: 'id1', name: 'Layer 1' },
|
|
187
|
+
* { id: 'id2', name: 'Layer 2' }
|
|
188
|
+
* ];
|
|
189
|
+
* arrayCustomSort(ids, dataList, item => item.id, item => item.id);
|
|
190
|
+
* // dataList 将被排序为 [
|
|
191
|
+
* // { id: 'id1', name: 'Layer 1' },
|
|
192
|
+
* // { id: 'id2', name: 'Layer 2' },
|
|
193
|
+
* // { id: 'id3', name: 'Layer 3' }
|
|
194
|
+
* // ]
|
|
195
|
+
* ```
|
|
196
|
+
*
|
|
197
|
+
* @description
|
|
198
|
+
* cbA 和 cbB 允许对数据列表中的不同类型项进行单独的处理。
|
|
199
|
+
*/
|
|
200
|
+
export declare function arrayCustomSort(ids: string[], dataList: any[], cbA: (item: any) => any, cbB: (item: any) => any): void;
|
|
201
|
+
/**
|
|
202
|
+
* jsonConvertTreeList 函数实现了把扁平的数据数组,按照父子节点的关系转换为树形结构。
|
|
203
|
+
*
|
|
204
|
+
* @param dataList - 输入的扁平数据数组,其中每个元素是一个对象,至少包含 id 和 pid 两个属性,表示自身的 ID 和其父节点的 ID。
|
|
205
|
+
* @returns any[] - 返回构建好的树形结构数组,其中每个节点包含 id,pid 和 children (如果有的话) 三个属性,children 属性是一个数组,里面包含其所有子节点。
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* ```typescript
|
|
209
|
+
* const flatData = [
|
|
210
|
+
* { id: 1, pid: null, name: '根节点' },
|
|
211
|
+
* { id: 2, pid: 1, name: '子节点 1' },
|
|
212
|
+
* { id: 3, pid: 1, name: '子节点 2' },
|
|
213
|
+
* { id: 4, pid: 2, name: '孙节点 1' }
|
|
214
|
+
* ];
|
|
215
|
+
* const tree = jsonConvertTreeList(flatData);
|
|
216
|
+
* // 返回树形结构:[
|
|
217
|
+
* // {
|
|
218
|
+
* // id: 1,
|
|
219
|
+
* // pid: null,
|
|
220
|
+
* // name: '根节点',
|
|
221
|
+
* // children: [
|
|
222
|
+
* // {
|
|
223
|
+
* // id: 2,
|
|
224
|
+
* // pid: 1,
|
|
225
|
+
* // name: '子节点 1',
|
|
226
|
+
* // children: [{ id: 4, pid: 2, name: '孙节点 1' }]
|
|
227
|
+
* // },
|
|
228
|
+
* // { id: 3, pid: 1, name: '子节点 2', children: [] }
|
|
229
|
+
* // ]
|
|
230
|
+
* // }
|
|
231
|
+
* // ]
|
|
232
|
+
* ```
|
|
233
|
+
*
|
|
234
|
+
* @description
|
|
235
|
+
* 函数首先定义了一个递归的函数 buildChildren,用于构建指定节点的所有子节点。然后通过 filter 方法找出所有的根节点,对每个根节点调用 buildChildren 函数构建其子树。
|
|
236
|
+
* 最后返回包含所有子树的数组。
|
|
237
|
+
*/
|
|
238
|
+
export declare function jsonConvertTreeList(dataList: any[]): any[];
|
|
239
|
+
/**
|
|
240
|
+
* jsonConvertGeneralList 函数是将嵌套的树形结构转换回扁平的数据数组。也可选择是否删除 children 属性。
|
|
241
|
+
*
|
|
242
|
+
* @param treeList - 输入的树形结构数组。
|
|
243
|
+
* @param delChildrenField - 可选,是否要删除每个节点的 children 字段,默认为 false,即默认不删除。
|
|
244
|
+
* @returns any[] - 返回扁平的数据数组。
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
* ```typescript
|
|
248
|
+
* const tree = [
|
|
249
|
+
* {
|
|
250
|
+
* id: 1,
|
|
251
|
+
* pid: null,
|
|
252
|
+
* name: '根节点',
|
|
253
|
+
* children: [
|
|
254
|
+
* { id: 2, pid: 1, name: '子节点 1', children: [] },
|
|
255
|
+
* { id: 3, pid: 1, name: '子节点 2', children: [] }
|
|
256
|
+
* ]
|
|
257
|
+
* }
|
|
258
|
+
* ];
|
|
259
|
+
* const flat = jsonConvertGeneralList(tree);
|
|
260
|
+
* // 返回扁平数组:[
|
|
261
|
+
* // { id: 1, pid: null, name: '根节点', children: [...] },
|
|
262
|
+
* // { id: 2, pid: 1, name: '子节点 1', children: [] },
|
|
263
|
+
* // { id: 3, pid: 1, name: '子节点 2', children: [] }
|
|
264
|
+
* // ]
|
|
265
|
+
* ```
|
|
266
|
+
*
|
|
267
|
+
* @description
|
|
268
|
+
* 函数首先定义了一个递归的函数 loop,用于处理每个节点和其子节点。然后对输入的树根节点调用 loop 函数。
|
|
269
|
+
* 如果设置了 delChildrenField 为 true,函数还会删除每个节点的 children 字段。
|
|
270
|
+
*/
|
|
271
|
+
export declare function jsonConvertGeneralList(treeList: any[], delChildrenField?: boolean): any[];
|
package/types/index.d.ts
CHANGED
|
@@ -5,44 +5,13 @@
|
|
|
5
5
|
* 支持 Tree Shaking,按需引入
|
|
6
6
|
*/
|
|
7
7
|
export type { ReadFileType, CompressOptions, DownloadOptions, CopyOptions, IFileInfo, IValidationResult, IFileValidationOptions } from './types';
|
|
8
|
-
export
|
|
9
|
-
export
|
|
10
|
-
export
|
|
11
|
-
export
|
|
12
|
-
export
|
|
13
|
-
export
|
|
14
|
-
export
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
import { copyToClipboard, pasteFromClipboard } from './copy';
|
|
19
|
-
import { generateTxtFile, generateCsvFile, generateJsonFile } from './generate';
|
|
20
|
-
import { checkFileType, checkFileSize, isImage, getImageDimensions, validateFile, formatFileSize } from './validators';
|
|
21
|
-
import { getFileExtension, getFileNameWithoutExtension, getFileInfo } from './utils';
|
|
22
|
-
declare const _default: {
|
|
23
|
-
readFile: typeof readFile;
|
|
24
|
-
readTxtFile: typeof readTxtFile;
|
|
25
|
-
readCsvFile: typeof readCsvFile;
|
|
26
|
-
readJsonFile: typeof readJsonFile;
|
|
27
|
-
downloadFile: typeof downloadFile;
|
|
28
|
-
downloadBlob: typeof downloadBlob;
|
|
29
|
-
downloadBase64: typeof downloadBase64;
|
|
30
|
-
downloadMultiple: typeof downloadMultiple;
|
|
31
|
-
fileToBase64: typeof fileToBase64;
|
|
32
|
-
base64ToBlob: typeof base64ToBlob;
|
|
33
|
-
copyToClipboard: typeof copyToClipboard;
|
|
34
|
-
pasteFromClipboard: typeof pasteFromClipboard;
|
|
35
|
-
generateTxtFile: typeof generateTxtFile;
|
|
36
|
-
generateCsvFile: typeof generateCsvFile;
|
|
37
|
-
generateJsonFile: typeof generateJsonFile;
|
|
38
|
-
checkFileType: typeof checkFileType;
|
|
39
|
-
checkFileSize: typeof checkFileSize;
|
|
40
|
-
isImage: typeof isImage;
|
|
41
|
-
getImageDimensions: typeof getImageDimensions;
|
|
42
|
-
validateFile: typeof validateFile;
|
|
43
|
-
formatFileSize: typeof formatFileSize;
|
|
44
|
-
getFileExtension: typeof getFileExtension;
|
|
45
|
-
getFileNameWithoutExtension: typeof getFileNameWithoutExtension;
|
|
46
|
-
getFileInfo: typeof getFileInfo;
|
|
47
|
-
};
|
|
48
|
-
export default _default;
|
|
8
|
+
export * from './read';
|
|
9
|
+
export * from './download';
|
|
10
|
+
export * from './convert';
|
|
11
|
+
export * from './compress';
|
|
12
|
+
export * from './copy';
|
|
13
|
+
export * from './generate';
|
|
14
|
+
export * from './validators';
|
|
15
|
+
export * from './utils';
|
|
16
|
+
export * from './helpers';
|
|
17
|
+
export * from './verify';
|
package/types/utils.d.ts
CHANGED
|
@@ -3,33 +3,33 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import type { IFileInfo } from './types';
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* 获取文件扩展名(后缀,不含点号)
|
|
7
7
|
*
|
|
8
8
|
* @param file - File 对象或文件名
|
|
9
|
-
* @returns string -
|
|
9
|
+
* @returns string - 文件扩展名(不含点号,如 'jpg')
|
|
10
10
|
*
|
|
11
11
|
* @example
|
|
12
12
|
* const file = new File(['content'], 'test.txt', { type: 'text/plain' });
|
|
13
|
-
* const ext =
|
|
13
|
+
* const ext = getFileNameSuffix(file); // "txt"
|
|
14
14
|
*
|
|
15
15
|
* @example
|
|
16
|
-
* const ext =
|
|
16
|
+
* const ext = getFileNameSuffix('image.png'); // "png"
|
|
17
17
|
*/
|
|
18
|
-
export declare function
|
|
18
|
+
export declare function getFileNameSuffix(file: File | string): string;
|
|
19
19
|
/**
|
|
20
|
-
*
|
|
20
|
+
* 获取文件名(前缀,不含扩展名)
|
|
21
21
|
*
|
|
22
22
|
* @param file - File 对象或文件名
|
|
23
23
|
* @returns string - 不含扩展名的文件名
|
|
24
24
|
*
|
|
25
25
|
* @example
|
|
26
26
|
* const file = new File(['content'], 'test.txt', { type: 'text/plain' });
|
|
27
|
-
* const name =
|
|
27
|
+
* const name = getFileNamePrefix(file); // "test"
|
|
28
28
|
*
|
|
29
29
|
* @example
|
|
30
|
-
* const name =
|
|
30
|
+
* const name = getFileNamePrefix('archive.tar.gz'); // "archive.tar"
|
|
31
31
|
*/
|
|
32
|
-
export declare function
|
|
32
|
+
export declare function getFileNamePrefix(file: File | string): string;
|
|
33
33
|
/**
|
|
34
34
|
* 获取文件信息
|
|
35
35
|
*
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 正则验证工具类 (增强版)
|
|
3
|
+
* 提供常用的表单验证功能,包括邮箱、手机、URL、坐标等
|
|
4
|
+
*
|
|
5
|
+
* @author 烟火里的尘埃 / AI 优化版
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* 验证结果接口
|
|
9
|
+
*/
|
|
10
|
+
export interface IVerifyResult {
|
|
11
|
+
/** 是否通过验证 */
|
|
12
|
+
valid: boolean;
|
|
13
|
+
/** 错误消息 */
|
|
14
|
+
message?: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* 提示语映射
|
|
18
|
+
*/
|
|
19
|
+
export interface IVerifyMessages {
|
|
20
|
+
email: string;
|
|
21
|
+
number: string;
|
|
22
|
+
phone: string;
|
|
23
|
+
url: string;
|
|
24
|
+
ip: string;
|
|
25
|
+
longitude: string;
|
|
26
|
+
latitude: string;
|
|
27
|
+
noSpace: string;
|
|
28
|
+
password: string;
|
|
29
|
+
empty: string;
|
|
30
|
+
chinese: string;
|
|
31
|
+
inputCoordinates: string;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 正则验证工具类 (增强版)
|
|
35
|
+
* 提供常用的表单验证功能,包括邮箱、手机、URL、坐标等
|
|
36
|
+
*/
|
|
37
|
+
export declare class VerifyUtils {
|
|
38
|
+
static messages: IVerifyMessages;
|
|
39
|
+
static isEmail: (val: string) => boolean;
|
|
40
|
+
static isNumber: (val: string) => boolean;
|
|
41
|
+
static isPhone: (val: string) => boolean;
|
|
42
|
+
static isUrl: (val: string) => boolean;
|
|
43
|
+
static isIP: (val: string) => boolean;
|
|
44
|
+
static isNoSpace: (val: string) => boolean;
|
|
45
|
+
static isChinese: (val: string) => boolean;
|
|
46
|
+
static isPassword: (val: string) => boolean;
|
|
47
|
+
/**
|
|
48
|
+
* 验证经度
|
|
49
|
+
* @param lng - 经度值
|
|
50
|
+
* @returns boolean - 是否在 -180 到 180 之间
|
|
51
|
+
*/
|
|
52
|
+
static isLongitude(lng: number | string): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* 验证纬度
|
|
55
|
+
* @param lat - 纬度值
|
|
56
|
+
* @returns boolean - 是否在 -90 到 90 之间
|
|
57
|
+
*/
|
|
58
|
+
static isLatitude(lat: number | string): boolean;
|
|
59
|
+
/**
|
|
60
|
+
* 检查是否为空
|
|
61
|
+
* @param value - 需要检查的值
|
|
62
|
+
* @returns boolean - 是否为空
|
|
63
|
+
*/
|
|
64
|
+
static isEmpty(value: any): boolean;
|
|
65
|
+
/**
|
|
66
|
+
* 验证输入坐标串 (递归平铺校验)
|
|
67
|
+
* 支持格式:"lng,lat" 或 "lng,lat;lng,lat" 或 "lng,lat;lng,lat|lng,lat"
|
|
68
|
+
* @param latLngS - 坐标字符串
|
|
69
|
+
* @returns boolean - 坐标格式是否正确
|
|
70
|
+
*/
|
|
71
|
+
static isInputCoordinates(latLngS: string): boolean;
|
|
72
|
+
/**
|
|
73
|
+
* 生成坐标数组 (支持多级分隔符)
|
|
74
|
+
* @param latLngS - 坐标字符串
|
|
75
|
+
* @returns any[] | null - 解析后的坐标数组
|
|
76
|
+
*/
|
|
77
|
+
static generateCoordinates(latLngS: string): any[] | null;
|
|
78
|
+
/**
|
|
79
|
+
* 快速生成 Form 表单校验规则
|
|
80
|
+
* @param type - 对应 messages 的 key
|
|
81
|
+
* @param trigger - 触发方式 'blur' | 'change'
|
|
82
|
+
* @returns any - Element UI 表单验证规则对象
|
|
83
|
+
*/
|
|
84
|
+
static getRule(type: string, trigger?: string): any;
|
|
85
|
+
/**
|
|
86
|
+
* 带错误消息的验证
|
|
87
|
+
* @param type - 验证类型
|
|
88
|
+
* @param value - 验证值
|
|
89
|
+
* @returns IVerifyResult - 验证结果
|
|
90
|
+
*/
|
|
91
|
+
static validate(type: keyof typeof this.messages, value: any): IVerifyResult;
|
|
92
|
+
}
|
|
93
|
+
export default VerifyUtils;
|