From 16e57f45fcd27bc7e5241f65fe303bc3aecbf03b Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sun, 12 May 2024 19:49:25 +0800 Subject: [PATCH] add function to test valid URL --- scripts/utils/URLs.js | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/scripts/utils/URLs.js b/scripts/utils/URLs.js index bd8e069..df5cae0 100644 --- a/scripts/utils/URLs.js +++ b/scripts/utils/URLs.js @@ -3,14 +3,33 @@ URL tools */ class URLs { - /* - Remove the protocol from the URL. + /* + Remove the protocol from the URL. - @param {string} URL the URL to clean - */ - static clean(URL) { - return((URL.trim().replace(/(^\w+:|^)\/\//, ``).split(`?`))[0]); - } + @param {string} URL the URL to clean + */ + static clean(URL) { + return((URL.trim().replace(/(^\w+:|^)\/\//, ``).split(`?`))[0]); + } + + /* + Check if the URL is valid through checking its patterns. + + @param {string} location the URL to check + @return {boolean} the state + */ + static test(location) { + let STATE = false; + + // Convert to URL object. + try { + STATE = !!(new URL(location)); + } catch(err) { + STATE = false; + } + + return STATE; + } } export {URLs}; \ No newline at end of file