smartstring/ts/smartstring.domain.ts

88 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-03-18 21:50:24 +00:00
import * as plugins from './smartstring.plugins.js';
2016-05-25 03:06:19 +00:00
export class Domain {
2019-04-16 06:54:27 +00:00
public fullName: string;
public level1: string;
public level2: string;
public level3: string;
public level4: string;
public level5: string;
public protocol: string;
public zoneName: string;
2017-10-05 13:55:59 +00:00
// aliases
2019-04-16 06:54:27 +00:00
public topLevel: string;
public domainName;
public subDomain;
public port;
public nodeParsedUrl: plugins.url.UrlWithStringQuery;
constructor(domainStringArg: string) {
2021-01-22 14:46:02 +00:00
// lets do the node standard stuff first
2021-01-22 18:27:50 +00:00
this.protocol = this._protocolRegex(domainStringArg);
if (!this.protocol) {
2022-03-18 21:52:38 +00:00
domainStringArg = `https://${domainStringArg}`;
2021-01-22 18:27:50 +00:00
}
2021-01-22 14:46:02 +00:00
this.nodeParsedUrl = plugins.url.parse(domainStringArg);
this.port = this.nodeParsedUrl.port;
// lets do the rest after
2022-03-18 21:52:38 +00:00
const regexMatches = this._domainRegex(
domainStringArg.replace(this.nodeParsedUrl.pathname, '')
);
this.fullName = '';
2017-10-05 13:55:59 +00:00
for (let i = 1; i <= 5; i++) {
if (regexMatches[i - 1]) {
2019-04-16 06:54:27 +00:00
const localMatch = regexMatches[i - 1];
this['level' + i.toString()] = localMatch;
2017-10-05 13:55:59 +00:00
if (this.fullName === '') {
this.fullName = localMatch;
2017-10-05 13:55:59 +00:00
} else {
this.fullName = localMatch + '.' + this.fullName;
2017-10-05 13:55:59 +00:00
}
} else {
this['level' + i.toString()] = undefined;
}
}
this.zoneName = this.level2 + '.' + this.level1;
2016-10-31 23:22:38 +00:00
2017-10-05 13:55:59 +00:00
// aliases
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
2019-04-16 06:54:27 +00:00
// helper functions
/** */
2019-04-16 06:55:37 +00:00
private _domainRegex(stringArg: string) {
2022-03-18 21:52:38 +00:00
const 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}$/;
2019-04-16 06:54:27 +00:00
const 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
2020-12-31 03:56:40 +00:00
const regexMatchesFiltered = regexMatches.filter(function (stringArg: string) {
2019-04-16 06:54:27 +00:00
return stringArg !== '';
});
return regexMatchesFiltered;
}
2019-04-16 06:55:37 +00:00
private _protocolRegex(stringArg: string) {
2019-04-16 06:54:27 +00:00
const regexString = /^([a-zA-Z0-9]*):\/\//;
const regexMatches = regexString.exec(stringArg);
if (regexMatches) {
return regexMatches[1];
} else {
return undefined;
}
2017-10-05 13:55:59 +00:00
}
2019-04-16 06:54:27 +00:00
2019-04-16 06:55:37 +00:00
private _portRegex(stringArg: string) {
2019-04-16 06:54:27 +00:00
const regexString = /^([a-zA-Z0-9]*):\/\//;
const regexMatches = regexString.exec(stringArg);
if (regexMatches) {
return regexMatches[1];
} else {
return undefined;
}
}
}