refactor Setting

This commit is contained in:
Timi
2026-04-30 11:04:28 +08:00
parent 28f0eabff2
commit ff3297e879
15 changed files with 239 additions and 231 deletions

View File

@@ -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");