Files
wp-media-rewind/src/utils/url.ts
T

26 lines
780 B
TypeScript

/** Strip trailing punctuation that often glues onto URLs in SQL strings. */
export function cleanUrl(u: string): string {
return u.replace(/[),;.'"\\]+$/g, "");
}
/** Normalize a site argument ("example.com", "https://example.com/") to a bare host. */
export function normalizeHost(site?: string): string | null {
if (!site) return null;
try {
const url = new URL(site.includes("://") ? site : `https://${site}`);
return url.host.toLowerCase();
} catch {
return site.toLowerCase();
}
}
/** Does `url` belong to `host`? When host is null, every URL passes. */
export function matchesHost(url: string, host: string | null): boolean {
if (!host) return true;
try {
return new URL(url).host.toLowerCase() === host;
} catch {
return false;
}
}