Last active
May 28, 2024 07:53
-
-
Save alwaisy/4111b97680c3551e92899d4a0dbf8711 to your computer and use it in GitHub Desktop.
Two ways to check URL validity. New || Old
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
author: @alwaisy -> github | |
reference: https://rahuulmiishra.medium.com/url-canparse-a-game-changer-in-web-development-8b0572f1354c | |
*/ | |
/* ======================== old way =================================== */ | |
// Js version | |
/** | |
* Checks if a given string is a valid URL. | |
* | |
* @param {string} url The string to be checked. | |
* @returns {boolean} True if the URL is valid, False otherwise. | |
*/ | |
function isValidUrl(url) { | |
try { | |
new URL(url); | |
return true; | |
} catch (e) { | |
console.error("You are a coder, but sorry it's not a valid URL"); | |
return false; | |
} | |
} | |
// Example usage | |
const valid_url = "https://www.google.com/"; | |
const invalid_url = "AmiCoder"; | |
console.log(isValidUrl(valid_url)); // Output: true | |
console.log(isValidUrl(invalid_url)); // Output: You are a coder, but sorry it's not a valid URL (error message logged to console), false | |
// TS Version | |
/** | |
* Checks if a given string is a valid URL. | |
* | |
* @param {string} url The string to be checked. | |
* @returns {boolean} True if the URL is valid, False otherwise. | |
*/ | |
function isValidUrl(url: string): boolean { | |
try { | |
new URL(url); | |
return true; | |
} catch (e) { | |
console.error("You are a coder, but sorry it's not a valid URL"); | |
return false; | |
} | |
} | |
// Example usage | |
const valid_url: string = "https://www.google.com/"; | |
const invalid_url: string = "AmiCoder"; | |
console.log(isValidUrl(valid_url)); // Output: true | |
console.log(isValidUrl(invalid_url)); // Output: You are a coder, but sorry it's not a valid URL (error message logged to console), false | |
/* ======================== new way =================================== */ | |
// JS Version | |
/** | |
* Checks if a given string is a valid URL. | |
* | |
* @param {string} url The string to be checked. | |
* @returns {boolean} True if the URL is valid, False otherwise. | |
*/ | |
function isValidUrl(url) { | |
try { | |
return URL.canParse(url); | |
} catch (e) { | |
return false; | |
} | |
} | |
// Example usage | |
const valid_url = "https://amicoder.dev/"; | |
const invalid_url = "AmiCoder"; | |
console.log(isValidUrl(valid_url)); // Output: true | |
console.log(isValidUrl(invalid_url)); // Output: false | |
// TS Version | |
/** | |
* Checks if a given string is a valid URL. | |
* | |
* @param {string} url The string to be checked. | |
* @returns {boolean} True if the URL is valid, False otherwise. | |
*/ | |
function isValidUrl(url: string): boolean { | |
try { | |
return URL.canParse(url); | |
} catch (e) { | |
return false; | |
} | |
} | |
// Example usage | |
const valid_url = "https://amicoder.dev/"; | |
const invalid_url = "AmiCoder"; | |
console.log(isValidUrl(valid_url)); // Output: true | |
console.log(isValidUrl(invalid_url)); // Output: false | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment