-
Notifications
You must be signed in to change notification settings - Fork 443
/
Copy pathurl.ts
67 lines (52 loc) · 1.79 KB
/
url.ts
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
export type Locatable = URL | string
export function expandURL(locatable: Locatable) {
return new URL(locatable.toString(), document.baseURI)
}
export function getAnchor(url: URL) {
let anchorMatch
if (url.hash) {
return url.hash.slice(1)
// eslint-disable-next-line no-cond-assign
} else if ((anchorMatch = url.href.match(/#(.*)$/))) {
return anchorMatch[1]
}
}
export function getAction(form: HTMLFormElement, submitter?: HTMLElement) {
const action = submitter?.getAttribute("formaction") || form.getAttribute("action") || form.action
return expandURL(action)
}
export function getExtension(url: URL) {
return (getLastPathComponent(url).match(/\.[^.]*$/) || [])[0] || ""
}
export function isHTML(url: URL) {
return !!getExtension(url).match(/^(?:|\.(?:htm|html|xhtml|php))$/)
}
export function isPrefixedBy(baseURL: URL, url: URL) {
const prefix = getPrefix(url)
return baseURL.href === expandURL(prefix).href || baseURL.href.startsWith(prefix)
}
export function locationIsVisitable(location: URL, rootLocation: URL) {
return isPrefixedBy(location, rootLocation) && isHTML(location)
}
export function getRequestURL(url: URL) {
const anchor = getAnchor(url)
return anchor != null ? url.href.slice(0, -(anchor.length + 1)) : url.href
}
export function toCacheKey(url: URL) {
return getRequestURL(url)
}
export function urlsAreEqual(left: string, right: string) {
return expandURL(left).href == expandURL(right).href
}
function getPathComponents(url: URL) {
return url.pathname.split("/").slice(1)
}
function getLastPathComponent(url: URL) {
return getPathComponents(url).slice(-1)[0]
}
function getPrefix(url: URL) {
return addTrailingSlash(url.origin + url.pathname)
}
function addTrailingSlash(value: string) {
return value.endsWith("/") ? value : value + "/"
}