import { escapeRegExp } from 'lodash' ; const createNumericMatcher = ( fileBaseName , pattern ) : RegExp => { let extStart = fileBaseName . indexOf ( '.' ) ; if ( extStart === - 1 ) { extStart = fileBaseName . length ; } const baseNameWithoutExt = escapeRegExp ( fileBaseName . substr ( 0 , extStart ) ) ; const extension = escapeRegExp ( fileBaseName . substr ( extStart , fileBaseName . length ) ) ; const processedPattern = escapeRegExp ( pattern ) . replace ( /%i/g , '(?<counter>\\d+)' ) ; return new RegExp ( `^${ baseNameWithoutExt }${ processedPattern }${ extension }$` ) ; } ; example export const getFileNameMatcher = ( logFileName , pattern ) => { const matcher = createNumericMatcher ( logFileName , pattern ) ; return ( fileName ) : number | undefined => { const match = matcher . exec ( fileName ) ; if ( ! match ) { return undefined ; } return parseInt ( match . groups ! . counter , 10 ) ; } ; } ; example export const getRollingFileName = ( fileBaseName , pattern , index ) : string => { let suffixStart = fileBaseName . indexOf ( '.' ) ; if ( suffixStart === - 1 ) { suffixStart = fileBaseName . length ; } const baseNameWithoutSuffix = fileBaseName . substr ( 0 , suffixStart ) ; const suffix = fileBaseName . substr ( suffixStart , fileBaseName . length ) ; const interpolatedPattern = pattern . replace ( '%i' , String ( index ) ) ; return `${ baseNameWithoutSuffix }${ interpolatedPattern }${ suffix }` ; } ;