18 lines
687 B
TypeScript
18 lines
687 B
TypeScript
|
|
export function normalizeHostname(valueArg: string): string {
|
||
|
|
const trimmedValue = valueArg.trim().toLowerCase();
|
||
|
|
if (!trimmedValue) return '';
|
||
|
|
|
||
|
|
const withoutProtocol = trimmedValue.replace(/^[a-z][a-z0-9+.-]*:\/\//, '');
|
||
|
|
const withoutPath = withoutProtocol.split('/')[0].split('?')[0].split('#')[0];
|
||
|
|
return withoutPath.replace(/:\d+$/, '').replace(/\.$/, '');
|
||
|
|
}
|
||
|
|
|
||
|
|
export function isValidHostname(hostnameArg: string): boolean {
|
||
|
|
if (!hostnameArg) return true;
|
||
|
|
if (hostnameArg.length > 253) return false;
|
||
|
|
return hostnameArg.split('.').every((label) => {
|
||
|
|
if (!label || label.length > 63) return false;
|
||
|
|
return /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(label);
|
||
|
|
});
|
||
|
|
}
|