diff --git a/scripts/utilities/URLs.js b/scripts/utilities/URLs.js new file mode 100644 index 0000000..a422749 --- /dev/null +++ b/scripts/utilities/URLs.js @@ -0,0 +1,54 @@ +/* +Custom URL tools for ShopAI +*/ + +class URLs { + /* + This constructor creates a URL object and is the same as new URL(). + + @param {string} PATH the URL + */ + constructor (PATH) { + const URL_OBJECT = new URL(PATH); + + /* Copy the object's data into this object. */ + (Object.keys(URL_OBJECT)).forEach((NAME, VALUE) => { + this[NAME] = VALUE; + }); + }; + + /* + Remove the protocol from the URL. + + @param {string} URL the URL to clean + */ + static clean(PATH) { + return((PATH.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. + + STATE = !!(new URL(location)); + + // Fail the URLs that claim to be invalid. + location.split(`.`).forEach((ENTRY) => { + [`invalidTLD`].forEach((FRAGMENT) => { + STATE = (STATE) ? !(ENTRY.includes(FRAGMENT)) : false; + }) + }) + + + return STATE; + } +} + +module.exports = {URLs}; \ No newline at end of file