From 4ae8904697ffa0cb0489b4fffe31465a750088af Mon Sep 17 00:00:00 2001 From: Timi Date: Fri, 10 Apr 2026 12:42:53 +0800 Subject: [PATCH] add FormatText.ts --- src/utils/FormatText.ts | 54 +++++++++++++++++++++++++++++++ src/utils/Time.ts | 17 ++++++++++ src/utils/Toolkit.ts | 72 ++++++++++++++++------------------------- src/utils/index.ts | 1 + 4 files changed, 99 insertions(+), 45 deletions(-) create mode 100644 src/utils/FormatText.ts diff --git a/src/utils/FormatText.ts b/src/utils/FormatText.ts new file mode 100644 index 0000000..4817dbe --- /dev/null +++ b/src/utils/FormatText.ts @@ -0,0 +1,54 @@ +import IOSize from "./IOSize"; + +export default class FormatText { + + public static keyValue(obj: object, assign: string, split: string): string { + let result = ""; + Object.entries(obj).forEach(([k, v]) => result += k + assign + v + split); + return result.substring(0, result.length - split.length); + } + + public static urlArgs(obj?: { [key: string]: any }): string { + if (!obj) { + return ""; + } + const args: string[] = []; + for (const key in obj) { + if (Object.prototype.hasOwnProperty.call(obj, key)) { + const value = obj[key]; + if (Array.isArray(value)) { + value.forEach((item) => { + args.push(`${encodeURIComponent(key)}=${encodeURIComponent(item)}`); + }); + } else { + args.push(`${encodeURIComponent(key)}=${encodeURIComponent(value.toString())}`); + } + } + } + return args.join("&"); + } + + public static template(template: string, variables: { [key: string]: any }): string { + return template.replace(/\$\{(\w+)}/g, (_, key) => variables[key]); + } + + public static unit(val: number | string | null | undefined, unit: string, fixed = 0): string { + if (!val) { + return ""; + } + if (typeof val === "number") { + if (Number.isNaN(val)) { + return ""; + } + return val.toFixed(fixed) + unit; + } + return val.trim() + unit; + }; + + public static speed(val?: number) { + if (!val) { + return ""; + } + return this.unit(IOSize.format(val, 2), " / s"); + } +} diff --git a/src/utils/Time.ts b/src/utils/Time.ts index 5f0a653..afdf409 100644 --- a/src/utils/Time.ts +++ b/src/utils/Time.ts @@ -1,3 +1,5 @@ +import Toolkit from "./Toolkit"; + export default class Time { /** 1 秒时间戳 */ @@ -104,6 +106,21 @@ export default class Time { return { l, y, d, h, m, s, ms }; } + public static duration(begin?: number, ms?: boolean): string { + if (!begin) { + return ""; + } + const r = Time.between(new Date(begin), new Date()); + const parts: string[] = []; + Toolkit.doWhere(0 < r.y, () => parts.push(`${r.y} 年`)); + Toolkit.doWhere(0 < r.d, () => parts.push(`${r.d} 天`)); + Toolkit.doWhere(0 < r.h, () => parts.push(`${r.h} 小时`)); + Toolkit.doWhere(0 < r.m, () => parts.push(`${r.m} 分钟`)); + Toolkit.doWhere(0 < r.s, () => parts.push(`${r.s} 秒`)); + Toolkit.doWhere(!!ms && 0 < r.ms, () => parts.push(`${r.ms} 毫秒`)); + return parts.join(" "); + } + public static toMediaTime(seconds: number): string { seconds = Math.floor(seconds); const hours = Math.floor(seconds / 3600); diff --git a/src/utils/Toolkit.ts b/src/utils/Toolkit.ts index 793716b..f0c728a 100644 --- a/src/utils/Toolkit.ts +++ b/src/utils/Toolkit.ts @@ -3,12 +3,13 @@ import { InstallRecord, UserLevelType } from "../types"; export default class Toolkit { - public static isFunction = (val: any) => typeof val === "function"; - public static isArray = Array.isArray; - public static isString = (val: any) => typeof val === "string"; - public static isSymbol = (val: any) => typeof val === "symbol"; - public static isObject = (val: any) => val !== null && typeof val === "object"; + public static isFunction = (val: any) => val && typeof val === "function"; + public static isArray = (val: any) => val && Array.isArray(val); + public static isString = (val: any) => val && typeof val === "string"; + public static isSymbol = (val: any) => val && typeof val === "symbol"; + public static isObject = (val: any) => val && typeof val === "object"; public static isFile = (val: any) => val instanceof File; + public static isNumber = (val: any) => val && typeof val === "number"; /** * 添加安装方法 @@ -181,7 +182,7 @@ export default class Toolkit { while (n--) { u8arr[n] = base64.charCodeAt(n); } - return new File([u8arr], fileName, {type: splitData[0].split(":")[1]}); + return new File([u8arr], fileName, { type: splitData[0].split(":")[1] }); } /** @@ -207,32 +208,6 @@ export default class Toolkit { return target; } - public static keyValueString(obj: object, assign: string, split: string): string { - let result = ""; - Object.entries(obj).forEach(([k, v]) => result += k + assign + v + split); - return result.substring(0, result.length - split.length); - } - - public static toURLArgs(obj?: { [key: string]: any }): string { - if (!obj) { - return ""; - } - const args: string[] = []; - for (const key in obj) { - if (Object.prototype.hasOwnProperty.call(obj, key)) { - const value = obj[key]; - if (Array.isArray(value)) { - value.forEach((item) => { - args.push(`${encodeURIComponent(key)}=${encodeURIComponent(item)}`); - }); - } else { - args.push(`${encodeURIComponent(key)}=${encodeURIComponent(value.toString())}`); - } - } - } - return args.join("&"); - } - public static toObject(map: Map): object { return Array.from(map.entries()).reduce((acc, [key, value]) => { acc[key] = value ?? null; @@ -309,7 +284,12 @@ export default class Toolkit { }; } } - return { exp, value: 8, percent: 1, nextLevelUp: 4096 }; + return { + exp, + value: 8, + percent: 1, + nextLevelUp: 4096 + }; } public static toFormData(root?: object): FormData { @@ -338,29 +318,25 @@ export default class Toolkit { return form; } - public static format(template: string, variables: { [key: string]: any }): string { - return template.replace(/\$\{(\w+)}/g, (_, key) => variables[key]); - } - public static leftClickCallback(event: MouseEvent, callback: Function): void { if (event.button === 0 && !event.ctrlKey && !event.metaKey && !event.shiftKey) { callback(); } } + public static doWhere(where: boolean | (() => boolean), func: () => void): void { + const condition = typeof where === 'function' ? where() : where; + if (condition) { + func(); + } + } + public static doNotNull(arg: any, func: (arg: any) => void): void { if (arg) { func(arg); } } - public static toCssSize(value: number | string): string { - if (typeof value === "number") { - return `${value}px`; - } - return value; - }; - /** * 设置随机间隔执行 * @@ -398,7 +374,13 @@ export default class Toolkit { min?: number; max?: number; }): NodeJS.Timeout { - const { handler, handleRate = 1, interval, min, max } = config; + const { + handler, + handleRate = 1, + interval, + min, + max + } = config; return setInterval(() => { if (Math.random() < handleRate) { if (min !== undefined && max !== undefined) { diff --git a/src/utils/index.ts b/src/utils/index.ts index cbbec71..3a10498 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -7,6 +7,7 @@ export { default as Prismjs } from "./Prismjs"; export { default as Resizer } from "./Resizer"; export { default as Scroller } from "./Scroller"; export { default as Storage } from "./Storage"; +export { default as Format } from "./FormatText"; export { default as Time } from "./Time"; export { default as Toolkit } from "./Toolkit"; export { default as IconMapper } from "./IconMapper";