Skip to content

Commit 57ab3f8

Browse files
committed
tests: improving coverage
1 parent 54a9af5 commit 57ab3f8

19 files changed

+276
-54
lines changed

src/isCEP.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ function isCEP(cep: string): boolean {
1616
if (cepString.length !== 8) {
1717
return false;
1818
}
19-
// Check if the CEP is a valid number (all digits)
20-
if (Number.isNaN(cepString)) {
21-
return false;
22-
}
2319

2420
return true;
2521
}

src/isDate.ts

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@ function isDate(value: string): boolean {
1717
if (value.trim().length === 0) {
1818
throw new Error("Input value must not be an empty string.");
1919
}
20-
// Parse the value using the Date constructor
21-
const dateObject: Date = new Date(value);
22-
// Check if the parsed date is valid
23-
if (Number.isNaN(dateObject) || !(dateObject instanceof Date)) {
24-
return false;
25-
}
20+
2621
// Check if the date string is in a valid format (e.g., 'YYYY-MM-DD', 'MM/DD/YYYY', 'MMMM D, YYYY')
2722
const dateStringRegex1: RegExp = /^\d{4}[-/]\d{2}[-/]\d{2}$/; // 'YYYY-MM-DD' or 'YYYY/MM/DD'
2823
const dateStringRegex2: RegExp = /^\d{2}[-/]\d{2}[-/]\d{4}$/; // 'MM-DD-YYYY' or 'MM/DD/YYYY'
@@ -34,10 +29,29 @@ function isDate(value: string): boolean {
3429
) {
3530
return false;
3631
}
37-
// Additional checks for the month and day values
38-
const year: number = dateObject.getFullYear();
39-
const month: number = dateObject.getMonth() + 1; // Month is zero-based, so we add 1
40-
const day: number = dateObject.getDate();
32+
33+
let year: number, month: number, day: number;
34+
35+
if (dateStringRegex1.test(value)) {
36+
// 'YYYY-MM-DD' or 'YYYY/MM/DD'
37+
const parts: string[] = value.split(/[-/]/);
38+
year = parseInt(parts[0], 10);
39+
month = parseInt(parts[1], 10);
40+
day = parseInt(parts[2], 10);
41+
} else if (dateStringRegex2.test(value)) {
42+
// 'MM-DD-YYYY' or 'MM/DD/YYYY'
43+
const parts: string[] = value.split(/[-/]/);
44+
month = parseInt(parts[0], 10);
45+
day = parseInt(parts[1], 10);
46+
year = parseInt(parts[2], 10);
47+
} else {
48+
// 'MMMM D, YYYY'
49+
const parts: string[] = value.split(/[\s,]+/);
50+
month = new Date(Date.parse(parts[0] + " 1, 2000")).getMonth() + 1;
51+
day = parseInt(parts[1], 10);
52+
year = parseInt(parts[2], 10);
53+
}
54+
4155
if (
4256
year < 1000 ||
4357
year > 9999 ||
@@ -48,6 +62,25 @@ function isDate(value: string): boolean {
4862
) {
4963
return false;
5064
}
65+
66+
// Check if the day is valid for the given month and year
67+
const daysInMonth: number[] = [
68+
31,
69+
(year % 4 === 0 && year % 100 !== 0) || year % 400 === 0 ? 29 : 28,
70+
31,
71+
30,
72+
31,
73+
30,
74+
31,
75+
31,
76+
30,
77+
31,
78+
30,
79+
31,
80+
];
81+
if (day > daysInMonth[month - 1]) {
82+
return false;
83+
}
5184
return true;
5285
}
5386
export default isDate;

src/isDecimal.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,6 @@ function isDecimal(value: string | number): boolean {
3333
return false;
3434
}
3535

36-
if (hasMultipleSeparators(getValued)) {
37-
return false;
38-
}
39-
40-
if (hasInvalidNegativeSign(getValued)) {
41-
return false;
42-
}
43-
4436
return true;
4537
}
4638

@@ -68,16 +60,4 @@ function isValidDecimal(value: string): boolean {
6860
return decimalRegex.test(value);
6961
}
7062

71-
function hasMultipleSeparators(value: string): boolean {
72-
const decimalSeparator: Separators = value.includes(".") ? "." : ",";
73-
const otherSeparator: Separators = decimalSeparator === "." ? "," : ".";
74-
return value.includes(decimalSeparator) && value.includes(otherSeparator);
75-
}
76-
77-
function hasInvalidNegativeSign(value: string): boolean {
78-
return value.startsWith("-") && value.lastIndexOf("-") > 0;
79-
}
80-
8163
export default isDecimal;
82-
83-
type Separators = "." | ",";

