import * as path from "path" ; export interface MappingEntry { readonly pattern : string ; readonly paths : ReadonlyArray < string > ; } export interface Paths { readonly [ key ] : ReadonlyArray < string > ; } param absoluteBaseUrl param paths param addMatchAll export function getAbsoluteMappingEntries ( absoluteBaseUrl , paths , addMatchAll ) { const sortedKeys = sortByLongestPrefix ( Object . keys ( paths ) ) ; const absolutePaths = [ ] ; for ( const key of sortedKeys ) { absolutePaths . push ( { pattern : key , paths : paths [ key ] . map ( ( pathToResolve ) => path . join ( absoluteBaseUrl , pathToResolve ) ) , } ) ; } if ( ! paths [ "*" ] && addMatchAll ) { absolutePaths . push ( { pattern : "*" , paths : [ `${ absoluteBaseUrl . replace ( /\/$/ , "" ) }/*` ] , } ) ; } return absolutePaths ; } /**
 * Sort path patterns.
 * If a module name can be matched with multiple patterns then pattern with the longest prefix will be picked.
 */ function sortByLongestPrefix ( arr ) { return arr . concat ( ) . sort ( ( a , b ) => getPrefixLength ( b ) - getPrefixLength ( a ) ) ; } function getPrefixLength ( pattern ) { const prefixLength = pattern . indexOf ( "*" ) ; return pattern . substr ( 0 , prefixLength ) . length ; }