feat(all): 新增工具

This commit is contained in:
2026-05-28 11:30:35 +08:00
parent 5dbedcaf0f
commit 3a55edfafc
85 changed files with 2902 additions and 2591 deletions
+40 -10
View File
@@ -13,7 +13,9 @@ describe("getValueOfRule - 文本模式 (text: true)", () => {
});
test("required + init:非数字文本回退到 init", () => {
expect(getValueOfRule("abc", { ...textOpts, required: true, init: "默认" })).toBe("默认");
expect(
getValueOfRule("abc", { ...textOpts, required: true, init: "默认" }),
).toBe("默认");
});
test("required 无 init:非数字文本保持原值", () => {
@@ -29,7 +31,9 @@ describe("getValueOfRule - 文本模式 (text: true)", () => {
});
test("pattern 匹配成功返回原值", () => {
expect(getValueOfRule("123", { ...textOpts, pattern: "^\\d+$" })).toBe("123");
expect(getValueOfRule("123", { ...textOpts, pattern: "^\\d+$" })).toBe(
"123",
);
});
});
@@ -107,7 +111,9 @@ describe("getValueOfRule - digits 小数位数", () => {
});
test("keepDecimal + number 返回数字类型", () => {
expect(getValueOfRule("3.10", { digits: 2, keepDecimal: true, number: true })).toBe(3.1);
expect(
getValueOfRule("3.10", { digits: 2, keepDecimal: true, number: true }),
).toBe(3.1);
});
});
@@ -127,11 +133,15 @@ describe("getValueOfRule - integer 整数模式", () => {
describe("getValueOfRule - noSign 不允许正负号", () => {
test("带正号的输入被视为非法", () => {
expect(getValueOfRule("+5", { noSign: true, required: true, init: 0 })).toBe("0");
expect(
getValueOfRule("+5", { noSign: true, required: true, init: 0 }),
).toBe("0");
});
test("带负号的输入被视为非法", () => {
expect(getValueOfRule("-5", { noSign: true, required: true, init: 0 })).toBe("0");
expect(
getValueOfRule("-5", { noSign: true, required: true, init: 0 }),
).toBe("0");
});
test("无符号数字正常通过", () => {
@@ -142,25 +152,45 @@ describe("getValueOfRule - noSign 不允许正负号", () => {
describe("getValueOfRule - callback", () => {
test("callback 接收最终处理后的值", () => {
let received: unknown;
getValueOfRule("42", { number: true }, (v) => { received = v; });
getValueOfRule("42", { number: true }, (v) => {
received = v;
});
expect(received).toBe(42);
});
});
describe("getValueOfRule - 综合场景", () => {
test("限制输入 min:0, max:99999.99, required, digits:2", () => {
expect(getValueOfRule("100.456", { min: 0, max: 99999.99, required: true, digits: 2 })).toBe("100.46");
expect(
getValueOfRule("100.456", {
min: 0,
max: 99999.99,
required: true,
digits: 2,
}),
).toBe("100.46");
});
test("超出 max 时截断到 max", () => {
expect(getValueOfRule("100000", { min: 0, max: 99999.99, required: true, digits: 2 })).toBe("99999.99");
expect(
getValueOfRule("100000", {
min: 0,
max: 99999.99,
required: true,
digits: 2,
}),
).toBe("99999.99");
});
test("空输入 required 回退到 min", () => {
expect(getValueOfRule("", { min: 0, max: 99999.99, required: true, digits: 2 })).toBe("0");
expect(
getValueOfRule("", { min: 0, max: 99999.99, required: true, digits: 2 }),
).toBe("0");
});
test("负数输入限制到 min:0", () => {
expect(getValueOfRule("-5", { min: 0, max: 100, required: true })).toBe("0");
expect(getValueOfRule("-5", { min: 0, max: 100, required: true })).toBe(
"0",
);
});
});