UNPKG

one

Version:

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

274 lines (273 loc) 12 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 getStateFromPath_mods_exports = {}; __export(getStateFromPath_mods_exports, { appendIsInitial: () => appendIsInitial, createConfigItemAdditionalProperties: () => createConfigItemAdditionalProperties, decodeURIComponentSafe: () => decodeURIComponentSafe, formatRegexPattern: () => formatRegexPattern, getRouteConfigSorter: () => getRouteConfigSorter, getUrlWithReactNavigationConcessions: () => getUrlWithReactNavigationConcessions, matchForEmptyPath: () => matchForEmptyPath, parseQueryParamsExtended: () => parseQueryParamsExtended, populateParams: () => populateParams, stripBaseUrl: () => stripBaseUrl }); module.exports = __toCommonJS(getStateFromPath_mods_exports); var import_escape_string_regexp = __toESM(require("escape-string-regexp"), 1), import_matchers = require("../router/matchers"); function getUrlWithReactNavigationConcessions(path) { var baseUrl = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : process.env.EXPO_BASE_URL, parsed; try { parsed = new URL(path, "https://phony.example"); } catch { return { path, cleanUrl: "", nonstandardPathname: "", url: new URL("https://phony.example") }; } var pathname = parsed.pathname, withoutBaseUrl = stripBaseUrl(pathname, baseUrl), pathWithoutGroups = (0, import_matchers.stripGroupSegmentsFromPath)(stripBaseUrl(path, baseUrl)); return { // The slashes are at the end, not the beginning path, nonstandardPathname: withoutBaseUrl.replace(/^\/+/g, "").replace(/\/+$/g, "") + "/", url: parsed, pathWithoutGroups }; } function matchForEmptyPath(configs) { var leafNodes = configs.filter(function(config) { return !config.hasChildren; }).map(function(value) { return { ...value, // Collapse all levels of group segments before testing. // This enables `app/(one)/(two)/index.js` to be matched. path: (0, import_matchers.stripGroupSegmentsFromPath)(value.path) }; }), _leafNodes_find, _ref, match = (_ref = (_leafNodes_find = leafNodes.find(function(config) { return ( // NOTE: Test leaf node index routes that either don't have a regex or match an empty string. config.path === "" && (!config.regex || config.regex.test("")) ); })) !== null && _leafNodes_find !== void 0 ? _leafNodes_find : leafNodes.find(function(config) { return ( // NOTE: Test leaf node dynamic routes that match an empty string. config.path.startsWith(":") && config.regex.test("") ); })) !== null && _ref !== void 0 ? _ref : ( // NOTE: Test leaf node deep dynamic routes that match a slash. // This should be done last to enable dynamic routes having a higher priority. leafNodes.find(function(config) { return config.path.startsWith("*") && config.regex.test("/"); }) ); return match; } function appendIsInitial(initialRoutes) { var resolvedInitialPatterns = initialRoutes.map(function(route) { return joinPaths(...route.parentScreens, route.initialRouteName); }); return function(config) { return config.isInitial = resolvedInitialPatterns.includes(config.routeNames.join("/")), config; }; } 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("/"); }; function getRouteConfigSorter() { var previousSegments = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : []; return function(a, b) { if (a.pattern === b.pattern) return b.routeNames.join(">").localeCompare(a.routeNames.join(">")); if (a.pattern.startsWith(b.pattern) && !b.isIndex) return -1; if (b.pattern.startsWith(a.pattern) && !a.isIndex) return 1; if (a.type === "static" && b.type !== "static") return -1; if (a.type !== "static" && b.type === "static") return 1; var similarToPreviousA = previousSegments.filter(function(value, index) { return value === a.expandedRouteNames[index] && value.startsWith("(") && value.endsWith(")"); }), similarToPreviousB = previousSegments.filter(function(value, index) { return value === b.expandedRouteNames[index] && value.startsWith("(") && value.endsWith(")"); }); if ((similarToPreviousA.length > 0 || similarToPreviousB.length > 0) && similarToPreviousA.length !== similarToPreviousB.length) return similarToPreviousB.length - similarToPreviousA.length; for (var i = 0; i < Math.max(a.parts.length, b.parts.length); i++) { if (a.parts[i] == null) return 1; if (b.parts[i] == null) return -1; var aWildCard = a.parts[i].startsWith("*"), bWildCard = b.parts[i].startsWith("*"); if (aWildCard && bWildCard) { var aNotFound = a.parts[i].match(/^[*]not-found$/), bNotFound = b.parts[i].match(/^[*]not-found$/); if (aNotFound && bNotFound) continue; if (aNotFound) return 1; if (bNotFound) return -1; continue; } if (aWildCard) return 1; if (bWildCard) return -1; var aSlug = a.parts[i].startsWith(":"), bSlug = b.parts[i].startsWith(":"); if (aSlug && bSlug) { var aNotFound1 = a.parts[i].match(/^[*]not-found$/), bNotFound1 = b.parts[i].match(/^[*]not-found$/); if (aNotFound1 && bNotFound1) continue; if (aNotFound1) return 1; if (bNotFound1) return -1; continue; } if (aSlug) return 1; if (bSlug) return -1; } return a.isInitial && !b.isInitial ? -1 : !a.isInitial && b.isInitial ? 1 : b.parts.length - a.parts.length; }; } function formatRegexPattern(it) { return it = it.replace(" ", "%20"), it.startsWith(":") ? `(([^/]+\\/)${it.endsWith("?") ? "?" : ""})` : it.startsWith("*") ? `((.*\\/)${it.endsWith("?") ? "?" : ""})` : (0, import_matchers.matchGroupName)(it) != null ? `(?:${escape(it)}\\/)?` : (0, import_escape_string_regexp.default)(it) + "\\/"; } function decodeURIComponentSafe(str) { try { return decodeURIComponent(str); } catch { return str; } } function populateParams(routes, params) { if (!(!routes || !params || Object.keys(params).length === 0)) { var _iteratorNormalCompletion = !0, _didIteratorError = !1, _iteratorError = void 0; try { for (var _iterator = routes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0) { var route = _step.value; Object.assign(route, { params }); } } catch (err) { _didIteratorError = !0, _iteratorError = err; } finally { try { !_iteratorNormalCompletion && _iterator.return != null && _iterator.return(); } finally { if (_didIteratorError) throw _iteratorError; } } return routes; } } function createConfigItemAdditionalProperties(screen, pattern, routeNames) { var config = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : {}, _Object_keys, parts = [], isDynamic = !1, isIndex = screen === "index" || screen.endsWith("/index"), _iteratorNormalCompletion = !0, _didIteratorError = !1, _iteratorError = void 0; try { for (var _iterator = pattern.split("/")[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0) { var part = _step.value; part && (isDynamic || (isDynamic = part.startsWith(":") || part.startsWith("*") || part.includes("*not-found")), (0, import_matchers.matchGroupName)(part) || parts.push(part)); } } catch (err) { _didIteratorError = !0, _iteratorError = err; } finally { try { !_iteratorNormalCompletion && _iterator.return != null && _iterator.return(); } finally { if (_didIteratorError) throw _iteratorError; } } var hasChildren = config.screens ? !!(!((_Object_keys = Object.keys(config.screens)) === null || _Object_keys === void 0) && _Object_keys.length) : !1, type = hasChildren ? "layout" : isDynamic ? "dynamic" : "static"; return isIndex && parts.push("index"), { type, isIndex, hasChildren, parts, userReadableName: [ ...routeNames.slice(0, -1), config.path || screen ].join("/"), expandedRouteNames: routeNames.flatMap(function(name) { return name.split("/"); }) }; } function parseQueryParamsExtended(path, route, parseConfig, hash) { var searchParams = new URL(path, "https://phony.example").searchParams, params = /* @__PURE__ */ Object.create(null); hash && (params["#"] = hash.slice(1)); var _iteratorNormalCompletion = !0, _didIteratorError = !1, _iteratorError = void 0; try { for (var _loop = function() { var name = _step.value, _route_params; if (!((_route_params = route.params) === null || _route_params === void 0) && _route_params[name]) process.env.NODE_ENV !== "production" && console.warn(`Route '/${route.name}' with param '${name}' was specified both in the path and as a param, removing from path`); else { var values = parseConfig != null && parseConfig.hasOwnProperty(name) ? searchParams.getAll(name).map(function(value) { return parseConfig[name](value); }) : searchParams.getAll(name); params[name] = values.length === 1 ? values[0] : values; } }, _iterator = searchParams.keys()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0) _loop(); } catch (err) { _didIteratorError = !0, _iteratorError = err; } finally { try { !_iteratorNormalCompletion && _iterator.return != null && _iterator.return(); } finally { if (_didIteratorError) throw _iteratorError; } } return Object.keys(params).length ? params : void 0; } function stripBaseUrl(path) { var baseUrl = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : process.env.EXPO_BASE_URL; return process.env.NODE_ENV !== "development" && baseUrl ? path.replace(/^\/+/g, "/").replace(new RegExp(`^\\/?${escape(baseUrl)}`, "g"), "") : path; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { appendIsInitial, createConfigItemAdditionalProperties, decodeURIComponentSafe, formatRegexPattern, getRouteConfigSorter, getUrlWithReactNavigationConcessions, matchForEmptyPath, parseQueryParamsExtended, populateParams, stripBaseUrl }); //# sourceMappingURL=getStateFromPath-mods.js.map