one
Version:
One is a new React Framework that makes Vite serve both native and web.
127 lines (126 loc) • 7.25 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: !0 });
}, __copyProps = (to, from, except, desc) => {
if (from && typeof from == "object" || typeof from == "function")
for (let key of __getOwnPropNames(from))
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target,
mod
)), __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
var getPathFromState_exports = {};
__export(getPathFromState_exports, {
default: () => getPathFromState_default,
getPathDataFromState: () => getPathDataFromState,
getPathFromState: () => getPathFromState
});
module.exports = __toCommonJS(getPathFromState_exports);
var import_getPathFromState_mods = require("./getPathFromState-mods"), sharedModUtils = __toESM(require("./_shared"), 1), import_validatePathConfig = require("./validatePathConfig");
const getActiveRoute = (state) => {
const route = typeof state.index == "number" ? state.routes[state.index] : state.routes[state.routes.length - 1];
return route.state ? getActiveRoute(route.state) : route;
}, cachedNormalizedConfigs = /* @__PURE__ */ new WeakMap(), getNormalizedConfigs = (options) => {
if (!options?.screens) return {};
const cached = cachedNormalizedConfigs.get(options?.screens);
if (cached) return cached;
const normalizedConfigs = createNormalizedConfigs(options.screens);
return cachedNormalizedConfigs.set(options.screens, normalizedConfigs), normalizedConfigs;
};
function getPathFromState(state, options) {
return getPathDataFromState(state, options).path;
}
function getPathDataFromState(state, options) {
if (state == null)
throw Error("Got 'undefined' for the navigation state. You must pass a valid state object.");
options && (0, import_validatePathConfig.validatePathConfig)(options);
const configs = getNormalizedConfigs(options);
let path = "/", current = state;
const allParams = {};
for (; current; ) {
let index = typeof current.index == "number" ? current.index : 0, route = current.routes[index], pattern, focusedParams;
const focusedRoute = getActiveRoute(state);
let currentOptions = configs;
const nestedRouteNames = [];
let hasNext = !0;
for (; route.name in currentOptions && hasNext; ) {
if (pattern = currentOptions[route.name].pattern, nestedRouteNames.push(route.name), route.params) {
const stringify = currentOptions[route.name]?.stringify, currentParams = Object.fromEntries(
Object.entries(route.params).flatMap(([key, value]) => key === "screen" || key === "params" ? [] : [
[
key,
stringify?.[key] ? stringify[key](value) : Array.isArray(value) ? value.map(String) : typeof value > "u" ? value : String(value)
]
])
);
Object.assign(allParams, currentParams), focusedRoute === route && (focusedParams = { ...currentParams }, pattern?.split("/").filter((p) => sharedModUtils.isDynamicPart(p)).forEach((p) => {
const name = sharedModUtils.getParamName(p);
focusedParams && delete focusedParams[name];
}));
}
if (!currentOptions[route.name].screens || route.state === void 0) {
const screens = currentOptions[route.name].screens, screen = route.params && "screen" in route.params ? route.params.screen?.toString() : screens ? Object.keys(screens)[0] : void 0;
screen && screens && currentOptions[route.name].screens?.[screen] ? (route = {
...screens[screen],
name: screen,
key: screen,
params: route.params?.params
}, currentOptions = screens) : hasNext = !1;
} else {
index = typeof route.state.index == "number" ? route.state.index : route.state.routes.length - 1;
const nextRoute = route.state.routes[index], nestedConfig = currentOptions[route.name].screens;
nestedConfig && nextRoute.name in nestedConfig ? (route = nextRoute, currentOptions = nestedConfig) : hasNext = !1;
}
}
if (currentOptions[route.name] !== void 0 ? (pattern === void 0 && (pattern = nestedRouteNames.join("/")), path += (0, import_getPathFromState_mods.getPathWithConventionsCollapsed)({
...options,
pattern,
route,
params: allParams,
initialRouteName: configs[route.name]?.initialRouteName
})) : route.name.startsWith("+") || (path += encodeURIComponent(route.name)), focusedParams || (focusedParams = focusedRoute.params), route.state)
path += "/";
else if (focusedParams) {
for (const param in focusedParams)
focusedParams[param] === "undefined" && delete focusedParams[param];
delete focusedParams["#"];
const query = new URLSearchParams(focusedParams).toString();
query && (path += `?${query}`);
}
current = route.state;
}
return path = path.replace(/\/+/g, "/"), path = path.length > 1 ? path.replace(/\/$/, "") : path, options?.path && (path = joinPaths(options.path, path)), path = (0, import_getPathFromState_mods.appendBaseUrl)(path), allParams["#"] && (path += `#${allParams["#"]}`), { path, params: allParams };
}
const joinPaths = (...paths) => [].concat(...paths.map((p) => p.split("/"))).filter(Boolean).join("/"), createConfigItem = (config, parentPattern) => {
if (typeof config == "string")
return { pattern: parentPattern ? joinPaths(parentPattern, config) : config };
if (config.exact && config.path === void 0)
throw new Error(
"A 'path' needs to be specified when specifying 'exact: true'. If you don't want this screen in the URL, specify it as empty string, e.g. `path: ''`."
);
const pattern = config.exact !== !0 ? joinPaths(parentPattern || "", config.path || "") : config.path || "", screens = config.screens ? createNormalizedConfigs(config.screens, pattern) : void 0;
return {
// Normalize pattern to remove any leading, trailing slashes, duplicate slashes etc.
pattern: pattern?.split("/").filter(Boolean).join("/"),
stringify: config.stringify,
screens
};
}, createNormalizedConfigs = (options, pattern) => Object.fromEntries(
Object.entries(options).map(([name, c]) => {
const result = createConfigItem(c, pattern);
return [name, result];
})
);
var getPathFromState_default = getPathFromState;
//# sourceMappingURL=getPathFromState.js.map