UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

152 lines (151 loc) 8.65 kB
"use strict"; 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"), getActiveRoute = function(state) { var 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 = function(options) { if (!(options != null && options.screens)) return {}; var cached = cachedNormalizedConfigs.get(options == null ? void 0 : options.screens); if (cached) return cached; var normalizedConfigs = createNormalizedConfigs(options.screens); return cachedNormalizedConfigs.set(options.screens, normalizedConfigs), normalizedConfigs; }; function getPathFromState(state, options) { return getPathDataFromState(state, options).path; } function getPathDataFromState(state, options) { var _loop = function() { for (var _loop2 = function() { if (pattern = currentOptions[route.name].pattern, nestedRouteNames.push(route.name), route.params) { var _currentOptions_route_name, stringify = (_currentOptions_route_name = currentOptions[route.name]) === null || _currentOptions_route_name === void 0 ? void 0 : _currentOptions_route_name.stringify, currentParams = Object.fromEntries(Object.entries(route.params).flatMap(function(param2) { var [key, value] = param2; return key === "screen" || key === "params" ? [] : [ [ key, stringify != null && 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 == null || pattern.split("/").filter(function(p) { return sharedModUtils.isDynamicPart(p); }).forEach(function(p) { var name = sharedModUtils.getParamName(p); focusedParams && delete focusedParams[name]; })); } if (!currentOptions[route.name].screens || route.state === void 0) { var _route_params_screen, _currentOptions_route_name_screens, screens = currentOptions[route.name].screens, screen = route.params && "screen" in route.params ? (_route_params_screen = route.params.screen) === null || _route_params_screen === void 0 ? void 0 : _route_params_screen.toString() : screens ? Object.keys(screens)[0] : void 0; if (screen && screens && (!((_currentOptions_route_name_screens = currentOptions[route.name].screens) === null || _currentOptions_route_name_screens === void 0) && _currentOptions_route_name_screens[screen])) { var _route_params; route = { ...screens[screen], name: screen, key: screen, params: (_route_params = route.params) === null || _route_params === void 0 ? void 0 : _route_params.params }, currentOptions = screens; } else hasNext = !1; } else { index = typeof route.state.index == "number" ? route.state.index : route.state.routes.length - 1; var nextRoute = route.state.routes[index], nestedConfig = currentOptions[route.name].screens; nestedConfig && nextRoute.name in nestedConfig ? (route = nextRoute, currentOptions = nestedConfig) : hasNext = !1; } }, index = typeof current.index == "number" ? current.index : 0, route = current.routes[index], pattern = void 0, focusedParams = void 0, focusedRoute = getActiveRoute(state), currentOptions = configs, nestedRouteNames = [], hasNext = !0; route.name in currentOptions && hasNext; ) _loop2(); if (currentOptions[route.name] !== void 0) { var _configs_route_name; pattern === void 0 && (pattern = nestedRouteNames.join("/")), path += (0, import_getPathFromState_mods.getPathWithConventionsCollapsed)({ ...options, pattern, route, params: allParams, initialRouteName: (_configs_route_name = configs[route.name]) === null || _configs_route_name === void 0 ? void 0 : _configs_route_name.initialRouteName }); } else route.name.startsWith("+") || (path += encodeURIComponent(route.name)); if (focusedParams || (focusedParams = focusedRoute.params), route.state) path += "/"; else if (focusedParams) { for (var param in focusedParams) focusedParams[param] === "undefined" && delete focusedParams[param]; delete focusedParams["#"]; var query = new URLSearchParams(focusedParams).toString(); query && (path += `?${query}`); } current = route.state; }; if (state == null) throw Error("Got 'undefined' for the navigation state. You must pass a valid state object."); options && (0, import_validatePathConfig.validatePathConfig)(options); for (var configs = getNormalizedConfigs(options), path = "/", current = state, allParams = {}; current; ) _loop(); return path = path.replace(/\/+/g, "/"), path = path.length > 1 ? path.replace(/\/$/, "") : path, options != null && options.path && (path = joinPaths(options.path, path)), path = (0, import_getPathFromState_mods.appendBaseUrl)(path), allParams["#"] && (path += `#${allParams["#"]}`), { path, params: allParams }; } var joinPaths = function() { for (var _len = arguments.length, paths = new Array(_len), _key = 0; _key < _len; _key++) paths[_key] = arguments[_key]; return [].concat(...paths.map(function(p) { return p.split("/"); })).filter(Boolean).join("/"); }, createConfigItem = function(config, parentPattern) { if (typeof config == "string") { var pattern = parentPattern ? joinPaths(parentPattern, config) : config; return { pattern }; } 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: ''`."); var pattern1 = config.exact !== !0 ? joinPaths(parentPattern || "", config.path || "") : config.path || "", screens = config.screens ? createNormalizedConfigs(config.screens, pattern1) : void 0; return { // Normalize pattern to remove any leading, trailing slashes, duplicate slashes etc. pattern: pattern1 == null ? void 0 : pattern1.split("/").filter(Boolean).join("/"), stringify: config.stringify, screens }; }, createNormalizedConfigs = function(options, pattern) { return Object.fromEntries(Object.entries(options).map(function(param) { var [name, c] = param, result = createConfigItem(c, pattern); return [ name, result ]; })); }, getPathFromState_default = getPathFromState; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { getPathDataFromState, getPathFromState }); //# sourceMappingURL=getPathFromState.js.map