64 lines
2.2 KiB
JavaScript
64 lines
2.2 KiB
JavaScript
String.fromCharCode;
|
|
var ENC_SLASH_RE = /%2f/gi;
|
|
function decode(text = "") {
|
|
try {
|
|
return decodeURIComponent("" + text);
|
|
} catch {
|
|
return "" + text;
|
|
}
|
|
}
|
|
function decodePath(text) {
|
|
return decode(text.replace(ENC_SLASH_RE, "%252F"));
|
|
}
|
|
var TRAILING_SLASH_RE = /\/$|\/\?|\/#/;
|
|
var JOIN_LEADING_SLASH_RE = /^\.?\//;
|
|
function hasTrailingSlash(input = "", respectQueryAndFragment) {
|
|
if (!respectQueryAndFragment) return input.endsWith("/");
|
|
return TRAILING_SLASH_RE.test(input);
|
|
}
|
|
function withoutTrailingSlash(input = "", respectQueryAndFragment) {
|
|
if (!respectQueryAndFragment) return (hasTrailingSlash(input) ? input.slice(0, -1) : input) || "/";
|
|
if (!hasTrailingSlash(input, true)) return input || "/";
|
|
let path = input;
|
|
let fragment = "";
|
|
const fragmentIndex = input.indexOf("#");
|
|
if (fragmentIndex !== -1) {
|
|
path = input.slice(0, fragmentIndex);
|
|
fragment = input.slice(fragmentIndex);
|
|
}
|
|
const [s0, ...s] = path.split("?");
|
|
return ((s0.endsWith("/") ? s0.slice(0, -1) : s0) || "/") + (s.length > 0 ? `?${s.join("?")}` : "") + fragment;
|
|
}
|
|
function withTrailingSlash(input = "", respectQueryAndFragment) {
|
|
if (!respectQueryAndFragment) return input.endsWith("/") ? input : input + "/";
|
|
if (hasTrailingSlash(input, true)) return input || "/";
|
|
let path = input;
|
|
let fragment = "";
|
|
const fragmentIndex = input.indexOf("#");
|
|
if (fragmentIndex !== -1) {
|
|
path = input.slice(0, fragmentIndex);
|
|
fragment = input.slice(fragmentIndex);
|
|
if (!path) return fragment;
|
|
}
|
|
const [s0, ...s] = path.split("?");
|
|
return s0 + "/" + (s.length > 0 ? `?${s.join("?")}` : "") + fragment;
|
|
}
|
|
function hasLeadingSlash(input = "") {
|
|
return input.startsWith("/");
|
|
}
|
|
function withLeadingSlash(input = "") {
|
|
return hasLeadingSlash(input) ? input : "/" + input;
|
|
}
|
|
function isNonEmptyURL(url) {
|
|
return url && url !== "/";
|
|
}
|
|
function joinURL(base, ...input) {
|
|
let url = base || "";
|
|
for (const segment of input.filter((url2) => isNonEmptyURL(url2))) if (url) {
|
|
const _segment = segment.replace(JOIN_LEADING_SLASH_RE, "");
|
|
url = withTrailingSlash(url) + _segment;
|
|
} else url = segment;
|
|
return url;
|
|
}
|
|
//#endregion
|
|
export { withoutTrailingSlash as i, joinURL as n, withLeadingSlash as r, decodePath as t };
|