This commit is contained in:
Timi
2026-04-10 15:03:48 +08:00
parent cc3bade990
commit 26e533dc69
5 changed files with 1190 additions and 298 deletions

View File

@@ -12,7 +12,10 @@
"dev:doc": "pnpm run -C docs dev",
"build": "vue-tsc --noEmit && vite build",
"build:doc": "pnpm run -C docs build",
"pub": "pnpm run build && npm publish --registry=https://nexus.imyeyu.com/repository/npm-timi/"
"pub": "pnpm run build && npm publish --registry=https://nexus.imyeyu.com/repository/npm-timi/",
"test": "vitest run",
"test:watch": "vitest",
"test:coverage": "vitest run --coverage"
},
"publishConfig": {
"registry": "https://nexus.imyeyu.com/repository/npm-timi/"
@@ -52,19 +55,23 @@
"@typescript-eslint/eslint-plugin": "^8.53.0",
"@typescript-eslint/parser": "^8.53.0",
"@vitejs/plugin-vue": "^6.0.3",
"@vitest/coverage-v8": "^4.1.4",
"@vue/test-utils": "^2.4.6",
"eslint": "^9.39.2",
"eslint-config-prettier": "^10.1.8",
"eslint-define-config": "^2.1.0",
"eslint-plugin-prettier": "^5.5.5",
"eslint-plugin-vue": "^10.6.2",
"happy-dom": "^20.8.9",
"prettier": "^3.8.0",
"typescript": "^5.9.3",
"unplugin-auto-import": "^21.0.0",
"unplugin-vue-components": "^31.0.0",
"vite": "7.1.11",
"vite": "8.0.5",
"vite-plugin-dts": "^4.5.4",
"vite-plugin-prismjs": "^0.0.11",
"vite-plugin-vue-setup-extend": "^0.4.0",
"vitest": "^4.1.4",
"vue": "^3.5.26",
"vue-tsc": "^3.2.2"
}

1362
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

85
src/utils/Text.test.ts Normal file
View File

@@ -0,0 +1,85 @@
import { describe, expect, it } from "vitest";
import Text from "./Text";
describe("Text", () => {
describe("keyValue", () => {
it("should join key value pairs", () => {
expect(Text.keyValue({ a: 1, b: "x" }, "=", "&")).toBe("a=1&b=x");
});
it("should return empty string for empty object", () => {
expect(Text.keyValue({}, "=", "&")).toBe("");
});
});
describe("urlArgs", () => {
it("should return empty string when input is undefined", () => {
expect(Text.urlArgs()).toBe("");
});
it("should encode primitive and array values", () => {
const query = Text.urlArgs({
name: "A B",
tags: ["x/y", "z"],
age: 18
});
expect(query).toBe("name=A%20B&tags=x%2Fy&tags=z&age=18");
});
it("should ignore inherited properties", () => {
const parent = { hidden: "parent" };
const obj = Object.create(parent) as { own: string };
obj.own = "child";
expect(Text.urlArgs(obj)).toBe("own=child");
});
});
describe("template", () => {
it("should replace template placeholders", () => {
expect(Text.template("Hi ${name}", { name: "Timi" })).toBe("Hi Timi");
});
it("should output undefined text when key is missing", () => {
expect(Text.template("Hi ${name} ${miss}", { name: "Timi" })).toBe("Hi Timi undefined");
});
});
describe("unit", () => {
it("should format number with fixed and unit", () => {
expect(Text.unitSpace(1.236, 2, "rem")).toBe("1.24 rem");
});
it("should format number with fixed and unit not space", () => {
expect(Text.unit(1.236, "rem", 2)).toBe("1.24rem");
});
it("should trim string and append unit", () => {
expect(Text.unit(" 12 ", "px")).toBe("12px");
});
it("should return empty string for nullish zero and NaN", () => {
expect(Text.unit(null, "px")).toBe("");
expect(Text.unit(undefined, "px")).toBe("");
expect(Text.unit(0, "px")).toBe("");
expect(Text.unit(Number.NaN, "px")).toBe("");
});
});
describe("display", () => {
it("should trim string values", () => {
expect(Text.display(" abc ")).toBe("abc");
expect(Text.display(" ")).toBe("");
});
it("should stringify finite numbers", () => {
expect(Text.display(0)).toBe("0");
expect(Text.display(12.3)).toBe("12.3");
});
it("should return empty string for invalid numbers and nullish", () => {
expect(Text.display(Number.NaN)).toBe("");
expect(Text.display(Number.POSITIVE_INFINITY)).toBe("");
expect(Text.display(null)).toBe("");
expect(Text.display(undefined)).toBe("");
});
});
});

View File

@@ -1,5 +1,3 @@
import IOSize from "./IOSize";
export default class Text {
public static keyValue(obj: object, assign: string, split: string): string {
@@ -32,17 +30,28 @@ export default class Text {
return template.replace(/\$\{(\w+)}/g, (_, key) => variables[key]);
}
public static unit(val: number | string | null | undefined, unit: string, fixed = 0): string {
public static unitSpace(val: number | string | null | undefined, fixed = 0, unit: string): string {
return this.unit(val, unit, fixed, true);
};
public static unit(val: number | string | null | undefined, unit: string, fixed = 0, space = false): string {
if (!val) {
return "";
}
const result = [];
if (typeof val === "number") {
if (Number.isNaN(val)) {
return "";
}
val = val.toFixed(fixed);
result.push(val.toFixed(fixed));
} else {
result.push(val.trim());
}
return `${val.trim()} ${unit}`;
if (space) {
result.push(" ");
}
result.push(unit);
return result.join("");
};
public static display(val: string | number | null | undefined) {

11
vitest.config.ts Normal file
View File

@@ -0,0 +1,11 @@
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
include: ["src/**/*.{test,spec}.ts"],
coverage: {
provider: "v8",
reporter: ["text", "html"]
}
}
});