buzzcode2007 2025-03-23 07:49:14 +00:00
parent 31e074a5a8
commit 33a4463994

54
scripts/utilities/URLs.js Normal file
View file

@ -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};