2024-08-28 16:32:38 +00:00
|
|
|
export function pathEscapeSegments(s: string): string {
|
2023-03-14 09:51:20 +00:00
|
|
|
return s.split('/').map(encodeURIComponent).join('/');
|
|
|
|
}
|
2024-03-08 15:15:58 +00:00
|
|
|
|
2024-08-28 16:32:38 +00:00
|
|
|
function stripSlash(url: string): string {
|
2024-03-08 15:15:58 +00:00
|
|
|
return url.endsWith('/') ? url.slice(0, -1) : url;
|
|
|
|
}
|
|
|
|
|
2024-08-28 16:32:38 +00:00
|
|
|
export function isUrl(url: string): boolean {
|
2024-03-08 15:15:58 +00:00
|
|
|
try {
|
|
|
|
return stripSlash((new URL(url).href)).trim() === stripSlash(url).trim();
|
|
|
|
} catch {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2024-12-11 13:54:30 +00:00
|
|
|
|
|
|
|
// Convert an absolute or relative URL to an absolute URL with the current origin. It only
|
|
|
|
// processes absolute HTTP/HTTPS URLs or relative URLs like '/xxx' or '//host/xxx'.
|
|
|
|
export function toOriginUrl(urlStr: string) {
|
|
|
|
try {
|
|
|
|
if (urlStr.startsWith('http://') || urlStr.startsWith('https://') || urlStr.startsWith('/')) {
|
|
|
|
const {origin, protocol, hostname, port} = window.location;
|
|
|
|
const url = new URL(urlStr, origin);
|
|
|
|
url.protocol = protocol;
|
|
|
|
url.hostname = hostname;
|
|
|
|
url.port = port || (protocol === 'https:' ? '443' : '80');
|
|
|
|
return url.toString();
|
|
|
|
}
|
|
|
|
} catch {}
|
|
|
|
return urlStr;
|
|
|
|
}
|