@@ -64,6 +64,15 @@ export class RouteTrie<T> {
6464 return [ route , params ] ;
6565 }
6666
67+ /**
68+ * Returns all routes that match the given path and their parameter values.
69+ */
70+ matchAll ( path : string ) : Array < [ T , Record < string , string > ] > {
71+ const matches : Array < [ T , Record < string , string > ] > = [ ] ;
72+ this . collectRoutes ( path , { } , matches ) ;
73+ return matches ;
74+ }
75+
6776 /**
6877 * Walks the route trie and calls a callback function for each route.
6978 */
@@ -165,6 +174,54 @@ export class RouteTrie<T> {
165174 return undefined ;
166175 }
167176
177+ private collectRoutes (
178+ urlPath : string ,
179+ params : Record < string , string > ,
180+ results : Array < [ T , Record < string , string > ] >
181+ ) {
182+ urlPath = this . normalizePath ( urlPath ) ;
183+ if ( urlPath === '' ) {
184+ if ( this . route ) {
185+ results . push ( [ this . route , { ...params } ] ) ;
186+ }
187+ if ( this . optCatchAllNodes ) {
188+ const optParams = { ...params } ;
189+ if ( urlPath ) {
190+ optParams [ this . optCatchAllNodes . name ] = urlPath ;
191+ }
192+ results . push ( [ this . optCatchAllNodes . route , optParams ] ) ;
193+ }
194+ return ;
195+ }
196+
197+ const [ head , tail ] = this . splitPath ( urlPath ) ;
198+
199+ const child = this . children [ head ] ;
200+ if ( child ) {
201+ child . collectRoutes ( tail , { ...params } , results ) ;
202+ }
203+
204+ if ( this . paramNodes ) {
205+ for ( const paramChild of Object . values ( this . paramNodes ) ) {
206+ const paramParams = { ...params , [ paramChild . name ] : head } ;
207+ paramChild . trie . collectRoutes ( tail , paramParams , results ) ;
208+ }
209+ }
210+
211+ if ( this . catchAllNodes ) {
212+ const caParams = { ...params , [ this . catchAllNodes . name ] : urlPath } ;
213+ results . push ( [ this . catchAllNodes . route , caParams ] ) ;
214+ }
215+
216+ if ( this . optCatchAllNodes ) {
217+ const ocParams = { ...params } ;
218+ if ( urlPath ) {
219+ ocParams [ this . optCatchAllNodes . name ] = urlPath ;
220+ }
221+ results . push ( [ this . optCatchAllNodes . route , ocParams ] ) ;
222+ }
223+ }
224+
168225 /**
169226 * Normalizes a path for inclusion into the route trie.
170227 */
0 commit comments