refactor Setting
This commit is contained in:
@@ -62,9 +62,9 @@ export default class IOSize {
|
||||
* @param stopUnit 停止单位
|
||||
* @return
|
||||
*/
|
||||
public static format(size?: number, fixed = 2, stopUnit?: Unit): string {
|
||||
if (!size) {
|
||||
return "";
|
||||
public static format(size?: number | null, fixed = 2, stopUnit?: Unit): string {
|
||||
if (size === undefined || size === null) {
|
||||
return "0 B";
|
||||
}
|
||||
const units = Object.keys(Unit);
|
||||
if (0 < size) {
|
||||
@@ -97,9 +97,9 @@ export default class IOSize {
|
||||
* @return 字节量
|
||||
* @throws Error 格式无效
|
||||
*/
|
||||
public static parse(sizeStr: string): number {
|
||||
if (sizeStr.trim() === "") {
|
||||
throw new Error("not found sizeStr");
|
||||
public static parse(sizeStr?: string | null): number {
|
||||
if (sizeStr === undefined || sizeStr === null || sizeStr.trim() === "") {
|
||||
return 0;
|
||||
}
|
||||
// 正则匹配:可选符号 + 数字(含小数点)+ 可选空格 + 单位
|
||||
const pattern = /([-+]?\d+(?:\.\d+)?)\s*([a-zA-Z]+)/;
|
||||
@@ -169,8 +169,8 @@ export default class IOSize {
|
||||
* @param unit 单位
|
||||
* @return 字节量
|
||||
*/
|
||||
private static toBytes(val: number | undefined, unit: Unit): number {
|
||||
if (!val) {
|
||||
private static toBytes(val: number | undefined | null, unit: Unit): number {
|
||||
if (val === undefined || val === null) {
|
||||
return 0;
|
||||
}
|
||||
const units = Object.values(Unit);
|
||||
@@ -178,8 +178,8 @@ export default class IOSize {
|
||||
return Math.round(val * Math.pow(1024, ordinal));
|
||||
}
|
||||
|
||||
public static speed(val?: number) {
|
||||
if (!val) {
|
||||
public static speed(val?: number | null) {
|
||||
if (val === undefined || val === null) {
|
||||
return "";
|
||||
}
|
||||
return Text.unit(IOSize.format(val, 2), " / s");
|
||||
|
||||
Reference in New Issue
Block a user