mirror of
https://codeberg.org/buzzcode2007/FCC-Project-URLShortener.git
synced 2025-05-21 03:06:34 +00:00
from https://codeberg.org/buzzcode2007/ShopAI-Extension/src/branch/development-chromium/src/scripts/utils/URLs.js
54 lines
No EOL
1 KiB
JavaScript
54 lines
No EOL
1 KiB
JavaScript
/*
|
|
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}; |