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