index.js
1.93 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'use strict';
const Domain = require('./domain');
const Email = require('./email');
const Tlds = require('./tlds');
const internals = {
defaultTlds: { allow: Tlds, deny: null }
};
module.exports = {
domain: {
analyze(domain, options) {
options = internals.options(options);
return Domain.analyze(domain, options);
},
isValid(domain, options) {
options = internals.options(options);
return Domain.isValid(domain, options);
}
},
email: {
analyze(email, options) {
options = internals.options(options);
return Email.analyze(email, options);
},
isValid(email, options) {
options = internals.options(options);
return Email.isValid(email, options);
}
}
};
internals.options = function (options) {
if (!options) {
return { tlds: internals.defaultTlds };
}
if (options.tlds === false) { // Defaults to true
return options;
}
if (!options.tlds ||
options.tlds === true) {
return Object.assign({}, options, { tlds: internals.defaultTlds });
}
if (typeof options.tlds !== 'object') {
throw new Error('Invalid options: tlds must be a boolean or an object');
}
if (options.tlds.deny) {
if (options.tlds.deny instanceof Set === false) {
throw new Error('Invalid options: tlds.deny must be a Set object');
}
if (options.tlds.allow) {
throw new Error('Invalid options: cannot specify both tlds.allow and tlds.deny lists');
}
return options;
}
if (options.tlds.allow === true) {
return Object.assign({}, options, { tlds: internals.defaultTlds });
}
if (options.tlds.allow instanceof Set === false) {
throw new Error('Invalid options: tlds.allow must be a Set object or true');
}
return options;
};