From 9b63efcce7842fcd24a2f94876b2ac84f5c047a1 Mon Sep 17 00:00:00 2001 From: Mahfoudh Arous Date: Mon, 12 Oct 2020 08:29:37 +0100 Subject: [PATCH] Add Email Validation Function (#462) * Add Email Validation Function * fix standard styling issues --- String/ValidateEmail.js | 19 +++++++++++++++++++ String/ValidateEmail.test.js | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 String/ValidateEmail.js create mode 100644 String/ValidateEmail.test.js diff --git a/String/ValidateEmail.js b/String/ValidateEmail.js new file mode 100644 index 000000000..af2ee3f09 --- /dev/null +++ b/String/ValidateEmail.js @@ -0,0 +1,19 @@ +/* + 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.com ->false +*/ + +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 +} + +export { validateEmail } diff --git a/String/ValidateEmail.test.js b/String/ValidateEmail.test.js new file mode 100644 index 000000000..a10f8279c --- /dev/null +++ b/String/ValidateEmail.test.js @@ -0,0 +1,19 @@ +import { validateEmail } from './ValidateEmail' + +describe('Validation of an Email Address', () => { + it('expects to return false', () => { + expect(validateEmail('mahfoudh.arous.com')).toEqual(false) + }) + + it('expects to return false', () => { + expect(validateEmail('mahfoudh.arous@com')).toEqual(false) + }) + + it('expects to return true', () => { + expect(validateEmail('mahfoudh.arous@gmail.com')).toEqual(true) + }) + + it('expects to throw a type error', () => { + expect(() => { validateEmail('') }).toThrow('Email Address String Null or Empty.') + }) +})