Files
demat-facturation/shared/webPortalUrl.ts
2026-09-30 14:55:13 +02:00

25 lines
853 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Ajoute HTTPS aux adresses de portail saisies sous forme de domaine simple.
* Les protocoles explicites sont conservés afin que la validation puisse refuser
* proprement ceux qui ne sont pas HTTP(S).
*/
export function normalizeWebPortalUrl(value: string): string {
const trimmed = value.trim();
if (!trimmed) return "";
if (trimmed.startsWith("//")) return `https:${trimmed}`;
if (/^[a-z][a-z\d+.-]*:\/\//i.test(trimmed) || /^(?:mailto|tel|data):/i.test(trimmed)) {
return trimmed;
}
return `https://${trimmed}`;
}
/** Vérifie qu’une URL de portail est une adresse HTTP(S) avec un hôte. */
export function isValidWebPortalUrl(value: string): boolean {
try {
const url = new URL(value);
return (url.protocol === "https:" || url.protocol === "http:") && Boolean(url.hostname);
} catch {
return false;
}
}