文件/容量尺寸格式化
/**
* 文件/容量尺寸格式化
* @param size number 容量数值
* @param type string 容量单位
* @param accuracy number 非必填 容量精度
*/
export const fileSizeFormat = (size, type, accuracy = 1) => {
const type_list = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
// 数值不是数值类型
let regx = /(^[1-9]\d*$)/;
if (
regx.test(size) &&
regx.test(accuracy) &&
type_list.includes(type.toUpperCase())
) {
let index = type_list.indexOf(type.toUpperCase());
// 大小超过1024,并且类型小于最大单位类型,进位,
while (size >= 1024 && index < type_list.length - 1) {
size /= 1024;
index++;
}
return size.toFixed(accuracy) + type_list[index];
}
return 'Argument(s) is illegal !';
};