Utils
import moment from 'moment'
export const hasExpired = (expiresIn?: string): boolean => {
return expiresIn === undefined || moment().isAfter(expiresIn)
}
export class CountryStateUtils {
static isValidPostcode = (postcode: string): boolean => {
const numericPostcode = Number(postcode)
return (
postcode.length === 4 && !isNaN(numericPostcode) && numericPostcode > 0 && Number.isInteger(numericPostcode)
)
}
static isValidNSWPostcode = (postcode: string): boolean => {
if (!CountryStateUtils.isValidPostcode(postcode)) {
return false
}
const numericPostcode = Number(postcode)
return (numericPostcode >= 1000 && numericPostcode <= 2999) || [3586, 3644, 3707].includes(numericPostcode)
}
}
export const isAustralianMobileNumber = (mobile: string): boolean => {
const newMobile = mobile.replace(/\s+/g, '')
const result =
newMobile.match(/^04[0-9]{8}$/) || newMobile.match(/^\+614[0-9]{8}$/) || newMobile.match(/^\+6104[0-9]{8}$/)
return !!result
}
export const isMobileNumber = (mobile: string): boolean => {
const newMobile = mobile.replace(/\s+/g, '')
const result = newMobile.match(/^(\+?)([0-9]){7,14}$/)
return !!result
}
export const isInternationalNumber = (mobile: string): boolean => {
const newMobile = mobile.replace(/\s+/g, '')
const result = newMobile.match(/^(\+)(?!61)([0-9]){7,14}$/)
return !!result
}
export const isValidEmail = (email: string): boolean => !!email.match(/^[\w-.]+@([\w-]+\.)+[\w]{2,4}$/)
export const removeAustralianMobilePrefix = (mobile: string): string => {
return mobile.replace(/^\+61\s*0?4/, '04')
}
export const isValidDOBFormat = (date: string): boolean => {
return /^(0[1-9]|[12][0-9]|3[01])[/](0[1-9]|1[012])[/]\d{4}$/.test(date)
}
export const isValidDateFormat = (date: string): boolean => {
return /^\d{4}[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])$/.test(date)
}
export const removeNonAlphaNumeric = (input: string): string => {
return input.replace(/[^0-9a-z]/gi, '')
}
export const isValidName = (name: string): boolean => {
return /^([A-Za-z]+['’\s-]*)+$/.test(name)
}
export const isOnlyNumeric = (input: string): boolean => {
return /^[0-9]+$|^$/.test(input)
}
export const getFullName = (firstName: string | null, familyName: string | null): string | undefined => {
if (firstName && familyName) {
return firstName + ' ' + familyName.toUpperCase()
}
if (familyName) {
return familyName.toUpperCase()
}
if (firstName) {
return firstName
}
return undefined
}
export const calculateHexWithOpacity = (color: string, opacity: number): string => {
const opacityHex = Math.round(opacity * 255)
.toString(16)
.toUpperCase()
.padStart(2, '0')
return color + opacityHex
}
export const getPropertyValue = <O extends object, K extends keyof O>(obj: O, key: K): O[K] | undefined => {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
return obj[key]
}
return undefined
}
export const isObjectsShallowEqual = (object1?: Record<string, any>, object2?: Record<string, any>): boolean => {
return JSON.stringify(object1) === JSON.stringify(object2)
}
export const generateNonce = (length = 8): string => {
let nonce = ''
const ALPHANUMERIC = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
for (let i = 0; i < length; i++) {
nonce += ALPHANUMERIC.charAt(Math.floor(Math.random() * ALPHANUMERIC.length) % ALPHANUMERIC.length)
}
return nonce
}
export const runWithTimeout = async <T>(task: () => Promise<T>, timeout: number): Promise<T> => {
const timer: Promise<string> = new Promise((resolve) => setTimeout(() => resolve('task_race_timeout'), timeout))
const taskResult = await Promise.race([task(), timer])
if (taskResult === 'task_race_timeout') {
throw new Error('Timeout')
}
return taskResult as T
}
export const sleep = (ms: number): Promise<NodeJS.Timeout> => {
return new Promise((resolve) => setTimeout(resolve, ms))
}
Comments
Post a Comment