add function to test valid URL

This commit is contained in:
buzz-lightsnack-2007 2024-05-12 19:49:25 +08:00
parent 4f07cb5b5d
commit 16e57f45fc

View file

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