14 lines
590 B
TypeScript
14 lines
590 B
TypeScript
export function isValidDomain(domain: string): boolean {
|
|
const d = domain.trim().replace(/\.$/, "");
|
|
const re = /^(?=.{1,253}$)(?!-)[A-Za-z0-9-]{1,63}(?<!-)(\.(?!-)[A-Za-z0-9-]{1,63}(?<!-))+$/;
|
|
return re.test(d);
|
|
}
|
|
export function normalizeDomain(domain: string): string {
|
|
return domain.trim().toLowerCase().replace(/^https?:\/\//, "").replace(/\/.*$/, "").replace(/\.$/, "");
|
|
}
|
|
export function normalizeUrl(input: string): string {
|
|
let url = input.trim();
|
|
if (!/^https?:\/\//i.test(url)) url = "https://" + url;
|
|
try { return new URL(url).toString(); } catch { return ""; }
|
|
}
|