v8-ui-atoms
Version:
A library of common base components for building ui
48 lines • 1.61 kB
JavaScript
export const startsWithSpecialChar = (href) => {
try {
if (!href) {
return false;
}
return href.startsWith('?') || href.startsWith('/');
}
catch (e) {
console.warn('startsWithSpecialChar() ', e, { href });
return false;
}
};
export const isAbsoluteUrl = (url) => {
try {
if (url.indexOf('//') === 0) {
return true; // URL is protocol-relative (= absolute)
}
if (url.indexOf('www.') === 0) {
return true;
}
if (url.indexOf('mailto:') === 0 || url.indexOf('tel:') === 0) {
return true;
}
if (url.indexOf('://') === -1) {
return false; // URL has no protocol (= relative)
}
if (url.indexOf('.') === -1) {
return false; // URL does not contain a dot, i.e. no TLD (= relative, possibly REST)
}
if (url.indexOf('/') === -1) {
return false; // URL does not contain a single slash (= relative)
}
if (url.indexOf(':') > url.indexOf('/')) {
return false; // The first colon comes after the first slash (= relative)
}
if (url.indexOf('://') < url.indexOf('.')) {
return true; // Protocol is defined before first dot (= absolute)
}
if (url.indexOf('#') === 0) {
return true;
}
}
catch (e) {
console.warn('isAbsoluteUrl() ', e, { url });
}
return false; // Anything else must be relative
};
//# sourceMappingURL=isAbsoluteUrl.js.map