@@ -27,12 +27,12 @@ export function href<Path extends keyof Args>(
2727 ...args : Args [ Path ]
2828) : string {
2929 let params = args [ 0 ] ;
30- let result = trimEndSplat ( path ) // Ignore trailing / and /*, we'll handle it below
30+ let result = trimTrailingSplat ( path ) // Ignore trailing / and /*, we'll handle it below
3131 . replace (
3232 / \/ : ( [ \w - ] + ) ( \? ) ? / g, // same regex as in .\router\utils.ts: compilePath().
3333 ( _ : string , param : string , questionMark : string | undefined ) => {
3434 const isRequired = questionMark === undefined ;
35- const value = params ? params [ param ] : undefined ;
35+ const value = params ?. [ param ] ;
3636 if ( isRequired && value === undefined ) {
3737 throw new Error (
3838 `Path '${ path } ' requires param '${ param } ' but it was not provided` ,
@@ -45,7 +45,7 @@ export function href<Path extends keyof Args>(
4545 if ( path . endsWith ( "*" ) ) {
4646 // treat trailing splat the same way as compilePath, and force it to be as if it were `/*`.
4747 // `react-router typegen` will not generate the params for a malformed splat, causing a type error, but we can still do the correct thing here.
48- const value = params ? params [ "*" ] : undefined ;
48+ const value = params ?. [ "*" ] ;
4949 if ( value !== undefined ) {
5050 result += "/" + value ;
5151 }
@@ -55,22 +55,20 @@ export function href<Path extends keyof Args>(
5555}
5656
5757/**
58- Removes a trailing splat and any number of slashes from the end of the path.
59-
60- Benchmarks as running faster than `path.replace(/\/*\*?$/, "")`, which backtracks.
58+ * Removes a trailing splat and any number of slashes from the end of the path.
59+ *
60+ * Benchmarked to be faster than `path.replace(/\/*\*?$/, "")`, which backtracks.
6161 */
62- function trimEndSplat ( path : string ) : string {
62+ function trimTrailingSplat ( path : string ) : string {
6363 let i = path . length - 1 ;
6464 let char = path [ i ] ;
65- if ( char !== "*" && char !== "/" ) {
66- return path ;
67- }
65+ if ( char !== "*" && char !== "/" ) return path ;
66+
67+ // for/break benchmarks faster than do/while
6868 i -- ;
6969 for ( ; i >= 0 ; i -- ) {
70- // for/break benchmarks faster than do/while
71- if ( path [ i ] !== "/" ) {
72- break ;
73- }
70+ if ( path [ i ] !== "/" ) break ;
7471 }
72+
7573 return path . slice ( 0 , i + 1 ) ;
7674}
0 commit comments