-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
49 lines (44 loc) · 1.52 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
export const regExps = {
strictlyAlphabetic: /^[a-ö]+$/gi,
partlyAlphabetic: /[a-ö]+/gi,
strictlyNumeric: /^\d+$/g,
partlyNumeric: /\d+/g,
};
export const containsAlphabetics = (domainName) =>
matchesRegExp(domainName, regExps.partlyAlphabetic);
export const containsNumerics = (domainName) =>
matchesRegExp(domainName, regExps.partlyNumeric);
export const getDomains = (domainName) => domainName.split(".");
export const getLength = (domainName) =>
getBaseLevelDomain(domainName).replace(".", "").length;
export const isAlphabetic = (domainName) =>
matchesRegExp(domainName, regExps.strictlyAlphabetic);
export const isHyphenated = (domainName) => domainName.includes("-");
export const isNumeric = (domainName) =>
matchesRegExp(domainName, regExps.strictlyNumeric);
export const isSubdomain = (domainName) => getDomains(domainName).length > 2;
export const matchesRegExp = (domainName, regexp) => {
const baseLevelDomainStr = getBaseLevelDomain(domainName).replace(".", "");
return null !== baseLevelDomainStr.match(regexp);
};
export const getBaseLevelDomain = (domainName) => {
const domains = getDomains(domainName);
return domains.slice(0, domains.length - 1).join(".");
};
export const getTopLevelDomain = (domainName) => {
const domains = getDomains(domainName);
return domains[domains.length - 1];
};
export default {
containsAlphabetics,
containsNumerics,
getBaseLevelDomain,
getDomains,
getLength,
getTopLevelDomain,
isAlphabetic,
isHyphenated,
isNumeric,
isSubdomain,
matchesRegExp,
};