update
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import Text from "./Text";
|
||||
|
||||
export enum Unit {
|
||||
|
||||
/** B */
|
||||
@@ -60,7 +62,10 @@ export default class IOSize {
|
||||
* @param stopUnit 停止单位
|
||||
* @return
|
||||
*/
|
||||
public static format(size: number, fixed = 2, stopUnit?: Unit): string {
|
||||
public static format(size?: number, fixed = 2, stopUnit?: Unit): string {
|
||||
if (!size) {
|
||||
return "";
|
||||
}
|
||||
const units = Object.keys(Unit);
|
||||
if (0 < size) {
|
||||
for (let i = 0; i < units.length; i++, size /= 1024) {
|
||||
@@ -93,7 +98,7 @@ export default class IOSize {
|
||||
* @throws Error 格式无效
|
||||
*/
|
||||
public static parse(sizeStr: string): number {
|
||||
if (!sizeStr || sizeStr.trim() === "") {
|
||||
if (sizeStr.trim() === "") {
|
||||
throw new Error("not found sizeStr");
|
||||
}
|
||||
// 正则匹配:可选符号 + 数字(含小数点)+ 可选空格 + 单位
|
||||
@@ -160,13 +165,23 @@ export default class IOSize {
|
||||
/**
|
||||
* 转换值到字节量
|
||||
*
|
||||
* @param value 值
|
||||
* @param val 值
|
||||
* @param unit 单位
|
||||
* @return 字节量
|
||||
*/
|
||||
private static toBytes(value: number, unit: Unit): number {
|
||||
private static toBytes(val: number | undefined, unit: Unit): number {
|
||||
if (!val) {
|
||||
return 0;
|
||||
}
|
||||
const units = Object.values(Unit);
|
||||
const ordinal = units.indexOf(unit);
|
||||
return Math.round(value * Math.pow(1024, ordinal));
|
||||
return Math.round(val * Math.pow(1024, ordinal));
|
||||
}
|
||||
|
||||
public static speed(val?: number) {
|
||||
if (!val) {
|
||||
return "";
|
||||
}
|
||||
return Text.unit(IOSize.format(val, 2), " / s");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user