Last active
February 2, 2021 15:55
-
-
Save MarcBrillault/87f8df9610cfa3ed112afc3f8da474e8 to your computer and use it in GitHub Desktop.
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
const appRoutes: Routes = [ | |
{ | |
path: '', | |
component: HomepageComponent, | |
}, | |
{ | |
matcher: UrlMatcher, | |
component: ProductComponent, | |
}, | |
{ | |
path: '**', | |
component: Error404Component, | |
}, | |
]; |
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
import { UrlMatchResult, UrlSegment } from '@angular/router'; | |
export function UrlMatcher(url: UrlSegment[], regex: RegExp, capturedParameterNames: string[]): urlMatchResult | null { | |
if (url.length === 0) { | |
return null; | |
} | |
capturedParameterNames.splice(0, 0, 'fullPath'); | |
let fullPath = ''; | |
let result: RegExpExecArray | null; | |
for (let i = 0; i < url.length; ++i) { | |
if (url[i].path) { | |
fullPath += '/' + url[i].path; | |
} | |
} | |
if (result = regex.exec(fullPath)) { | |
const posParams: {[p: string]: UrlSegment} = {}; | |
for (let index = 0; index < result.length; index++) { | |
posParams[capturedParameterNames[index]] = new UrlSegment(result[index], {}); | |
} | |
return {consumed: url, posParams: posParams}; | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For mine use case where a matcher can have optionally parameter