Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit ec60c7c

Browse files
cueBall123Kent C. Dodds
authored and
Kent C. Dodds
committed
feat(validateEmail): Add validateEmail function (#42)
* feat(validateEmail): Add validateEmail function * WIP:Fixed lint error but now the regex fails * feat(validateEmail): Add validateEmail function
1 parent c37e6eb commit ec60c7c

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import sortObjectsArray from './sort-objects-array'
88
import objectValuesToString from './object-values-to-string'
99
import getObjectSize from './get-object-size'
1010
import isArray from './is-array'
11+
import validateEmail from './validateEmail'
1112

1213
export {
1314
flatten,
@@ -20,4 +21,5 @@ export {
2021
objectValuesToString,
2122
getObjectSize,
2223
isArray,
24+
validateEmail,
2325
}

src/validateEmail.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default validateEmail
2+
3+
const regex = /^(([^<>()\[\]\\.,;:\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,}))$/ // eslint-disable-line max-len
4+
5+
/**
6+
* Orginal Source: http://stackoverflow.com/questions/46155
7+
* This method will return an array with the given value prefilled
8+
*
9+
* @param {String} email - The email address to be validated
10+
* @return {Boolean} - True if the string passes the regex else False
11+
*/
12+
function validateEmail(email) {
13+
return regex.test(email)
14+
}

test/validateEmail.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import test from 'ava'
2+
import {validateEmail} from '../src'
3+
4+
5+
test('validate correct email', t => {
6+
const original = 'validate@email.com'
7+
const expected = true
8+
const actual = validateEmail(original)
9+
t.deepEqual(actual,expected)
10+
})
11+
12+
test('validate wrong format', t => {
13+
const original = 'validateemail'
14+
const expected = false
15+
const actual = validateEmail(original)
16+
t.deepEqual(actual,expected)
17+
})
18+
19+
test('validate wrong format with @', t => {
20+
const original = 'validate@'
21+
const expected = false
22+
const actual = validateEmail(original)
23+
t.deepEqual(actual,expected)
24+
})

0 commit comments

Comments
 (0)