one
Version:
One is a new React Framework that makes Vite serve both native and web.
43 lines (42 loc) • 1.74 kB
JavaScript
import { stripBaseUrl } from "../fork/getStateFromPath-mods.mjs";
function stripPathSuffix(path) {
const queryIndex = path.indexOf("?");
const hashIndex = path.indexOf("#");
let end = path.length;
if (queryIndex >= 0) end = Math.min(end, queryIndex);
if (hashIndex >= 0) end = Math.min(end, hashIndex);
return path.slice(0, end);
}
function stripTrailingSlash(pathname) {
return pathname.length > 1 && pathname.endsWith("/") ? pathname.slice(0, -1) : pathname;
}
function normalizeRoutePathname(pathname) {
return stripTrailingSlash(stripBaseUrl(stripPathSuffix(pathname)));
}
function hasLostDynamicSegment(path) {
return stripPathSuffix(path).split("/").includes("undefined");
}
function getSafeWindowPathname() {
if (typeof window === "undefined" || !window.location || typeof window.location.pathname !== "string") {
return void 0;
}
return window.location.pathname;
}
function getSafeWindowPath() {
const pathname = getSafeWindowPathname();
if (pathname === void 0) return void 0;
return pathname + (window.location.search || "");
}
function getPathWithRecoveredDynamicSegment(paths, fallbackPath = getSafeWindowPath()) {
for (const path of paths) {
if (path !== void 0 && !hasLostDynamicSegment(path)) {
return path;
}
}
return fallbackPath;
}
function getPathnameWithRecoveredDynamicSegment(pathname, fallbackPath = getSafeWindowPath()) {
return normalizeRoutePathname(getPathWithRecoveredDynamicSegment([pathname], fallbackPath) ?? pathname);
}
export { getPathWithRecoveredDynamicSegment, getPathnameWithRecoveredDynamicSegment, getSafeWindowPath, getSafeWindowPathname, hasLostDynamicSegment, normalizeRoutePathname, stripTrailingSlash };
//# sourceMappingURL=path.mjs.map