@giszhc/file-utils 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +710 -0
- package/dist/file-utils.js +349 -0
- package/package.json +36 -0
- package/types/convert.d.ts +37 -0
- package/types/download.d.ts +96 -0
- package/types/generate.d.ts +93 -0
- package/types/index.d.ts +44 -0
- package/types/read.d.ts +70 -0
- package/types/utils.d.ts +43 -0
- package/types/validators.d.ts +107 -0
package/types/read.d.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 文件读取相关方法
|
|
3
|
+
*/
|
|
4
|
+
import type { ReadFileType } from './types';
|
|
5
|
+
/**
|
|
6
|
+
* 读取文件内容
|
|
7
|
+
*
|
|
8
|
+
* @param file - File 或 Blob 对象
|
|
9
|
+
* @param type - 读取类型,可选值:'text' | 'arrayBuffer' | 'dataURL' | 'binaryString'
|
|
10
|
+
* 默认值:'text'
|
|
11
|
+
* @returns Promise<string | ArrayBuffer> - 返回读取后的内容
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* // 读取文本文件
|
|
15
|
+
* const content = await readFile(file, 'text');
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* // 读取为 ArrayBuffer(二进制数据)
|
|
19
|
+
* const buffer = await readFile(file, 'arrayBuffer');
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* // 读取为 Data URL(Base64 编码)
|
|
23
|
+
* const dataUrl = await readFile(file, 'dataURL');
|
|
24
|
+
*/
|
|
25
|
+
export declare function readFile(file: File | Blob, type?: ReadFileType): Promise<string | ArrayBuffer>;
|
|
26
|
+
/**
|
|
27
|
+
* 读取 TXT 文件
|
|
28
|
+
*
|
|
29
|
+
* @param file - File 或 Blob 对象
|
|
30
|
+
* @param encoding - 字符编码,默认 'utf-8'
|
|
31
|
+
* @returns Promise<string> - 返回文本内容
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* const content = await readTxtFile(file);
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* // 指定编码
|
|
38
|
+
* const content = await readTxtFile(file, 'gbk');
|
|
39
|
+
*/
|
|
40
|
+
export declare function readTxtFile(file: File | Blob, encoding?: string): Promise<string>;
|
|
41
|
+
/**
|
|
42
|
+
* 读取 CSV 文件
|
|
43
|
+
*
|
|
44
|
+
* @param file - File 或 Blob 对象
|
|
45
|
+
* @param options - CSV 解析选项
|
|
46
|
+
* - separator: 字段分隔符,默认 ','
|
|
47
|
+
* - hasHeader: 是否包含表头,默认 true
|
|
48
|
+
* @returns Promise<any[]> - 解析后的数据数组
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* // 读取 CSV 文件
|
|
52
|
+
* const data = await readCsvFile(file);
|
|
53
|
+
* console.log(data); // [{name: '张三', age: '25'}, ...]
|
|
54
|
+
*/
|
|
55
|
+
export declare function readCsvFile(file: File | Blob, options?: {
|
|
56
|
+
separator?: string;
|
|
57
|
+
hasHeader?: boolean;
|
|
58
|
+
}): Promise<any[]>;
|
|
59
|
+
/**
|
|
60
|
+
* 读取 JSON 文件
|
|
61
|
+
*
|
|
62
|
+
* @param file - File 或 Blob 对象
|
|
63
|
+
* @returns Promise<T> - 解析后的 JSON 数据
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* // 读取 JSON 文件
|
|
67
|
+
* const data = await readJsonFile<{ name: string; age: number }>(file);
|
|
68
|
+
* console.log(data.name);
|
|
69
|
+
*/
|
|
70
|
+
export declare function readJsonFile<T = any>(file: File | Blob): Promise<T>;
|
package/types/utils.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 工具函数相关方法
|
|
3
|
+
*/
|
|
4
|
+
import type { IFileInfo } from './types';
|
|
5
|
+
/**
|
|
6
|
+
* 获取文件扩展名(后缀)
|
|
7
|
+
*
|
|
8
|
+
* @param file - File 对象或文件名
|
|
9
|
+
* @returns string - 文件扩展名(包含点号,如 '.jpg')
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const file = new File(['content'], 'test.txt', { type: 'text/plain' });
|
|
13
|
+
* const ext = getFileExtension(file); // ".txt"
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const ext = getFileExtension('image.png'); // ".png"
|
|
17
|
+
*/
|
|
18
|
+
export declare function getFileExtension(file: File | string): string;
|
|
19
|
+
/**
|
|
20
|
+
* 获取文件名(不含扩展名)
|
|
21
|
+
*
|
|
22
|
+
* @param file - File 对象或文件名
|
|
23
|
+
* @returns string - 不含扩展名的文件名
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* const file = new File(['content'], 'test.txt', { type: 'text/plain' });
|
|
27
|
+
* const name = getFileNameWithoutExtension(file); // "test"
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* const name = getFileNameWithoutExtension('archive.tar.gz'); // "archive.tar"
|
|
31
|
+
*/
|
|
32
|
+
export declare function getFileNameWithoutExtension(file: File | string): string;
|
|
33
|
+
/**
|
|
34
|
+
* 获取文件信息
|
|
35
|
+
*
|
|
36
|
+
* @param file - File 对象
|
|
37
|
+
* @returns IFileInfo - 包含文件基本信息的对象
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* const fileInfo = getFileInfo(file);
|
|
41
|
+
* console.log(`文件名:${fileInfo.name}, 大小:${(fileInfo.size / 1024).toFixed(2)} KB`);
|
|
42
|
+
*/
|
|
43
|
+
export declare function getFileInfo(file: File): IFileInfo;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 文件验证相关方法
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* 检查文件类型是否匹配
|
|
6
|
+
*
|
|
7
|
+
* @param file - File 对象
|
|
8
|
+
* @param acceptTypes - 接受的类型数组,支持:
|
|
9
|
+
* - MIME 类型:'image/jpeg', 'application/pdf'
|
|
10
|
+
* - 通配符:'image/*', 'audio/*', 'video/*'
|
|
11
|
+
* - 扩展名:'.jpg', '.png', '.pdf'
|
|
12
|
+
* @returns boolean - 如果文件类型匹配返回 true
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* // 检查是否为 JPEG 图片
|
|
16
|
+
* checkFileType(file, ['image/jpeg']);
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* // 检查是否为图片或 PDF
|
|
20
|
+
* checkFileType(file, ['image/*', 'application/pdf']);
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* // 使用扩展名检查
|
|
24
|
+
* checkFileType(file, ['.jpg', '.png', '.gif']);
|
|
25
|
+
*/
|
|
26
|
+
export declare function checkFileType(file: File, acceptTypes: string[]): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* 检查文件大小是否符合要求
|
|
29
|
+
*
|
|
30
|
+
* @param file - File 对象
|
|
31
|
+
* @param maxSize - 最大文件大小(字节)
|
|
32
|
+
* @returns boolean - 如果文件大小符合要求返回 true
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* // 限制不超过 2MB
|
|
36
|
+
* checkFileSize(file, 2 * 1024 * 1024);
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* // 限制不超过 500KB
|
|
40
|
+
* checkFileSize(file, 500 * 1024);
|
|
41
|
+
*/
|
|
42
|
+
export declare function checkFileSize(file: File, maxSize: number): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* 快速判断文件是否为图片格式
|
|
45
|
+
*
|
|
46
|
+
* @param file - File 对象
|
|
47
|
+
* @returns boolean - 如果是图片返回 true
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* if (isImage(file)) {
|
|
51
|
+
* console.log('这是一个图片文件');
|
|
52
|
+
* }
|
|
53
|
+
*/
|
|
54
|
+
export declare function isImage(file: File): boolean;
|
|
55
|
+
/**
|
|
56
|
+
* 获取图片的原始尺寸(宽度和高度)
|
|
57
|
+
*
|
|
58
|
+
* @param file - File 对象(必须是图片文件)
|
|
59
|
+
* @returns Promise<{ width: number, height: number }> - 图片的宽度和高度
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* const dimensions = await getImageDimensions(file);
|
|
63
|
+
* console.log(`图片尺寸:${dimensions.width} x ${dimensions.height}`);
|
|
64
|
+
*
|
|
65
|
+
* @throws 如果文件不是图片格式,会抛出错误
|
|
66
|
+
*/
|
|
67
|
+
export declare function getImageDimensions(file: File): Promise<{
|
|
68
|
+
width: number;
|
|
69
|
+
height: number;
|
|
70
|
+
}>;
|
|
71
|
+
/**
|
|
72
|
+
* 综合文件验证
|
|
73
|
+
*
|
|
74
|
+
* @param file - File 对象
|
|
75
|
+
* @param options - 验证选项配置
|
|
76
|
+
* @returns Promise<IValidationResult> - 验证结果
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* // 验证文件:类型、大小、图片尺寸
|
|
80
|
+
* const result = await validateFile(file, {
|
|
81
|
+
* acceptTypes: ['image/jpeg', 'image/png'],
|
|
82
|
+
* maxSize: 2 * 1024 * 1024, // 2MB
|
|
83
|
+
* mustBeImage: true,
|
|
84
|
+
* minWidth: 800,
|
|
85
|
+
* maxWidth: 4096,
|
|
86
|
+
* minHeight: 600,
|
|
87
|
+
* maxHeight: 4096
|
|
88
|
+
* });
|
|
89
|
+
*
|
|
90
|
+
* if (!result.valid) {
|
|
91
|
+
* console.log('验证失败:', result.errors);
|
|
92
|
+
* }
|
|
93
|
+
*/
|
|
94
|
+
export declare function validateFile(file: File, options?: import('./types').IFileValidationOptions): Promise<import('./types').IValidationResult>;
|
|
95
|
+
/**
|
|
96
|
+
* 格式化文件大小
|
|
97
|
+
*
|
|
98
|
+
* @param bytes - 文件大小(字节)
|
|
99
|
+
* @param decimal - 小数位数,默认 2
|
|
100
|
+
* @returns string - 格式化后的大小字符串(如:1.5 MB)
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* formatFileSize(1024); // "1.00 KB"
|
|
104
|
+
* formatFileSize(1048576); // "1.00 MB"
|
|
105
|
+
* formatFileSize(1073741824); // "1.00 GB"
|
|
106
|
+
*/
|
|
107
|
+
export declare function formatFileSize(bytes: number, decimal?: number): string;
|