2016-10-31 23:22:38 +00:00
|
|
|
import * as plugins from './smartstring.plugins';
|
2016-05-25 03:06:19 +00:00
|
|
|
|
|
|
|
export class Domain {
|
2018-07-21 12:37:39 +00:00
|
|
|
fullName: string;
|
|
|
|
level1: string;
|
|
|
|
level2: string;
|
|
|
|
level3: string;
|
|
|
|
level4: string;
|
|
|
|
level5: string;
|
|
|
|
protocol: string;
|
|
|
|
zoneName: string;
|
2017-10-05 13:55:59 +00:00
|
|
|
// aliases
|
2018-07-21 12:37:39 +00:00
|
|
|
topLevel: string;
|
|
|
|
domainName;
|
|
|
|
subDomain;
|
|
|
|
constructor(domainStringArg: string) {
|
2017-10-05 13:55:59 +00:00
|
|
|
let regexMatches = domainRegex(domainStringArg);
|
2018-07-21 12:37:39 +00:00
|
|
|
this.fullName = '';
|
2017-10-05 13:55:59 +00:00
|
|
|
for (let i = 1; i <= 5; i++) {
|
2018-07-21 12:37:39 +00:00
|
|
|
if (regexMatches[i - 1]) {
|
|
|
|
let localMatch = regexMatches[i - 1];
|
|
|
|
this['level' + i.toString()] = localMatch;
|
2017-10-05 13:55:59 +00:00
|
|
|
if (this.fullName === '') {
|
2018-07-21 12:37:39 +00:00
|
|
|
this.fullName = localMatch;
|
2017-10-05 13:55:59 +00:00
|
|
|
} else {
|
2018-07-21 12:37:39 +00:00
|
|
|
this.fullName = localMatch + '.' + this.fullName;
|
2017-10-05 13:55:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-07-21 12:37:39 +00:00
|
|
|
this['level' + i.toString()] = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.protocol = protocolRegex(domainStringArg);
|
|
|
|
this.zoneName = this.level2 + '.' + this.level1;
|
2016-10-31 23:22:38 +00:00
|
|
|
|
2017-10-05 13:55:59 +00:00
|
|
|
// aliases
|
2018-07-21 12:37:39 +00:00
|
|
|
this.topLevel = this.level1;
|
|
|
|
this.domainName = this.level2;
|
|
|
|
this.subDomain = this.level3;
|
2017-10-05 13:55:59 +00:00
|
|
|
}
|
2016-05-25 03:06:19 +00:00
|
|
|
}
|
|
|
|
|
2018-07-21 12:37:39 +00:00
|
|
|
let domainRegex = function(stringArg: string) {
|
|
|
|
let regexString = /([a-zA-Z0-9\-\_]*)\.{0,1}([a-zA-Z0-9\-\_]*)\.{0,1}([a-zA-Z0-9\-\_]*)\.{0,1}([a-zA-Z0-9\-\_]*)\.{0,1}([a-zA-Z0-9\-\_]*)\.{0,1}$/;
|
|
|
|
let regexMatches = regexString.exec(stringArg);
|
|
|
|
regexMatches.reverse(); //make sure we build the domain from toplevel to subdomain (reversed order)
|
|
|
|
regexMatches.pop(); // pop the last element, which is, since we reversed the Array, the full String of matched elements
|
|
|
|
let regexMatchesFiltered = regexMatches.filter(function(stringArg: string) {
|
|
|
|
return stringArg !== '';
|
2017-10-05 13:55:59 +00:00
|
|
|
});
|
2018-07-21 12:37:39 +00:00
|
|
|
return regexMatchesFiltered;
|
2016-05-25 04:54:55 +00:00
|
|
|
};
|
|
|
|
|
2018-07-21 12:37:39 +00:00
|
|
|
let protocolRegex = function(stringArg: string) {
|
|
|
|
let regexString = /^([a-zA-Z0-9]*):\/\//;
|
|
|
|
let regexMatches = regexString.exec(stringArg);
|
2017-10-05 13:55:59 +00:00
|
|
|
if (regexMatches) {
|
2018-07-21 12:37:39 +00:00
|
|
|
return regexMatches[1];
|
2017-10-05 13:55:59 +00:00
|
|
|
} else {
|
2018-07-21 12:37:39 +00:00
|
|
|
return undefined;
|
2017-10-05 13:55:59 +00:00
|
|
|
}
|
2018-07-21 12:37:39 +00:00
|
|
|
};
|