forked from camptocamp/ogc-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl-utils.ts
More file actions
52 lines (48 loc) · 1.47 KB
/
url-utils.ts
File metadata and controls
52 lines (48 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* Returns the base url of the given url
*
* @param url - the url to get the base url from
* @returns the base url
*/
export function getBaseUrl(url?: string): string | URL {
if (url && typeof url === 'string') {
return new URL(url);
}
if ('location' in globalThis && typeof globalThis.location === 'object') {
return globalThis.location.toString();
}
return new URL('http://localhost');
}
/**
* Returns the parent path from a URL based on a version pattern (x.y.z).
*/
export function getParentPath(url: string): string | null {
const urlObj = new URL(url, getBaseUrl());
let pathParts = urlObj.pathname.split('/');
if (pathParts.length <= 2) {
// cannot go further up
return null;
}
if (pathParts[pathParts.length - 1] === '') {
pathParts = pathParts.slice(0, -1); // remove trailing slash if present
}
pathParts = pathParts.slice(0, -1); // remove last part to go one level up
if (pathParts.length === 2 && pathParts[1] !== '') {
// push a trailing slash if we're on the "app context" part of the url
pathParts.push('');
}
urlObj.pathname = pathParts.join('/');
return urlObj.toString();
}
/**
* Appends a new fragment to the URL's path
*/
export function getChildPath(url: string, childFragment: string): string {
const urlObj = new URL(url, getBaseUrl());
if (urlObj.pathname.endsWith('/')) {
urlObj.pathname += childFragment;
} else {
urlObj.pathname += `/${childFragment}`;
}
return urlObj.toString();
}