Skip to content

Commit

Permalink
Merge pull request #118 from tahmid-saj/dev-general-cleanup-1
Browse files Browse the repository at this point in the history
regex constants
  • Loading branch information
tahmid-saj authored Jun 16, 2024
2 parents d56bca6 + 4289e7d commit fc2c530
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
7 changes: 4 additions & 3 deletions src/utils/validations/calories-burned.validations.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { errorOnInvalidTrackedDate } from "../errors/calories-burned.errors"
import { REGEX_PATTERNS } from "./regex.constants"

// calories burned validations

// context

export const validateSearchActivity = (trackedDayInfo) => {
// number
if (!(/^[0-9]*$/.test(String(trackedDayInfo.weightPounds))) || Number(trackedDayInfo.weightPounds) < 0 ||
!(/^[0-9]*$/.test(String(trackedDayInfo.durationMinutes))) || Number(trackedDayInfo.durationMinutes) < 0) {
if (!(REGEX_PATTERNS.floatNumbers.test(String(trackedDayInfo.weightPounds))) || Number(trackedDayInfo.weightPounds) < 0 ||
!(REGEX_PATTERNS.floatNumbers.test(String(trackedDayInfo.durationMinutes))) || Number(trackedDayInfo.durationMinutes) < 0) {
return true
}

Expand All @@ -29,7 +30,7 @@ export const validateFilterActivityDates = (filterConditions) => {
const today = new Date()

// number
if (!(/^[0-9]*$/.test(String(filterConditions.durationMinutes))) || Number(filterConditions.durationMinutes) < 0) {
if (!(REGEX_PATTERNS.floatNumbers.test(String(filterConditions.durationMinutes))) || Number(filterConditions.durationMinutes) < 0) {
return true
}

Expand Down
21 changes: 11 additions & 10 deletions src/utils/validations/nutrition-tracker.validations.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { errorOnTrackedDayExists, errorOnInvalidMacronutrientInputs,
errorOnInvalidMicronutrientInput, errorOnEmptyMicronutrients,
errorOnDayNotTracked, errorOnStartDateBeforeEndDate } from "../errors/nutrition-tracker.errors";
import { REGEX_PATTERNS } from "./regex.constants";

// nutrition tracker validation functions

Expand All @@ -17,13 +18,13 @@ export const validateAddDayTracked = (nutritionTrackedDays, trackedDayInfo) => {
}

// check if macronutrients data types are valid
if (!(/^[0-9]*$/.test(String(trackedDayInfo.calories))) ||
if (!(REGEX_PATTERNS.floatNumbers.test(String(trackedDayInfo.calories))) ||
Number(trackedDayInfo.calories) < 0 ||
!(/^[0-9]*$/.test(String(trackedDayInfo.macronutrients.carbohydrates))) ||
!(REGEX_PATTERNS.floatNumbers.test(String(trackedDayInfo.macronutrients.carbohydrates))) ||
Number(trackedDayInfo.macronutrients.carbohydrates) < 0 ||
!(/^[0-9]*$/.test(String(trackedDayInfo.macronutrients.protein))) ||
!(REGEX_PATTERNS.floatNumbers.test(String(trackedDayInfo.macronutrients.protein))) ||
Number(trackedDayInfo.macronutrients.protein) < 0 ||
!(/^[0-9]*$/.test(String(trackedDayInfo.macronutrients.fat))) ||
!(REGEX_PATTERNS.floatNumbers.test(String(trackedDayInfo.macronutrients.fat))) ||
Number(trackedDayInfo.macronutrients.fat) < 0) {

errorOnInvalidMacronutrientInputs();
Expand All @@ -41,7 +42,7 @@ export const validateAddDayTracked = (nutritionTrackedDays, trackedDayInfo) => {
return true;
}

if (!(/^[0-9]*$/.test(String(micronutrient.amount))) ||
if (!(REGEX_PATTERNS.floatNumbers.test(String(micronutrient.amount))) ||
Number(micronutrient.amount) <= 0) {

errorOnInvalidMicronutrientInput();
Expand Down Expand Up @@ -86,13 +87,13 @@ export const validateUpdateDayTracked = (nutritionTrackedDays, updatedTrackedDay
}

// check if macronutrients data types are valid
if (!(/^[0-9]*$/.test(String(updatedTrackedDayInfo.calories))) ||
if (!(REGEX_PATTERNS.floatNumbers.test(String(updatedTrackedDayInfo.calories))) ||
Number(updatedTrackedDayInfo.calories) < 0 ||
!(/^[0-9]*$/.test(String(updatedTrackedDayInfo.macronutrients.carbohydrates))) ||
!(REGEX_PATTERNS.floatNumbers.test(String(updatedTrackedDayInfo.macronutrients.carbohydrates))) ||
Number(updatedTrackedDayInfo.macronutrients.carbohydrates) < 0 ||
!(/^[0-9]*$/.test(String(updatedTrackedDayInfo.macronutrients.protein))) ||
!(REGEX_PATTERNS.floatNumbers.test(String(updatedTrackedDayInfo.macronutrients.protein))) ||
Number(updatedTrackedDayInfo.macronutrients.protein) < 0 ||
!(/^[0-9]*$/.test(String(updatedTrackedDayInfo.macronutrients.fat))) ||
!(REGEX_PATTERNS.floatNumbers.test(String(updatedTrackedDayInfo.macronutrients.fat))) ||
Number(updatedTrackedDayInfo.macronutrients.fat) < 0) {

errorOnInvalidMacronutrientInputs();
Expand All @@ -110,7 +111,7 @@ export const validateUpdateDayTracked = (nutritionTrackedDays, updatedTrackedDay
return true;
}

if (!(/^[0-9]*$/.test(String(micronutrient.amount))) ||
if (!(REGEX_PATTERNS.floatNumbers.test(String(micronutrient.amount))) ||
Number(micronutrient.amount) <= 0) {

errorOnInvalidMicronutrientInput();
Expand Down
8 changes: 8 additions & 0 deletions src/utils/validations/regex.constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// regex constants

export const REGEX_PATTERNS = {
names: /^[a-zA-Z0-9_.-]+$/,
descriptions: /^[a-zA-Z0-9_.\- ]+[a-zA-Z0-9_.\-]*[a-zA-Z0-9_.\- ]+$|^[a-zA-Z0-9_.\-]+$/,
floatNumbers: /^(?!0\d)\d*(\.\d{1,2})?$/,
currency: /^[A-Z]*$/
}

0 comments on commit fc2c530

Please sign in to comment.