initial commit

This commit is contained in:
Inshal
2024-10-25 01:02:11 +05:00
commit 6e65bc3a62
1710 changed files with 273609 additions and 0 deletions

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,46 @@
import { isToday } from './index'
export const avatarText = value => {
if (!value)
return ''
const nameArray = value.split(' ')
return nameArray.map(word => word.charAt(0).toUpperCase()).join('')
}
// TODO: Try to implement this: https://twitter.com/fireship_dev/status/1565424801216311297
export const kFormatter = num => {
const regex = /\B(?=(\d{3})+(?!\d))/g
return Math.abs(num) > 9999 ? `${Math.sign(num) * +((Math.abs(num) / 1000).toFixed(1))}k` : Math.abs(num).toFixed(0).replace(regex, ',')
}
/**
* Format and return date in Humanize format
* Intl docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/format
* Intl Constructor: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat
* @param {String} value date to format
* @param {Intl.DateTimeFormatOptions} formatting Intl object to format with
*/
export const formatDate = (value, formatting = { month: 'short', day: 'numeric', year: 'numeric' }) => {
if (!value)
return value
return new Intl.DateTimeFormat('en-US', formatting).format(new Date(value))
}
/**
* Return short human friendly month representation of date
* Can also convert date to only time if date is of today (Better UX)
* @param {String} value date to format
* @param {Boolean} toTimeForCurrentDay Shall convert to time if day is today/current
*/
export const formatDateToMonthShort = (value, toTimeForCurrentDay = true) => {
const date = new Date(value)
let formatting = { month: 'short', day: 'numeric' }
if (toTimeForCurrentDay && isToday(date))
formatting = { hour: 'numeric', minute: 'numeric' }
return new Intl.DateTimeFormat('en-US', formatting).format(new Date(value))
}
export const prefixWithPlus = value => value > 0 ? `+${value}` : value

View File

@@ -0,0 +1,31 @@
// 👉 IsEmpty
export const isEmpty = value => {
if (value === null || value === undefined || value === '')
return true
return !!(Array.isArray(value) && value.length === 0)
}
// 👉 IsNullOrUndefined
export const isNullOrUndefined = value => {
return value === null || value === undefined
}
// 👉 IsEmptyArray
export const isEmptyArray = arr => {
return Array.isArray(arr) && arr.length === 0
}
// 👉 IsObject
export const isObject = obj => obj !== null && !!obj && typeof obj === 'object' && !Array.isArray(obj)
export const isToday = date => {
const today = new Date()
return (
/* eslint-disable operator-linebreak */
date.getDate() === today.getDate() &&
date.getMonth() === today.getMonth() &&
date.getFullYear() === today.getFullYear()
/* eslint-enable */
)
}

View File

