forked from willwhite/freemail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (36 loc) · 1.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const tldjs = require('tldjs')
const disposable = lookup(() => require('./disposable.js'))
const free = lookup(() => require('./free.js'))
function isFree (email) {
if (typeof email !== 'string') throw new TypeError('email must be a string')
const split = email.split('@')
const domain = getDomain(split[1] || split[0])
return domain && free(domain)
}
function isDisposable (email) {
if (typeof email !== 'string') throw new TypeError('email must be a string')
const split = email.split('@')
const domain = getDomain(split[1] || split[0])
return domain && (disposable(domain) || free(domain))
}
function getDomain (host) {
const split = host.split('.')
// Performance optimization for .com TLD
if (split.length >= 2 && split[split.length - 1] === 'com') {
return split.slice(-2).join('.')
}
return tldjs.getDomain(host)
}
function lookup (load) {
let set
return function (email) {
if (set === undefined) {
set = new Set(load().split('\n'))
}
return set.has(email)
}
}
module.exports = {
isFree: isFree,
isDisposable: isDisposable
}