add Test
This commit is contained in:
85
src/utils/Text.test.ts
Normal file
85
src/utils/Text.test.ts
Normal 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("");
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user