Skip to content

Instantly share code, notes, and snippets.

@tytskyi
Forked from matanshukry/complex.url.matcher.ts
Created May 13, 2017 21:51
Show Gist options
  • Save tytskyi/2e4ae91fd8f13164a08c70046f1b6f43 to your computer and use it in GitHub Desktop.
Save tytskyi/2e4ae91fd8f13164a08c70046f1b6f43 to your computer and use it in GitHub Desktop.
/**
* 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 };
}
}
/**
* 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 { }
@FabienDehopre
Copy link

This doesn't work with AOT.
Here is the error message produced when compiling with AOT Error encountered resolving symbol values statically. Expression form not supported

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment