add FormatText.ts
This commit is contained in:
54
src/utils/FormatText.ts
Normal file
54
src/utils/FormatText.ts
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import Toolkit from "./Toolkit";
|
||||||
|
|
||||||
export default class Time {
|
export default class Time {
|
||||||
|
|
||||||
/** 1 秒时间戳 */
|
/** 1 秒时间戳 */
|
||||||
@@ -104,6 +106,21 @@ export default class Time {
|
|||||||
return { l, y, d, h, m, s, ms };
|
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 {
|
public static toMediaTime(seconds: number): string {
|
||||||
seconds = Math.floor(seconds);
|
seconds = Math.floor(seconds);
|
||||||
const hours = Math.floor(seconds / 3600);
|
const hours = Math.floor(seconds / 3600);
|
||||||
|
|||||||
@@ -3,12 +3,13 @@ import { InstallRecord, UserLevelType } from "../types";
|
|||||||
|
|
||||||
export default class Toolkit {
|
export default class Toolkit {
|
||||||
|
|
||||||
public static isFunction = (val: any) => typeof val === "function";
|
public static isFunction = (val: any) => val && typeof val === "function";
|
||||||
public static isArray = Array.isArray;
|
public static isArray = (val: any) => val && Array.isArray(val);
|
||||||
public static isString = (val: any) => typeof val === "string";
|
public static isString = (val: any) => val && typeof val === "string";
|
||||||
public static isSymbol = (val: any) => typeof val === "symbol";
|
public static isSymbol = (val: any) => val && typeof val === "symbol";
|
||||||
public static isObject = (val: any) => val !== null && typeof val === "object";
|
public static isObject = (val: any) => val && typeof val === "object";
|
||||||
public static isFile = (val: any) => val instanceof File;
|
public static isFile = (val: any) => val instanceof File;
|
||||||
|
public static isNumber = (val: any) => val && typeof val === "number";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加安装方法
|
* 添加安装方法
|
||||||
@@ -207,32 +208,6 @@ export default class Toolkit {
|
|||||||
return target;
|
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 {
|
public static toObject(map: Map<any, any>): object {
|
||||||
return Array.from(map.entries()).reduce((acc, [key, value]) => {
|
return Array.from(map.entries()).reduce((acc, [key, value]) => {
|
||||||
acc[key] = value ?? null;
|
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 {
|
public static toFormData(root?: object): FormData {
|
||||||
@@ -338,29 +318,25 @@ export default class Toolkit {
|
|||||||
return form;
|
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 {
|
public static leftClickCallback(event: MouseEvent, callback: Function): void {
|
||||||
if (event.button === 0 && !event.ctrlKey && !event.metaKey && !event.shiftKey) {
|
if (event.button === 0 && !event.ctrlKey && !event.metaKey && !event.shiftKey) {
|
||||||
callback();
|
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 {
|
public static doNotNull(arg: any, func: (arg: any) => void): void {
|
||||||
if (arg) {
|
if (arg) {
|
||||||
func(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;
|
min?: number;
|
||||||
max?: number;
|
max?: number;
|
||||||
}): NodeJS.Timeout {
|
}): NodeJS.Timeout {
|
||||||
const { handler, handleRate = 1, interval, min, max } = config;
|
const {
|
||||||
|
handler,
|
||||||
|
handleRate = 1,
|
||||||
|
interval,
|
||||||
|
min,
|
||||||
|
max
|
||||||
|
} = config;
|
||||||
return setInterval(() => {
|
return setInterval(() => {
|
||||||
if (Math.random() < handleRate) {
|
if (Math.random() < handleRate) {
|
||||||
if (min !== undefined && max !== undefined) {
|
if (min !== undefined && max !== undefined) {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ export { default as Prismjs } from "./Prismjs";
|
|||||||
export { default as Resizer } from "./Resizer";
|
export { default as Resizer } from "./Resizer";
|
||||||
export { default as Scroller } from "./Scroller";
|
export { default as Scroller } from "./Scroller";
|
||||||
export { default as Storage } from "./Storage";
|
export { default as Storage } from "./Storage";
|
||||||
|
export { default as Format } from "./FormatText";
|
||||||
export { default as Time } from "./Time";
|
export { default as Time } from "./Time";
|
||||||
export { default as Toolkit } from "./Toolkit";
|
export { default as Toolkit } from "./Toolkit";
|
||||||
export { default as IconMapper } from "./IconMapper";
|
export { default as IconMapper } from "./IconMapper";
|
||||||
|
|||||||
Reference in New Issue
Block a user