@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,88 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { formatDate } from "../formatDate";
|
|
3
|
+
|
|
4
|
+
describe("formatDate", () => {
|
|
5
|
+
it("should format a Brazilian date to ISO format", () => {
|
|
6
|
+
const result = formatDate(
|
|
7
|
+
"25/12/2023",
|
|
8
|
+
"00:00:00",
|
|
9
|
+
"brazilianDate",
|
|
10
|
+
"YYYY-MM-DD"
|
|
11
|
+
);
|
|
12
|
+
expect(result).toBe("2023-12-25");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should format an ISO date to Brazilian format", () => {
|
|
16
|
+
const result = formatDate(
|
|
17
|
+
"2023-12-25",
|
|
18
|
+
"00:00:00",
|
|
19
|
+
"isoDate",
|
|
20
|
+
"DD/MM/YYYY"
|
|
21
|
+
);
|
|
22
|
+
expect(result).toBe("25/12/2023");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("should format a date with time components", () => {
|
|
26
|
+
const result = formatDate(
|
|
27
|
+
"2023-12-25",
|
|
28
|
+
"14:30:00",
|
|
29
|
+
"isoDate",
|
|
30
|
+
"YYYY-MM-DD hh:mm:ss"
|
|
31
|
+
);
|
|
32
|
+
expect(result).toBe("2023-12-25 14:30:00");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("should apply timezone adjustment when formatting", () => {
|
|
36
|
+
const result = formatDate(
|
|
37
|
+
"2023-12-25",
|
|
38
|
+
"14:30:00",
|
|
39
|
+
"isoDate",
|
|
40
|
+
"YYYY-MM-DD hh:mm:ss",
|
|
41
|
+
-3
|
|
42
|
+
);
|
|
43
|
+
expect(result).toBe("2023-12-25 11:30:00");
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("should handle single-digit days and months in Brazilian format", () => {
|
|
47
|
+
const result = formatDate(
|
|
48
|
+
"5/1/2023",
|
|
49
|
+
"00:00:00",
|
|
50
|
+
"brazilianDate",
|
|
51
|
+
"YYYY-MM-DD"
|
|
52
|
+
);
|
|
53
|
+
expect(result).toBe("2023-01-05");
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("should handle single-digit days and months in ISO format", () => {
|
|
57
|
+
const result = formatDate("2023-1-5", "00:00:00", "isoDate", "DD/MM/YYYY");
|
|
58
|
+
expect(result).toBe("05/01/2023");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("should handle leap years correctly", () => {
|
|
62
|
+
const result = formatDate(
|
|
63
|
+
"29/02/2024",
|
|
64
|
+
"00:00:00",
|
|
65
|
+
"brazilianDate",
|
|
66
|
+
"YYYY-MM-DD"
|
|
67
|
+
);
|
|
68
|
+
expect(result).toBe("2024-02-29");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("should throw an error for invalid input format", () => {
|
|
72
|
+
expect(() =>
|
|
73
|
+
formatDate("25/12/2023", "00:00:00", "invalidFormat" as any, "YYYY-MM-DD")
|
|
74
|
+
).toThrow("Invalid input format");
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("should throw an error for invalid date string", () => {
|
|
78
|
+
expect(() =>
|
|
79
|
+
formatDate("invalid-date", "00:00:00", "brazilianDate", "YYYY-MM-DD")
|
|
80
|
+
).toThrow("Invalid date");
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("should throw an error for invalid time string", () => {
|
|
84
|
+
expect(() =>
|
|
85
|
+
formatDate("25/12/2023", "invalid-time", "brazilianDate", "YYYY-MM-DD")
|
|
86
|
+
).toThrow("Invalid date");
|
|
87
|
+
});
|
|
88
|
+
});
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { formatJsonObject } from "../formatJsonObject";
|
|
3
|
+
|
|
4
|
+
describe("formatJsonObject", () => {
|
|
5
|
+
it("should format a simple JSON object", () => {
|
|
6
|
+
const obj = { name: "John", age: 30 };
|
|
7
|
+
const result = formatJsonObject(obj, 0);
|
|
8
|
+
expect(result).toBe(`{
|
|
9
|
+
"name": "John",
|
|
10
|
+
"age": 30
|
|
11
|
+
}`);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("should format a nested JSON object", () => {
|
|
15
|
+
const obj = { name: "John", details: { age: 30, city: "New York" } };
|
|
16
|
+
const result = formatJsonObject(obj, 0);
|
|
17
|
+
expect(result).toBe(`{
|
|
18
|
+
"name": "John",
|
|
19
|
+
"details": {
|
|
20
|
+
"age": 30,
|
|
21
|
+
"city": "New York"
|
|
22
|
+
}
|
|
23
|
+
}`);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("should format a JSON array", () => {
|
|
27
|
+
const obj = ["apple", "banana", "cherry"];
|
|
28
|
+
const result = formatJsonObject(obj, 0);
|
|
29
|
+
expect(result).toBe(`[
|
|
30
|
+
"apple",
|
|
31
|
+
"banana",
|
|
32
|
+
"cherry"
|
|
33
|
+
]`);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("should format a nested JSON array", () => {
|
|
37
|
+
const obj = { fruits: ["apple", "banana", "cherry"] };
|
|
38
|
+
const result = formatJsonObject(obj, 0);
|
|
39
|
+
expect(result).toBe(`{
|
|
40
|
+
"fruits": [
|
|
41
|
+
"apple",
|
|
42
|
+
"banana",
|
|
43
|
+
"cherry"
|
|
44
|
+
]
|
|
45
|
+
}`);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("should format a primitive value", () => {
|
|
49
|
+
const result = formatJsonObject(42, 0);
|
|
50
|
+
expect(result).toBe("42");
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("should format a string value", () => {
|
|
54
|
+
const result = formatJsonObject("hello", 0);
|
|
55
|
+
expect(result).toBe('"hello"');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("should format a JSON string", () => {
|
|
59
|
+
const jsonString = '{"name":"John","age":30}';
|
|
60
|
+
const result = formatJsonObject(jsonString, 0);
|
|
61
|
+
expect(result).toBe(`{
|
|
62
|
+
"name": "John",
|
|
63
|
+
"age": 30
|
|
64
|
+
}`);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("should handle invalid JSON strings as plain strings", () => {
|
|
68
|
+
const invalidJson = "{name:John}";
|
|
69
|
+
const result = formatJsonObject(invalidJson, 0);
|
|
70
|
+
expect(result).toBe('"{name:John}"');
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("should handle null values", () => {
|
|
74
|
+
const result = formatJsonObject(null, 0);
|
|
75
|
+
expect(result).toBe("null");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("should handle empty objects", () => {
|
|
79
|
+
const result = formatJsonObject({}, 0);
|
|
80
|
+
expect(result).toBe(`{}`);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("should handle empty arrays", () => {
|
|
84
|
+
const result = formatJsonObject([], 0);
|
|
85
|
+
expect(result).toBe(`[]`);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from "vitest";
|
|
2
|
+
import { formatJsonString } from "../formatJsonString";
|
|
3
|
+
|
|
4
|
+
describe("formatJsonString", () => {
|
|
5
|
+
it("should format a valid JSON string", () => {
|
|
6
|
+
const jsonString =
|
|
7
|
+
'{"name":"John","age":30,"hobbies":["reading","gaming"]}';
|
|
8
|
+
const result = formatJsonString(jsonString);
|
|
9
|
+
expect(result).toBe(`{
|
|
10
|
+
"name": "John",
|
|
11
|
+
"age": 30,
|
|
12
|
+
"hobbies": [
|
|
13
|
+
"reading",
|
|
14
|
+
"gaming"
|
|
15
|
+
]
|
|
16
|
+
}`);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("should handle a nested JSON string", () => {
|
|
20
|
+
const jsonString =
|
|
21
|
+
'{"user":{"name":"John","details":{"age":30,"city":"New York"}}}';
|
|
22
|
+
const result = formatJsonString(jsonString);
|
|
23
|
+
expect(result).toBe(`{
|
|
24
|
+
"user": {
|
|
25
|
+
"name": "John",
|
|
26
|
+
"details": {
|
|
27
|
+
"age": 30,
|
|
28
|
+
"city": "New York"
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}`);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should handle an empty JSON object", () => {
|
|
35
|
+
const jsonString = "{}";
|
|
36
|
+
const result = formatJsonString(jsonString);
|
|
37
|
+
expect(result).toBe(`{}`);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("should handle an empty JSON array", () => {
|
|
41
|
+
const jsonString = "[]";
|
|
42
|
+
const result = formatJsonString(jsonString);
|
|
43
|
+
expect(result).toBe(`[]`);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("should handle a JSON array", () => {
|
|
47
|
+
const jsonString = '["apple","banana","cherry"]';
|
|
48
|
+
const result = formatJsonString(jsonString);
|
|
49
|
+
expect(result).toBe(`[
|
|
50
|
+
"apple",
|
|
51
|
+
"banana",
|
|
52
|
+
"cherry"
|
|
53
|
+
]`);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("should return an empty string for invalid JSON", () => {
|
|
57
|
+
const invalidJsonString = '{"name":"John", "age":30,';
|
|
58
|
+
expect(() => formatJsonString(invalidJsonString)).toThrow();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("should log an error for invalid JSON", () => {
|
|
62
|
+
const invalidJsonString = '{"name":"John", "age":30,';
|
|
63
|
+
expect(() => formatJsonString(invalidJsonString)).toThrow();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("should handle a primitive JSON value", () => {
|
|
67
|
+
const jsonString = "42";
|
|
68
|
+
const result = formatJsonString(jsonString);
|
|
69
|
+
expect(result).toBe("42");
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("should handle a JSON string value", () => {
|
|
73
|
+
const jsonString = '"hello"';
|
|
74
|
+
const result = formatJsonString(jsonString);
|
|
75
|
+
expect(result).toBe('"hello"');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("should handle null JSON value", () => {
|
|
79
|
+
const jsonString = "null";
|
|
80
|
+
const result = formatJsonString(jsonString);
|
|
81
|
+
expect(result).toBe("null");
|
|
82
|
+
});
|
|
83
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { formatToCep } from "../formatToCep";
|
|
3
|
+
|
|
4
|
+
describe("formatToCep", () => {
|
|
5
|
+
it("should format a valid CEP with only numeric characters", () => {
|
|
6
|
+
const result = formatToCep("12345678");
|
|
7
|
+
expect(result).toBe("12345-678");
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should format a valid CEP with non-numeric characters", () => {
|
|
11
|
+
const result = formatToCep("12345-678");
|
|
12
|
+
expect(result).toBe("12345-678");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should throw an error for invalid CEP length", () => {
|
|
16
|
+
expect(() => formatToCep("1234")).toThrow("Invalid CEP format");
|
|
17
|
+
expect(() => formatToCep("123456789")).toThrow("Invalid CEP format");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should handle empty input and throw an error", () => {
|
|
21
|
+
expect(() => formatToCep("")).toThrow("Invalid CEP format");
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("should ignore non-numeric characters and format correctly", () => {
|
|
25
|
+
const result = formatToCep("12345-678abc");
|
|
26
|
+
expect(result).toBe("12345-678");
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should format a CEP with extra spaces", () => {
|
|
30
|
+
const result = formatToCep(" 12345678 ");
|
|
31
|
+
expect(result).toBe("12345-678");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should throw an error for completely invalid input", () => {
|
|
35
|
+
expect(() => formatToCep("abcdefg")).toThrow("Invalid CEP format");
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { formatToCnpj } from "../formatToCnpj";
|
|
3
|
+
|
|
4
|
+
describe("formatToCnpj", () => {
|
|
5
|
+
it("should format a valid CNPJ with only numeric characters", () => {
|
|
6
|
+
const result = formatToCnpj("12345678000195");
|
|
7
|
+
expect(result).toBe("12.345.678/0001-95");
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should format a valid CNPJ with non-numeric characters", () => {
|
|
11
|
+
const result = formatToCnpj("12.345.678/0001-95");
|
|
12
|
+
expect(result).toBe("12.345.678/0001-95");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should throw an error for invalid CNPJ length", () => {
|
|
16
|
+
expect(() => formatToCnpj("123")).toThrow("Invalid CNPJ length");
|
|
17
|
+
expect(() => formatToCnpj("123456789012345")).toThrow(
|
|
18
|
+
"Invalid CNPJ length"
|
|
19
|
+
);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("should handle empty input and throw an error", () => {
|
|
23
|
+
expect(() => formatToCnpj("")).toThrow("Invalid CNPJ length");
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("should ignore non-numeric characters and format correctly", () => {
|
|
27
|
+
const result = formatToCnpj("12.345.678/0001-95abc");
|
|
28
|
+
expect(result).toBe("12.345.678/0001-95");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("should format a CNPJ with extra spaces", () => {
|
|
32
|
+
const result = formatToCnpj(" 12345678000195 ");
|
|
33
|
+
expect(result).toBe("12.345.678/0001-95");
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { formatToCpf } from "../formatToCpf";
|
|
3
|
+
|
|
4
|
+
describe("formatToCpf", () => {
|
|
5
|
+
it("should format a valid CPF with only numeric characters", () => {
|
|
6
|
+
const result = formatToCpf("12345678909");
|
|
7
|
+
expect(result).toBe("123.456.789-09");
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should format a valid CPF with non-numeric characters", () => {
|
|
11
|
+
const result = formatToCpf("123.456.789-09");
|
|
12
|
+
expect(result).toBe("123.456.789-09");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should throw an error for invalid CPF length", () => {
|
|
16
|
+
expect(() => formatToCpf("12345")).toThrow("Invalid CPF format");
|
|
17
|
+
expect(() => formatToCpf("123456789012")).toThrow("Invalid CPF format");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should handle empty input and throw an error", () => {
|
|
21
|
+
expect(() => formatToCpf("")).toThrow("Invalid CPF format");
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("should ignore non-numeric characters and format correctly", () => {
|
|
25
|
+
const result = formatToCpf("123.456.789-09abc");
|
|
26
|
+
expect(result).toBe("123.456.789-09");
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should format a CPF with extra spaces", () => {
|
|
30
|
+
const result = formatToCpf(" 12345678909 ");
|
|
31
|
+
expect(result).toBe("123.456.789-09");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should throw an error for completely invalid input", () => {
|
|
35
|
+
expect(() => formatToCpf("abcdefg")).toThrow("Invalid CPF format");
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { formatToCpfCnpj } from "../formatToCpfCnpj";
|
|
3
|
+
|
|
4
|
+
describe("formatToCpfCnpj", () => {
|
|
5
|
+
it("should format a valid CPF with non-numeric characters", () => {
|
|
6
|
+
const result = formatToCpfCnpj("123.456.789-09");
|
|
7
|
+
expect(result).toBe("123.456.789-09");
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should format a valid CPF with only numeric characters", () => {
|
|
11
|
+
const result = formatToCpfCnpj("12345678909");
|
|
12
|
+
expect(result).toBe("123.456.789-09");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should format a valid CNPJ with non-numeric characters", () => {
|
|
16
|
+
const result = formatToCpfCnpj("12.345.678/0001-95");
|
|
17
|
+
expect(result).toBe("12.345.678/0001-95");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should format a valid CNPJ with only numeric characters", () => {
|
|
21
|
+
const result = formatToCpfCnpj("12345678000195");
|
|
22
|
+
expect(result).toBe("12.345.678/0001-95");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("should throw an error for invalid CPF or CNPJ length", () => {
|
|
26
|
+
expect(() => formatToCpfCnpj("123")).toThrow("Invalid CPF or CNPJ length");
|
|
27
|
+
expect(() => formatToCpfCnpj("123456")).toThrow(
|
|
28
|
+
"Invalid CPF or CNPJ length"
|
|
29
|
+
);
|
|
30
|
+
expect(() => formatToCpfCnpj("123456789012345")).toThrow(
|
|
31
|
+
"Invalid CPF or CNPJ length"
|
|
32
|
+
);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("should handle empty input and throw an error", () => {
|
|
36
|
+
expect(() => formatToCpfCnpj("")).toThrow("Invalid CPF or CNPJ length");
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("should ignore non-numeric characters and format correctly", () => {
|
|
40
|
+
const result = formatToCpfCnpj("123.456.789-09abc");
|
|
41
|
+
expect(result).toBe("123.456.789-09");
|
|
42
|
+
});
|
|
43
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { formatToCurrency } from "../formatToCurrency";
|
|
3
|
+
|
|
4
|
+
describe("formatToCurrency", () => {
|
|
5
|
+
it("should format a number to USD currency with prefix", () => {
|
|
6
|
+
const result = formatToCurrency(1234.56, "USD", { showPrefix: true });
|
|
7
|
+
expect(result).toBe("$1,234.56");
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should format a number to USD currency without prefix", () => {
|
|
11
|
+
const result = formatToCurrency(1234.56, "USD", { showPrefix: false });
|
|
12
|
+
expect(result).toBe("1,234.56");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should format a number to BRL currency with prefix", () => {
|
|
16
|
+
const result = formatToCurrency(1234.56, "BRL", { showPrefix: true });
|
|
17
|
+
expect(result).toBe("R$ 1.234,56");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should format a number to BRL currency without prefix", () => {
|
|
21
|
+
const result = formatToCurrency(1234.56, "BRL", { showPrefix: false });
|
|
22
|
+
expect(result).toBe("1.234,56");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("should format a number to EUR currency with prefix", () => {
|
|
26
|
+
const result = formatToCurrency(1234.56, "EUR", { showPrefix: true });
|
|
27
|
+
expect(result).toEqual("1.234,56 €");
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should format a number to EUR currency without prefix", () => {
|
|
31
|
+
const result = formatToCurrency(1234.56, "EUR", { showPrefix: false });
|
|
32
|
+
expect(result).toBe("1.234,56");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("should handle zero value correctly", () => {
|
|
36
|
+
const result = formatToCurrency(0, "USD", { showPrefix: true });
|
|
37
|
+
expect(result).toBe("$0.00");
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("should handle negative values correctly", () => {
|
|
41
|
+
const result = formatToCurrency(-1234.56, "USD", { showPrefix: true });
|
|
42
|
+
expect(result).toBe("-$1,234.56");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("should throw an error for unsupported currency codes", () => {
|
|
46
|
+
expect(() =>
|
|
47
|
+
formatToCurrency(1234.56, "XYZ" as any, { showPrefix: true })
|
|
48
|
+
).toThrow("Unsupported currency code");
|
|
49
|
+
});
|
|
50
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { formatToEllipsis } from "../formatToEllipsis";
|
|
3
|
+
|
|
4
|
+
describe("formatToEllipsis", () => {
|
|
5
|
+
it("should truncate text and append ellipsis if it exceeds the maxLength", () => {
|
|
6
|
+
const result = formatToEllipsis("Hello, world!", 5);
|
|
7
|
+
expect(result).toBe("Hello...");
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should return the original text if it does not exceed the maxLength", () => {
|
|
11
|
+
const result = formatToEllipsis("Hello", 10);
|
|
12
|
+
expect(result).toBe("Hello");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should handle empty strings", () => {
|
|
16
|
+
const result = formatToEllipsis("", 5);
|
|
17
|
+
expect(result).toBe("");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should handle maxLength of 0", () => {
|
|
21
|
+
const result = formatToEllipsis("Hello, world!", 0);
|
|
22
|
+
expect(result).toBe("...");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("should handle maxLength greater than the text length", () => {
|
|
26
|
+
const result = formatToEllipsis("Short text", 20);
|
|
27
|
+
expect(result).toBe("Short text");
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should handle text exactly equal to maxLength", () => {
|
|
31
|
+
const result = formatToEllipsis("Exact", 5);
|
|
32
|
+
expect(result).toBe("Exact");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("should handle special characters in the text", () => {
|
|
36
|
+
const result = formatToEllipsis("Olá, mundo!", 4);
|
|
37
|
+
expect(result).toBe("Olá...");
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("should handle whitespace correctly", () => {
|
|
41
|
+
const result = formatToEllipsis("Hello world", 6);
|
|
42
|
+
expect(result).toBe("Hello...");
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { formatToHiddenDigits } from "../formatToHiddenDigits";
|
|
3
|
+
|
|
4
|
+
describe("formatToHiddenDigits", () => {
|
|
5
|
+
it("should hide the first 3 digits by default", () => {
|
|
6
|
+
const result = formatToHiddenDigits("123-456-7890", {});
|
|
7
|
+
expect(result).toBe("***-456-7890");
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should hide the first n digits when range is a positive number", () => {
|
|
11
|
+
const result = formatToHiddenDigits("123-456-7890", { range: 5 });
|
|
12
|
+
expect(result).toBe("***-**6-7890");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should hide the last n digits when range is a negative number", () => {
|
|
16
|
+
const result = formatToHiddenDigits("123-456-7890", { range: -4 });
|
|
17
|
+
expect(result).toBe("123-456-****");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should hide digits within a specific range", () => {
|
|
21
|
+
const result = formatToHiddenDigits("123-456-7890", { range: [4, 6] });
|
|
22
|
+
expect(result).toBe("123-***-7890");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("should use a custom hider character", () => {
|
|
26
|
+
const result = formatToHiddenDigits("123-456-7890", {
|
|
27
|
+
range: 3,
|
|
28
|
+
hider: "#",
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
expect(result).toBe("###-456-7890");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should handle strings with no digits", () => {
|
|
35
|
+
const result = formatToHiddenDigits("abc-def-ghij", { range: 3 });
|
|
36
|
+
expect(result).toBe("abc-def-ghij");
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("should handle empty strings", () => {
|
|
40
|
+
const result = formatToHiddenDigits("", { range: 3 });
|
|
41
|
+
expect(result).toBe("");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("should handle ranges larger than the number of digits", () => {
|
|
45
|
+
const result = formatToHiddenDigits("123-456", { range: 10 });
|
|
46
|
+
expect(result).toBe("***-***");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("should handle ranges that do not overlap with any digits", () => {
|
|
50
|
+
const result = formatToHiddenDigits("123-456-7890", { range: [11, 12] });
|
|
51
|
+
expect(result).toBe("123-456-7890");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("should hide all digits when range covers all digits", () => {
|
|
55
|
+
const result = formatToHiddenDigits("123-456-7890", { range: [1, 10] });
|
|
56
|
+
expect(result).toBe("***-***-****");
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { formatToPhone } from "../formatToPhone";
|
|
3
|
+
|
|
4
|
+
describe("formatToPhone", () => {
|
|
5
|
+
it("should format a Brazilian phone number with 9 digits", () => {
|
|
6
|
+
const result = formatToPhone("+55 11912345678");
|
|
7
|
+
expect(result).toBe("(11) 91234-5678");
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should format a Brazilian phone number with 8 digits", () => {
|
|
11
|
+
const result = formatToPhone("+55 1123456789");
|
|
12
|
+
expect(result).toBe("(11) 2345-6789");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should format a US phone number with a prefix", () => {
|
|
16
|
+
const result = formatToPhone("+1-408 4567890");
|
|
17
|
+
expect(result).toBe("(408) 456-7890");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should format a US phone number without a prefix", () => {
|
|
21
|
+
expect(() => formatToPhone("+1 4567890")).toThrow("Invalid country code");
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("should throw an error for invalid phone number format", () => {
|
|
25
|
+
expect(() => formatToPhone("+55")).toThrow("Invalid phone number format");
|
|
26
|
+
expect(() => formatToPhone("+55-11")).toThrow(
|
|
27
|
+
"Invalid phone number format"
|
|
28
|
+
);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("should throw an error for unrecognized country code", () => {
|
|
32
|
+
expect(() => formatToPhone("+99 123456789")).toThrow(
|
|
33
|
+
"Invalid country code"
|
|
34
|
+
);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("should throw an error for unrecognized country code or prefix", () => {
|
|
38
|
+
expect(() => formatToPhone("+1-999 123456789")).toThrow(
|
|
39
|
+
"Invalid country code or prefix"
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("should handle empty input and throw an error", () => {
|
|
44
|
+
expect(() => formatToPhone("")).toThrow("Invalid phone number format");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("should format a phone number with special characters removed", () => {
|
|
48
|
+
expect(() => formatToPhone("+55 11 91234-5678")).toThrow(
|
|
49
|
+
"Invalid phone number format. Expected format: +<countryCode>-<optionalPrefix> <phoneNumber>"
|
|
50
|
+
);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("should format a phone number with spaces and special characters", () => {
|
|
54
|
+
expect(() => formatToPhone("+55 11 91234 5678")).toThrow(
|
|
55
|
+
"Invalid phone number format. Expected format: +<countryCode>-<optionalPrefix> <phoneNumber>"
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { generateColorByString } from "../generateColorByString";
|
|
3
|
+
|
|
4
|
+
describe("generateColorByString", () => {
|
|
5
|
+
it("should generate a consistent hex color for the same input string", () => {
|
|
6
|
+
const color1 = generateColorByString("example");
|
|
7
|
+
const color2 = generateColorByString("example");
|
|
8
|
+
expect(color1).toBe(color2);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it("should generate different hex colors for different input strings", () => {
|
|
12
|
+
const color1 = generateColorByString("example1");
|
|
13
|
+
const color2 = generateColorByString("example2");
|
|
14
|
+
expect(color1).not.toBe(color2);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("should return a valid hex color code", () => {
|
|
18
|
+
const color = generateColorByString("test");
|
|
19
|
+
expect(color).toMatch(/^#[0-9a-fA-F]{6}$/);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("should handle empty strings and return a valid hex color", () => {
|
|
23
|
+
const color = generateColorByString("");
|
|
24
|
+
expect(color).toMatch(/^#[0-9a-fA-F]{6}$/);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("should handle strings with special characters", () => {
|
|
28
|
+
const color = generateColorByString("!@#$%^&*()");
|
|
29
|
+
expect(color).toMatch(/^#[0-9a-fA-F]{6}$/);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("should handle long strings and return a valid hex color", () => {
|
|
33
|
+
const longString = "a".repeat(1000);
|
|
34
|
+
const color = generateColorByString(longString);
|
|
35
|
+
expect(color).toMatch(/^#[0-9a-fA-F]{6}$/);
|
|
36
|
+
});
|
|
37
|
+
});
|