import { Formats } from '../../types/core' ; import { has , isArray , isDate } from '../../utils/typeOf' ; import { getDay , getMonth , getWeekRange , getYear , getYearWeek , } from '../../utils/date' ; import { DateType } from '../../types/utils' ; import { State } from '../../types/store' ; const token = /d{1,2}|M{1,2}|w{1,2}|yy(?:yy)?|"[^"]*"|'[^']*'/g ; const formats = { d : ( date ) => getDay ( date ) , dd : ( date ) => pad ( getDay ( date ) ) , M : ( date ) => getMonth ( date ) , MM : ( date ) => pad ( getMonth ( date ) ) , yy : ( date ) => String ( getYear ( date ) ) . substr ( 2 ) , yyyy : ( date ) => getYear ( date ) , w : ( date , locale ) => getYearWeek ( date , locale ) , ww : ( date , locale ) => getYearWeek ( date , locale ) , } ; function pad ( val , len ? ) { val = String ( val ) ; len = len || 2 ; while ( val . length < len ) { val = '0' + val ; } return val ; } export function getFormatDate ( this , date , formatConfig ) { const { locale , format } = this ; const separator = ' - ' ; function formatParse ( dateStr ) { if ( ! dateStr ) return null ; if ( has ( format , 'w' ) && isDate ( dateStr ) ) { dateStr = getWeekRange ( dateStr , locale . weekStart ) . start ; } return formatConfig . replace ( token , ( val ) => formats [ val as 'dd' ] ( new Date ( dateStr ! . toString ( ) ) , locale ) ) ; } if ( isArray ( date ) ) { const res = date . map ( ( d ) => formatParse ( d ) ) . join ( separator ) ; return res === separator ? '' : res ; } return formatParse ( date ) ; }