add FormatText.ts

This commit is contained in:
Timi
2026-04-10 12:42:53 +08:00
parent 5472d00154
commit 4ae8904697
4 changed files with 99 additions and 45 deletions

54
src/utils/FormatText.ts Normal file
View File

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

View File

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

View File

@@ -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<any, any>): 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) {

View File

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