@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/README.md +232 -0
- package/dist/file-utils.js +2815 -265
- package/package.json +4 -1
- package/types/compress.d.ts +28 -0
- package/types/convert.d.ts +13 -0
- package/types/helpers.d.ts +140 -0
- package/types/index.d.ts +21 -2
- package/types/verify.d.ts +93 -0
package/README.md
CHANGED
|
@@ -7,15 +7,92 @@
|
|
|
7
7
|
- **文件读取**:支持读取文本、二进制数据、Data URL 等
|
|
8
8
|
- **文件下载**:支持从 URL、Blob、Base64 等多种方式下载文件
|
|
9
9
|
- **文件转换**:支持文件格式转换(如 Base64 转 Blob、File 转 Base64 等)
|
|
10
|
+
- **文件压缩**:支持将多个文件打包为 ZIP 压缩包
|
|
10
11
|
- **文件压缩**:支持图片压缩处理
|
|
11
12
|
- **文件生成**:支持生成 TXT、CSV、JSON 格式文件,可选择返回 File 对象而不下载
|
|
12
13
|
- **文件验证**:提供完整的文件验证和类型判断功能
|
|
13
14
|
- **剪贴板操作**:支持一键复制文本到剪贴板,并支持成功/失败回调
|
|
15
|
+
- **辅助工具**:提供深拷贝、UUID 生成、URL 参数解析、对象转 FormData 等实用工具
|
|
16
|
+
- **正则验证**:提供邮箱、手机、URL、坐标等多种表单验证功能
|
|
14
17
|
- **自动清理**:自动管理临时创建的 Blob URL,防止内存泄漏
|
|
15
18
|
- **TypeScript**:完善的类型定义支持
|
|
16
19
|
|
|
17
20
|
------
|
|
18
21
|
|
|
22
|
+
## 方法列表
|
|
23
|
+
|
|
24
|
+
### 文件读取
|
|
25
|
+
- `readFile` - 读取文件内容为文本、ArrayBuffer 或 DataURL
|
|
26
|
+
- `readTxtFile` - 读取 TXT 文件内容
|
|
27
|
+
- `readCsvFile` - 读取 CSV 文件并解析为对象数组
|
|
28
|
+
- `readJsonFile` - 读取 JSON 文件并解析为 JavaScript 对象
|
|
29
|
+
|
|
30
|
+
### 文件下载
|
|
31
|
+
- `downloadFile` - 从 URL 下载文件
|
|
32
|
+
- `downloadBlob` - 下载 Blob 对象
|
|
33
|
+
- `downloadBase64` - 下载 Base64 编码的文件
|
|
34
|
+
- `downloadMultiple` - 批量下载多个文件
|
|
35
|
+
|
|
36
|
+
### 文件转换
|
|
37
|
+
- `fileToBase64` - 将 File/Blob 转换为 Base64
|
|
38
|
+
- `base64ToBlob` - 将 Base64 转换为 Blob
|
|
39
|
+
- `blobToFile` - 将 Blob 转换为指定文件名的 File
|
|
40
|
+
- `compressImage` - 压缩图片
|
|
41
|
+
|
|
42
|
+
### 文件压缩
|
|
43
|
+
- `fileListToZip` - 将文件列表打包为 ZIP
|
|
44
|
+
- `downloadFileListAsZip` - 下载文件列表的 ZIP 压缩包
|
|
45
|
+
|
|
46
|
+
### 文件生成
|
|
47
|
+
- `generateTxtFile` - 生成 TXT 文件
|
|
48
|
+
- `generateCsvFile` - 生成 CSV 文件
|
|
49
|
+
- `generateJsonFile` - 生成 JSON 文件
|
|
50
|
+
|
|
51
|
+
### 文件验证
|
|
52
|
+
- `checkFileType` - 检查文件类型
|
|
53
|
+
- `checkFileSize` - 检查文件大小
|
|
54
|
+
- `isImage` - 判断是否为图片
|
|
55
|
+
- `getImageDimensions` - 获取图片尺寸
|
|
56
|
+
- `validateFile` - 综合文件验证
|
|
57
|
+
- `formatFileSize` - 格式化文件大小显示
|
|
58
|
+
- `getFileExtension` - 获取文件扩展名
|
|
59
|
+
- `getFileNameWithoutExtension` - 获取文件名(不含扩展名)
|
|
60
|
+
- `getFileInfo` - 获取文件完整信息
|
|
61
|
+
|
|
62
|
+
### 剪贴板操作
|
|
63
|
+
- `copyToClipboard` - 复制文本到剪贴板
|
|
64
|
+
- `pasteFromClipboard` - 从剪贴板读取文本
|
|
65
|
+
|
|
66
|
+
### 辅助工具
|
|
67
|
+
- `deepClone` - 深拷贝对象和数组
|
|
68
|
+
- `numberFixed` - 数字保留指定小数位
|
|
69
|
+
- `parseUrlParams` - 解析 URL 参数为对象
|
|
70
|
+
- `objectToFormData` - 对象转换为 FormData
|
|
71
|
+
- `omitKeys` - 从对象中移除指定的键
|
|
72
|
+
- `generateUUID` - 生成 UUID(支持移除横杠)
|
|
73
|
+
- `arraySum` - 数字数组求和
|
|
74
|
+
- `formatAmount` - 格式化金额(千分位)
|
|
75
|
+
- `maskString` - 字符串中间部分替换为星号
|
|
76
|
+
- `formatPhone` - 格式化手机号码(隐藏中间 4 位)
|
|
77
|
+
|
|
78
|
+
### 正则验证 (VerifyUtils)
|
|
79
|
+
- `VerifyUtils.isEmail` - 邮箱验证
|
|
80
|
+
- `VerifyUtils.isNumber` - 数字验证
|
|
81
|
+
- `VerifyUtils.isPhone` - 手机号验证
|
|
82
|
+
- `VerifyUtils.isUrl` - URL 链接验证
|
|
83
|
+
- `VerifyUtils.isIP` - IP 地址验证
|
|
84
|
+
- `VerifyUtils.isNoSpace` - 无空格验证
|
|
85
|
+
- `VerifyUtils.isChinese` - 中文验证
|
|
86
|
+
- `VerifyUtils.isPassword` - 密码强度验证
|
|
87
|
+
- `VerifyUtils.isLongitude` - 经度验证
|
|
88
|
+
- `VerifyUtils.isLatitude` - 纬度验证
|
|
89
|
+
- `VerifyUtils.isInputCoordinates` - 坐标串验证
|
|
90
|
+
- `VerifyUtils.isEmpty` - 空值检查
|
|
91
|
+
- `VerifyUtils.getRule` - 生成表单验证规则
|
|
92
|
+
- `VerifyUtils.validate` - 带错误消息的验证
|
|
93
|
+
|
|
94
|
+
------
|
|
95
|
+
|
|
19
96
|
## 安装
|
|
20
97
|
|
|
21
98
|
你可以通过 npm 安装该库:
|
|
@@ -380,6 +457,161 @@ const text = await pasteFromClipboard();
|
|
|
380
457
|
console.log('剪贴板内容:', text);
|
|
381
458
|
```
|
|
382
459
|
|
|
460
|
+
### 21. 深拷贝对象
|
|
461
|
+
|
|
462
|
+
```ts
|
|
463
|
+
import { deepClone } from '@giszhc/file-utils';
|
|
464
|
+
|
|
465
|
+
// 深拷贝对象
|
|
466
|
+
const obj = { a: 1, b: { c: 2 }, d: [1, 2, 3] };
|
|
467
|
+
const copy = deepClone(obj);
|
|
468
|
+
copy.b.c = 3;
|
|
469
|
+
console.log(obj.b.c); // 2 (原对象不受影响)
|
|
470
|
+
|
|
471
|
+
// 支持 Date、RegExp 等特殊类型
|
|
472
|
+
const data = {
|
|
473
|
+
date: new Date(),
|
|
474
|
+
regex: /test/g,
|
|
475
|
+
nested: { array: [1, 2, 3] }
|
|
476
|
+
};
|
|
477
|
+
const cloned = deepClone(data);
|
|
478
|
+
```
|
|
479
|
+
|
|
480
|
+
### 22. 数字保留小数位
|
|
481
|
+
|
|
482
|
+
```ts
|
|
483
|
+
import { numberFixed } from '@giszhc/file-utils';
|
|
484
|
+
|
|
485
|
+
// 保留 2 位小数(默认)
|
|
486
|
+
const num1 = numberFixed(3.14159); // 3.14
|
|
487
|
+
|
|
488
|
+
// 自定义小数位数
|
|
489
|
+
const num2 = numberFixed(10, 3); // 10
|
|
490
|
+
const num3 = numberFixed(2.5678, 1); // 2.6
|
|
491
|
+
```
|
|
492
|
+
|
|
493
|
+
### 23. 解析 URL 参数
|
|
494
|
+
|
|
495
|
+
```ts
|
|
496
|
+
import { parseUrlParams } from '@giszhc/file-utils';
|
|
497
|
+
|
|
498
|
+
// 解析当前页面 URL 参数
|
|
499
|
+
const params = parseUrlParams();
|
|
500
|
+
console.log(params.id); // 从当前 URL 获取 id 参数
|
|
501
|
+
|
|
502
|
+
// 解析指定 URL 参数
|
|
503
|
+
const url = 'https://example.com?name=John&age=30';
|
|
504
|
+
const urlParams = parseUrlParams(url);
|
|
505
|
+
// { name: 'John', age: '30' }
|
|
506
|
+
```
|
|
507
|
+
|
|
508
|
+
### 24. 对象转 FormData
|
|
509
|
+
|
|
510
|
+
```ts
|
|
511
|
+
import { objectToFormData } from '@giszhc/file-utils';
|
|
512
|
+
|
|
513
|
+
// 简单对象转换
|
|
514
|
+
const obj = { name: 'John', age: 30 };
|
|
515
|
+
const formData = objectToFormData(obj);
|
|
516
|
+
|
|
517
|
+
// 嵌套对象
|
|
518
|
+
const user = {
|
|
519
|
+
name: 'John',
|
|
520
|
+
profile: {
|
|
521
|
+
email: 'john@example.com',
|
|
522
|
+
phone: '123456'
|
|
523
|
+
}
|
|
524
|
+
};
|
|
525
|
+
const fd = objectToFormData(user);
|
|
526
|
+
|
|
527
|
+
// 包含文件
|
|
528
|
+
const fileInput = document.querySelector('input[type="file"]');
|
|
529
|
+
const data = {
|
|
530
|
+
avatar: fileInput.files[0],
|
|
531
|
+
description: 'User avatar'
|
|
532
|
+
};
|
|
533
|
+
const fdWithFile = objectToFormData(data);
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
### 25. 移除对象的特定键
|
|
537
|
+
|
|
538
|
+
```ts
|
|
539
|
+
import { omitKeys } from '@giszhc/file-utils';
|
|
540
|
+
|
|
541
|
+
// 移除指定的键
|
|
542
|
+
const obj = { a: 1, b: 2, c: 3, d: 4 };
|
|
543
|
+
const result = omitKeys(obj, ['b', 'd']);
|
|
544
|
+
// { a: 1, c: 3 }
|
|
545
|
+
|
|
546
|
+
// 反向操作:只保留指定的键
|
|
547
|
+
const filtered = omitKeys(obj, ['a', 'c'], true);
|
|
548
|
+
// { a: 1, c: 3 }
|
|
549
|
+
```
|
|
550
|
+
|
|
551
|
+
### 26. 生成 UUID
|
|
552
|
+
|
|
553
|
+
```ts
|
|
554
|
+
import { generateUUID } from '@giszhc/file-utils';
|
|
555
|
+
|
|
556
|
+
// 生成带横杠的 UUID(默认)
|
|
557
|
+
const uuid1 = generateUUID();
|
|
558
|
+
// "550e8400-e29b-41d4-a716-446655440000"
|
|
559
|
+
|
|
560
|
+
// 生成不带横杠的 UUID
|
|
561
|
+
const uuid2 = generateUUID(true);
|
|
562
|
+
// "550e8400e29b41d4a716446655440000"
|
|
563
|
+
|
|
564
|
+
// 使用场景:生成唯一 ID
|
|
565
|
+
const userId = generateUUID();
|
|
566
|
+
const requestId = generateUUID(true); // 无横杠版本用于数据库存储
|
|
567
|
+
```
|
|
568
|
+
|
|
569
|
+
### 27. 正则验证工具类
|
|
570
|
+
|
|
571
|
+
```ts
|
|
572
|
+
import { VerifyUtils } from '@giszhc/file-utils';
|
|
573
|
+
|
|
574
|
+
// 基础验证
|
|
575
|
+
VerifyUtils.isEmail('test@example.com'); // true
|
|
576
|
+
VerifyUtils.isPhone('13800138000'); // true
|
|
577
|
+
VerifyUtils.isUrl('https://example.com'); // true
|
|
578
|
+
VerifyUtils.isNumber('123.45'); // true
|
|
579
|
+
VerifyUtils.isNoSpace('hello'); // true
|
|
580
|
+
VerifyUtils.isChinese('你好'); // true
|
|
581
|
+
VerifyUtils.isPassword('Abc@1234'); // true
|
|
582
|
+
|
|
583
|
+
// 经纬度验证
|
|
584
|
+
VerifyUtils.isLongitude(116.4); // true
|
|
585
|
+
VerifyUtils.isLatitude(39.9); // true
|
|
586
|
+
|
|
587
|
+
// 坐标串验证(支持多级分隔符)
|
|
588
|
+
VerifyUtils.isInputCoordinates('116.4,39.9'); // true
|
|
589
|
+
VerifyUtils.isInputCoordinates('116.4,39.9;117.4,40.9'); // true
|
|
590
|
+
VerifyUtils.isInputCoordinates('116.4,39.9|117.4,40.9'); // true
|
|
591
|
+
|
|
592
|
+
// 空值检查
|
|
593
|
+
VerifyUtils.isEmpty(''); // true
|
|
594
|
+
VerifyUtils.isEmpty(null); // true
|
|
595
|
+
VerifyUtils.isEmpty([]); // true
|
|
596
|
+
VerifyUtils.isEmpty({}); // true
|
|
597
|
+
|
|
598
|
+
// 生成 Element UI 表单验证规则
|
|
599
|
+
const rules = {
|
|
600
|
+
email: [
|
|
601
|
+
{ required: true, message: '请输入邮箱', trigger: 'blur' },
|
|
602
|
+
VerifyUtils.getRule('email', 'blur')
|
|
603
|
+
],
|
|
604
|
+
phone: [
|
|
605
|
+
{ required: true, message: '请输入手机号', trigger: 'blur' },
|
|
606
|
+
VerifyUtils.getRule('phone', 'blur')
|
|
607
|
+
],
|
|
608
|
+
url: [VerifyUtils.getRule('url')]
|
|
609
|
+
};
|
|
610
|
+
|
|
611
|
+
// 获取错误提示语
|
|
612
|
+
console.log(VerifyUtils.messages.email); // "请输入正确的邮箱地址"
|
|
613
|
+
```
|
|
614
|
+
|
|
383
615
|
------
|
|
384
616
|
|
|
385
617
|
## 使用方法
|