-
Notifications
You must be signed in to change notification settings - Fork 1
/
getUrlInformation.js
42 lines (32 loc) · 1.1 KB
/
getUrlInformation.js
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
export function getUrlInformation(definitions, url) {
// ash.js uses the hash part of the URL to identify the current page
const currentPathWithSearch = "/" + (url.split("#")[1] || "");
const path = currentPathWithSearch.split("?")[0] || "";
const search = url.split("?")[1] || "";
// Parse the search parameters
const searchParams = Object.fromEntries(new URLSearchParams(search));
// Parse the URL parameters
let urlParams = {};
let matchedDefinition = null;
if (definitions) {
const currentPathParts = path.split("/");
for (const definition of definitions) {
const parts = definition.split("/");
if (parts.length !== currentPathParts.length) {
continue;
}
const isMatch = parts.every((part, index) => {
if (part.startsWith(":")) {
urlParams[part.substring(1)] = currentPathParts[index];
return true;
}
return part === currentPathParts[index];
});
if (isMatch) {
matchedDefinition = definition;
break;
}
}
}
return { path, searchParams, urlParams, matchedDefinition };
}