@@ -138,12 +138,11 @@ export default class UrlMatcher {
138
138
segment = pattern . substring ( last , m . index ) ;
139
139
regexp = isSearch ? m [ 4 ] : m [ 4 ] || ( m [ 1 ] == '*' ? '.*' : null ) ;
140
140
type = paramTypes . type ( regexp || "string" ) || inherit ( paramTypes . type ( "string" ) , { pattern : new RegExp ( regexp , config . caseInsensitive ? 'i' : undefined ) } ) ;
141
- return {
142
- id : id , regexp : regexp , segment : segment , type : type , cfg : cfg
143
- } ;
141
+ return { id, regexp, segment, type, cfg} ;
144
142
}
145
143
146
144
var p , param , segment ;
145
+
147
146
while ( ( m = placeholder . exec ( pattern ) ) ) {
148
147
p = matchDetails ( m , false ) ;
149
148
if ( p . segment . indexOf ( '?' ) >= 0 ) break ; // we're into the search part
@@ -339,49 +338,47 @@ export default class UrlMatcher {
339
338
* @param {Object } values the values to substitute for the parameters in this pattern.
340
339
* @returns {string } the formatted URL (path and optionally search part).
341
340
*/
342
- format ( values ) {
343
- values = values || { } ;
344
- var segments = this . segments , params = this . parameters ( ) , paramset = this . params ;
345
- if ( ! this . validates ( values ) ) return null ;
341
+ format ( values = { } ) {
342
+ const url = {
343
+ params : this . parameters ( ) ,
344
+ paramSet : this . params ,
345
+ nPath : this . segments . length - 1
346
+ } ;
347
+ var i , search = false , result = this . segments [ 0 ] ;
346
348
347
- var i , search = false , nPath = segments . length - 1 , nTotal = params . length , result = segments [ 0 ] ;
349
+ if ( ! this . validates ( values ) ) return null ;
348
350
349
351
function encodeDashes ( str ) { // Replace dashes with encoded "\-"
350
- return encodeURIComponent ( str ) . replace ( / - / g, function ( c ) { return `%5C%${ c . charCodeAt ( 0 ) . toString ( 16 ) . toUpperCase ( ) } ` ; } ) ;
352
+ return encodeURIComponent ( str ) . replace ( / - / g, c => `%5C%${ c . charCodeAt ( 0 ) . toString ( 16 ) . toUpperCase ( ) } ` ) ;
351
353
}
352
354
353
- for ( i = 0 ; i < nTotal ; i ++ ) {
354
- var isPathParam = i < nPath ;
355
- var name = params [ i ] , param : Param = paramset [ name ] , value = param . value ( values [ name ] ) ;
356
- var isDefaultValue = param . isOptional && param . type . equals ( param . value ( ) , value ) ;
355
+ url . params . map ( ( name , i ) => {
356
+ var isPathParam = i < url . nPath ;
357
+ var param : Param = url . paramSet [ name ] , value = param . value ( values [ name ] ) ;
358
+ var isDefaultValue = param . isDefaultValue ( value ) ;
357
359
var squash = isDefaultValue ? param . squash : false ;
358
360
var encoded = param . type . encode ( value ) ;
359
361
360
- if ( isPathParam ) {
361
- var nextSegment = segments [ i + 1 ] ;
362
- if ( squash === false ) {
363
- if ( encoded != null ) {
364
- if ( isArray ( encoded ) ) {
365
- result += map ( < string [ ] > encoded , encodeDashes ) . join ( "-" ) ;
366
- } else {
367
- result += encodeURIComponent ( < string > encoded ) ;
368
- }
369
- }
370
- result += nextSegment ;
371
- } else if ( squash === true ) {
372
- var capture = result . match ( / \/ $ / ) ? / \/ ? ( .* ) / : / ( .* ) / ;
373
- result += nextSegment . match ( capture ) [ 1 ] ;
374
- } else if ( isString ( squash ) ) {
375
- result += squash + nextSegment ;
376
- }
377
- } else {
378
- if ( encoded == null || ( isDefaultValue && squash !== false ) ) continue ;
379
- if ( ! isArray ( encoded ) ) encoded = [ < string > encoded ] ;
380
- encoded = map ( < string [ ] > encoded , encodeURIComponent ) . join ( `&${ name } =` ) ;
362
+ if ( ! isPathParam ) {
363
+ if ( encoded == null || ( isDefaultValue && squash !== false ) ) return ;
364
+ if ( ! isArray ( encoded ) ) encoded = [ < string > encoded ] ;
365
+
366
+ encoded = map ( < string [ ] > encoded , encodeURIComponent ) . join ( `&${ name } =` ) ;
381
367
result += ( search ? '&' : '?' ) + ( `${ name } =${ encoded } ` ) ;
382
368
search = true ;
369
+ return ;
383
370
}
384
- }
371
+
372
+ result += ( ( segment , result ) => {
373
+ if ( squash === true ) return segment . match ( result . match ( / \/ $ / ) ? / \/ ? ( .* ) / : / ( .* ) / ) [ 1 ] ;
374
+ if ( isString ( squash ) ) return squash + segment ;
375
+ if ( squash !== false ) return "" ;
376
+ if ( encoded == null ) return segment ;
377
+ if ( isArray ( encoded ) ) return map ( < string [ ] > encoded , encodeDashes ) . join ( "-" ) + segment ;
378
+ if ( param . type . raw ) return encoded + segment ;
379
+ return encodeURIComponent ( < string > encoded ) + segment ;
380
+ } ) ( this . segments [ i + 1 ] , result ) ;
381
+ } ) ;
385
382
386
383
if ( values [ "#" ] ) result += "#" + values [ "#" ] ;
387
384
0 commit comments