UNPKG

one

Version:

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

239 lines (237 loc) 9.78 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__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: true }) : target, mod)); var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var getPathFromState_exports = {}; __export(getPathFromState_exports, { default: () => getPathFromState_default, getPathDataFromState: () => getPathDataFromState, getPathFromState: () => getPathFromState }); module.exports = __toCommonJS(getPathFromState_exports); var sharedModUtils = __toESM(require("./_shared.native.js"), 1); var import_getPathFromState_mods = require("./getPathFromState-mods.native.js"); var import_validatePathConfig = require("./validatePathConfig.native.js"); var NAV_PARAM_SCREEN = "screen"; var NAV_PARAM_PARAMS = "params"; var NAV_PARAM_KEY = "key"; var getActiveRoute = function (state) { var route = typeof state.index === "number" ? state.routes[state.index] : state.routes[state.routes.length - 1]; if (route.state) { return getActiveRoute(route.state); } return route; }; var cachedNormalizedConfigs = /* @__PURE__ */new WeakMap(); var getNormalizedConfigs = function (options) { if (!(options === null || options === void 0 ? void 0 : options.screens)) return {}; var cached = cachedNormalizedConfigs.get(options === null || options === void 0 ? void 0 : options.screens); if (cached) return cached; var normalizedConfigs = createNormalizedConfigs(options.screens); cachedNormalizedConfigs.set(options.screens, normalizedConfigs); return normalizedConfigs; }; function getPathFromState(state, options) { return getPathDataFromState(state, options).path; } function getPathDataFromState(state, options) { var _loop = function () { var _loop2 = function () { pattern = currentOptions[route.name].pattern; nestedRouteNames.push(route.name); if (route.params) { var _currentOptions_route_name; var stringify = (_currentOptions_route_name = currentOptions[route.name]) === null || _currentOptions_route_name === void 0 ? void 0 : _currentOptions_route_name.stringify; var currentParams = Object.fromEntries(Object.entries(route.params).flatMap(function (param2) { var [key, value] = param2; if (key === NAV_PARAM_SCREEN || key === NAV_PARAM_PARAMS || key === NAV_PARAM_KEY) { return []; } return [[key, (stringify === null || stringify === void 0 ? void 0 : stringify[key]) ? stringify[key](value) : Array.isArray(value) ? value.map(String) : typeof value === "undefined" ? value : String(value)]]; })); Object.assign(allParams, currentParams); if (focusedRoute === route) { focusedParams = { ...currentParams }; pattern === null || pattern === void 0 ? void 0 : pattern.split("/").filter(function (p) { return sharedModUtils.isDynamicPart(p); }).forEach(function (p) { var name = sharedModUtils.getParamName(p); if (focusedParams) { delete focusedParams[name]; } }); } } if (!currentOptions[route.name].screens || route.state === void 0) { var _route_params_NAV_PARAM_SCREEN, _currentOptions_route_name_screens; var screens = currentOptions[route.name].screens; var screen = route.params && NAV_PARAM_SCREEN in route.params ? (_route_params_NAV_PARAM_SCREEN = route.params[NAV_PARAM_SCREEN]) === null || _route_params_NAV_PARAM_SCREEN === void 0 ? void 0 : _route_params_NAV_PARAM_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 ? 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[NAV_PARAM_PARAMS] }; currentOptions = screens; } else { hasNext = false; } } else { index = typeof route.state.index === "number" ? route.state.index : route.state.routes.length - 1; var nextRoute = route.state.routes[index]; var nestedConfig = currentOptions[route.name].screens; if (nestedConfig && nextRoute.name in nestedConfig) { route = nextRoute; currentOptions = nestedConfig; } else { hasNext = false; } } }; var _current_routes; if (!((_current_routes = current.routes) === null || _current_routes === void 0 ? void 0 : _current_routes.length)) { return "break"; } var index = typeof current.index === "number" ? current.index : 0; var route = current.routes[index]; var pattern = void 0; var focusedParams = void 0; var focusedRoute = getActiveRoute(state); var currentOptions = configs; var nestedRouteNames = []; var hasNext = true; while (route.name in currentOptions && hasNext) _loop2(); if (currentOptions[route.name] !== void 0) { var _configs_route_name; if (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 if (!route.name.startsWith("+")) { path += encodeURIComponent(route.name); } if (!focusedParams) { focusedParams = { ...focusedRoute.params }; } if (route.state) { path += "/"; } else if (focusedParams) { for (var param in focusedParams) { if (focusedParams[param] === "undefined") { delete focusedParams[param]; } } delete focusedParams["#"]; var query = new URLSearchParams(focusedParams).toString(); if (query) { path += `?${query}`; } } current = route.state; }; if (state == null) { throw Error("Got 'undefined' for the navigation state. You must pass a valid state object."); } if (options) { (0, import_validatePathConfig.validatePathConfig)(options); } var configs = getNormalizedConfigs(options); var path = "/"; var current = state; var allParams = {}; while (current) { var _ret = _loop(); if (_ret === "break") break; } path = path.replace(/\/+/g, "/"); path = path.length > 1 ? path.replace(/\/$/, "") : path; if (options === null || options === void 0 ? void 0 : options.path) { path = joinPaths(options.path, path); } path = (0, import_getPathFromState_mods.appendBaseUrl)(path); if (allParams["#"]) { path += `#${allParams["#"]}`; } return { 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("/"); }; var 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 !== true ? joinPaths(parentPattern || "", config.path || "") : config.path || ""; var screens = config.screens ? createNormalizedConfigs(config.screens, pattern1) : void 0; return { // Normalize pattern to remove any leading, trailing slashes, duplicate slashes etc. pattern: pattern1 === null || pattern1 === void 0 ? void 0 : pattern1.split("/").filter(Boolean).join("/"), stringify: config.stringify, screens }; }; var createNormalizedConfigs = function (options, pattern) { return Object.fromEntries(Object.entries(options).map(function (param) { var [name, c] = param; var result = createConfigItem(c, pattern); return [name, result]; })); }; var getPathFromState_default = getPathFromState; //# sourceMappingURL=getPathFromState.native.js.map