This commit is contained in:
Timi
2026-04-10 14:37:14 +08:00
parent 73496f7768
commit cc3bade990
6 changed files with 36 additions and 18 deletions

View File

@@ -5,7 +5,7 @@
"module": "./dist/timi-web.mjs", "module": "./dist/timi-web.mjs",
"style": "./dist/timi-web.css", "style": "./dist/timi-web.css",
"private": false, "private": false,
"version": "0.0.4", "version": "0.0.10",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",

View File

@@ -1,7 +1,7 @@
import { TemplateBizType } from "../types"; import { TemplateBizType } from "../types";
import Toolkit from "../utils/Toolkit"; import Toolkit from "../utils/Toolkit";
import { axios } from "./BaseAPI"; import { axios } from "./BaseAPI";
import TextFormat from "../utils/TextFormat"; import Text from "../utils/Text";
const getCaptchaAPI = () => axios.defaults.baseURL + "/captcha"; const getCaptchaAPI = () => axios.defaults.baseURL + "/captcha";
@@ -12,7 +12,7 @@ async function getTemplate(bizType: TemplateBizType, code: string): Promise<stri
} }
async function getSetting(key: string, args?: { [key: string]: any }): Promise<string> { async function getSetting(key: string, args?: { [key: string]: any }): Promise<string> {
return axios.get(`/setting/${key}?${TextFormat.urlArgs(args)}`); return axios.get(`/setting/${key}?${Text.urlArgs(args)}`);
} }
async function listSetting(keyMap: Map<string, object | undefined>): Promise<Map<string, string>> { async function listSetting(keyMap: Map<string, object | undefined>): Promise<Map<string, string>> {

View File

@@ -71,10 +71,10 @@ export enum ImageType {
export type KeyValue<V, K = string> = { export type KeyValue<V, K = string> = {
key: K; key: K;
value: V; value?: V;
} }
export type LabelValue<L, V = string> = { export type LabelValue<L, V = string> = {
label: L; label: L;
value: V; value?: V;
} }

View File

@@ -1,3 +1,5 @@
import Text from "./Text";
export enum Unit { export enum Unit {
/** B */ /** B */
@@ -60,7 +62,10 @@ export default class IOSize {
* @param stopUnit 停止单位 * @param stopUnit 停止单位
* @return * @return
*/ */
public static format(size: number, fixed = 2, stopUnit?: Unit): string { public static format(size?: number, fixed = 2, stopUnit?: Unit): string {
if (!size) {
return "";
}
const units = Object.keys(Unit); const units = Object.keys(Unit);
if (0 < size) { if (0 < size) {
for (let i = 0; i < units.length; i++, size /= 1024) { for (let i = 0; i < units.length; i++, size /= 1024) {
@@ -93,7 +98,7 @@ export default class IOSize {
* @throws Error 格式无效 * @throws Error 格式无效
*/ */
public static parse(sizeStr: string): number { public static parse(sizeStr: string): number {
if (!sizeStr || sizeStr.trim() === "") { if (sizeStr.trim() === "") {
throw new Error("not found sizeStr"); throw new Error("not found sizeStr");
} }
// 正则匹配:可选符号 + 数字(含小数点)+ 可选空格 + 单位 // 正则匹配:可选符号 + 数字(含小数点)+ 可选空格 + 单位
@@ -160,13 +165,23 @@ export default class IOSize {
/** /**
* 转换值到字节量 * 转换值到字节量
* *
* @param value * @param val 值
* @param unit 单位 * @param unit 单位
* @return 字节量 * @return 字节量
*/ */
private static toBytes(value: number, unit: Unit): number { private static toBytes(val: number | undefined, unit: Unit): number {
if (!val) {
return 0;
}
const units = Object.values(Unit); const units = Object.values(Unit);
const ordinal = units.indexOf(unit); const ordinal = units.indexOf(unit);
return Math.round(value * Math.pow(1024, ordinal)); return Math.round(val * Math.pow(1024, ordinal));
}
public static speed(val?: number) {
if (!val) {
return "";
}
return Text.unit(IOSize.format(val, 2), " / s");
} }
} }

View File

@@ -1,6 +1,6 @@
import IOSize from "./IOSize"; import IOSize from "./IOSize";
export default class TextFormat { export default class Text {
public static keyValue(obj: object, assign: string, split: string): string { public static keyValue(obj: object, assign: string, split: string): string {
let result = ""; let result = "";
@@ -40,15 +40,18 @@ export default class TextFormat {
if (Number.isNaN(val)) { if (Number.isNaN(val)) {
return ""; return "";
} }
return val.toFixed(fixed) + unit; val = val.toFixed(fixed);
} }
return val.trim() + unit; return `${val.trim()} ${unit}`;
}; };
public static speed(val?: number) { public static display(val: string | number | null | undefined) {
if (!val) { if (typeof val === "string") {
return ""; return val.trim() || "";
} }
return this.unit(IOSize.format(val, 2), " / s"); if (typeof val === "number" && Number.isFinite(val)) {
return val.toString();
}
return "";
} }
} }

View File

@@ -7,7 +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 "./TextFormat"; export { default as Text } from "./Text";
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";