forked from activeprospect/freemail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (57 loc) · 1.6 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
var fs = require('fs');
var tldjs = require('tldjs');
var disposable = load('/data/disposable.txt');
var free = extend(load('/data/free.txt'), disposable);
function isFree(email) {
if (typeof email !== 'string') throw new TypeError('email must be a string');
var split = email.split('@');
var 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');
var split = email.split('@');
var domain = getDomain(split[1] || split[0]);
return !!(domain && disposable[domain]);
}
function handleFreemailValidation(email) {
if (typeof email !== 'string') throw new TypeError('email must be a string');
var split = email.split('@');
var domain = getDomain(split[1] || split[0]);
return {
isFree: !!(domain && free[domain]),
isDisposable: !!(domain && disposable[domain])
};
}
function getDomain(host) {
var 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 load(path) {
return fs.readFileSync(__dirname + path, 'utf8')
.split('\n')
.reduce(function(res, cur) {
res[cur] = true;
return res;
}, {});
}
function extend(a, b) {
var res = {};
var key;
for (key in a) {
res[key] = a[key];
}
for (key in b) {
res[key] = b[key];
}
return res;
}
module.exports = {
isFree: isFree,
isDisposable: isDisposable,
handleFreemailValidation: handleFreemailValidation
};