From a57323b5d9a07ad286c8b1b440d819bb09e97ae6 Mon Sep 17 00:00:00 2001 From: Cristian Baciu Date: Mon, 19 Oct 2020 16:08:17 +0300 Subject: [PATCH] Modify ValidateEmail.js to accept all suffixes --- String/ValidateEmail.js | 6 ++---- String/{ => test}/ValidateEmail.test.js | 6 +++++- 2 files changed, 7 insertions(+), 5 deletions(-) rename String/{ => test}/ValidateEmail.test.js (75%) diff --git a/String/ValidateEmail.js b/String/ValidateEmail.js index af2ee3f09d..1e1ace2f15 100644 --- a/String/ValidateEmail.js +++ b/String/ValidateEmail.js @@ -2,6 +2,7 @@ function that takes a string input and return either it is true of false a valid email address e.g.: mahfoudh.arous@gmail.com -> true + e.g.: mahfoudh.arous@helsinki.edu -> true e.g.: mahfoudh.arous.com ->false */ @@ -9,11 +10,8 @@ const validateEmail = (str) => { if (str === '' || str === null) { throw new TypeError('Email Address String Null or Empty.') } - if (str.startsWith('@') === true || !str.includes('@') || !str.endsWith('.com')) { - return false - } - return true + return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(str) } export { validateEmail } diff --git a/String/ValidateEmail.test.js b/String/test/ValidateEmail.test.js similarity index 75% rename from String/ValidateEmail.test.js rename to String/test/ValidateEmail.test.js index a10f8279cf..ec1046c275 100644 --- a/String/ValidateEmail.test.js +++ b/String/test/ValidateEmail.test.js @@ -1,4 +1,4 @@ -import { validateEmail } from './ValidateEmail' +import { validateEmail } from '../ValidateEmail' describe('Validation of an Email Address', () => { it('expects to return false', () => { @@ -13,6 +13,10 @@ describe('Validation of an Email Address', () => { expect(validateEmail('mahfoudh.arous@gmail.com')).toEqual(true) }) + it('expects to return true', () => { + expect(validateEmail('icristianbaciu@.helsinki.edu')).toEqual(true) + }) + it('expects to throw a type error', () => { expect(() => { validateEmail('') }).toThrow('Email Address String Null or Empty.') })