import isWeekend from '../isWeekend/index' ; import toDate from '../toDate/index' ; import toInteger from '../_lib/toInteger/index' ; import requiredArgs from '../_lib/requiredArgs/index' ; import isSunday from '../isSunday/index' ; import isSaturday from '../isSaturday/index' ; name category summary description param { Date | Number } date param { Number } amount returns { Date } throws example export default function addBusinessDays ( dirtyDate , dirtyAmount ) { requiredArgs ( 2 , arguments ) ; const date = toDate ( dirtyDate ) ; const startedOnWeekend = isWeekend ( date ) ; const amount = toInteger ( dirtyAmount ) ; if ( isNaN ( amount ) ) return new Date ( NaN ) ; const hours = date . getHours ( ) ; const sign = amount < 0 ? - 1 : 1 ; const fullWeeks = toInteger ( amount / 5 ) ; date . setDate ( date . getDate ( ) + fullWeeks * 7 ) ; let restDays = Math . abs ( amount % 5 ) ; while ( restDays > 0 ) { date . setDate ( date . getDate ( ) + sign ) ; if ( ! isWeekend ( date ) ) restDays -= 1 ; } if ( startedOnWeekend && isWeekend ( date ) && amount !== 0 ) { if ( isSaturday ( date ) ) date . setDate ( date . getDate ( ) + ( sign < 0 ? 2 : - 1 ) ) ; if ( isSunday ( date ) ) date . setDate ( date . getDate ( ) + ( sign < 0 ? 1 : - 2 ) ) ; } date . setHours ( hours ) ; return date ; }