forked from perry-mitchell/webdav-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.js
32 lines (28 loc) · 774 Bytes
/
url.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
const URL = require("url-parse");
function extractURLPath(fullURL) {
const url = new URL(fullURL);
let urlPath = url.pathname;
if (urlPath.length <= 0) {
urlPath = "/";
}
return normalisePath(urlPath);
}
function normaliseHREF(href) {
const normalisedHref = href.replace(/^https?:\/\/[^\/]+/, "");
return normalisedHref;
}
function normalisePath(pathStr) {
let normalisedPath = pathStr;
if (normalisedPath[0] !== "/") {
normalisedPath = "/" + normalisedPath;
}
if (/^.+\/$/.test(normalisedPath)) {
normalisedPath = normalisedPath.substr(0, normalisedPath.length - 1);
}
return decodeURIComponent(normalisedPath);
}
module.exports = {
extractURLPath,
normaliseHREF,
normalisePath
};