UNPKG

one

Version:

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

138 lines (137 loc) 7.06 kB
import escape_ from "escape-string-regexp"; import { matchGroupName, stripGroupSegmentsFromPath } from "../router/matchers.mjs"; function getUrlWithReactNavigationConcessions(path, baseUrl = process.env.EXPO_BASE_URL) { const pathWithoutGroups = stripGroupSegmentsFromPath(stripBaseUrl(path, baseUrl)); let pathname = "", hash = ""; try { const parsed = new URL(path, "file:"); pathname = parsed.pathname, hash = parsed.hash; } catch {} const withoutBaseUrl = stripBaseUrl(pathname, baseUrl); return { path, // Make sure there is a trailing slash // The slashes are at the end, not the beginning nonstandardPathname: withoutBaseUrl.replace(/^\/+/g, "").replace(/\/+$/g, "") + "/", hash, pathWithoutGroups }; } function matchForEmptyPath(configs) { const leafNodes = configs.filter(config => !config.hasChildren).map(value => ({ ...value, // Collapse all levels of group segments before testing. // This enables `app/(one)/(two)/index.js` to be matched. path: stripGroupSegmentsFromPath(value.path) })); return leafNodes.find(config => // 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(""))) ?? leafNodes.find(config => // NOTE: Test leaf node dynamic routes that match an empty string. config.path.startsWith(":") && config.regex.test("")) ?? // 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(config => config.path.startsWith("*") && config.regex.test("/")); } function appendIsInitial(initialRoutes) { const resolvedInitialPatterns = initialRoutes.map(route => joinPaths(...route.parentScreens, route.initialRouteName)); return config => (config.isInitial = resolvedInitialPatterns.includes(config.routeNames.join("/")), config); } const joinPaths = (...paths) => [].concat(...paths.map(p => p.split("/"))).filter(Boolean).join("/"); function getRouteConfigSorter(previousSegments = []) { 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; if (a.staticPartCount !== b.staticPartCount) return b.staticPartCount - a.staticPartCount; const similarToPreviousA = previousSegments.filter((value, index) => value === a.expandedRouteNames[index] && value.startsWith("(") && value.endsWith(")")), similarToPreviousB = previousSegments.filter((value, index) => 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 (let 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; const aWildCard = a.parts[i].startsWith("*"), bWildCard = b.parts[i].startsWith("*"); if (aWildCard && bWildCard) { const 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; const aSlug = a.parts[i].startsWith(":"), bSlug = b.parts[i].startsWith(":"); if (aSlug && bSlug) { const 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 (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("?") ? "?" : ""})` : matchGroupName(it) != null ? `(?:${escape(it)}\\/)?` : escape_(it) + "\\/"; } function decodeURIComponentSafe(str) { try { return decodeURIComponent(str); } catch { return str; } } function populateParams(routes, params) { if (!(!routes || !params || Object.keys(params).length === 0)) { for (const route of routes) Object.assign(route, { params }); return routes; } } function createConfigItemAdditionalProperties(screen, pattern, routeNames, config = {}) { const parts = []; let isDynamic = !1, staticPartCount = 0; const isIndex = screen === "index" || screen.endsWith("/index"); for (const part of pattern.split("/")) if (part) { const isDynamicPart = part.startsWith(":") || part.startsWith("*") || part.includes("*not-found"); isDynamic ||= isDynamicPart, matchGroupName(part) || (parts.push(part), isDynamicPart || staticPartCount++); } const hasChildren = config.screens ? !!Object.keys(config.screens)?.length : !1, type = hasChildren ? "layout" : isDynamic ? "dynamic" : "static"; return isIndex && (parts.push("index"), staticPartCount++), { type, isIndex, hasChildren, parts, staticPartCount, userReadableName: [...routeNames.slice(0, -1), config.path || screen].join("/"), expandedRouteNames: routeNames.flatMap(name => name.split("/")) }; } function parseQueryParamsExtended(path, route, parseConfig, hash) { const searchParams = new URL(path, "file:").searchParams, params = /* @__PURE__ */Object.create(null); hash && (params["#"] = hash.slice(1)); for (const name of searchParams.keys()) if (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 { const values = parseConfig?.hasOwnProperty(name) ? searchParams.getAll(name).map(value => parseConfig[name](value)) : searchParams.getAll(name); params[name] = values.length === 1 ? values[0] : values; } return Object.keys(params).length ? params : void 0; } function stripBaseUrl(path, baseUrl = process.env.EXPO_BASE_URL) { return process.env.NODE_ENV !== "development" && baseUrl ? path.replace(/^\/+/g, "/").replace(new RegExp(`^\\/?${escape(baseUrl)}`, "g"), "") : path; } export { appendIsInitial, createConfigItemAdditionalProperties, decodeURIComponentSafe, formatRegexPattern, getRouteConfigSorter, getUrlWithReactNavigationConcessions, matchForEmptyPath, parseQueryParamsExtended, populateParams, stripBaseUrl }; //# sourceMappingURL=getStateFromPath-mods.mjs.map