Skip to content

Commit 54a9af5

Browse files
committed
improving code formatting with sonar lint and adding more tests
1 parent fb68a37 commit 54a9af5

25 files changed

+641
-429
lines changed

index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
21
import cpfIsValid from "./src/cpfValidator";
32
import cnpjIsValid from "./src/cnpjValidator";
43
import getOnlyEmail from "./src/getOnlyEmail";

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "multiform-validator",
3-
"version": "2.2.4",
3+
"version": "2.2.5",
44
"description": "Javascript library made to validate, several form fields, such as: email, images, phone, password, cpf etc.",
55
"main": "./dist/cjs/index.cjs",
66
"module": "./dist/esm/index.mjs",

src/cnpjValidator.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ function cnpjIsValid(
5959
// caso contrario retorna um ERRO
6060
if (errorMsg) {
6161
if (!Array.isArray(errorMsg)) throw new Error("Must be an Array");
62-
for (let index: number = 0; index < errorMsg.length; index += 1) {
63-
if (errorMsg[index] != null && typeof errorMsg[index] !== "string") {
62+
for (const element of errorMsg) {
63+
if (element != null && typeof element !== "string") {
6464
throw new TypeError(
6565
"All values within the array must be strings or null/undefined.",
6666
);
@@ -71,7 +71,7 @@ function cnpjIsValid(
7171
// Função interna para obter a mensagem de erro
7272
function getErrorMessage(index: number): string {
7373
const errorMessage: string | null = errorMsg ? errorMsg[index] : null;
74-
return errorMessage != null ? errorMessage : defaultErrorMsg[index];
74+
return errorMessage ?? defaultErrorMsg[index];
7575
}
7676

7777
if (!cnpj) {

src/cpfValidator.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ function cpfIsValid(
3131

3232
if (errorMsg) {
3333
if (!Array.isArray(errorMsg)) throw new TypeError("Must be an Array");
34-
for (let index: number = 0; index < errorMsg.length; index += 1) {
35-
if (errorMsg[index] != null && typeof errorMsg[index] !== "string") {
34+
for (const element of errorMsg) {
35+
if (element != null && typeof element !== "string") {
3636
throw new TypeError(
3737
"All values within the array must be strings or null/undefined.",
3838
);
@@ -42,7 +42,7 @@ function cpfIsValid(
4242

4343
function getErrorMessage(index: number): string {
4444
const errorMessage: string | null = errorMsg ? errorMsg[index] : null;
45-
return errorMessage != null ? errorMessage : defaultErrorMsg[index];
45+
return errorMessage ?? defaultErrorMsg[index];
4646
}
4747

4848
if (!cpf) {

src/isDate.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@ function isDate(value: string): boolean {
2424
return false;
2525
}
2626
// Check if the date string is in a valid format (e.g., 'YYYY-MM-DD', 'MM/DD/YYYY', 'MMMM D, YYYY')
27-
const dateStringRegex: RegExp =
28-
/^(?:\d{4}[-/]\d{2}[-/]\d{2}|\d{2}[-/]\d{2}[-/]\d{4}|[A-Za-z]+\s\d{1,2}, \d{4})$/;
29-
if (!dateStringRegex.test(value)) {
27+
const dateStringRegex1: RegExp = /^\d{4}[-/]\d{2}[-/]\d{2}$/; // 'YYYY-MM-DD' or 'YYYY/MM/DD'
28+
const dateStringRegex2: RegExp = /^\d{2}[-/]\d{2}[-/]\d{4}$/; // 'MM-DD-YYYY' or 'MM/DD/YYYY'
29+
const dateStringRegex3: RegExp = /^[A-Za-z]+\s\d{1,2}, \d{4}$/; // 'MMMM D, YYYY'
30+
if (
31+
!dateStringRegex1.test(value) &&
32+
!dateStringRegex2.test(value) &&
33+
!dateStringRegex3.test(value)
34+
) {
3035
return false;
3136
}
3237
// Additional checks for the month and day values

src/isDecimal.ts

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,56 +12,72 @@
1212
*/
1313
function isDecimal(value: string | number): boolean {
1414
let getValued: string | number = value;
15-
if (typeof getValued === "number" && Number.isNaN(getValued)) {
16-
throw new TypeError("Input value must not be NaN.");
17-
}
18-
19-
if (typeof getValued === "number" && !isFinite(getValued)) {
20-
throw new TypeError("Input value must not be Infinity, -Infinity or NaN.");
21-
}
15+
validateInput(getValued);
2216

23-
if (typeof getValued !== "string") {
24-
if (typeof getValued === "number") {
25-
if (Number.isInteger(getValued)) {
26-
return false;
27-
}
28-
getValued = getValued.toString();
29-
} else {
30-
throw new TypeError("Input value must be a string or a number.");
17+
if (typeof getValued === "number") {
18+
if (Number.isInteger(getValued)) {
19+
return false;
3120
}
21+
getValued = getValued.toString();
3222
}
23+
3324
if (getValued.trim().length === 0) {
3425
throw new Error("Input value must not be an empty string.");
3526
}
3627

37-
const integerRegex: RegExp = /^\d+$/;
38-
if (integerRegex.test(getValued)) {
28+
if (isInteger(getValued)) {
3929
return false;
4030
}
4131

42-
// Regular expression to validate decimal numbers
43-
const decimalRegex: RegExp = /^[-+]?(?:\d+(?:[,.]\d*)?|\d*[,.]\d+)$/;
44-
if (!decimalRegex.test(getValued)) {
32+
if (!isValidDecimal(getValued)) {
4533
return false;
4634
}
47-
// Check for multiple decimal separators
48-
const decimalSeparator: Separators = getValued.includes(".") ? "." : ",";
49-
const otherSeparator: Separators = decimalSeparator === "." ? "," : ".";
50-
if (
51-
getValued.includes(decimalSeparator) &&
52-
getValued.includes(otherSeparator)
53-
) {
35+
36+
if (hasMultipleSeparators(getValued)) {
5437
return false;
5538
}
56-
// Additional checks for negative sign
57-
if (getValued.startsWith("-")) {
58-
// Ensure the negative sign is only at the beginning and not elsewhere
59-
if (getValued.lastIndexOf("-") > 0) {
60-
return false;
61-
}
39+
40+
if (hasInvalidNegativeSign(getValued)) {
41+
return false;
6242
}
43+
6344
return true;
6445
}
46+
47+
function validateInput(value: string | number): void {
48+
if (typeof value === "number" && Number.isNaN(value)) {
49+
throw new TypeError("Input value must not be NaN.");
50+
}
51+
52+
if (typeof value === "number" && !isFinite(value)) {
53+
throw new TypeError("Input value must not be Infinity, -Infinity or NaN.");
54+
}
55+
56+
if (typeof value !== "string" && typeof value !== "number") {
57+
throw new TypeError("Input value must be a string or a number.");
58+
}
59+
}
60+
61+
function isInteger(value: string): boolean {
62+
const integerRegex: RegExp = /^\d+$/;
63+
return integerRegex.test(value);
64+
}
65+
66+
function isValidDecimal(value: string): boolean {
67+
const decimalRegex: RegExp = /^[-+]?(?:\d+(?:[,.]\d*)?|\d*[,.]\d+)$/;
68+
return decimalRegex.test(value);
69+
}
70+
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+
6581
export default isDecimal;
6682

6783
type Separators = "." | ",";

src/isEmail.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,35 +35,38 @@ function isEmail(email: string): boolean {
3535

3636
// Check if email starts with a special character
3737
const startsWithSpecialChar: RegExp = /^[^a-zA-Z0-9]/;
38-
if (startsWithSpecialChar.test(email)) return false;
38+
if (startsWithSpecialChar.test(email)) {
39+
return false;
40+
}
3941

4042
const regex: RegExp = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
4143

42-
if (Number(email[0])) return false;
44+
if (Number(email[0])) {
45+
return false;
46+
}
4347

44-
if (!regex.test(email)) return false;
48+
if (!regex.test(email)) {
49+
return false;
50+
}
4551

4652
const antesDoArroba: number = email.indexOf("@");
4753

4854
const depoisDoArroba: number = email.indexOf("@") + 1;
4955

50-
const depoisDoUltimoPonto: number = email.lastIndexOf(".");
51-
52-
if (Number(email[depoisDoArroba])) return false;
53-
54-
if (Number(email[depoisDoUltimoPonto])) return false;
55-
56-
if (email.substring(0, antesDoArroba).includes("..")) return false;
56+
if (Number(email[depoisDoArroba])) {
57+
return false;
58+
}
5759

58-
if (email.substring(0, antesDoArroba).endsWith(".")) return false;
60+
if (email.substring(0, antesDoArroba).includes("..")) {
61+
return false;
62+
}
5963

60-
const parts: string[] = email.split(".");
61-
if (parts.length > 2 && parts[parts.length - 2] === parts[parts.length - 3]) {
64+
if (email.substring(0, antesDoArroba).endsWith(".")) {
6265
return false;
6366
}
6467

65-
// Check if there is more than one @
66-
if (email.split("@").length - 1 > 1) {
68+
const parts: string[] = email.split(".");
69+
if (parts.length > 2 && parts[parts.length - 2] === parts[parts.length - 3]) {
6770
return false;
6871
}
6972

src/isTime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function isTime(time: string): boolean {
1616
}
1717
// Regular expression to validate time in the format "hh:mm" or "hh:mm AM/PM" or "hh:mm:ss" or "hh:mm:ss AM/PM"
1818
const timeRegex: RegExp =
19-
/^(?:2[0-3]|1\d|0?[0-9]):[0-5]\d(?::[0-5]\d)?(?: [APap][Mm])?$/;
19+
/^(?:2[0-3]|1\d|0?\d):[0-5]\d(?::[0-5]\d)?(?: [APap][Mm])?$/;
2020

2121
return timeRegex.test(time);
2222
}

src/isValidTxt/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ export default function isValidTxt(fileBuffer: Buffer): boolean {
77
if (fileBuffer.length === 0) {
88
return false;
99
}
10-
for (let i: number = 0; i < fileBuffer.length; i++) {
10+
for (const element of fileBuffer) {
1111
if (
12-
(fileBuffer[i] < 0x20 || fileBuffer[i] > 0x7e) &&
13-
fileBuffer[i] !== 0x0a &&
14-
fileBuffer[i] !== 0x0d
12+
(element < 0x20 || element > 0x7e) &&
13+
element !== 0x0a &&
14+
element !== 0x0d
1515
) {
1616
return false;
1717
}

0 commit comments

Comments
 (0)