@@ -5,7 +5,12 @@ import {
5
5
parsePathname ,
6
6
processRouteTree ,
7
7
} from '../src'
8
- import { SEGMENT_TYPE_OPTIONAL_PARAM , SEGMENT_TYPE_PARAM , SEGMENT_TYPE_PATHNAME , SEGMENT_TYPE_WILDCARD } from "../src/path"
8
+ import {
9
+ SEGMENT_TYPE_OPTIONAL_PARAM ,
10
+ SEGMENT_TYPE_PARAM ,
11
+ SEGMENT_TYPE_PATHNAME ,
12
+ SEGMENT_TYPE_WILDCARD ,
13
+ } from '../src/path'
9
14
import { format } from 'prettier'
10
15
11
16
interface TestRoute {
@@ -162,13 +167,19 @@ describe('work in progress', () => {
162
167
` )
163
168
} )
164
169
165
- const parsedRoutes = result . flatRoutes . map ( ( route ) : ParsedRoute => ( {
166
- path : route . fullPath ,
167
- segments : parsePathname ( route . fullPath ) ,
168
- rank : route . rank !
169
- } ) )
170
+ const parsedRoutes = result . flatRoutes . map (
171
+ ( route ) : ParsedRoute => ( {
172
+ path : route . fullPath ,
173
+ segments : parsePathname ( route . fullPath ) ,
174
+ rank : route . rank ! ,
175
+ } ) ,
176
+ )
170
177
171
- type ParsedRoute = { path : string , segments : ReturnType < typeof parsePathname > , rank : number }
178
+ type ParsedRoute = {
179
+ path : string
180
+ segments : ReturnType < typeof parsePathname >
181
+ rank : number
182
+ }
172
183
173
184
let fn = 'const baseSegments = parsePathname(from).map(s => s.value);'
174
185
fn += '\nconst l = baseSegments.length;'
@@ -280,16 +291,22 @@ describe('work in progress', () => {
280
291
maxLengths [ maxLength ] = ( maxLengths [ maxLength ] || 0 ) + 1
281
292
}
282
293
} )
283
- const allMinLengths = Object . keys ( minLengths ) . sort ( ( a , b ) => Number ( a ) - Number ( b ) )
294
+ const allMinLengths = Object . keys ( minLengths ) . sort (
295
+ ( a , b ) => Number ( a ) - Number ( b ) ,
296
+ )
284
297
for ( let i = 0 ; i < allMinLengths . length ; i ++ ) {
285
298
for ( let j = i + 1 ; j < allMinLengths . length ; j ++ ) {
286
- minLengths [ Number ( allMinLengths [ i ] ! ) ] ! += minLengths [ Number ( allMinLengths [ j ] ! ) ] !
299
+ minLengths [ Number ( allMinLengths [ i ] ! ) ] ! +=
300
+ minLengths [ Number ( allMinLengths [ j ] ! ) ] !
287
301
}
288
302
}
289
- const allMaxLengths = Object . keys ( maxLengths ) . sort ( ( a , b ) => Number ( b ) - Number ( a ) )
303
+ const allMaxLengths = Object . keys ( maxLengths ) . sort (
304
+ ( a , b ) => Number ( b ) - Number ( a ) ,
305
+ )
290
306
for ( let i = 0 ; i < allMaxLengths . length ; i ++ ) {
291
307
for ( let j = i + 1 ; j < allMaxLengths . length ; j ++ ) {
292
- maxLengths [ Number ( allMaxLengths [ i ] ! ) ] ! += maxLengths [ Number ( allMaxLengths [ j ] ! ) ] !
308
+ maxLengths [ Number ( allMaxLengths [ i ] ! ) ] ! +=
309
+ maxLengths [ Number ( allMaxLengths [ j ] ! ) ] !
293
310
}
294
311
}
295
312
let bestMinLength
@@ -313,21 +330,30 @@ describe('work in progress', () => {
313
330
// console.log(`Best maxLength: ${bestMaxLength} with score: ${maxLengths[bestMaxLength!]} / ${total}`)
314
331
315
332
// determine which of the 3 discriminants to use (condition, minLength, maxLength) to match as close to 50% of the routes as possible
316
- const discriminant = bestKey && ( ! bestMinLength || conditionCounts [ bestKey ] > minLengths [ bestMinLength ! ] ) && ( ! bestMaxLength || conditionCounts [ bestKey ] > maxLengths [ bestMaxLength ! ] )
317
- ? { key : bestKey , type : 'condition' , } as const
318
- : bestMinLength && ( ! bestMaxLength || minLengths [ bestMinLength ! ] > maxLengths [ bestMaxLength ! ] ) && ( ! bestKey || minLengths [ bestMinLength ! ] > conditionCounts [ bestKey ] )
319
- ? { key : bestMinLength ! , type : 'minLength' } as const
320
- : bestMaxLength
321
- ? { key : bestMaxLength ! , type : 'maxLength' } as const
322
- : undefined
333
+ const discriminant =
334
+ bestKey &&
335
+ ( ! bestMinLength ||
336
+ conditionCounts [ bestKey ] > minLengths [ bestMinLength ! ] ) &&
337
+ ( ! bestMaxLength || conditionCounts [ bestKey ] > maxLengths [ bestMaxLength ! ] )
338
+ ? ( { key : bestKey , type : 'condition' } as const )
339
+ : bestMinLength &&
340
+ ( ! bestMaxLength ||
341
+ minLengths [ bestMinLength ! ] > maxLengths [ bestMaxLength ! ] ) &&
342
+ ( ! bestKey || minLengths [ bestMinLength ! ] > conditionCounts [ bestKey ] )
343
+ ? ( { key : bestMinLength ! , type : 'minLength' } as const )
344
+ : bestMaxLength
345
+ ? ( { key : bestMaxLength ! , type : 'maxLength' } as const )
346
+ : undefined
323
347
324
348
if ( discriminant ) {
325
349
// split all routes into 2 groups (matching and not matching) based on the discriminant
326
350
const matchingRoutes : Array < WithConditions > = [ ]
327
351
const nonMatchingRoutes : Array < WithConditions > = [ ]
328
352
for ( const route of parsedRoutes ) {
329
353
if ( discriminant . type === 'condition' ) {
330
- const condition = route . conditions . find ( c => c . key === discriminant . key )
354
+ const condition = route . conditions . find (
355
+ ( c ) => c . key === discriminant . key ,
356
+ )
331
357
if ( condition ) {
332
358
matchingRoutes . push ( route )
333
359
} else {
@@ -352,7 +378,9 @@ describe('work in progress', () => {
352
378
} else if ( matchingRoutes . length ) {
353
379
// add `if` for the discriminant
354
380
if ( discriminant . type === 'condition' ) {
355
- const condition = matchingRoutes [ 0 ] ! . conditions . find ( c => c . key === discriminant . key ) !
381
+ const condition = matchingRoutes [ 0 ] ! . conditions . find (
382
+ ( c ) => c . key === discriminant . key ,
383
+ ) !
356
384
fn += `if (${ conditionToString ( condition ) || 'true' } ) {`
357
385
} else if ( discriminant . type === 'minLength' ) {
358
386
if ( discriminant . key === length . max ) {
@@ -367,25 +395,30 @@ describe('work in progress', () => {
367
395
fn += `if (l <= ${ discriminant . key } ) {`
368
396
}
369
397
} else {
370
- throw new Error ( `Unknown discriminant type: ${ JSON . stringify ( discriminant ) } ` )
398
+ throw new Error (
399
+ `Unknown discriminant type: ${ JSON . stringify ( discriminant ) } ` ,
400
+ )
371
401
}
372
402
// recurse
373
403
recursiveStaticMatch (
374
404
matchingRoutes ,
375
- { min : discriminant . type === 'minLength' ? discriminant . key : length . min , max : discriminant . type === 'maxLength' ? discriminant . key : length . max } ,
376
- discriminant . type === 'condition' ? [ ...preconditions , discriminant . key ] : preconditions
405
+ {
406
+ min :
407
+ discriminant . type === 'minLength' ? discriminant . key : length . min ,
408
+ max :
409
+ discriminant . type === 'maxLength' ? discriminant . key : length . max ,
410
+ } ,
411
+ discriminant . type === 'condition'
412
+ ? [ ...preconditions , discriminant . key ]
413
+ : preconditions ,
377
414
)
378
415
fn += '}'
379
416
}
380
417
if ( nonMatchingRoutes . length === 1 ) {
381
418
outputRoute ( nonMatchingRoutes [ 0 ] ! , length , preconditions )
382
419
} else if ( nonMatchingRoutes . length ) {
383
420
// recurse
384
- recursiveStaticMatch (
385
- nonMatchingRoutes ,
386
- length ,
387
- preconditions ,
388
- )
421
+ recursiveStaticMatch ( nonMatchingRoutes , length , preconditions )
389
422
}
390
423
} else {
391
424
for ( const route of parsedRoutes ) {
@@ -394,10 +427,14 @@ describe('work in progress', () => {
394
427
}
395
428
}
396
429
397
- function prepareOptionalParams ( parsedRoutes : Array < ParsedRoute > ) : Array < ParsedRoute > {
430
+ function prepareOptionalParams (
431
+ parsedRoutes : Array < ParsedRoute > ,
432
+ ) : Array < ParsedRoute > {
398
433
const result : Array < ParsedRoute > = [ ]
399
434
for ( const route of parsedRoutes ) {
400
- const index = route . segments . findIndex ( ( s ) => s . type === SEGMENT_TYPE_OPTIONAL_PARAM )
435
+ const index = route . segments . findIndex (
436
+ ( s ) => s . type === SEGMENT_TYPE_OPTIONAL_PARAM ,
437
+ )
401
438
if ( index === - 1 ) {
402
439
result . push ( route )
403
440
continue
@@ -412,7 +449,9 @@ describe('work in progress', () => {
412
449
}
413
450
const withRegular : ParsedRoute = {
414
451
...route ,
415
- segments : route . segments . map ( ( s , i ) => i === index ? { ...s , type : SEGMENT_TYPE_PARAM } : s ) ,
452
+ segments : route . segments . map ( ( s , i ) =>
453
+ i === index ? { ...s , type : SEGMENT_TYPE_PARAM } : s ,
454
+ ) ,
416
455
}
417
456
const chunk = prepareOptionalParams ( [ withRegular , withoutOptional ] )
418
457
result . push ( ...chunk )
@@ -421,12 +460,14 @@ describe('work in progress', () => {
421
460
}
422
461
423
462
type Condition =
424
- | { key : string , kind : 'static' ; index : number ; value : string }
425
- | { key : string , kind : 'startsWith' ; index : number ; value : string }
426
- | { key : string , kind : 'endsWith' ; index : number ; value : string }
427
- | { key : string , kind : 'wildcardEndsWith' ; value : string }
463
+ | { key : string ; kind : 'static' ; index : number ; value : string }
464
+ | { key : string ; kind : 'startsWith' ; index : number ; value : string }
465
+ | { key : string ; kind : 'endsWith' ; index : number ; value : string }
466
+ | { key : string ; kind : 'wildcardEndsWith' ; value : string }
428
467
429
- const withConditions : Array < WithConditions > = prepareOptionalParams ( parsedRoutes ) . map ( r => {
468
+ const withConditions : Array < WithConditions > = prepareOptionalParams (
469
+ parsedRoutes ,
470
+ ) . map ( ( r ) => {
430
471
let minLength = 0
431
472
let maxLength = 0
432
473
const conditions : Array < Condition > = r . segments . flatMap ( ( s , i ) => {
@@ -437,17 +478,32 @@ describe('work in progress', () => {
437
478
return [ ]
438
479
}
439
480
return [
440
- { kind : 'static' , index : i , value : s . value , key : `static-${ i } -${ s . value } ` } ,
481
+ {
482
+ kind : 'static' ,
483
+ index : i ,
484
+ value : s . value ,
485
+ key : `static-${ i } -${ s . value } ` ,
486
+ } ,
441
487
]
442
488
} else if ( s . type === SEGMENT_TYPE_PARAM ) {
443
489
minLength += 1
444
490
maxLength += 1
445
491
const conds : Array < Condition > = [ ]
446
492
if ( s . prefixSegment ) {
447
- conds . push ( { kind : 'startsWith' , index : i , value : s . prefixSegment , key : `startsWith-${ i } -${ s . prefixSegment } ` } )
493
+ conds . push ( {
494
+ kind : 'startsWith' ,
495
+ index : i ,
496
+ value : s . prefixSegment ,
497
+ key : `startsWith-${ i } -${ s . prefixSegment } ` ,
498
+ } )
448
499
}
449
500
if ( s . suffixSegment ) {
450
- conds . push ( { kind : 'endsWith' , index : i , value : s . suffixSegment , key : `endsWith-${ i } -${ s . suffixSegment } ` } )
501
+ conds . push ( {
502
+ kind : 'endsWith' ,
503
+ index : i ,
504
+ value : s . suffixSegment ,
505
+ key : `endsWith-${ i } -${ s . suffixSegment } ` ,
506
+ } )
451
507
}
452
508
return conds
453
509
} else if ( s . type === SEGMENT_TYPE_WILDCARD ) {
@@ -457,10 +513,19 @@ describe('work in progress', () => {
457
513
minLength += 1
458
514
}
459
515
if ( s . prefixSegment ) {
460
- conds . push ( { kind : 'startsWith' , index : i , value : s . prefixSegment , key : `startsWith-${ i } -${ s . prefixSegment } ` } )
516
+ conds . push ( {
517
+ kind : 'startsWith' ,
518
+ index : i ,
519
+ value : s . prefixSegment ,
520
+ key : `startsWith-${ i } -${ s . prefixSegment } ` ,
521
+ } )
461
522
}
462
523
if ( s . suffixSegment ) {
463
- conds . push ( { kind : 'wildcardEndsWith' , value : s . suffixSegment , key : `wildcardEndsWith-${ s . suffixSegment } ` } )
524
+ conds . push ( {
525
+ kind : 'wildcardEndsWith' ,
526
+ value : s . suffixSegment ,
527
+ key : `wildcardEndsWith-${ s . suffixSegment } ` ,
528
+ } )
464
529
}
465
530
return conds
466
531
}
@@ -711,4 +776,4 @@ describe('work in progress', () => {
711
776
)
712
777
expect ( buildMatch ) . toBe ( originalMatch )
713
778
} )
714
- } )
779
+ } )
0 commit comments