filesize-utils
v1.0.1
Published
关于文件大小的工具类函数
Downloads
5
Readme
关于filesize-utils
关于文件大小的单位统一大写,目前仅支持以下几个单位:
['B', 'KB', 'MB', 'GB', 'TB', 'PB']
说明
这是一个帮助文件大小处理的一些工具类
API
1.getFileSize(file)
file: File文件对象
获取file文件的大小(没错就是file.size这么简单,别管我就喜欢套一层)。
2.formatSize(size, unit, options)
size: 文件大小(number)
unit: 文件大小单位(string,参考上面)
options:配置对象(object)
- targetUnit(string):可以选择你想要转换的目标单位。
- precision(number):精度,可以控制小数位数。
- format('string' | 'array' | 'object'):返回格式,
如果传入'string',则会返回'25 KB'
如果传入'array',则会返回[25, 'KB']
如果传入'object',则会返回{ size: 25, unit: 'KB' }
3.compareSize(file1, file2):比较两个不同单位文件大小
file1,file2(string):文件大小,例如'5.2KB'
返回file1 > file2 的结果(boolean)
4.isOverSize(size, max):判断文件大小是否超过阈值max
size(string):文件大小,例如'5.2KB'
max(string):阈值max,例如'2GB'
返回 size > max 的结果(boolean)
例如:FileSizeUtils.isOverSize('1200KB', '1MB')
5.validateSize(size, range):判断文件大小是否在指定的范围内
size(string):文件大小,例如'5.2KB'
range(Array):文件大小范围,例如['2MB', '5MB']
返回 size 是否在range的范围内 的结果(boolean)
例如:FileSizeUtils.validateSize('1200KB', ['2MB', '5MB'])
使用方法
import FileSizeUtils from 'filesize-utils'
...
FileSizeUtils.getFileSize(file)
FileSizeUtils.formatSize(size, unit, options)
FileSizeUtils.compareSize(file1, file2)
FileSizeUtils.isOverSize(size, max)
FileSizeUtils.validateSize(size, range)