-
-
Save W3SS/c66a2a2d26b4247704d625c89050811b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| * Copyright (c) Matan Shukry | |
| * All rights reserved. | |
| */ | |
| import { UrlSegment, UrlSegmentGroup, Route } from '@angular/router'; | |
| // export type UrlMatchResult = { | |
| // consumed: UrlSegment[]; posParams?: { [name: string]: UrlSegment }; | |
| // }; | |
| export function ComplexUrlMatcher(paramName: string, regex: RegExp) { | |
| return ( | |
| segments: UrlSegment[], | |
| segmentGroup: UrlSegmentGroup, | |
| route: Route) => { | |
| const parts = [regex]; | |
| const posParams: { [key: string]: UrlSegment } = {}; | |
| const consumed: UrlSegment[] = []; | |
| let currentIndex = 0; | |
| for (let i = 0; i < parts.length; ++i) { | |
| if (currentIndex >= segments.length) { | |
| return null; | |
| } | |
| const current = segments[currentIndex]; | |
| const part = parts[i]; | |
| if (!part.test(current.path)) { | |
| return null; | |
| } | |
| posParams[paramName] = current; | |
| consumed.push(current); | |
| currentIndex++; | |
| } | |
| if (route.pathMatch === 'full' && | |
| (segmentGroup.hasChildren() || currentIndex < segments.length)) { | |
| return null; | |
| } | |
| return { consumed, posParams }; | |
| } | |
| } |
This file contains hidden or 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
| /** | |
| * Copyright (c) Matan Shukry | |
| * All rights reserved. | |
| */ | |
| export const UserRoutes: Routes = [ | |
| { | |
| path: 'users', | |
| component: UserComponent, | |
| children: [ | |
| { | |
| path: '', | |
| component: UserListComponent | |
| }, | |
| { | |
| matcher: ComplexUrlMatcher("id", /[0-9]+/), | |
| component: UserItemComponent | |
| }, | |
| ] | |
| } | |
| ]; | |
| @NgModule({ | |
| imports: [RouterModule.forChild(UserRoutes)], | |
| exports: [RouterModule] | |
| }) | |
| export class UserRoutingModule { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment