@arkyn/shared 1.4.57 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +24 -0
- package/README.md +113 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -1
- package/dist/validations/validateCep.d.ts +24 -0
- package/dist/validations/validateCep.d.ts.map +1 -0
- package/dist/validations/validateCep.js +33 -0
- package/dist/validations/validateCnpj.d.ts +22 -0
- package/dist/validations/validateCnpj.d.ts.map +1 -0
- package/dist/validations/validateCnpj.js +52 -0
- package/dist/validations/validateCpf.d.ts.map +1 -1
- package/dist/validations/validateCpf.js +2 -4
- package/dist/validations/validateDate.d.ts +1 -1
- package/dist/validations/validateDate.js +8 -8
- package/dist/validations/validatePassword.d.ts +21 -0
- package/dist/validations/validatePassword.d.ts.map +1 -0
- package/dist/validations/validatePassword.js +34 -0
- package/dist/validations/validatePhone.d.ts +1 -1
- package/dist/validations/validatePhone.js +4 -4
- package/dist/validations/validateRg.d.ts +22 -0
- package/dist/validations/validateRg.d.ts.map +1 -0
- package/dist/validations/validateRg.js +31 -0
- package/package.json +8 -5
- package/src/formats/__test__/formatDate.spec.ts +88 -0
- package/src/formats/__test__/formatJsonObject.spec.ts +87 -0
- package/src/formats/__test__/formatJsonString.spec.ts +83 -0
- package/src/formats/__test__/formatToCep.spec.ts +37 -0
- package/src/formats/__test__/formatToCnpj.spec.ts +35 -0
- package/src/formats/__test__/formatToCpf.spec.ts +37 -0
- package/src/formats/__test__/formatToCpfCnpj.spec.ts +43 -0
- package/src/formats/__test__/formatToCurrency.spec.ts +50 -0
- package/src/formats/__test__/formatToEllipsis.spec.ts +44 -0
- package/src/formats/__test__/formatToHiddenDigits.spec.ts +58 -0
- package/src/formats/__test__/formatToPhone.spec.ts +58 -0
- package/src/generators/__test__/generateColorByString.spec.ts +37 -0
- package/src/generators/__test__/generateId.spec.ts +44 -0
- package/src/generators/__test__/generateSlug.spec.ts +47 -0
- package/src/index.ts +7 -2
- package/src/services/__test__/calculateCardInstallment.spec.ts +87 -0
- package/src/services/__test__/maskSensitiveData.spec.ts +102 -0
- package/src/services/__test__/removeCurrencySymbols.spec.ts +41 -0
- package/src/services/__test__/removeNonNumeric.spec.ts +33 -0
- package/src/services/__test__/truncateLargeFields.spec.ts +90 -0
- package/src/validations/__test__/validateCep.spec.ts +40 -0
- package/src/validations/__test__/validateCnpj.spec.ts +42 -0
- package/src/validations/__test__/validateCpf.spec.ts +38 -0
- package/src/validations/__test__/validateDate.spec.ts +81 -0
- package/src/validations/__test__/validatePassword.spec.ts +43 -0
- package/src/validations/__test__/validatePhone.spec.ts +39 -0
- package/src/validations/__test__/validateRg.spec.ts +48 -0
- package/src/validations/validateCep.ts +40 -0
- package/src/validations/validateCnpj.ts +64 -0
- package/src/validations/validateCpf.ts +2 -5
- package/src/validations/validateDate.ts +8 -8
- package/src/validations/validatePassword.ts +41 -0
- package/src/validations/validatePhone.ts +4 -4
- package/src/validations/validateRg.ts +37 -0
- package/tsconfig.json +1 -1
- package/dist/regex/index.d.ts +0 -6
- package/dist/regex/index.d.ts.map +0 -1
- package/dist/regex/index.js +0 -4
- package/src/regex/index.ts +0 -8
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { generateId } from "../generateId";
|
|
3
|
+
|
|
4
|
+
describe("generateId", () => {
|
|
5
|
+
it("should generate a valid UUID v4 as text", () => {
|
|
6
|
+
const id = generateId("text", "v4");
|
|
7
|
+
expect(typeof id).toBe("string");
|
|
8
|
+
expect(id).toMatch(
|
|
9
|
+
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
|
|
10
|
+
);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("should generate a valid UUID v4 as binary", () => {
|
|
14
|
+
const id = generateId("binary", "v4");
|
|
15
|
+
expect(id).toBeInstanceOf(Uint8Array);
|
|
16
|
+
expect(id.length).toBe(16); // UUIDs in binary format are 16 bytes
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("should generate a valid UUID v7 as text", () => {
|
|
20
|
+
const id = generateId("text", "v7");
|
|
21
|
+
expect(typeof id).toBe("string");
|
|
22
|
+
expect(id).toMatch(
|
|
23
|
+
/^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
|
|
24
|
+
);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("should generate a valid UUID v7 as binary", () => {
|
|
28
|
+
const id = generateId("binary", "v7");
|
|
29
|
+
expect(id).toBeInstanceOf(Uint8Array);
|
|
30
|
+
expect(id.length).toBe(16); // UUIDs in binary format are 16 bytes
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("should throw an error for invalid type", () => {
|
|
34
|
+
expect(() => generateId("invalid" as any, "v4")).toThrow(
|
|
35
|
+
"Invalid type or format"
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("should throw an error for invalid format", () => {
|
|
40
|
+
expect(() => generateId("text", "v8" as any)).toThrow(
|
|
41
|
+
"Invalid type or format"
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { generateSlug } from "../generateSlug";
|
|
3
|
+
|
|
4
|
+
describe("generateSlug", () => {
|
|
5
|
+
it("should convert a string to a URL-friendly slug", () => {
|
|
6
|
+
expect(generateSlug("Hello World")).toBe("hello-world");
|
|
7
|
+
expect(generateSlug("This is a Test")).toBe("this-is-a-test");
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should remove diacritical marks (accents)", () => {
|
|
11
|
+
expect(generateSlug("Olá Mundo")).toBe("ola-mundo");
|
|
12
|
+
expect(generateSlug("Crème brûlée")).toBe("creme-brulee");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should remove non-alphanumeric characters except spaces and hyphens", () => {
|
|
16
|
+
expect(generateSlug("Hello, World!")).toBe("hello-world");
|
|
17
|
+
expect(generateSlug("C# Programming")).toBe("c-programming");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should collapse multiple spaces into a single hyphen", () => {
|
|
21
|
+
expect(generateSlug("Hello World")).toBe("hello-world");
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("should collapse multiple consecutive hyphens into a single hyphen", () => {
|
|
25
|
+
expect(generateSlug("Hello--World")).toBe("hello-world");
|
|
26
|
+
expect(generateSlug("Hello---World")).toBe("hello-world");
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should trim leading and trailing hyphens", () => {
|
|
30
|
+
expect(generateSlug("-Hello World-")).toBe("hello-world");
|
|
31
|
+
expect(generateSlug("--Hello World--")).toBe("hello-world");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should handle empty strings", () => {
|
|
35
|
+
expect(generateSlug("")).toBe("");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("should handle strings with only special characters", () => {
|
|
39
|
+
expect(generateSlug("!@#$%^&*()")).toBe("");
|
|
40
|
+
expect(generateSlug("---")).toBe("");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("should handle strings with numbers", () => {
|
|
44
|
+
expect(generateSlug("Version 2.0")).toBe("version-20");
|
|
45
|
+
expect(generateSlug("123 Testing")).toBe("123-testing");
|
|
46
|
+
});
|
|
47
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// formats
|
|
1
2
|
export { formatDate } from "./formats/formatDate";
|
|
2
3
|
export { formatJsonObject } from "./formats/formatJsonObject";
|
|
3
4
|
export { formatJsonString } from "./formats/formatJsonString";
|
|
@@ -10,17 +11,21 @@ export { formatToEllipsis } from "./formats/formatToEllipsis";
|
|
|
10
11
|
export { formatToHiddenDigits } from "./formats/formatToHiddenDigits";
|
|
11
12
|
export { formatToPhone } from "./formats/formatToPhone";
|
|
12
13
|
|
|
14
|
+
// generators
|
|
13
15
|
export { generateColorByString } from "./generators/generateColorByString";
|
|
14
16
|
export { generateId } from "./generators/generateId";
|
|
15
17
|
export { generateSlug } from "./generators/generateSlug";
|
|
16
18
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
+
// services
|
|
19
20
|
export { calculateCardInstallment } from "./services/calculateCardInstallment";
|
|
20
21
|
export { maskSensitiveData } from "./services/maskSensitiveData";
|
|
21
22
|
export { removeNonNumeric } from "./services/removeNonNumeric";
|
|
22
23
|
export { truncateLargeFields } from "./services/truncateLargeFields";
|
|
23
24
|
|
|
25
|
+
// utils
|
|
26
|
+
export { validateCep } from "./validations/validateCep";
|
|
27
|
+
export { validateCnpj } from "./validations/validateCnpj";
|
|
24
28
|
export { validateCpf } from "./validations/validateCpf";
|
|
25
29
|
export { validateDate } from "./validations/validateDate";
|
|
26
30
|
export { validatePhone } from "./validations/validatePhone";
|
|
31
|
+
export { validateRg } from "./validations/validateRg";
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { calculateCardInstallment } from "../calculateCardInstallment";
|
|
3
|
+
|
|
4
|
+
describe("calculateCardInstallment", () => {
|
|
5
|
+
it("should calculate the total and installment price with default fees", () => {
|
|
6
|
+
const result = calculateCardInstallment({
|
|
7
|
+
cashPrice: 1000,
|
|
8
|
+
numberInstallments: 12,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
expect(result).toEqual({
|
|
12
|
+
totalPrice: 1241.08,
|
|
13
|
+
installmentPrice: 103.42,
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("should calculate the total and installment price with custom fees", () => {
|
|
18
|
+
const result = calculateCardInstallment({
|
|
19
|
+
cashPrice: 1000,
|
|
20
|
+
numberInstallments: 12,
|
|
21
|
+
fees: 0.02,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
expect(result).toEqual({
|
|
25
|
+
totalPrice: 1134.72,
|
|
26
|
+
installmentPrice: 94.56,
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should calculate correctly for a single installment (no interest)", () => {
|
|
31
|
+
const result = calculateCardInstallment({
|
|
32
|
+
cashPrice: 1000,
|
|
33
|
+
numberInstallments: 1,
|
|
34
|
+
fees: 0,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
expect(result).toEqual({
|
|
38
|
+
totalPrice: 1000,
|
|
39
|
+
installmentPrice: 1000,
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("should calculate correctly for zero fees", () => {
|
|
44
|
+
const result = calculateCardInstallment({
|
|
45
|
+
cashPrice: 500,
|
|
46
|
+
numberInstallments: 5,
|
|
47
|
+
fees: 0,
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
expect(result).toEqual({
|
|
51
|
+
totalPrice: 500,
|
|
52
|
+
installmentPrice: 100,
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("should handle edge case with zero installments", () => {
|
|
57
|
+
expect(() =>
|
|
58
|
+
calculateCardInstallment({
|
|
59
|
+
cashPrice: 1000,
|
|
60
|
+
numberInstallments: 0,
|
|
61
|
+
})
|
|
62
|
+
).toThrow("Number of installments must be greater than 0");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("should handle edge case with negative fees", () => {
|
|
66
|
+
expect(() =>
|
|
67
|
+
calculateCardInstallment({
|
|
68
|
+
cashPrice: 1000,
|
|
69
|
+
numberInstallments: 12,
|
|
70
|
+
fees: -0.01,
|
|
71
|
+
})
|
|
72
|
+
).toThrow("Fees must be greater than or equal to 0");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("should handle large numbers for cash price and installments", () => {
|
|
76
|
+
const result = calculateCardInstallment({
|
|
77
|
+
cashPrice: 1000000,
|
|
78
|
+
numberInstallments: 24,
|
|
79
|
+
fees: 0.01,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
expect(result).toEqual({
|
|
83
|
+
totalPrice: 1129763.33,
|
|
84
|
+
installmentPrice: 47073.47,
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
});
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { maskSensitiveData } from "../maskSensitiveData";
|
|
3
|
+
|
|
4
|
+
describe("maskSensitiveData", () => {
|
|
5
|
+
it("should mask sensitive keys in a flat JSON object", () => {
|
|
6
|
+
const jsonString = JSON.stringify({
|
|
7
|
+
username: "user123",
|
|
8
|
+
password: "secret",
|
|
9
|
+
email: "user@example.com",
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
const result = maskSensitiveData(jsonString, ["password"]);
|
|
13
|
+
expect(result).toBe(
|
|
14
|
+
'{"username":"user123","password":"****","email":"user@example.com"}'
|
|
15
|
+
);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("should mask sensitive keys in nested objects", () => {
|
|
19
|
+
const jsonString = JSON.stringify({
|
|
20
|
+
profile: {
|
|
21
|
+
username: "user123",
|
|
22
|
+
creditCard: "1234-5678-9012-3456",
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const result = maskSensitiveData(jsonString, ["creditCard"]);
|
|
27
|
+
expect(result).toBe(
|
|
28
|
+
'{"profile":{"username":"user123","creditCard":"****"}}'
|
|
29
|
+
);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("should mask sensitive keys in arrays", () => {
|
|
33
|
+
const jsonString = JSON.stringify({
|
|
34
|
+
users: [
|
|
35
|
+
{ username: "user1", password: "pass1" },
|
|
36
|
+
{ username: "user2", password: "pass2" },
|
|
37
|
+
],
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const result = maskSensitiveData(jsonString, ["password"]);
|
|
41
|
+
expect(result).toBe(
|
|
42
|
+
'{"users":[{"username":"user1","password":"****"},{"username":"user2","password":"****"}]}'
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("should handle non-JSON strings gracefully", () => {
|
|
47
|
+
const invalidJson = "not a json string";
|
|
48
|
+
const result = maskSensitiveData(invalidJson, ["password"]);
|
|
49
|
+
expect(result).toBe("not a json string");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("should handle empty JSON objects", () => {
|
|
53
|
+
const jsonString = JSON.stringify({});
|
|
54
|
+
const result = maskSensitiveData(jsonString, ["password"]);
|
|
55
|
+
expect(result).toBe("{}");
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("should handle empty arrays", () => {
|
|
59
|
+
const jsonString = JSON.stringify([]);
|
|
60
|
+
const result = maskSensitiveData(jsonString, ["password"]);
|
|
61
|
+
expect(result).toBe("[]");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("should use default sensitive keys if none are provided", () => {
|
|
65
|
+
const jsonString = JSON.stringify({
|
|
66
|
+
username: "user123",
|
|
67
|
+
password: "secret",
|
|
68
|
+
confirmPassword: "secret",
|
|
69
|
+
creditCard: "1234-5678-9012-3456",
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const result = maskSensitiveData(jsonString);
|
|
73
|
+
expect(result).toBe(
|
|
74
|
+
'{"username":"user123","password":"****","confirmPassword":"****","creditCard":"****"}'
|
|
75
|
+
);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("should not mask keys that are not in the sensitive keys list", () => {
|
|
79
|
+
const jsonString = JSON.stringify({
|
|
80
|
+
username: "user123",
|
|
81
|
+
email: "user@example.com",
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const result = maskSensitiveData(jsonString, ["password"]);
|
|
85
|
+
expect(result).toBe('{"username":"user123","email":"user@example.com"}');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("should handle deeply nested objects", () => {
|
|
89
|
+
const jsonString = JSON.stringify({
|
|
90
|
+
level1: {
|
|
91
|
+
level2: {
|
|
92
|
+
level3: {
|
|
93
|
+
password: "secret",
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
const result = maskSensitiveData(jsonString, ["password"]);
|
|
100
|
+
expect(result).toBe('{"level1":{"level2":{"level3":{"password":"****"}}}}');
|
|
101
|
+
});
|
|
102
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { removeCurrencySymbols } from "../removeCurrencySymbols";
|
|
3
|
+
|
|
4
|
+
describe("removeCurrencySymbols", () => {
|
|
5
|
+
it("should remove common currency symbols from the string", () => {
|
|
6
|
+
expect(removeCurrencySymbols("R$13,45")).toBe("13,45");
|
|
7
|
+
expect(removeCurrencySymbols("$123.45")).toBe("123.45");
|
|
8
|
+
expect(removeCurrencySymbols("€99.99")).toBe("99.99");
|
|
9
|
+
expect(removeCurrencySymbols("¥1,000")).toBe("1,000");
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("should handle strings without currency symbols", () => {
|
|
13
|
+
expect(removeCurrencySymbols("123.45")).toBe("123.45");
|
|
14
|
+
expect(removeCurrencySymbols("1,000")).toBe("1,000");
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("should handle strings with multiple currency symbols", () => {
|
|
18
|
+
expect(removeCurrencySymbols("R$ $123.45")).toBe("123.45");
|
|
19
|
+
expect(removeCurrencySymbols("€¥99,99")).toBe("99,99");
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("should handle strings with leading and trailing whitespace", () => {
|
|
23
|
+
expect(removeCurrencySymbols(" $123.45 ")).toBe("123.45");
|
|
24
|
+
expect(removeCurrencySymbols("\t€99.99\n")).toBe("99.99");
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("should return an empty string if the input is only currency symbols", () => {
|
|
28
|
+
expect(removeCurrencySymbols("R$")).toBe("");
|
|
29
|
+
expect(removeCurrencySymbols("$")).toBe("");
|
|
30
|
+
expect(removeCurrencySymbols("€")).toBe("");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("should handle an empty string input", () => {
|
|
34
|
+
expect(removeCurrencySymbols("")).toBe("");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("should handle strings with special characters but no currency symbols", () => {
|
|
38
|
+
expect(removeCurrencySymbols("123-45")).toBe("123-45");
|
|
39
|
+
expect(removeCurrencySymbols("1,000.00")).toBe("1,000.00");
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { removeNonNumeric } from "../removeNonNumeric";
|
|
3
|
+
|
|
4
|
+
describe("removeNonNumeric", () => {
|
|
5
|
+
it("should remove all non-numeric characters from a string", () => {
|
|
6
|
+
expect(removeNonNumeric("abc123def456")).toBe("123456");
|
|
7
|
+
expect(removeNonNumeric("!@#123$%^456&*()")).toBe("123456");
|
|
8
|
+
expect(removeNonNumeric("a1b2c3")).toBe("123");
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it("should return an empty string if there are no numeric characters", () => {
|
|
12
|
+
expect(removeNonNumeric("abcdef")).toBe("");
|
|
13
|
+
expect(removeNonNumeric("!@#$%^&*()")).toBe("");
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("should return the same string if it contains only numeric characters", () => {
|
|
17
|
+
expect(removeNonNumeric("123456")).toBe("123456");
|
|
18
|
+
expect(removeNonNumeric("000123")).toBe("000123");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("should handle an empty string input", () => {
|
|
22
|
+
expect(removeNonNumeric("")).toBe("");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("should handle strings with spaces and remove them", () => {
|
|
26
|
+
expect(removeNonNumeric("123 456 789")).toBe("123456789");
|
|
27
|
+
expect(removeNonNumeric(" 1 2 3 ")).toBe("123");
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should handle strings with special characters and preserve only numbers", () => {
|
|
31
|
+
expect(removeNonNumeric("12-34.56/78")).toBe("12345678");
|
|
32
|
+
});
|
|
33
|
+
});
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { truncateLargeFields } from "../truncateLargeFields";
|
|
3
|
+
|
|
4
|
+
describe("truncateLargeFields", () => {
|
|
5
|
+
it("should truncate string fields exceeding the maxLength", () => {
|
|
6
|
+
const json = JSON.stringify({
|
|
7
|
+
name: "John",
|
|
8
|
+
description: "A very long description that exceeds the maximum length...",
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
const result = truncateLargeFields(json, 20);
|
|
12
|
+
expect(result).toBe(
|
|
13
|
+
'{"name":"John","description":"To large information: field as 58 characters"}'
|
|
14
|
+
);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("should handle nested objects and truncate large string fields", () => {
|
|
18
|
+
const json = JSON.stringify({
|
|
19
|
+
nested: {
|
|
20
|
+
details: "Another long string that needs truncation.",
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const result = truncateLargeFields(json, 20);
|
|
25
|
+
expect(result).toBe(
|
|
26
|
+
'{"nested":{"details":"To large information: field as 42 characters"}}'
|
|
27
|
+
);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should handle arrays and truncate large string fields within them", () => {
|
|
31
|
+
const json = JSON.stringify({
|
|
32
|
+
items: ["short", "A very long string in an array that exceeds the limit"],
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const result = truncateLargeFields(json, 20);
|
|
36
|
+
expect(result).toBe(
|
|
37
|
+
'{"items":["short","To large information: field as 53 characters"]}'
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("should not modify fields that are within the maxLength", () => {
|
|
42
|
+
const json = JSON.stringify({
|
|
43
|
+
name: "John",
|
|
44
|
+
description: "Short description",
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const result = truncateLargeFields(json, 20);
|
|
48
|
+
expect(result).toBe('{"name":"John","description":"Short description"}');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("should throw an error for invalid JSON strings", () => {
|
|
52
|
+
const invalidJson = "{ name: 'John' }"; // Invalid JSON
|
|
53
|
+
expect(() => truncateLargeFields(invalidJson, 20)).toThrow(
|
|
54
|
+
"Invalid JSON string"
|
|
55
|
+
);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("should handle empty JSON objects", () => {
|
|
59
|
+
const json = JSON.stringify({});
|
|
60
|
+
const result = truncateLargeFields(json, 20);
|
|
61
|
+
expect(result).toBe("{}");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("should handle empty arrays", () => {
|
|
65
|
+
const json = JSON.stringify([]);
|
|
66
|
+
const result = truncateLargeFields(json, 20);
|
|
67
|
+
expect(result).toBe("[]");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("should handle non-string fields without modification", () => {
|
|
71
|
+
const json = JSON.stringify({
|
|
72
|
+
number: 123,
|
|
73
|
+
boolean: true,
|
|
74
|
+
nullValue: null,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
const result = truncateLargeFields(json, 20);
|
|
78
|
+
expect(result).toBe('{"number":123,"boolean":true,"nullValue":null}');
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("should use the default maxLength of 1000 if not provided", () => {
|
|
82
|
+
const longString = "a".repeat(1500);
|
|
83
|
+
const json = JSON.stringify({ longField: longString });
|
|
84
|
+
const result = truncateLargeFields(json);
|
|
85
|
+
|
|
86
|
+
expect(result).toBe(
|
|
87
|
+
`{"longField":"To large information: field as 1500 characters"}`
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { validateCep } from "../validateCep";
|
|
3
|
+
|
|
4
|
+
describe("validateCep", () => {
|
|
5
|
+
it("should return true for a valid CEP with formatting", () => {
|
|
6
|
+
const validCep = "12345-678";
|
|
7
|
+
expect(validateCep(validCep)).toBe(true);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should return true for a valid CEP without formatting", () => {
|
|
11
|
+
const validCep = "12345678";
|
|
12
|
+
expect(validateCep(validCep)).toBe(true);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should return false for a CEP with letters", () => {
|
|
16
|
+
const invalidCep = "ABCDE-123";
|
|
17
|
+
expect(validateCep(invalidCep)).toBe(false);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should return false for a CEP with special characters", () => {
|
|
21
|
+
const invalidCep = "12345@678";
|
|
22
|
+
expect(validateCep(invalidCep)).toBe(false);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("should return false for a CEP with invalid length", () => {
|
|
26
|
+
const shortCep = "12345";
|
|
27
|
+
const longCep = "123456789";
|
|
28
|
+
expect(validateCep(shortCep)).toBe(false);
|
|
29
|
+
expect(validateCep(longCep)).toBe(false);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("should return false for an empty CEP", () => {
|
|
33
|
+
expect(validateCep("")).toBe(false);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("should return false for a null or undefined CEP", () => {
|
|
37
|
+
expect(validateCep(null as unknown as string)).toBe(false);
|
|
38
|
+
expect(validateCep(undefined as unknown as string)).toBe(false);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { validateCnpj } from "../validateCnpj";
|
|
3
|
+
|
|
4
|
+
describe("validateCnpj", () => {
|
|
5
|
+
it("should return true for a valid CNPJ", () => {
|
|
6
|
+
const validCnpj = "11.444.777/0001-61";
|
|
7
|
+
expect(validateCnpj(validCnpj)).toBe(true);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should return false for an invalid CNPJ", () => {
|
|
11
|
+
const invalidCnpj = "12.345.679/0001-95";
|
|
12
|
+
expect(validateCnpj(invalidCnpj)).toBe(false);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should return false for a CNPJ with all digits equal", () => {
|
|
16
|
+
const invalidCnpj = "11111111111111";
|
|
17
|
+
expect(validateCnpj(invalidCnpj)).toBe(false);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should return false for a CNPJ with invalid length", () => {
|
|
21
|
+
const shortCnpj = "12345678";
|
|
22
|
+
const longCnpj = "123456789012345";
|
|
23
|
+
expect(validateCnpj(shortCnpj)).toBe(false);
|
|
24
|
+
expect(validateCnpj(longCnpj)).toBe(false);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("should return false for an empty CNPJ", () => {
|
|
28
|
+
expect(validateCnpj("")).toBe(false);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("should return false for a null or undefined CNPJ", () => {
|
|
32
|
+
expect(validateCnpj(null as unknown as string)).toBe(false);
|
|
33
|
+
expect(validateCnpj(undefined as unknown as string)).toBe(false);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("should handle CNPJs with non-digit characters", () => {
|
|
37
|
+
const validCnpj = "11.444.777/0001-61";
|
|
38
|
+
const formattedCnpj = "11444777000161";
|
|
39
|
+
expect(validateCnpj(validCnpj)).toBe(true);
|
|
40
|
+
expect(validateCnpj(formattedCnpj)).toBe(true);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { validateCpf } from "../validateCpf";
|
|
3
|
+
|
|
4
|
+
describe("validateCpf", () => {
|
|
5
|
+
it("should return false for an empty CPF", () => {
|
|
6
|
+
expect(validateCpf("")).toBe(false);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it("should return false for a CPF with invalid length", () => {
|
|
10
|
+
expect(validateCpf("123.456")).toBe(false);
|
|
11
|
+
expect(validateCpf("123.456.789.123")).toBe(false);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("should return false for a CPF with all digits equal", () => {
|
|
15
|
+
expect(validateCpf("111.111.111-11")).toBe(false);
|
|
16
|
+
expect(validateCpf("222.222.222-22")).toBe(false);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("should return false for an invalid CPF", () => {
|
|
20
|
+
expect(validateCpf("123.456.789-19")).toBe(false);
|
|
21
|
+
expect(validateCpf("987.654.321-01")).toBe(false);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("should return true for a valid CPF", () => {
|
|
25
|
+
expect(validateCpf("111.444.777-35")).toBe(true);
|
|
26
|
+
expect(validateCpf("935.411.347-80")).toBe(true);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should ignore formatting characters and validate correctly", () => {
|
|
30
|
+
expect(validateCpf("11144477735")).toBe(true);
|
|
31
|
+
expect(validateCpf("935.411.347-80")).toBe(true);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should return false for a CPF with non-digit characters", () => {
|
|
35
|
+
expect(validateCpf("111.444.777-3a")).toBe(false);
|
|
36
|
+
expect(validateCpf("935.411.347-8x")).toBe(false);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { validateDate } from "../validateDate";
|
|
3
|
+
|
|
4
|
+
describe("validateDate", () => {
|
|
5
|
+
it("should return true for valid dates in DD/MM/YYYY format", () => {
|
|
6
|
+
expect(validateDate("31/12/2023")).toBe(true);
|
|
7
|
+
expect(validateDate("29/02/2024", { inputFormat: "DD/MM/YYYY" })).toBe(
|
|
8
|
+
true
|
|
9
|
+
); // Leap year
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("should return false for invalid dates in DD/MM/YYYY format", () => {
|
|
13
|
+
expect(validateDate("31/04/2023", { inputFormat: "DD/MM/YYYY" })).toBe(
|
|
14
|
+
false
|
|
15
|
+
); // April has 30 days
|
|
16
|
+
expect(validateDate("29/02/2023", { inputFormat: "DD/MM/YYYY" })).toBe(
|
|
17
|
+
false
|
|
18
|
+
); // Not a leap year
|
|
19
|
+
expect(validateDate("32/01/2023", { inputFormat: "DD/MM/YYYY" })).toBe(
|
|
20
|
+
false
|
|
21
|
+
); // Invalid day
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("should return true for valid dates in MM-DD-YYYY format", () => {
|
|
25
|
+
expect(validateDate("12-31-2023", { inputFormat: "MM-DD-YYYY" })).toBe(
|
|
26
|
+
true
|
|
27
|
+
);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should return false for invalid dates in MM-DD-YYYY format", () => {
|
|
31
|
+
expect(validateDate("04-31-2023", { inputFormat: "MM-DD-YYYY" })).toBe(
|
|
32
|
+
false
|
|
33
|
+
); // April has 30 days
|
|
34
|
+
expect(validateDate("02-29-2023", { inputFormat: "MM-DD-YYYY" })).toBe(
|
|
35
|
+
false
|
|
36
|
+
); // Not a leap year
|
|
37
|
+
expect(validateDate("13-01-2023", { inputFormat: "MM-DD-YYYY" })).toBe(
|
|
38
|
+
false
|
|
39
|
+
); // Invalid month
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("should return true for valid dates in YYYY-MM-DD format", () => {
|
|
43
|
+
expect(validateDate("2023-12-31", { inputFormat: "YYYY-MM-DD" })).toBe(
|
|
44
|
+
true
|
|
45
|
+
);
|
|
46
|
+
expect(validateDate("2024-02-29", { inputFormat: "YYYY-MM-DD" })).toBe(
|
|
47
|
+
true
|
|
48
|
+
); // Leap year
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("should return false for invalid dates in YYYY-MM-DD format", () => {
|
|
52
|
+
expect(validateDate("2023-04-31", { inputFormat: "YYYY-MM-DD" })).toBe(
|
|
53
|
+
false
|
|
54
|
+
); // April has 30 days
|
|
55
|
+
expect(validateDate("2023-02-29", { inputFormat: "YYYY-MM-DD" })).toBe(
|
|
56
|
+
false
|
|
57
|
+
); // Not a leap year
|
|
58
|
+
expect(validateDate("2023-13-01", { inputFormat: "YYYY-MM-DD" })).toBe(
|
|
59
|
+
false
|
|
60
|
+
); // Invalid month
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("should respect minYear and maxYear configuration", () => {
|
|
64
|
+
expect(validateDate("01/01/1899", { minYear: 1900 })).toBe(false); // Below minYear
|
|
65
|
+
expect(validateDate("01/01/1900", { minYear: 1900 })).toBe(true); // Equal to minYear
|
|
66
|
+
expect(validateDate("31/12/3001", { maxYear: 3000 })).toBe(false); // Above maxYear
|
|
67
|
+
expect(validateDate("31/12/3000", { maxYear: 3000 })).toBe(true); // Equal to maxYear
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("should throw an error for unsupported date formats", () => {
|
|
71
|
+
expect(() =>
|
|
72
|
+
validateDate("2023.12.31", { inputFormat: "YYYY.MM.DD" } as any)
|
|
73
|
+
).toThrow("Invalid date format");
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("should return false for completely invalid date strings", () => {
|
|
77
|
+
expect(validateDate("invalid-date")).toBe(false);
|
|
78
|
+
expect(validateDate("12345678")).toBe(false);
|
|
79
|
+
expect(validateDate("")).toBe(false);
|
|
80
|
+
});
|
|
81
|
+
});
|