mirror of
https://codeberg.org/buzzcode2007/FCC-Project-URLShortener.git
synced 2025-05-21 03:06:34 +00:00
Import and modify URL tester
from https://codeberg.org/buzzcode2007/ShopAI-Extension/src/branch/development-chromium/src/scripts/utils/URLs.js
This commit is contained in:
parent
31e074a5a8
commit
33a4463994
1 changed files with 54 additions and 0 deletions
54
scripts/utilities/URLs.js
Normal file
54
scripts/utilities/URLs.js
Normal 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};
|
Loading…
Add table
Add a link
Reference in a new issue