@giszhc/file-utils 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@giszhc/file-utils",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -23,6 +23,9 @@
23
23
  "download"
24
24
  ],
25
25
  "homepage": "https://github.com/giszhc/file-utils#readme",
26
+ "dependencies": {
27
+ "jszip": "^3.10.1"
28
+ },
26
29
  "devDependencies": {
27
30
  "typescript": "~5.9.3",
28
31
  "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>;
@@ -35,3 +35,16 @@ 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;
@@ -0,0 +1,140 @@
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
+ *
92
+ * @param arr - 数字数组
93
+ * @returns number - 数组元素的总和
94
+ *
95
+ * @example
96
+ * arraySum([1, 2, 3, 4, 5]) // 15
97
+ * arraySum([10, 20, 30]) // 60
98
+ */
99
+ export declare function arraySum(arr: number[]): number;
100
+ /**
101
+ * 格式化数字金额,使用逗号分隔(千分位)
102
+ *
103
+ * @param amount - 需要格式化的数字金额
104
+ * @param decimals - 小数位数,默认为 2
105
+ * @returns string - 格式化后的字符串(如:1,234,567.89)
106
+ *
107
+ * @example
108
+ * formatAmount(1234567.89) // "1,234,567.89"
109
+ * formatAmount(1000000, 0) // "1,000,000"
110
+ * formatAmount(999) // "999.00"
111
+ */
112
+ export declare function formatAmount(amount: number, decimals?: number): string;
113
+ /**
114
+ * 将字符串中间部分替换为星号(*)
115
+ * 常用于隐藏电话号码、身份证号等敏感信息
116
+ *
117
+ * @param str - 需要处理的字符串
118
+ * @param start - 开始保留的字符数(从左侧计数)
119
+ * @param end - 结束保留的字符数(从右侧计数)
120
+ * @param maskChar - 用于替换的字符,默认为 '*'
121
+ * @returns string - 处理后的字符串
122
+ *
123
+ * @example
124
+ * maskString('13800138000', 3, 4) // "138****8000"
125
+ * maskString('1234567890', 2, 2) // "12******90"
126
+ * maskString('abc', 1, 1) // "a*b"
127
+ */
128
+ export declare function maskString(str: string, start?: number, end?: number, maskChar?: string): string;
129
+ /**
130
+ * 格式化手机号码,隐藏中间 4 位
131
+ *
132
+ * @param phone - 手机号码
133
+ * @param maskChar - 用于替换的字符,默认为 '*'
134
+ * @returns string - 格式化后的手机号码(如:138****8000)
135
+ *
136
+ * @example
137
+ * formatPhone('13800138000') // "138****8000"
138
+ * formatPhone('13900139000', '#') // "139####9000"
139
+ */
140
+ export declare function formatPhone(phone: string, maskChar?: string): string;
package/types/index.d.ts CHANGED
@@ -7,18 +7,23 @@
7
7
  export type { ReadFileType, CompressOptions, DownloadOptions, CopyOptions, IFileInfo, IValidationResult, IFileValidationOptions } from './types';
8
8
  export { readFile, readTxtFile, readCsvFile, readJsonFile } from './read';
9
9
  export { downloadFile, downloadBlob, downloadBase64, downloadMultiple } from './download';
10
- export { fileToBase64, base64ToBlob } from './convert';
10
+ export { fileToBase64, base64ToBlob, blobToFile } from './convert';
11
+ export { fileListToZip, downloadFileListAsZip } from './compress';
11
12
  export { copyToClipboard, pasteFromClipboard } from './copy';
12
13
  export { generateTxtFile, generateCsvFile, generateJsonFile } from './generate';
13
14
  export { checkFileType, checkFileSize, isImage, getImageDimensions, validateFile, formatFileSize } from './validators';
14
15
  export { getFileExtension, getFileNameWithoutExtension, getFileInfo } from './utils';
16
+ export { deepClone, numberFixed, parseUrlParams, objectToFormData, omitKeys, generateUUID, arraySum, formatAmount, maskString, formatPhone } from './helpers';
17
+ export { VerifyUtils, type IVerifyResult, type IVerifyMessages } from './verify';
15
18
  import { readFile, readTxtFile, readCsvFile, readJsonFile } from './read';
16
19
  import { downloadFile, downloadBlob, downloadBase64, downloadMultiple } from './download';
17
- import { fileToBase64, base64ToBlob } from './convert';
20
+ import { fileToBase64, base64ToBlob, blobToFile } from './convert';
18
21
  import { copyToClipboard, pasteFromClipboard } from './copy';
19
22
  import { generateTxtFile, generateCsvFile, generateJsonFile } from './generate';
20
23
  import { checkFileType, checkFileSize, isImage, getImageDimensions, validateFile, formatFileSize } from './validators';
21
24
  import { getFileExtension, getFileNameWithoutExtension, getFileInfo } from './utils';
25
+ import { deepClone, parseUrlParams, objectToFormData, omitKeys, generateUUID, arraySum, formatAmount, maskString, formatPhone } from './helpers';
26
+ import { VerifyUtils } from './verify';
22
27
  declare const _default: {
23
28
  readFile: typeof readFile;
24
29
  readTxtFile: typeof readTxtFile;
@@ -30,6 +35,9 @@ declare const _default: {
30
35
  downloadMultiple: typeof downloadMultiple;
31
36
  fileToBase64: typeof fileToBase64;
32
37
  base64ToBlob: typeof base64ToBlob;
38
+ blobToFile: typeof blobToFile;
39
+ fileListToZip: (fileList: File[], filename: string) => Promise<File>;
40
+ downloadFileListAsZip: (fileList: File[], filename: string) => Promise<void>;
33
41
  copyToClipboard: typeof copyToClipboard;
34
42
  pasteFromClipboard: typeof pasteFromClipboard;
35
43
  generateTxtFile: typeof generateTxtFile;
@@ -44,5 +52,16 @@ declare const _default: {
44
52
  getFileExtension: typeof getFileExtension;
45
53
  getFileNameWithoutExtension: typeof getFileNameWithoutExtension;
46
54
  getFileInfo: typeof getFileInfo;
55
+ deepClone: typeof deepClone;
56
+ numberFixed: (number: number, fractionDigits?: number) => number;
57
+ parseUrlParams: typeof parseUrlParams;
58
+ objectToFormData: typeof objectToFormData;
59
+ omitKeys: typeof omitKeys;
60
+ generateUUID: typeof generateUUID;
61
+ arraySum: typeof arraySum;
62
+ formatAmount: typeof formatAmount;
63
+ maskString: typeof maskString;
64
+ formatPhone: typeof formatPhone;
65
+ VerifyUtils: typeof VerifyUtils;
47
66
  };
48
67
  export default _default;
@@ -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;