import { DEFAULT_DATE_FORMAT , DATE_REGEX } from './constants' ; import moment from 'moment' ; export default class MomentDateRegex { replace ( input ) { const dateRegex = DATE_REGEX ; const customFolderString = input ; const matches = [ ] ; let match ; while ( match = dateRegex . exec ( customFolderString ) ) { matches . push ( match ) ; } if ( ! matches || matches . length === 0 ) { return input ; } const now = new Date ( ) ; const formattedDates = matches . map ( m => { const dateFormat = m . groups . date === '' ? DEFAULT_DATE_FORMAT : m . groups . date ; return [ m . groups . target , this . getMoment ( now , dateFormat ) ] ; } ) ; let output = customFolderString ; formattedDates . forEach ( fd => { output = output . replace ( fd [ 0 ] , fd [ 1 ] ) ; } ) ; return output ; } getMoment ( now , dateFormat ) { if ( ( window as any ) . moment ) { return ( window as any ) . moment ( now ) . format ( dateFormat ) ; } else { return moment ( now ) . format ( dateFormat ) ; } } }