refactor(router): cleanup & simplifications
This commit is contained in:
@ -108,34 +108,36 @@ export function isNavigationCancelingError(error: Error) {
|
||||
return (error as any)[NAVIGATION_CANCELING_ERROR];
|
||||
}
|
||||
|
||||
// Matches the route configuration (`route`) against the actual URL (`segments`).
|
||||
export function defaultUrlMatcher(
|
||||
segments: UrlSegment[], segmentGroup: UrlSegmentGroup, route: Route): UrlMatchResult {
|
||||
const path = route.path;
|
||||
const parts = path.split('/');
|
||||
const posParams: {[key: string]: UrlSegment} = {};
|
||||
const consumed: UrlSegment[] = [];
|
||||
segments: UrlSegment[], segmentGroup: UrlSegmentGroup, route: Route): UrlMatchResult|null {
|
||||
const parts = route.path.split('/');
|
||||
|
||||
let currentIndex = 0;
|
||||
|
||||
for (let i = 0; i < parts.length; ++i) {
|
||||
if (currentIndex >= segments.length) return null;
|
||||
const current = segments[currentIndex];
|
||||
|
||||
const p = parts[i];
|
||||
const isPosParam = p.startsWith(':');
|
||||
|
||||
if (!isPosParam && p !== current.path) return null;
|
||||
if (isPosParam) {
|
||||
posParams[p.substring(1)] = current;
|
||||
}
|
||||
consumed.push(current);
|
||||
currentIndex++;
|
||||
if (parts.length > segments.length) {
|
||||
// The actual URL is shorter than the config, no match
|
||||
return null;
|
||||
}
|
||||
|
||||
if (route.pathMatch === 'full' &&
|
||||
(segmentGroup.hasChildren() || currentIndex < segments.length)) {
|
||||
(segmentGroup.hasChildren() || parts.length < segments.length)) {
|
||||
// The config is longer than the actual URL but we are looking for a full match, return null
|
||||
return null;
|
||||
} else {
|
||||
return {consumed, posParams};
|
||||
}
|
||||
|
||||
const posParams: {[key: string]: UrlSegment} = {};
|
||||
|
||||
// Check each config part against the actual URL
|
||||
for (let index = 0; index < parts.length; index++) {
|
||||
const part = parts[index];
|
||||
const segment = segments[index];
|
||||
const isParameter = part.startsWith(':');
|
||||
if (isParameter) {
|
||||
posParams[part.substring(1)] = segment;
|
||||
} else if (part !== segment.path) {
|
||||
// The actual URL part does not match the config, no match
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return {consumed: segments.slice(0, parts.length), posParams};
|
||||
}
|
||||
|
Reference in New Issue
Block a user