refactor: rename variables and remove useless if statement

This commit is contained in:
P. Reis 2024-05-24 13:55:05 -03:00
parent e3ff41f31f
commit 613ac145c6

View file

@ -1,23 +1,18 @@
function sanitizeWebsite(website: string | undefined): string | undefined {
if (!website) return undefined;
if (
'https://'.includes(website) ||
'http://'.includes(website)
) return undefined;
try {
// See if 'website' is a valid url
new URL(website);
return website;
} catch (_) {
try {
const websiteWithPrefix = 'https://' + website;
const prefixed = 'https://' + website;
// 'website' could still be a valid url
// try adding a 'https' prefix to 'website'
new URL(websiteWithPrefix);
new URL(prefixed);
return websiteWithPrefix;
return prefixed;
} catch (_) {
// 'website' is not a valid url even with 'https' prefix
return undefined;