@@ -339,49 +339,48 @@ export default class UrlMatcher {
339
339
* @param {Object } values the values to substitute for the parameters in this pattern.
340
340
* @returns {string } the formatted URL (path and optionally search part).
341
341
*/
342
- format ( values ) {
343
- values = values || { } ;
344
- var segments = this . segments , params = this . parameters ( ) , paramset = this . params ;
345
- if ( ! this . validates ( values ) ) return null ;
342
+ format ( values = { } ) {
343
+ const url = {
344
+ params : this . parameters ( ) ,
345
+ paramset : this . params ,
346
+ nPath : this . segments . length - 1 ,
347
+ nTotal : this . parameters ( ) . length
348
+ } ;
349
+ var i , search = false , result = this . segments [ 0 ] ;
346
350
347
- var i , search = false , nPath = segments . length - 1 , nTotal = params . length , result = segments [ 0 ] ;
351
+ if ( ! this . validates ( values ) ) return null ;
348
352
349
353
function encodeDashes ( str ) { // Replace dashes with encoded "\-"
350
- return encodeURIComponent ( str ) . replace ( / - / g, function ( c ) { return `%5C%${ c . charCodeAt ( 0 ) . toString ( 16 ) . toUpperCase ( ) } ` ; } ) ;
354
+ return encodeURIComponent ( str ) . replace ( / - / g, c => `%5C%${ c . charCodeAt ( 0 ) . toString ( 16 ) . toUpperCase ( ) } ` ) ;
351
355
}
352
356
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 ) ;
357
+ url . params . map ( ( name , i ) => {
358
+ var isPathParam = i < url . nPath ;
359
+ var param : Param = url . paramset [ name ] , value = param . value ( values [ name ] ) ;
360
+ var isDefaultValue = param . isDefaultValue ( value ) ;
357
361
var squash = isDefaultValue ? param . squash : false ;
358
362
var encoded = param . type . encode ( value ) ;
359
363
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 } =` ) ;
364
+ if ( ! isPathParam ) {
365
+ if ( encoded == null || ( isDefaultValue && squash !== false ) ) return ;
366
+ if ( ! isArray ( encoded ) ) encoded = [ < string > encoded ] ;
367
+
368
+ encoded = map ( < string [ ] > encoded , encodeURIComponent ) . join ( `&${ name } =` ) ;
381
369
result += ( search ? '&' : '?' ) + ( `${ name } =${ encoded } ` ) ;
382
370
search = true ;
371
+ return ;
383
372
}
384
- }
373
+
374
+ result += ( ( segment , result ) => {
375
+ if ( squash === true ) return segment . match ( result . match ( / \/ $ / ) ? / \/ ? ( .* ) / : / ( .* ) / ) [ 1 ] ;
376
+ if ( isString ( squash ) ) return squash + segment ;
377
+ if ( squash !== false ) return "" ;
378
+ if ( encoded == null ) return segment ;
379
+ if ( isArray ( encoded ) ) return map ( < string [ ] > encoded , encodeDashes ) . join ( "-" ) + segment ;
380
+ if ( param . type . raw ) return encoded + segment ;
381
+ return encodeURIComponent ( < string > encoded ) + segment ;
382
+ } ) ( this . segments [ i + 1 ] , result ) ;
383
+ } ) ;
385
384
386
385
if ( values [ "#" ] ) result += "#" + values [ "#" ] ;
387
386
0 commit comments