-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
made changes to normalizePath function within routeUtils.ts to better…
… remove leading slashes
- Loading branch information
1 parent
c438d80
commit 5849313
Showing
1 changed file
with
15 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,37 @@ | ||
// Define the supported locales. Adjust as needed to match your project. | ||
const LOCALES = ['en', 'fr', 'de']; | ||
|
||
export function isRouteActive(routePath: string): boolean { | ||
const route = useRoute(); | ||
/** | ||
* Normalize a given path by removing leading and trailing slashes. | ||
*/ | ||
function normalizePath(path: string): string { | ||
return path.replace(/^\/|\/$/g, ''); | ||
} | ||
|
||
/** | ||
* Remove the leading locale segment from the given route segments if present. | ||
* For example, ['en', 'organizations'] -> ['organizations']. | ||
*/ | ||
function removeLocaleSegment(segments: string[]): string[] { | ||
if (segments.length > 0 && LOCALES.includes(segments[0])) { | ||
return segments.slice(1); | ||
} | ||
return segments; | ||
} | ||
|
||
/** | ||
* Remove locale prefixes from route names that follow the pattern "locale___restOfTheRoute". | ||
* For example, "en___organizations" -> "organizations". | ||
*/ | ||
function removeLocaleFromRouteName(routeName: string): string { | ||
const parts = routeName.split('___'); | ||
if (parts.length > 1 && LOCALES.includes(parts[0])) { | ||
return parts.slice(1).join('___'); | ||
} | ||
return routeName; | ||
} | ||
|
||
export function isRouteActive(routePath: string): boolean { | ||
const route = useRoute(); | ||
|
||
const currentPath = normalizePath(route.path); | ||
const targetPath = normalizePath(routePath); | ||
|
||
let currentSegments = currentPath.split('/'); | ||
let targetSegments = targetPath.split('/'); | ||
// Handle prefixed routes by ignoring the base path | ||
// Split paths into segments and compare relative paths | ||
const currentSegments = currentPath.split('/'); | ||
const targetSegments = targetPath.split('/'); | ||
|
||
// Remove locale segments from both current and target paths | ||
currentSegments = removeLocaleSegment(currentSegments); | ||
targetSegments = removeLocaleSegment(targetSegments); | ||
|
||
// If current route is shorter than the target, it cannot match | ||
if (currentSegments.length < targetSegments.length) { | ||
return false; | ||
} | ||
|
||
// Check if the target segments match the end of the current segments | ||
// Check if the target segments match the corresponding end of current segments | ||
return targetSegments.every( | ||
(segment, index) => | ||
currentSegments[currentSegments.length - targetSegments.length + index] === segment | ||
); | ||
} | ||
|
||
export function isCurrentRoutePathSubpageOf(path: string, routeName: string): boolean { | ||
const baseName = removeLocaleFromRouteName(routeName); | ||
|
||
// Split the baseName on `path + '-'` to find a subpage if one exists | ||
// For example, if path = 'organizations' and routeName = 'organizations-subpage', | ||
// splitting on 'organizations-' gives ['', 'subpage']. | ||
const segments = baseName.split(`${path}-`); | ||
export function isCurrentRoutePathSubpageOf(path: string, routeName: string) { | ||
// The first split is to remove the localization path. | ||
const segments = routeName.split("___")[0].split(path + "-"); | ||
const subpage = segments.length > 1 ? segments[1] : ""; | ||
|
||
// Check that this subpage is valid and not one of the excluded routes | ||
return subpage !== "search" && subpage !== "create" && subpage.length > 0; | ||
} | ||
|
||
export function currentRoutePathIncludes(path: string, routeName: string): boolean { | ||
const baseName = removeLocaleFromRouteName(routeName); | ||
return baseName.includes(path); | ||
export function currentRoutePathIncludes( | ||
path: string, | ||
routeName: string | ||
): boolean { | ||
return routeName.includes(path); | ||
} |