UNPKG

@jay-js/system

Version:

A powerful and flexible TypeScript library for UI, state management, lazy loading, routing and managing draggable elements in modern web applications.

67 lines 3.02 kB
import { createMatcher } from "../../utils/helpers"; import { resolvedRoutes } from "../configuration"; export function getPotentialMatch(pathName) { const potentialMatches = getPotentialMatches(pathName); const firstRoute = resolvedRoutes.values().next().value; if (potentialMatches.length === 0) { return { route: firstRoute, result: null }; } // Sort matches by specificity (more segments is more specific) // This is a better approach than just comparing result length potentialMatches.sort((a, b) => { // Count number of path segments const aSegments = a.route.path.split("/").filter(Boolean).length; const bSegments = b.route.path.split("/").filter(Boolean).length; // More segments = higher priority if (aSegments !== bSegments) { return bSegments - aSegments; } // If same number of segments, check for parameters (static routes are more specific) const aParams = a.route.path.match(/:[^/]+/g) || []; const bParams = b.route.path.match(/:[^/]+/g) || []; // Fewer parameters = higher priority (more static segments) return aParams.length - bParams.length; }); return potentialMatches[0]; } export function getPotentialMatches(pathName) { // Normalize path (remove trailing slash except for root) if (pathName !== "/" && pathName.substring(pathName.length - 1) === "/") { pathName = pathName.substring(0, pathName.length - 1); } const potentialMatches = []; for (const route of resolvedRoutes.values()) { // Use path-to-regexp matcher for better matching const matcher = createMatcher(route.path); const matchResult = matcher(pathName); if (matchResult) { // Extract param values and ensure they are strings const paramValues = Object.values(matchResult.params || {}) .map((value) => (typeof value === "string" ? value : Array.isArray(value) ? value.join("/") : String(value))) .filter(Boolean); potentialMatches.push({ route: route, result: [pathName, ...paramValues], }); } } return potentialMatches; } export function getPotentialMatchIndex(pathName) { const potentialMatches = getPotentialMatches(pathName); if (potentialMatches.length === 0) { const firstRoute = resolvedRoutes.values().next().value; // If no matches, return the first route as a fallback // This is useful for root navigation or when no routes are registered if (!firstRoute) { throw new Error("No routes registered and no fallback route available."); } return { route: firstRoute, result: [location.pathname] }; } if (potentialMatches.length === 2) { return potentialMatches[1]; } // Return the most specific match (first after sorting) return potentialMatches[0]; } //# sourceMappingURL=get-potential-match.js.map