const distinct = ( value , index , self ) => self . indexOf ( value ) === index ; const clean = ( array ) => array . filter ( ( n ) => n ) ; const shuffle = ( arr ) => { let currentIndex = arr . length ; let temporaryValue ; let randomIndex ; while ( currentIndex !== 0 ) { randomIndex = Math . floor ( Math . random ( ) * currentIndex ) ; currentIndex -= 1 ; temporaryValue = arr [ currentIndex ] ; arr [ currentIndex ] = arr [ randomIndex ] ; arr [ randomIndex ] = temporaryValue ; } return arr ; } ; export const arrayUtils = { clean , distinct , shuffle } ; const regexTargetEmail = /[a-zà-úA-ZÀ-Ú\s]+<(([^<>()[\]\\.,;:\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,}))>/ ; const isValidTargetEmail = ( value ) => regexTargetEmail . test ( value ) ; const regexTargetPhone = /^[a-zà-úA-ZÀ-Ú\s']+\s<(\+\d{2})?\d{6,7}-?\d{4}>$/ ; const isValidTargetPhone = ( value ) => regexTargetPhone . test ( value ) ; const PRESSURE_TYPE_EMAIL = 'email' ; const PRESSURE_TYPE_PHONE = 'phone' ; const getType = ( targets = [ ] ) => { if ( targets . length ) { if ( isValidTargetEmail ( targets [ 0 ] ) ) return PRESSURE_TYPE_EMAIL ; if ( isValidTargetPhone ( targets [ 0 ] ) ) return PRESSURE_TYPE_PHONE ; return 'invalid' ; } return 'invalid' ; } ; export const pressureUtils = { getType , PRESSURE_TYPE_EMAIL , PRESSURE_TYPE_PHONE , } ; const regexEmail = /^(([^<>()[\]\\.,;:\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,}))$/ ; const isValidEmail = ( email ) => regexEmail . test ( email ) ; const regexPhoneE164 = /^\+\d{12,13}$/ ; const isValidPhoneE164 = ( { code , invalid } ) => ( value ) => { const phoneE164 = /^\+/ . test ( value ) ? value : `+${ value }` ; const message = [ 11 , 12 ] . includes ( phoneE164 . length ) ? code : invalid ; return regexPhoneE164 . test ( phoneE164 ) ? undefined : message ; } ; const checkEmailTargetsList = ( message , targetList ) => ( value ) => targetList . some ( ( target ) => target . match ( `<${ value }>` ) ) ? message : undefined ; const checkPhoneTargetsList = ( message , targetList ) => ( value ) => targetList . some ( ( target ) => target . replace ( /\D/g , '' ) . match ( `${ value . replace ( /\D/g , '' ) }` ) ) ? message : undefined ; export const parseTarget = ( target ) => { const targetSplit = target . split ( '<' ) ; const valid = targetSplit . length === 2 ; return valid ? { name : targetSplit [ 0 ] ?. trim ( ) , value : targetSplit [ 1 ] ?. replace ( '>' , '' ) } : null ; } ; export const getEmailTarget = ( target ) => { const targetSplit = target . split ( '<' ) ; return targetSplit [ 1 ] ?. replace ( '>' , '' ) ; } ; export const getTargetList = ( targets ) : Array < string > => { if ( typeof targets === 'object' ) return targets ; return targets . split ( ';' ) . filter ( ( target ) => ! ! target . trim ( ) ) ; } ; export const validateUtils = { isValidEmail , isValidPhoneE164 , checkEmailTargetsList , checkPhoneTargetsList , } ;