filebrowser/frontend/src/utils/url.js

48 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-05-02 13:47:22 +00:00
export function removeLastDir(url) {
2021-03-21 11:51:58 +00:00
var arr = url.split("/");
if (arr.pop() === "") {
arr.pop();
2018-02-01 12:17:04 +00:00
}
2021-03-21 11:51:58 +00:00
return arr.join("/");
2018-02-01 12:17:04 +00:00
}
// this code borrow from mozilla
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent#Examples
2022-05-02 13:47:22 +00:00
export function encodeRFC5987ValueChars(str) {
2021-03-21 11:51:58 +00:00
return (
encodeURIComponent(str)
// Note that although RFC3986 reserves "!", RFC5987 does not,
// so we do not need to escape it
2021-03-21 11:51:58 +00:00
.replace(/['()]/g, escape) // i.e., %27 %28 %29
.replace(/\*/g, "%2A")
// The following are not required for percent-encoding per RFC5987,
// so we can allow for a little better readability over the wire: |`^
.replace(/%(?:7C|60|5E)/g, unescape)
);
}
2022-05-02 13:47:22 +00:00
export function encodePath(str) {
2021-03-21 11:51:58 +00:00
return str
.split("/")
.map((v) => encodeURIComponent(v))
.join("/");
}
2024-07-30 17:45:27 +00:00
// Function to remove trailing slash
export function removeTrailingSlash(url) {
return url.endsWith("/") ? url.slice(0, -1) : url;
}
export function pathsMatch(url1, url2) {
return removeTrailingSlash(url1) == removeTrailingSlash(url2);
}
2018-02-01 12:17:04 +00:00
export default {
2024-07-30 17:45:27 +00:00
pathsMatch,
removeTrailingSlash,
2022-05-02 13:47:22 +00:00
encodeRFC5987ValueChars,
removeLastDir,
encodePath,
2021-03-21 11:51:58 +00:00
};