Skip to content

Commit

Permalink
improving code formatting with sonar lint and adding more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Oct 3, 2024
1 parent fb68a37 commit 54a9af5
Show file tree
Hide file tree
Showing 25 changed files with 641 additions and 429 deletions.
1 change: 0 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import cpfIsValid from "./src/cpfValidator";
import cnpjIsValid from "./src/cnpjValidator";
import getOnlyEmail from "./src/getOnlyEmail";
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "multiform-validator",
"version": "2.2.4",
"version": "2.2.5",
"description": "Javascript library made to validate, several form fields, such as: email, images, phone, password, cpf etc.",
"main": "./dist/cjs/index.cjs",
"module": "./dist/esm/index.mjs",
Expand Down
6 changes: 3 additions & 3 deletions src/cnpjValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ function cnpjIsValid(
// caso contrario retorna um ERRO
if (errorMsg) {
if (!Array.isArray(errorMsg)) throw new Error("Must be an Array");
for (let index: number = 0; index < errorMsg.length; index += 1) {
if (errorMsg[index] != null && typeof errorMsg[index] !== "string") {
for (const element of errorMsg) {
if (element != null && typeof element !== "string") {
throw new TypeError(
"All values within the array must be strings or null/undefined.",
);
Expand All @@ -71,7 +71,7 @@ function cnpjIsValid(
// Função interna para obter a mensagem de erro
function getErrorMessage(index: number): string {
const errorMessage: string | null = errorMsg ? errorMsg[index] : null;
return errorMessage != null ? errorMessage : defaultErrorMsg[index];
return errorMessage ?? defaultErrorMsg[index];
}

if (!cnpj) {
Expand Down
6 changes: 3 additions & 3 deletions src/cpfValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ function cpfIsValid(

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

function getErrorMessage(index: number): string {
const errorMessage: string | null = errorMsg ? errorMsg[index] : null;
return errorMessage != null ? errorMessage : defaultErrorMsg[index];
return errorMessage ?? defaultErrorMsg[index];
}

if (!cpf) {
Expand Down
11 changes: 8 additions & 3 deletions src/isDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ function isDate(value: string): boolean {
return false;
}
// Check if the date string is in a valid format (e.g., 'YYYY-MM-DD', 'MM/DD/YYYY', 'MMMM D, YYYY')
const dateStringRegex: RegExp =
/^(?:\d{4}[-/]\d{2}[-/]\d{2}|\d{2}[-/]\d{2}[-/]\d{4}|[A-Za-z]+\s\d{1,2}, \d{4})$/;
if (!dateStringRegex.test(value)) {
const dateStringRegex1: RegExp = /^\d{4}[-/]\d{2}[-/]\d{2}$/; // 'YYYY-MM-DD' or 'YYYY/MM/DD'
const dateStringRegex2: RegExp = /^\d{2}[-/]\d{2}[-/]\d{4}$/; // 'MM-DD-YYYY' or 'MM/DD/YYYY'
const dateStringRegex3: RegExp = /^[A-Za-z]+\s\d{1,2}, \d{4}$/; // 'MMMM D, YYYY'
if (
!dateStringRegex1.test(value) &&
!dateStringRegex2.test(value) &&
!dateStringRegex3.test(value)
) {
return false;
}
// Additional checks for the month and day values
Expand Down
82 changes: 49 additions & 33 deletions src/isDecimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,72 @@
*/
function isDecimal(value: string | number): boolean {
let getValued: string | number = value;
if (typeof getValued === "number" && Number.isNaN(getValued)) {
throw new TypeError("Input value must not be NaN.");
}

if (typeof getValued === "number" && !isFinite(getValued)) {
throw new TypeError("Input value must not be Infinity, -Infinity or NaN.");
}
validateInput(getValued);

if (typeof getValued !== "string") {
if (typeof getValued === "number") {
if (Number.isInteger(getValued)) {
return false;
}
getValued = getValued.toString();
} else {
throw new TypeError("Input value must be a string or a number.");
if (typeof getValued === "number") {
if (Number.isInteger(getValued)) {
return false;
}
getValued = getValued.toString();
}

if (getValued.trim().length === 0) {
throw new Error("Input value must not be an empty string.");
}

const integerRegex: RegExp = /^\d+$/;
if (integerRegex.test(getValued)) {
if (isInteger(getValued)) {
return false;
}

// Regular expression to validate decimal numbers
const decimalRegex: RegExp = /^[-+]?(?:\d+(?:[,.]\d*)?|\d*[,.]\d+)$/;
if (!decimalRegex.test(getValued)) {
if (!isValidDecimal(getValued)) {
return false;
}
// Check for multiple decimal separators
const decimalSeparator: Separators = getValued.includes(".") ? "." : ",";
const otherSeparator: Separators = decimalSeparator === "." ? "," : ".";
if (
getValued.includes(decimalSeparator) &&
getValued.includes(otherSeparator)
) {

if (hasMultipleSeparators(getValued)) {
return false;
}
// Additional checks for negative sign
if (getValued.startsWith("-")) {
// Ensure the negative sign is only at the beginning and not elsewhere
if (getValued.lastIndexOf("-") > 0) {
return false;
}

if (hasInvalidNegativeSign(getValued)) {
return false;
}

return true;
}

function validateInput(value: string | number): void {
if (typeof value === "number" && Number.isNaN(value)) {
throw new TypeError("Input value must not be NaN.");
}

if (typeof value === "number" && !isFinite(value)) {
throw new TypeError("Input value must not be Infinity, -Infinity or NaN.");
}

if (typeof value !== "string" && typeof value !== "number") {
throw new TypeError("Input value must be a string or a number.");
}
}

function isInteger(value: string): boolean {
const integerRegex: RegExp = /^\d+$/;
return integerRegex.test(value);
}

function isValidDecimal(value: string): boolean {
const decimalRegex: RegExp = /^[-+]?(?:\d+(?:[,.]\d*)?|\d*[,.]\d+)$/;
return decimalRegex.test(value);
}

function hasMultipleSeparators(value: string): boolean {
const decimalSeparator: Separators = value.includes(".") ? "." : ",";
const otherSeparator: Separators = decimalSeparator === "." ? "," : ".";
return value.includes(decimalSeparator) && value.includes(otherSeparator);
}

function hasInvalidNegativeSign(value: string): boolean {
return value.startsWith("-") && value.lastIndexOf("-") > 0;
}

export default isDecimal;

type Separators = "." | ",";
33 changes: 18 additions & 15 deletions src/isEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,38 @@ function isEmail(email: string): boolean {

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

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

if (Number(email[0])) return false;
if (Number(email[0])) {
return false;
}

if (!regex.test(email)) return false;
if (!regex.test(email)) {
return false;
}

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

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

const depoisDoUltimoPonto: number = email.lastIndexOf(".");

if (Number(email[depoisDoArroba])) return false;

if (Number(email[depoisDoUltimoPonto])) return false;

if (email.substring(0, antesDoArroba).includes("..")) return false;
if (Number(email[depoisDoArroba])) {
return false;
}

if (email.substring(0, antesDoArroba).endsWith(".")) return false;
if (email.substring(0, antesDoArroba).includes("..")) {
return false;
}

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/isTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function isTime(time: string): boolean {
}
// 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"
const timeRegex: RegExp =
/^(?:2[0-3]|1\d|0?[0-9]):[0-5]\d(?::[0-5]\d)?(?: [APap][Mm])?$/;
/^(?:2[0-3]|1\d|0?\d):[0-5]\d(?::[0-5]\d)?(?: [APap][Mm])?$/;

return timeRegex.test(time);
}
Expand Down
8 changes: 4 additions & 4 deletions src/isValidTxt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ export default function isValidTxt(fileBuffer: Buffer): boolean {
if (fileBuffer.length === 0) {
return false;
}
for (let i: number = 0; i < fileBuffer.length; i++) {
for (const element of fileBuffer) {
if (
(fileBuffer[i] < 0x20 || fileBuffer[i] > 0x7e) &&
fileBuffer[i] !== 0x0a &&
fileBuffer[i] !== 0x0d
(element < 0x20 || element > 0x7e) &&
element !== 0x0a &&
element !== 0x0d
) {
return false;
}
Expand Down
Loading

0 comments on commit 54a9af5

Please sign in to comment.