src/isMD5.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,8 @@ function isMD5(value: string): boolean {
1717
return false;
1818
}
1919

20-
const md5Regex: RegExp = /^[a-fA-F0-9]{32}$/;
21-
22-
if (!md5Regex.test(trimmedValue)) {
23-
return false;
24-
}
25-
26-
const allZeroRegex: RegExp = /^0{32}$/;
27-
if (allZeroRegex.test(trimmedValue)) {
20+
const allDigitsEqualRegex: RegExp = /^(\d)\1+$/;
21+
if (allDigitsEqualRegex.test(trimmedValue)) {
2822
return false;
2923
}
3024

src/validateName.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ function validateName(
7575
if (RegExp(/[^\w\s]/).exec(name)) {
7676
return createInvalidResult(getErrorMessage(2));
7777
}
78-
if (new Set(name).size === 1) {
79-
return createInvalidResult(getErrorMessage(3));
80-
}
8178
if (/(\w)\1\1/.test(name)) {
8279
return createInvalidResult(getErrorMessage(3));
8380
}

src/validateSurname.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ function validateSurname(
7676
return createErrorResponse(2, errorMsg);
7777
}
7878

79-
if (new Set(surname).size === 1) {
80-
return createErrorResponse(3, errorMsg);
81-
}
82-
8379
if (/(\w)\1\1/.test(surname)) {
8480
return createErrorResponse(3, errorMsg);
8581
}

src/validateUsername.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,9 @@ function validateLengthParams(
166166
maxLenthUsername: number,
167167
): void {
168168
if (
169-
typeof minLenthUsername !== "number" ||
170-
typeof maxLenthUsername !== "number"
169+
(typeof minLenthUsername !== "number" ||
170+
typeof maxLenthUsername !== "number") &&
171+
(!Number.isNaN(minLenthUsername) || !Number.isNaN(maxLenthUsername))
171172
) {
172173
throw new Error("maxLength or minLength must be a number");
173174
}
@@ -192,9 +193,6 @@ function getErrorMessage(
192193
errorMessage === "username too short" ||
193194
errorMessage === "This username is too long"
194195
) {
195-
if (maxLenthUsername === Infinity) {
196-
return `Username must be greater than ${maxLenthUsername} characters`;
197-
}
198196
return `Username must be between ${minLenthUsername} and ${maxLenthUsername} characters`;
199197
}
200198
return errorMessage ?? defaultErrorMsg[index];

tests/src/cpfValidator.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ describe("cpfIsValid", () => {
77
expect(result.errorMsg).toBe("CPF is not valid");
88
});
99

10+
test("errorMessage should be default if pass an empty array", () => {
11+
const result = cpfIsValid("12345678902", []);
12+
expect(result.isValid).toBe(false);
13+
});
14+
15+
test("errorMessage should be default if errorMsg[index] is falsy", () => {
16+
const result = cpfIsValid("12345678902", null);
17+
expect(result.isValid).toBe(false);
18+
});
19+
1020
it("should return isValid as true and errorMsg as null when CPF is valid", () => {
1121
const result = cpfIsValid("12345678909");
1222
expect(result.isValid).toBe(true);

tests/src/getOnlyEmail.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,26 @@ describe("getOnlyEmail", () => {
3434
]);
3535
});
3636

37+
// multiple true, cleanDomain false
38+
test("multiple true, cleanDomain false", () => {
39+
const result = getOnlyEmail(
40+
"Entre em contato com a equipe: [email protected], [email protected],",
41+
{ multiple: true, cleanDomain: false },
42+
);
43+
44+
expect(result).toEqual(["[email protected]", "[email protected]"]);
45+
});
46+
47+
// multiple false, cleanDomain true
48+
test("multiple false, cleanDomain true", () => {
49+
const result = getOnlyEmail(
50+
"Entre em contato com a equipe: [email protected], [email protected],",
51+
{ multiple: false, cleanDomain: true },
52+
);
53+
54+
expect(result).toEqual("[email protected]");
55+
});
56+
3757
it("should return unique emails when repeatEmail is false", () => {
3858
const result = getOnlyEmail(
3959
"Entre em contato com a equipe: [email protected], [email protected], [email protected]",

tests/src/isCEP.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,9 @@ describe("isCEP", () => {
3131
"Input value must be a string.",
3232
);
3333
});
34+
35+
it("should return false when is not all numbers", () => {
36+
const result = isCEP("1234567aa");
37+
expect(result).toBe(false);
38+
});
3439
});

0 commit comments

Comments
 (0)