emailvalid
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -16,2 +16,3 @@ 'use strict' | ||
if (result.errors.includes('blacklist')) s += `you just blacklisted it` | ||
if (result.typo) s += `Did you mean ${result.typo}?` | ||
} | ||
@@ -45,2 +46,7 @@ console.log(s) | ||
// If check for possible typos | ||
displayInfos( | ||
ev.check('john@gmal.com') | ||
) | ||
// You can easily add some blacklisted domains on the fly if needed | ||
@@ -47,0 +53,0 @@ ev.blacklist('email.io') |
21
index.js
const domains = require('./domains.json') | ||
const popularDomains = require('./popular-domains.json') | ||
const levenstein = require('./utils') | ||
@@ -7,2 +9,3 @@ // Source of the email regexp : | ||
const emailReg = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/i | ||
const maxDistance = 3 | ||
@@ -22,3 +25,9 @@ const defaultOptions = { | ||
check (email) { | ||
const result = { email, domain: null, valid: false, errors: [] } | ||
const result = { | ||
email, | ||
domain: null, | ||
valid: false, | ||
errors: [], | ||
typo: null | ||
} | ||
@@ -38,2 +47,12 @@ if (!email || !emailReg.test(email) || !email.split('@')) { | ||
type === 'blacklist' && result.errors.push(type) | ||
} else { | ||
let smallestDistance = result.domain.length | ||
for (const domain of popularDomains) { | ||
const distance = levenstein(domain, result.domain) | ||
if (distance > 0 && distance < maxDistance && distance < smallestDistance) { | ||
smallestDistance = distance | ||
const prefix = result.email.split('@').shift() | ||
result.typo = `${prefix}@${domain}` | ||
} | ||
} | ||
} | ||
@@ -40,0 +59,0 @@ |
@@ -46,2 +46,14 @@ 'use strict' | ||
}) | ||
it('should not suggest possible typos for gmail.com', () => { | ||
const result = ev.check('roger@gmail.com') | ||
expect(result.typo).eq(null) | ||
}) | ||
it('should suggest possible typos for gmal.com', () => { | ||
const result = ev.check('roger@gmal.com') | ||
expect(result.typo).eq('roger@gmail.com') | ||
}) | ||
it('should suggest possible typos for gnaul.com', () => { | ||
const result = ev.check('roger@gnaul.com') | ||
expect(result.typo).eq('roger@gmail.com') | ||
}) | ||
}) | ||
@@ -48,0 +60,0 @@ describe('Detection of disposable and free emails', () => { |
{ | ||
"name": "emailvalid", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Email validation with more than 10K disposable/temporary email domains", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -27,2 +27,3 @@ <h1 align="center">Email Validation</h1> | ||
- Disposable email address (@maildrop.cc, @fakemail.net, @trashmail.com, ...) | ||
- Possible typos in popular email domains | ||
@@ -73,2 +74,6 @@ Email Validation has **0 dependency, 100% coverage, and is [fully configurable](#configuration)**. | ||
// You can also check for possible typos | ||
const result3 = ev.check('john@gmil.com') | ||
if (result3.typo) console.log(`Did you mean ${result3.typo}?`) | ||
``` | ||
@@ -146,2 +151,3 @@ | ||
- `errors` _(Array)_ List of errors if any | ||
- `typo` _(String)_ Is there any possible typo in the email? | ||
@@ -168,3 +174,4 @@ Errors contains strings and can be one of : | ||
// valid: false, | ||
// errors: ['freemail'] | ||
// errors: ['freemail'], | ||
// typo: null | ||
// { | ||
@@ -171,0 +178,0 @@ |
378305
16
10885
189