Files
smartstring/ts/smartstring.domain.ts
T

84 lines
2.6 KiB
TypeScript
Raw Normal View History

2016-05-25 05:06:19 +02:00
export class Domain {
2019-04-16 08:54:27 +02:00
public fullName: string;
public level1?: string;
public level2?: string;
public level3?: string;
public level4?: string;
public level5?: string;
public protocol?: string;
2019-04-16 08:54:27 +02:00
public zoneName: string;
2017-10-05 15:55:59 +02:00
// aliases
public topLevel?: string;
public domainName?: string;
public subDomain?: string;
public port: string;
public nodeParsedUrl: URL;
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 22:52:38 +01:00
domainStringArg = `https://${domainStringArg}`;
2021-01-22 18:27:50 +00:00
}
this.nodeParsedUrl = new URL(domainStringArg);
2021-01-22 14:46:02 +00:00
this.port = this.nodeParsedUrl.port;
// lets do the rest after
2022-03-18 22:52:38 +01:00
const regexMatches = this._domainRegex(
domainStringArg.replace(this.nodeParsedUrl.pathname, '')
);
[this.level1, this.level2, this.level3, this.level4, this.level5] = regexMatches;
this.fullName = '';
for (const localMatch of regexMatches) {
if (this.fullName === '') {
this.fullName = localMatch;
2017-10-05 15:55:59 +02:00
} else {
this.fullName = localMatch + '.' + this.fullName;
}
}
this.zoneName = [this.level2, this.level1].filter(Boolean).join('.');
2016-11-01 00:22:38 +01:00
2017-10-05 15:55:59 +02:00
// aliases
this.topLevel = this.level1;
this.domainName = this.level2;
this.subDomain = this.level3;
2017-10-05 15:55:59 +02:00
}
2016-05-25 05:06:19 +02:00
2019-04-16 08:54:27 +02:00
// helper functions
/** */
private _domainRegex(stringArg: string): string[] {
2022-03-18 22:52:38 +01: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 08:54:27 +02:00
const regexMatches = regexString.exec(stringArg);
if (!regexMatches) {
return [];
}
const reversedMatches = [...regexMatches].reverse(); // make sure we build the domain from top level to subdomain
reversedMatches.pop(); // pop the last element, which is, since we reversed the Array, the full String of matched elements
const regexMatchesFiltered = reversedMatches.filter(function (stringArg: string) {
2019-04-16 08:54:27 +02:00
return stringArg !== '';
});
return regexMatchesFiltered;
}
private _protocolRegex(stringArg: string): string | undefined {
2019-04-16 08:54:27 +02:00
const regexString = /^([a-zA-Z0-9]*):\/\//;
const regexMatches = regexString.exec(stringArg);
if (regexMatches) {
return regexMatches[1];
} else {
return undefined;
}
2017-10-05 15:55:59 +02:00
}
2019-04-16 08:54:27 +02:00
private _portRegex(stringArg: string): string | undefined {
2019-04-16 08:54:27 +02:00
const regexString = /^([a-zA-Z0-9]*):\/\//;
const regexMatches = regexString.exec(stringArg);
if (regexMatches) {
return regexMatches[1];
} else {
return undefined;
}
}
}