@@ -371,25 +371,36 @@ function maximisePath() {
371371 }
372372 }
373373
374- // ensure each element in the path exists or match where there is a pattern
374+ // ensure each element in the path exists
375+ // where it is a regex then match it and replace the element with a string
375376 var elements = Array . prototype . slice . call ( arguments ) ;
376377 for ( var i = 1 ; i < elements . length ; i ++ ) {
378+
379+ // the directory is elements 0 .. i-1 joined
377380 var directory = path . resolve ( path . join . apply ( path , elements . slice ( 0 , i ) ) ) ;
381+
382+ // no directory implies failure
378383 if ( ! fs . existsSync ( directory ) ) {
379384 return null ;
380- } else if ( ( typeof elements [ i ] !== 'string' ) && ( 'test' in elements [ i ] ) ) {
381- var matches = fs . readdirSync ( directory )
382- . filter ( function eachDirectoryItem ( item ) {
385+ }
386+ // regex element is matched
387+ else if ( ( typeof elements [ i ] !== 'string' ) && ( 'test' in elements [ i ] ) ) {
388+
389+ // find all matches, with the highest numeric index first
390+ var matches = fs . readdirSync ( directory ) . filter ( function eachDirectoryItem ( item ) {
383391 var resolved = path . resolve ( path . join ( directory , item ) ) ;
384392 return elements [ i ] . test ( item ) && fs . statSync ( resolved ) . isDirectory ( ) ;
385- } )
386- . sort ( compare ) ;
393+ } ) . sort ( compare ) ;
394+
395+ // no match implies failure, else use the item with the highest numeric index
387396 if ( matches . length === 0 ) {
388397 return null ;
389398 } else {
390399 elements [ i ] = matches [ 0 ] ;
391400 }
392- } else {
401+ }
402+ // anything else is cast to string
403+ else {
393404 elements [ i ] = String ( elements [ i ] ) ;
394405 }
395406 }
0 commit comments