@@ -0,0 +1,208 @@
import { isEmpty, isEmptyArray, isNullOrUndefined } from './index';
// 👉 Required Validator
export const requiredValidator = value => {
if (isNullOrUndefined(value) || isEmptyArray(value) || value === false)
return 'This field is required'
return !!String(value).trim().length || 'This field is required'
}
export const cardNumberValidator = value => {
// Adjust the regex based on your credit card number pattern
const cardNumberPattern = /^(\d{14}|\d{15}|\d{16})$/;
return cardNumberPattern.test(value) || 'Invalid credit card number';
};
export const requiredGender = (value) => !!value || 'Gender is required'
export const requiredLicenseNumber = (value) => !!value || 'Medical License Number is required'
export const requiredYearsofExperience = (value) => !!value || 'Years of Experience is required'
export const requiredSpecialty = (value) => !!value || 'Practice or Provider of Specialty is required'
export const requiredFirstName = (value) => !!value || 'First Name is required'
export const requiredZip = (value) => !!value || 'Zip Code is required'
export const expiryValidator = value => {
// Check if the format is MM/YY
const formatRegex = /^(0[1-9]|1[0-2])\/\d{2}$/;
if (!formatRegex.test(value)) {
return 'Invalid date format. Please use MM/YY';
}
// Check if the date is not expired (assuming the current date is 01/24 for example)
const currentDate = new Date();
const currentYear = currentDate.getFullYear() % 100;
const currentMonth = currentDate.getMonth() + 1;
const [inputMonth, inputYear] = value.split('/').map(Number);
if (inputYear < currentYear || (inputYear === currentYear && inputMonth < currentMonth)) {
return 'The card has expired';
}
return true;
}
export const cvvValidator = value => {
return /^\d{3}$/.test(value) || 'Must be a 3-digit number';
}
export const requiredAddress = value => {
if (isNullOrUndefined(value) || isEmptyArray(value) || value === false)
return 'Address is required'
return !!String(value).trim().length || 'Address is required'
}
export const requiredLocation = value => {
if (isNullOrUndefined(value) || isEmptyArray(value) || value === false)
return 'Location is required'
return !!String(value).trim().length || 'Location is required'
}
export const requiredCity = value => {
if (isNullOrUndefined(value) || isEmptyArray(value) || value === false)
return 'City is required'
return !!String(value).trim().length || 'City is required'
}
export const requiredPassword = value => {
if (isNullOrUndefined(value) || isEmptyArray(value) || value === false)
return 'Password field is required'
return !!String(value).trim().length || 'Password field is required'
}
export const requiredConfirm = value => {
if (isNullOrUndefined(value) || isEmptyArray(value) || value === false)
return 'Confirm Password field is required'
return !!String(value).trim().length || ' Confirm Password field is required'
}
export const requiredName = value => {
if (isNullOrUndefined(value) || isEmptyArray(value) || value === false)
return 'Name field is required'
return !!String(value).trim().length || 'Name is required'
}
export const requiredLastName = value => {
if (isNullOrUndefined(value) || isEmptyArray(value) || value === false)
return 'Last Name field is required'
return !!String(value).trim().length || ' Last Name is required'
}
export const requiredPhone = value => {
if (isNullOrUndefined(value) || isEmptyArray(value) || value === false)
return 'Phone is required'
return !!String(value).trim().length || ' Phone is required'
}
export const requiredEmail = value => {
if (isNullOrUndefined(value) || isEmptyArray(value) || value === false)
return 'Email field is required'
return !!String(value).trim().length || 'Email is required'
}
export const requiredState = value => {
if (isNullOrUndefined(value) || isEmptyArray(value) || value === false)
return 'State field is required'
return !!String(value).trim().length || 'State is required'
}
export const requiredDate = value => {
if (isNullOrUndefined(value) || isEmptyArray(value) || value === false)
return 'Date of Birth field is required'
return !!String(value).trim().length || 'Date of Birth is required'
}
// 👉 Email Validator
export const emailValidator = value => {
if (isEmpty(value))
return true
const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
if (Array.isArray(value))
return value.every(val => re.test(String(val))) || 'The Email field must be a valid email'
return re.test(String(value)) || 'The Email field must be a valid email'
}
// 👉 Password Validator
export const passwordValidator = password => {
const regExp = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%&*()]).{8,}/
const validPassword = regExp.test(password)
return (
// eslint-disable-next-line operator-linebreak
validPassword ||
'Field must contain at least one uppercase, lowercase, special character and digit with min 8 chars')
}
// 👉 Confirm Password Validator
export const confirmedValidator = (value, target) => value === target || 'The Confirm Password field confirmation does not match'
// 👉 Between Validator
export const betweenValidator = (value, min, max) => {
const valueAsNumber = Number(value)
return (Number(min) <= valueAsNumber && Number(max) >= valueAsNumber) || `Enter number between ${min} and ${max}`
}
// 👉 Integer Validator
export const integerValidator = value => {
if (isEmpty(value))
return true
if (Array.isArray(value))
return value.every(val => /^-?[0-9]+$/.test(String(val))) || 'This field must be an integer'
return /^-?[0-9]+$/.test(String(value)) || 'This field must be an integer'
}
// 👉 Regex Validator
export const regexValidator = (value, regex) => {
if (isEmpty(value))
return true
let regeX = regex
if (typeof regeX === 'string')
regeX = new RegExp(regeX)
if (Array.isArray(value))
return value.every(val => regexValidator(val, regeX))
return regeX.test(String(value)) || 'The Regex field format is invalid'
}
// 👉 Alpha Validator
export const alphaValidator = value => {
if (isEmpty(value))
return true
return /^[A-Z]*$/i.test(String(value)) || 'The Alpha field may only contain alphabetic characters'
}
// 👉 URL Validator
export const urlValidator = value => {
if (isEmpty(value))
return true
const re = /^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/
return re.test(String(value)) || 'URL is invalid'
}
// 👉 Length Validator
export const lengthValidator = (value, length) => {
if (isEmpty(value))
return true
return String(value).length === length || `The Min Character field must be at least ${length} characters`
}
// 👉 Alpha-dash Validator
export const alphaDashValidator = value => {
if (isEmpty(value))
return true
const valueAsString = String(value)
return /^[0-9A-Z_-]*$/i.test(valueAsString) || 'All Character are not valid'
}
export const validUSAPhone = value => {
if (isEmpty(value))
return true
const valueAsString = String(value)
return /^\(\d{3}\)\s\d{3}-\d{4}$/i.test(valueAsString) || 'Phone are not valid'
}