const chntbl = { '天' : 0 , '日' : 0 , '一' : 1 , '二' : 2 , '兩' : 2 , '三' : 3 , '四' : 4 , '五' : 5 , '六' : 6 , '七' : 7 , '八' : 8 , '九' : 9 , '十' : 10 } ; const wordtbl = { today : '(?:今|今天|今日)' , tomorrow : '(?:明|明天|明日)' , acquired : '(?:後天|後日)' } ; param { string } vacationDate returns { Date } export let parseDate = ( vacationDate ) : Date => { let today = new Date ( ) ; let theDate ; theDate = Date . parse ( vacationDate ) ; if ( ! isNaN ( theDate ) ) { theDate = new Date ( theDate ) ; theDate . setYear ( today . getFullYear ( ) ) ; return theDate ; } if ( vacationDate . match ( new RegExp ( wordtbl . today ) ) ) { return new Date ( today . getFullYear ( ) , today . getMonth ( ) , today . getDate ( ) ) ; } else if ( vacationDate . match ( new RegExp ( wordtbl . tomorrow ) ) ) { return new Date ( today . getFullYear ( ) , today . getMonth ( ) , today . getDate ( ) + 1 ) ; } else if ( vacationDate . match ( new RegExp ( wordtbl . acquired ) ) ) { return new Date ( today . getFullYear ( ) , today . getMonth ( ) , today . getDate ( ) + 2 ) ; } let regex = new RegExp ( /(.*)?月(.*)?[日,號]/ ) ; let mat = vacationDate . match ( regex ) ; if ( mat !== null ) { let month = mat [ 1 ] ; let date = mat [ 2 ] ; let monthInt ; let dateInt ; if ( ! /\d/ . test ( month ) ) { let m = 0 ; for ( let i = 0 ; i < month . length ; ++ i ) { m = m * 10 + chntbl [ month [ i ] ] ; } monthInt = m ; } else { monthInt = Number . parseInt ( month ) ; } if ( ! /\d/ . test ( date ) ) { let d = 0 ; for ( let i = 0 ; i < date . length ; ++ i ) { d = d * 10 + chntbl [ date [ i ] ] ; } dateInt = d ; } else { dateInt = Number . parseInt ( date ) ; } return new Date ( today . getFullYear ( ) , monthInt - 1 , dateInt ) ; } regex = new RegExp ( /([天日一二三四五六七八九十\d])/ ) ; mat = vacationDate . match ( regex ) ; if ( mat !== null ) { let day = mat [ 1 ] ; let dayInt ; let dist = 0 ; let thisDay = today . getDay ( ) ; if ( ! /\d/ . test ( day ) ) { dayInt = chntbl [ day ] ; } else { dayInt = Number . parseInt ( day ) ; } if ( vacationDate . indexOf ( '下' ) > - 1 ) { dist = 7 - thisDay + dayInt ; } else { dist = dayInt - thisDay ; } return new Date ( today . getTime ( ) + dist * 86400000 ) ; } return theDate ; } ; param { string } vacationLength returns { number } export let parseHours = ( vacationLength ) : number => { let num = 0 ; num = parseFloat ( vacationLength ) ; if ( ! isNaN ( num ) ) { return num ; } let regex = new RegExp ( /([一二兩三四五六七八九十])/ ) ; let mat = vacationLength . match ( regex ) ; if ( mat !== null ) { num = chntbl [ mat [ 1 ] ] ; if ( vacationLength . indexOf ( '半' ) > - 1 ) { num += 0.5 ; } } if ( num > 0 ) { return num ; } if ( vacationLength . indexOf ( '天' ) > - 1 ) { if ( vacationLength . indexOf ( '半' ) > - 1 ) { num = 4 ; } else if ( vacationLength . indexOf ( '全' ) > - 1 || vacationLength . indexOf ( '整' ) > - 1 ) { num = 8 ; } } return num ; } ;