@giszhc/file-utils 0.0.5 → 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 +253 -6
- package/dist/file-utils.js +2075 -1959
- package/package.json +2 -1
- package/types/convert.d.ts +60 -0
- package/types/helpers.d.ts +131 -0
- package/types/index.d.ts +10 -60
- package/types/utils.d.ts +9 -9
package/README.md
CHANGED
|
@@ -37,6 +37,9 @@
|
|
|
37
37
|
- `fileToBase64` - 将 File/Blob 转换为 Base64
|
|
38
38
|
- `base64ToBlob` - 将 Base64 转换为 Blob
|
|
39
39
|
- `blobToFile` - 将 Blob 转换为指定文件名的 File
|
|
40
|
+
- `base64ToFile` - 将 Base64 转换为 File
|
|
41
|
+
- `imageUrlToBase64` - 将图片 URL 转换为 Base64
|
|
42
|
+
- `fileChangedImageDPI` - 修改图片的 DPI 分辨率
|
|
40
43
|
- `compressImage` - 压缩图片
|
|
41
44
|
|
|
42
45
|
### 文件压缩
|
|
@@ -55,8 +58,8 @@
|
|
|
55
58
|
- `getImageDimensions` - 获取图片尺寸
|
|
56
59
|
- `validateFile` - 综合文件验证
|
|
57
60
|
- `formatFileSize` - 格式化文件大小显示
|
|
58
|
-
- `
|
|
59
|
-
- `
|
|
61
|
+
- `getFileNameSuffix` - 获取文件扩展名(不含点号)
|
|
62
|
+
- `getFileNamePrefix` - 获取文件名(不含扩展名)
|
|
60
63
|
- `getFileInfo` - 获取文件完整信息
|
|
61
64
|
|
|
62
65
|
### 剪贴板操作
|
|
@@ -74,6 +77,10 @@
|
|
|
74
77
|
- `formatAmount` - 格式化金额(千分位)
|
|
75
78
|
- `maskString` - 字符串中间部分替换为星号
|
|
76
79
|
- `formatPhone` - 格式化手机号码(隐藏中间 4 位)
|
|
80
|
+
- `arrayStringFormatNumber` - 字符串数组转数值数组
|
|
81
|
+
- `arrayCustomSort` - 自定义排序(按指定 id 顺序)
|
|
82
|
+
- `jsonConvertTreeList` - 扁平数据转树形结构
|
|
83
|
+
- `jsonConvertGeneralList` - 树形结构转扁平数据
|
|
77
84
|
|
|
78
85
|
### 正则验证 (VerifyUtils)
|
|
79
86
|
- `VerifyUtils.isEmail` - 邮箱验证
|
|
@@ -316,15 +323,15 @@ console.log(file.size); // 文件大小(字节)
|
|
|
316
323
|
### 11. 获取文件扩展名和文件名
|
|
317
324
|
|
|
318
325
|
```ts
|
|
319
|
-
import {
|
|
326
|
+
import { getFileNameSuffix, getFileNamePrefix } from '@giszhc/file-utils';
|
|
320
327
|
|
|
321
328
|
const file = new File(['content'], 'document.pdf', { type: 'application/pdf' });
|
|
322
329
|
|
|
323
|
-
//
|
|
324
|
-
const ext =
|
|
330
|
+
// 获取扩展名(不含点号)
|
|
331
|
+
const ext = getFileNameSuffix(file); // "pdf"
|
|
325
332
|
|
|
326
333
|
// 获取文件名(不含扩展名)
|
|
327
|
-
const name =
|
|
334
|
+
const name = getFileNamePrefix(file); // "document"
|
|
328
335
|
```
|
|
329
336
|
|
|
330
337
|
### 12. 文件类型验证
|
|
@@ -751,6 +758,92 @@ const base64 = 'data:image/png;base64,iVBORw0KGgoAAAANS...';
|
|
|
751
758
|
const blob = base64ToBlob(base64);
|
|
752
759
|
```
|
|
753
760
|
|
|
761
|
+
### base64ToFile(base64: string, filename?: string, mimeType?: string): File
|
|
762
|
+
|
|
763
|
+
将 Base64 编码转换为 File 对象。
|
|
764
|
+
|
|
765
|
+
```ts
|
|
766
|
+
import { base64ToFile } from '@giszhc/file-utils';
|
|
767
|
+
|
|
768
|
+
// 转换带前缀的 Base64
|
|
769
|
+
const base64 = 'data:image/png;base64,iVBORw0KGgoAAAANS...';
|
|
770
|
+
const file = base64ToFile(base64, 'image.png');
|
|
771
|
+
console.log(file.name); // "image.png"
|
|
772
|
+
|
|
773
|
+
// 转换不带前缀的 Base64
|
|
774
|
+
const pureBase64 = 'iVBORw0KGgoAAAANS...';
|
|
775
|
+
const file2 = base64ToFile(pureBase64, 'image.png', 'image/png');
|
|
776
|
+
```
|
|
777
|
+
|
|
778
|
+
### imageUrlToBase64(imageUrl: string, mimeType?: string, quality?: number): Promise<string>
|
|
779
|
+
|
|
780
|
+
将图片 URL 转换为 Base64 编码。使用 Canvas 将图片绘制后转换为 Base64。
|
|
781
|
+
|
|
782
|
+
```ts
|
|
783
|
+
import { imageUrlToBase64 } from '@giszhc/file-utils';
|
|
784
|
+
|
|
785
|
+
// 转换远程图片
|
|
786
|
+
const base64 = await imageUrlToBase64('https://example.com/image.jpg');
|
|
787
|
+
console.log(base64); // data:image/png;base64,...
|
|
788
|
+
|
|
789
|
+
// 转换为 JPEG 格式,80% 质量
|
|
790
|
+
const jpegBase64 = await imageUrlToBase64(
|
|
791
|
+
'https://example.com/image.png',
|
|
792
|
+
'image/jpeg',
|
|
793
|
+
0.8
|
|
794
|
+
);
|
|
795
|
+
```
|
|
796
|
+
|
|
797
|
+
### fileChangedImageDPI(source: string | Blob, dpi?: number): Promise<string | Blob>
|
|
798
|
+
|
|
799
|
+
修改图片的分辨率 DPI(每英寸点数)。支持 Base64 字符串和 Blob 对象。返回 Promise。
|
|
800
|
+
|
|
801
|
+
```ts
|
|
802
|
+
import { fileChangedImageDPI } from '@giszhc/file-utils';
|
|
803
|
+
|
|
804
|
+
// 修改 Base64 图片的 DPI 为 300(适合打印)
|
|
805
|
+
const base64 = 'data:image/png;base64,iVBORw0KGgoAAAANS...';
|
|
806
|
+
const highDpiBase64 = await fileChangedImageDPI(base64, 300);
|
|
807
|
+
console.log('高分辨率图片:', highDpiBase64);
|
|
808
|
+
|
|
809
|
+
// 修改 Blob 图片的 DPI
|
|
810
|
+
const blob = new Blob([imageData], { type: 'image/png' });
|
|
811
|
+
const newBlob = await fileChangedImageDPI(blob, 300);
|
|
812
|
+
// newBlob 是修改后的 Blob 对象
|
|
813
|
+
|
|
814
|
+
// 使用默认 DPI (96,屏幕显示标准)
|
|
815
|
+
const defaultDpiResult = await fileChangedImageDPI(base64);
|
|
816
|
+
|
|
817
|
+
// 设置为 72 DPI(网页优化)
|
|
818
|
+
const webOptimized = await fileChangedImageDPI(base64, 72);
|
|
819
|
+
|
|
820
|
+
// 结合其他方法使用
|
|
821
|
+
import { fileToBase64, base64ToBlob, fileChangedImageDPI } from '@giszhc/file-utils';
|
|
822
|
+
|
|
823
|
+
const handleImageUpload = async (file: File) => {
|
|
824
|
+
// 方式 1: 使用 Base64
|
|
825
|
+
const base64 = await fileToBase64(file);
|
|
826
|
+
const highResBase64 = await fileChangedImageDPI(base64, 300);
|
|
827
|
+
|
|
828
|
+
// 方式 2: 直接使用 Blob
|
|
829
|
+
const highResBlob = await fileChangedImageDPI(file, 300);
|
|
830
|
+
const highResFile = base64ToFile(highResBase64, 'high-res.png');
|
|
831
|
+
};
|
|
832
|
+
```
|
|
833
|
+
|
|
834
|
+
**应用场景:**
|
|
835
|
+
- 🖼️ **打印前提升 DPI**:将网络图片的 DPI 从 72 提升到 300,获得更好的打印质量
|
|
836
|
+
- 🌐 **网页优化**:降低 DPI 到 72,减少文件体积,加快网页加载速度
|
|
837
|
+
- 📱 **设备适配**:根据不同设备的显示需求调整 DPI
|
|
838
|
+
- 📄 **出版印刷**:满足专业印刷的 300 DPI 或更高要求
|
|
839
|
+
|
|
840
|
+
**注意事项:**
|
|
841
|
+
- 该方法仅修改图片元数据中的 DPI 值,不会改变图片的像素尺寸
|
|
842
|
+
- DPI 值越高,打印时的物理尺寸越小,但细节越清晰
|
|
843
|
+
- 标准屏幕显示通常使用 96 DPI,印刷品通常需要 300 DPI
|
|
844
|
+
- 输入类型为 Base64 时返回 Base64,输入类型为 Blob 时返回 Blob
|
|
845
|
+
- **该方法是异步函数,需要使用 await 调用**
|
|
846
|
+
|
|
754
847
|
### compressImage(file: File, quality?: number, maxWidth?: number, maxHeight?: number): Promise<Blob>
|
|
755
848
|
|
|
756
849
|
压缩图片文件。支持调整质量和最大尺寸。
|
|
@@ -941,6 +1034,160 @@ if (!result.valid) {
|
|
|
941
1034
|
}
|
|
942
1035
|
```
|
|
943
1036
|
|
|
1037
|
+
### arrayStringFormatNumber(arrayList: string[]): number[]
|
|
1038
|
+
|
|
1039
|
+
将字符串数组转换为数值数组。
|
|
1040
|
+
|
|
1041
|
+
```ts
|
|
1042
|
+
import { arrayStringFormatNumber } from '@giszhc/file-utils';
|
|
1043
|
+
|
|
1044
|
+
// 将字符串数字数组转换为数值数组
|
|
1045
|
+
const strArr = ["0", "1", "2", "3", "4"];
|
|
1046
|
+
const numArr = arrayStringFormatNumber(strArr);
|
|
1047
|
+
console.log(numArr); // [0, 1, 2, 3, 4]
|
|
1048
|
+
|
|
1049
|
+
// 处理带小数点的字符串
|
|
1050
|
+
const decimalStrs = ["1.5", "2.7", "3.14"];
|
|
1051
|
+
const decimalNums = arrayStringFormatNumber(decimalStrs);
|
|
1052
|
+
console.log(decimalNums); // [1.5, 2.7, 3.14]
|
|
1053
|
+
```
|
|
1054
|
+
|
|
1055
|
+
### arrayCustomSort(ids: string[], dataList: any[], cbA: Function, cbB: Function): void
|
|
1056
|
+
|
|
1057
|
+
根据给定的 id 顺序对数据列表进行排序。
|
|
1058
|
+
|
|
1059
|
+
```ts
|
|
1060
|
+
import { arrayCustomSort } from '@giszhc/file-utils';
|
|
1061
|
+
|
|
1062
|
+
// 基本使用
|
|
1063
|
+
const ids = ['id1', 'id2', 'id3'];
|
|
1064
|
+
const dataList = [
|
|
1065
|
+
{ id: 'id3', name: 'Layer 3' },
|
|
1066
|
+
{ id: 'id1', name: 'Layer 1' },
|
|
1067
|
+
{ id: 'id2', name: 'Layer 2' }
|
|
1068
|
+
];
|
|
1069
|
+
|
|
1070
|
+
arrayCustomSort(ids, dataList, item => item.id, item => item.id);
|
|
1071
|
+
// 排序后:[
|
|
1072
|
+
// { id: 'id1', name: 'Layer 1' },
|
|
1073
|
+
// { id: 'id2', name: 'Layer 2' },
|
|
1074
|
+
// { id: 'id3', name: 'Layer 3' }
|
|
1075
|
+
// ]
|
|
1076
|
+
|
|
1077
|
+
// 使用不同的回调函数处理不同类型的数据
|
|
1078
|
+
const orderIds = ['admin', 'user', 'guest'];
|
|
1079
|
+
const roles = [
|
|
1080
|
+
{ roleId: 'guest', roleName: '访客' },
|
|
1081
|
+
{ roleId: 'admin', roleName: '管理员' },
|
|
1082
|
+
{ roleId: 'user', roleName: '普通用户' }
|
|
1083
|
+
];
|
|
1084
|
+
|
|
1085
|
+
arrayCustomSort(orderIds, roles, role => role.roleId, role => role.roleId);
|
|
1086
|
+
// 按指定顺序排序:管理员、普通用户、访客
|
|
1087
|
+
```
|
|
1088
|
+
|
|
1089
|
+
### jsonConvertTreeList(dataList: any[]): any[]
|
|
1090
|
+
|
|
1091
|
+
将扁平数据数组转换为树形结构。
|
|
1092
|
+
|
|
1093
|
+
```ts
|
|
1094
|
+
import { jsonConvertTreeList } from '@giszhc/file-utils';
|
|
1095
|
+
|
|
1096
|
+
// 扁平数据
|
|
1097
|
+
const flatData = [
|
|
1098
|
+
{ id: 1, pid: null, name: '根节点' },
|
|
1099
|
+
{ id: 2, pid: 1, name: '子节点 1' },
|
|
1100
|
+
{ id: 3, pid: 1, name: '子节点 2' },
|
|
1101
|
+
{ id: 4, pid: 2, name: '孙节点 1' }
|
|
1102
|
+
];
|
|
1103
|
+
|
|
1104
|
+
const tree = jsonConvertTreeList(flatData);
|
|
1105
|
+
/*
|
|
1106
|
+
返回树形结构:
|
|
1107
|
+
[
|
|
1108
|
+
{
|
|
1109
|
+
id: 1,
|
|
1110
|
+
pid: null,
|
|
1111
|
+
name: '根节点',
|
|
1112
|
+
children: [
|
|
1113
|
+
{
|
|
1114
|
+
id: 2,
|
|
1115
|
+
pid: 1,
|
|
1116
|
+
name: '子节点 1',
|
|
1117
|
+
children: [
|
|
1118
|
+
{ id: 4, pid: 2, name: '孙节点 1' }
|
|
1119
|
+
]
|
|
1120
|
+
},
|
|
1121
|
+
{
|
|
1122
|
+
id: 3,
|
|
1123
|
+
pid: 1,
|
|
1124
|
+
name: '子节点 2',
|
|
1125
|
+
children: []
|
|
1126
|
+
}
|
|
1127
|
+
]
|
|
1128
|
+
}
|
|
1129
|
+
]
|
|
1130
|
+
*/
|
|
1131
|
+
|
|
1132
|
+
// 应用场景:菜单生成、组织架构展示
|
|
1133
|
+
const menuData = [
|
|
1134
|
+
{ id: 1, pid: null, title: '系统管理' },
|
|
1135
|
+
{ id: 2, pid: 1, title: '用户管理' },
|
|
1136
|
+
{ id: 3, pid: 1, title: '角色管理' },
|
|
1137
|
+
{ id: 4, pid: 2, title: '用户列表' },
|
|
1138
|
+
{ id: 5, pid: 2, title: '用户新增' }
|
|
1139
|
+
];
|
|
1140
|
+
|
|
1141
|
+
const menuTree = jsonConvertTreeList(menuData);
|
|
1142
|
+
```
|
|
1143
|
+
|
|
1144
|
+
### jsonConvertGeneralList(treeList: any[], delChildrenField?: boolean): any[]
|
|
1145
|
+
|
|
1146
|
+
将树形结构转换回扁平数据数组。
|
|
1147
|
+
|
|
1148
|
+
```ts
|
|
1149
|
+
import { jsonConvertGeneralList } from '@giszhc/file-utils';
|
|
1150
|
+
|
|
1151
|
+
// 树形结构
|
|
1152
|
+
const tree = [
|
|
1153
|
+
{
|
|
1154
|
+
id: 1,
|
|
1155
|
+
pid: null,
|
|
1156
|
+
name: '根节点',
|
|
1157
|
+
children: [
|
|
1158
|
+
{
|
|
1159
|
+
id: 2,
|
|
1160
|
+
pid: 1,
|
|
1161
|
+
name: '子节点 1',
|
|
1162
|
+
children: [
|
|
1163
|
+
{ id: 4, pid: 2, name: '孙节点 1' }
|
|
1164
|
+
]
|
|
1165
|
+
},
|
|
1166
|
+
{
|
|
1167
|
+
id: 3,
|
|
1168
|
+
pid: 1,
|
|
1169
|
+
name: '子节点 2',
|
|
1170
|
+
children: []
|
|
1171
|
+
}
|
|
1172
|
+
]
|
|
1173
|
+
}
|
|
1174
|
+
];
|
|
1175
|
+
|
|
1176
|
+
// 保留 children 字段
|
|
1177
|
+
const flatWithChildren = jsonConvertGeneralList(tree);
|
|
1178
|
+
console.log(flatWithChildren.length); // 4
|
|
1179
|
+
|
|
1180
|
+
// 删除 children 字段
|
|
1181
|
+
const flatWithoutChildren = jsonConvertGeneralList(tree, true);
|
|
1182
|
+
console.log(flatWithoutChildren[0]);
|
|
1183
|
+
// { id: 1, pid: null, name: '根节点' } (不含 children)
|
|
1184
|
+
|
|
1185
|
+
// 应用场景:将树形菜单保存为扁平数据存储到数据库
|
|
1186
|
+
const menuTree = [...]; // 树形菜单
|
|
1187
|
+
const flatMenuData = jsonConvertGeneralList(menuTree, true);
|
|
1188
|
+
// 可以发送到后端保存到数据库
|
|
1189
|
+
```
|
|
1190
|
+
|
|
944
1191
|
------
|
|
945
1192
|
|
|
946
1193
|
## 类型定义
|