66 * 64 = @
77 * 46 = .
88 */
9- const ValidChars = { min : 97 , max : 122 , specials : [ 95 , 45 , 64 , 46 ] } ;
9+ const ValidChars = { min : 97 , max : 122 , specials : [ 95 , 45 , 64 , 46 , 43 ] } ;
1010
1111const ValidCharsNum = { min : 48 , max : 57 } ;
1212
@@ -81,13 +81,24 @@ const IsValidCountry = (country: string): boolean => {
8181 return true ;
8282} ;
8383
84+ const IsValidNick = ( email : string ) : boolean => {
85+ const nick = email . split ( '@' ) ?. [ 0 ] ;
86+ return (
87+ ! nick . startsWith ( '+' ) &&
88+ ! nick . endsWith ( '+' ) &&
89+ ! nick . startsWith ( '-' ) &&
90+ ! nick . endsWith ( '-' )
91+ ) ;
92+ } ;
93+
8494const IsValidDomain = ( email : string ) : boolean => {
8595 const domain = email . split ( '@' ) ;
8696 const parts = domain [ 1 ] . split ( '.' ) ;
8797 if ( parts . length === 1 || parts . length > 3 ) return false ;
8898
8999 const isInValidStartAndEnd =
90100 domain [ 1 ] . startsWith ( '-' ) || domain [ 1 ] . endsWith ( '-' ) ;
101+
91102 if ( isInValidStartAndEnd ) return false ;
92103
93104 const isLessThanMax = email . length <= 64 ;
@@ -117,24 +128,22 @@ const IsValidDomain = (email: string): boolean => {
117128 * @returns true if is a valid email and false if not
118129 */
119130export const IsValidEmail = ( email : string ) : boolean => {
120- const AtCode = 64 ;
121131 const isString : boolean = typeof email === 'string' ;
122132 if ( ! isString ) return false ;
123133
124134 const trimEmail = email . trim ( ) . toLowerCase ( ) ;
125135
126136 const isValidFirsChar : boolean =
127137 IsAlphabet ( trimEmail [ 0 ] ) || IsNumber ( trimEmail [ 0 ] ) ;
138+
128139 if ( ! isValidFirsChar ) return false ;
129140
130141 const hasOnlyOneAt : boolean =
131- trimEmail . split ( '' ) . reduce ( ( total , current ) : number => {
132- const isAtCode = GetCharCode ( current ) === AtCode ;
133- if ( isAtCode ) return ( total = total + 1 ) ;
134- return total ;
135- } , 0 ) === 1 ;
142+ trimEmail . split ( '' ) . filter ( ( char ) => char === '@' ) . length === 1 ;
143+ const hasOnlyOnePlus : boolean =
144+ trimEmail . split ( '' ) . filter ( ( char ) => char === '+' ) . length > 1 ;
136145
137- if ( ! hasOnlyOneAt ) return false ;
146+ if ( ! hasOnlyOneAt || hasOnlyOnePlus ) return false ;
138147
139148 const isValidLength = HasValidLength ( trimEmail ) ;
140149
@@ -144,6 +153,8 @@ export const IsValidEmail = (email: string): boolean => {
144153
145154 if ( ! isValidDomain ) return false ;
146155
156+ if ( ! IsValidNick ( trimEmail ) ) return false ;
157+
147158 const hasInvalidChar = trimEmail
148159 . split ( '' )
149160 . map ( ( char ) : boolean => IsValidChar ( char ) )
0 commit comments