@giszhc/file-utils 0.0.5 → 0.0.7

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.5",
3
+ "version": "0.0.7",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -24,6 +24,7 @@
24
24
  ],
25
25
  "homepage": "https://github.com/giszhc/file-utils#readme",
26
26
  "dependencies": {
27
+ "changedpi": "^1.0.4",
27
28
  "jszip": "^3.10.1"
28
29
  },
29
30
  "devDependencies": {
@@ -33,7 +34,8 @@
33
34
  "scripts": {
34
35
  "dev": "vite",
35
36
  "build:types": "tsc -p tsconfig.build.json",
36
- "build": "pnpm run build:types && vite build",
37
+ "build": "vite build && pnpm run build:types",
38
+ "build:example": "vite build --config vite.example.config.ts",
37
39
  "preview": "vite preview"
38
40
  }
39
41
  }
@@ -48,3 +48,63 @@ export declare function base64ToBlob(base64: string, mimeType?: string): Blob;
48
48
  * console.log(file.name); // "hello.txt"
49
49
  */
50
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>;
@@ -86,6 +86,22 @@ export declare function omitKeys<T extends Record<string, any>>(obj: T, keys: (k
86
86
  * generateUUID(true) // "550e8400e29b41d4a716446655440000" (无横杠)
87
87
  */
88
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>;
89
105
  /**
90
106
  * 数组求和
91
107
  *
@@ -138,3 +154,118 @@ export declare function maskString(str: string, start?: number, end?: number, ma
138
154
  * formatPhone('13900139000', '#') // "139####9000"
139
155
  */
140
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,63 +5,13 @@
5
5
  * 支持 Tree Shaking,按需引入
6
6
  */
7
7
  export type { ReadFileType, CompressOptions, DownloadOptions, CopyOptions, IFileInfo, IValidationResult, IFileValidationOptions } from './types';
8
- export { readFile, readTxtFile, readCsvFile, readJsonFile } from './read';
9
- export { downloadFile, downloadBlob, downloadBase64, downloadMultiple } from './download';
10
- export { fileToBase64, base64ToBlob, blobToFile } from './convert';
11
- export { fileListToZip, downloadFileListAsZip } from './compress';
12
- export { copyToClipboard, pasteFromClipboard } from './copy';
13
- export { generateTxtFile, generateCsvFile, generateJsonFile } from './generate';
14
- export { checkFileType, checkFileSize, isImage, getImageDimensions, validateFile, formatFileSize } from './validators';
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';
18
- import { readFile, readTxtFile, readCsvFile, readJsonFile } from './read';
19
- import { downloadFile, downloadBlob, downloadBase64, downloadMultiple } from './download';
20
- import { fileToBase64, base64ToBlob, blobToFile } from './convert';
21
- import { copyToClipboard, pasteFromClipboard } from './copy';
22
- import { generateTxtFile, generateCsvFile, generateJsonFile } from './generate';
23
- import { checkFileType, checkFileSize, isImage, getImageDimensions, validateFile, formatFileSize } from './validators';
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';
27
- declare const _default: {
28
- readFile: typeof readFile;
29
- readTxtFile: typeof readTxtFile;
30
- readCsvFile: typeof readCsvFile;
31
- readJsonFile: typeof readJsonFile;
32
- downloadFile: typeof downloadFile;
33
- downloadBlob: typeof downloadBlob;
34
- downloadBase64: typeof downloadBase64;
35
- downloadMultiple: typeof downloadMultiple;
36
- fileToBase64: typeof fileToBase64;
37
- base64ToBlob: typeof base64ToBlob;
38
- blobToFile: typeof blobToFile;
39
- fileListToZip: (fileList: File[], filename: string) => Promise<File>;
40
- downloadFileListAsZip: (fileList: File[], filename: string) => Promise<void>;
41
- copyToClipboard: typeof copyToClipboard;
42
- pasteFromClipboard: typeof pasteFromClipboard;
43
- generateTxtFile: typeof generateTxtFile;
44
- generateCsvFile: typeof generateCsvFile;
45
- generateJsonFile: typeof generateJsonFile;
46
- checkFileType: typeof checkFileType;
47
- checkFileSize: typeof checkFileSize;
48
- isImage: typeof isImage;
49
- getImageDimensions: typeof getImageDimensions;
50
- validateFile: typeof validateFile;
51
- formatFileSize: typeof formatFileSize;
52
- getFileExtension: typeof getFileExtension;
53
- getFileNameWithoutExtension: typeof getFileNameWithoutExtension;
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;
66
- };
67
- 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 - 文件扩展名(包含点号,如 '.jpg')
9
+ * @returns string - 文件扩展名(不含点号,如 'jpg')
10
10
  *
11
11
  * @example
12
12
  * const file = new File(['content'], 'test.txt', { type: 'text/plain' });
13
- * const ext = getFileExtension(file); // ".txt"
13
+ * const ext = getFileNameSuffix(file); // "txt"
14
14
  *
15
15
  * @example
16
- * const ext = getFileExtension('image.png'); // ".png"
16
+ * const ext = getFileNameSuffix('image.png'); // "png"
17
17
  */
18
- export declare function getFileExtension(file: File | string): string;
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 = getFileNameWithoutExtension(file); // "test"
27
+ * const name = getFileNamePrefix(file); // "test"
28
28
  *
29
29
  * @example
30
- * const name = getFileNameWithoutExtension('archive.tar.gz'); // "archive.tar"
30
+ * const name = getFileNamePrefix('archive.tar.gz'); // "archive.tar"
31
31
  */
32
- export declare function getFileNameWithoutExtension(file: File | string): string;
32
+ export declare function getFileNamePrefix(file: File | string): string;
33
33
  /**
34
34
  * 获取文件信息
35
35
  *