-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #257 from zto-sbenning/issue-248
fix(router): fix the sort function of the router
- Loading branch information
Showing
6 changed files
with
259 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* Checks if given route segment is static | ||
* @param {string} segment - The route segment | ||
* @returns {boolean} - Whether the segment is static | ||
*/ | ||
|
||
import type { Route, RouterSchema, RoutesPrioritiesMap } from '~/types' | ||
|
||
export function isStaticRouteSegment(segment: string): boolean { | ||
return !segment.includes('[') | ||
} | ||
|
||
/** | ||
* Checks if given route segment is catch-all | ||
* @param {string} segment - The route segment | ||
* @returns {boolean} - Whether the segment is catch-all | ||
*/ | ||
|
||
export function isCatchAllRouteSegment(segment: string): boolean { | ||
return segment.includes('...') | ||
} | ||
|
||
/** | ||
* Checks if given route segment is dynamic | ||
* @param {string} segment - The route segment | ||
* @returns {boolean} - Whether the segment is dynamic | ||
*/ | ||
|
||
export function isDynamicRouteSegment(segment: string): boolean { | ||
return segment.includes('[') && !isCatchAllRouteSegment(segment) | ||
} | ||
|
||
/** | ||
* Gets priority of a route segment based on its nature | ||
* @param {string} segment - The route segment | ||
* @returns {number} - The priority of the segment | ||
*/ | ||
|
||
export function getRouteSegmentPriority(segment: string): number { | ||
if (isStaticRouteSegment(segment)) { | ||
return 1 | ||
} else if (isDynamicRouteSegment(segment)) { | ||
return 2 | ||
} else if (isCatchAllRouteSegment(segment)) { | ||
return 3 | ||
} | ||
return 0 | ||
} | ||
|
||
/** | ||
* Computes priority of a route based on its segments nature | ||
* @param {Route} route - The route to compute priority for | ||
* @returns {number} - The priority of the route | ||
*/ | ||
|
||
export function computeRoutePriority(route: Route): number { | ||
const segments = route.name.split('/').filter((segment) => segment.length > 0) // filter out empty segments | ||
let priority = '0.' | ||
for (const segment of segments) { | ||
const segmentPriority = getRouteSegmentPriority(segment) | ||
priority += segmentPriority | ||
} | ||
return parseFloat(priority) | ||
} | ||
|
||
/** | ||
* Creates a map of route priorities based on the routing schema | ||
* @param {RouterSchema} schema - The routing schema | ||
* @returns {RoutesPrioritiesMap} - The map of route priorities | ||
*/ | ||
|
||
export function createRoutesPrioritiesMap( | ||
schema: RouterSchema | ||
): RoutesPrioritiesMap { | ||
const routesPrioritiesMap: RoutesPrioritiesMap = {} | ||
const routes = schema.routes[schema.defaultLocale] || [] // No need to create a map for each locale as the names are the same | ||
|
||
for (const route of routes) { | ||
routesPrioritiesMap[route.name] = computeRoutePriority(route) | ||
} | ||
|
||
return routesPrioritiesMap | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { RouterSchema } from '~/types' | ||
import { createRoutesPrioritiesMap } from './route-utils' | ||
|
||
export function sanitizeSchema(schema: RouterSchema) { | ||
const routesPrioritiesMap = createRoutesPrioritiesMap(schema) | ||
Object.keys(schema.routes).forEach((locale) => { | ||
schema.routes[locale] = schema.routes[locale].sort((a, b) => { | ||
const priorityA = routesPrioritiesMap[a.name] | ||
const priorityB = routesPrioritiesMap[b.name] | ||
return priorityA - priorityB | ||
}) | ||
}) | ||
|
||
return schema | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters