-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·54 lines (47 loc) · 1.06 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
"use strict";
function removeOptionalTrailingDot(str, allowTrailingDot) {
if (allowTrailingDot && str[str.length - 1] === ".") {
return str.substring(0, str.length - 1);
}
return str;
}
function isFQDN(
_str,
{ requireTld = true, allowUnderscores = false, allowTrailingDot = false } = {}
) {
if (typeof _str !== "string") {
return false;
}
const str = removeOptionalTrailingDot(_str, allowTrailingDot);
const parts = str.split(".");
if (requireTld) {
const tld = parts.pop();
if (
!parts.length ||
!/^([a-z\u00a1-\uffff]{2,}|xn[a-z0-9-]{2,})$/i.test(tld)
) {
return false;
}
}
for (let part, i = 0; i < parts.length; i++) {
part = parts[i];
if (allowUnderscores) {
if (part.indexOf("__") >= 0) {
return false;
}
part = part.replace(/_/g, "");
}
if (!/^[a-z\u00a1-\uffff0-9-]+$/i.test(part)) {
return false;
}
if (/[\uff01-\uff5e]/.test(part)) {
// disallow full-width chars
return false;
}
if (part[0] === "-" || part[part.length - 1] === "-") {
return false;
}
}
return true;
}
module.exports = isFQDN;