nl_tokens: Match the path defined by path elements where some may be RegExp . When there is more than one candidate prefer the one with greatest interger value .
pl_tokens: function maximisePath ( ) { // Ensure each element in the path exists, // where it is a regex, match it and replace the element with a string var elements = Array . prototype . slice . <mask> ( arguments ) ; for ( var i = 1 ; i < elements . length ; i ++ ) { // the directory is elements 0 .. i-1 joined var directory = path . resolve ( path . join . apply ( path , elements . slice ( 0 , i ) ) ) ; // no directory implies failure if ( ! fs . existsSync ( directory ) ) { return null ; } // regex element is matched else if ( ( typeof elements [ i ] !== 'string' ) && ( 'test' in elements [ i ] ) ) { var matches = resolveDirMatches ( directory , elements [ i ] ) ; // no match implies failure, else use the item with the highest numeric index if ( matches . length === 0 ) { return null ; } else { elements [ i ] = matches [ 0 ] ; } } // anything else is cast to string else { elements [ i ] = String ( elements [ i ] ) ; } } // now join them all together // do a final check to make sure it exists var result = path . resolve ( elements . join ( path . sep ) ) ; return fs . existsSync ( result ) && result ; }