Skip to content

Commit

Permalink
refactor after feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
robertparkinson committed Oct 22, 2024
1 parent cc34488 commit 6c3beb2
Show file tree
Hide file tree
Showing 2 changed files with 467 additions and 251 deletions.
140 changes: 88 additions & 52 deletions app/lib/return-cycle-dates.lib.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict'

/**
* General helper methods
* @module DatesLib
* Helper methods to deal with return cycle dates
* @module ReturnCycleDatesLib
*/

const { returnCycleDates } = require('./static-lookups.lib.js')
const { formatDateObjectToISO } = require('./dates.lib.js')
const { returnCycleDates } = require('./static-lookups.lib.js')

/**
* Get the due date of next provided cycle, either summer or winter and all year, formatted as YYYY-MM-DD
Expand All @@ -29,19 +29,25 @@ function cycleDueDate (summer) {
const year = today.getFullYear()
const month = today.getMonth()

let cycleDueYear
let cycleDueMonth
let cycleDueDay

if (summer) {
if (month > returnCycleDates.summer.endDate.month) {
return new Date(year + 1, returnCycleDates.summer.dueDate.month, returnCycleDates.summer.dueDate.day)
}
cycleDueDay = returnCycleDates.summer.dueDate.day
cycleDueMonth = returnCycleDates.summer.dueDate.month + 1

return new Date(year, returnCycleDates.summer.dueDate.month, returnCycleDates.summer.dueDate.day)
}
cycleDueYear = month > returnCycleDates.summer.endDate.month ? year + 1 : year
} else {
cycleDueDay = returnCycleDates.allYear.dueDate.day
cycleDueMonth = returnCycleDates.allYear.dueDate.month + 1

if (month > returnCycleDates.allYear.endDate.month) {
return new Date(year + 1, returnCycleDates.allYear.dueDate.month, returnCycleDates.allYear.dueDate.day)
cycleDueYear = month > returnCycleDates.allYear.endDate.month ? year + 1 : year
}

return new Date(year, returnCycleDates.allYear.dueDate.month, returnCycleDates.allYear.dueDate.day)
const cycleDueDate = new Date(`${cycleDueYear}-${cycleDueMonth}-${cycleDueDay}`)

return cycleDueDate
}

/**
Expand All @@ -55,19 +61,25 @@ function cycleDueDateByDate (date, summer) {
const year = date.getFullYear()
const month = date.getMonth()

let cycleDueYear
let cycleDueMonth
let cycleDueDay

if (summer) {
if (month > returnCycleDates.summer.endDate.month) {
return formatDateObjectToISO(new Date(`${year + 1}-${returnCycleDates.summer.dueDate.month + 1}-${returnCycleDates.summer.dueDate.day}`))
}
cycleDueDay = returnCycleDates.summer.dueDate.day
cycleDueMonth = returnCycleDates.summer.dueDate.month + 1

return formatDateObjectToISO(new Date(`${year}-${returnCycleDates.summer.dueDate.month + 1}-${returnCycleDates.summer.dueDate.day}`))
}
cycleDueYear = month > returnCycleDates.summer.endDate.month ? year + 1 : year
} else {
cycleDueDay = returnCycleDates.allYear.dueDate.day
cycleDueMonth = returnCycleDates.allYear.dueDate.month + 1

if (month > returnCycleDates.allYear.endDate.month) {
return formatDateObjectToISO(new Date(`${year + 1}-${returnCycleDates.allYear.dueDate.month + 1}-${returnCycleDates.allYear.dueDate.day}`))
cycleDueYear = month > returnCycleDates.allYear.endDate.month ? year + 1 : year
}

return formatDateObjectToISO(new Date(`${year}-${returnCycleDates.allYear.dueDate.month + 1}-${returnCycleDates.allYear.dueDate.day}`))
const cycleDueDate = new Date(`${cycleDueYear}-${cycleDueMonth}-${cycleDueDay}`)

return formatDateObjectToISO(cycleDueDate)
}

/**
Expand All @@ -91,23 +103,29 @@ function cycleEndDate (summer) {
const year = today.getFullYear()
const month = today.getMonth()

let cycleEndYear
let cycleEndMonth
let cycleEndDay

if (summer) {
if (month > returnCycleDates.summer.endDate.month) {
return new Date(year + 1, returnCycleDates.summer.endDate.month, returnCycleDates.summer.endDate.day)
}
cycleEndDay = returnCycleDates.summer.endDate.day
cycleEndMonth = returnCycleDates.summer.endDate.month + 1

return new Date(year, returnCycleDates.summer.endDate.month, returnCycleDates.summer.endDate.day)
}
cycleEndYear = month > returnCycleDates.summer.endDate.month ? year + 1 : year
} else {
cycleEndDay = returnCycleDates.allYear.endDate.day
cycleEndMonth = returnCycleDates.allYear.endDate.month + 1

if (month > returnCycleDates.allYear.endDate.month) {
return new Date(year + 1, returnCycleDates.allYear.endDate.month, returnCycleDates.allYear.endDate.day)
cycleEndYear = month > returnCycleDates.allYear.endDate.month ? year + 1 : year
}

return new Date(year, returnCycleDates.allYear.endDate.month, returnCycleDates.allYear.endDate.day)
const cycleEndDate = new Date(`${cycleEndYear}-${cycleEndMonth}-${cycleEndDay}`)

return cycleEndDate
}

/**
* Given an arbitary date and if it is summer or all-year return the end date of that cycle
* Given an arbitrary date and if it is summer or all-year return the end date of that cycle
*
* @param {Date} date - the date whose start date you want to find.
* @param {boolean} summer - true for summer, false for winter and all year.
Expand All @@ -117,19 +135,25 @@ function cycleEndDateByDate (date, summer) {
const year = date.getFullYear()
const month = date.getMonth()

let cycleEndYear
let cycleEndMonth
let cycleEndDay

if (summer) {
if (month > returnCycleDates.summer.endDate.month) {
return formatDateObjectToISO(new Date(`${year + 1}-${returnCycleDates.summer.endDate.month + 1}-${returnCycleDates.summer.endDate.day}`))
}
cycleEndDay = returnCycleDates.summer.endDate.day
cycleEndMonth = returnCycleDates.summer.endDate.month + 1

return formatDateObjectToISO(new Date(`${year}-${returnCycleDates.summer.endDate.month + 1}-${returnCycleDates.summer.endDate.day}`))
}
cycleEndYear = month > returnCycleDates.summer.endDate.month ? year + 1 : year
} else {
cycleEndDay = returnCycleDates.allYear.endDate.day
cycleEndMonth = returnCycleDates.allYear.endDate.month + 1

if (month > returnCycleDates.allYear.endDate.month) {
return formatDateObjectToISO(new Date(`${year + 1}-${returnCycleDates.allYear.endDate.month + 1}-${returnCycleDates.allYear.endDate.day}`))
cycleEndYear = month > returnCycleDates.allYear.endDate.month ? year + 1 : year
}

return formatDateObjectToISO(new Date(`${year}-${returnCycleDates.allYear.endDate.month + 1}-${returnCycleDates.allYear.endDate.day}`))
const cycleEndDate = new Date(`${cycleEndYear}-${cycleEndMonth}-${cycleEndDay}`)

return formatDateObjectToISO(cycleEndDate)
}

/**
Expand All @@ -153,19 +177,25 @@ function cycleStartDate (summer) {
const year = today.getFullYear()
const month = today.getMonth()

let cycleStartYear
let cycleStartMonth
let cycleStartDay

if (summer) {
if (month < returnCycleDates.summer.startDate.month) {
return new Date(year - 1, returnCycleDates.summer.startDate.month, returnCycleDates.summer.startDate.day)
}
cycleStartDay = returnCycleDates.summer.startDate.day
cycleStartMonth = returnCycleDates.summer.startDate.month + 1

return new Date(year, returnCycleDates.summer.startDate.month, returnCycleDates.summer.startDate.day)
}
cycleStartYear = month < returnCycleDates.summer.startDate.month ? year - 1 : year
} else {
cycleStartDay = returnCycleDates.allYear.startDate.day
cycleStartMonth = returnCycleDates.allYear.startDate.month + 1

if (month < returnCycleDates.allYear.startDate.month) {
return new Date(year - 1, returnCycleDates.allYear.startDate.month, returnCycleDates.allYear.startDate.day)
cycleStartYear = month < returnCycleDates.allYear.startDate.month ? year - 1 : year
}

return new Date(year, returnCycleDates.allYear.startDate.month, returnCycleDates.allYear.startDate.day)
const cycleEndDate = new Date(`${cycleStartYear}-${cycleStartMonth}-${cycleStartDay}`)

return cycleEndDate
}

/**
Expand All @@ -179,19 +209,25 @@ function cycleStartDateByDate (date, summer) {
const year = date.getFullYear()
const month = date.getMonth()

let cycleStartYear
let cycleStartMonth
let cycleStartDay

if (summer) {
if (month < returnCycleDates.summer.startDate.month) {
return formatDateObjectToISO(new Date(`${year - 1}-${returnCycleDates.summer.startDate.month + 1}-${returnCycleDates.summer.startDate.day}`))
}
cycleStartDay = returnCycleDates.summer.startDate.day
cycleStartMonth = returnCycleDates.summer.startDate.month + 1

return formatDateObjectToISO(new Date(`${year}-${returnCycleDates.summer.startDate.month + 1}-${returnCycleDates.summer.startDate.day}`))
}
cycleStartYear = month < returnCycleDates.summer.startDate.month ? year - 1 : year
} else {
cycleStartDay = returnCycleDates.allYear.startDate.day
cycleStartMonth = returnCycleDates.allYear.startDate.month + 1

if (month < returnCycleDates.allYear.startDate.month) {
return formatDateObjectToISO(new Date(`${year - 1}-${returnCycleDates.allYear.startDate.month + 1}-${returnCycleDates.allYear.startDate.day}`))
cycleStartYear = month < returnCycleDates.allYear.startDate.month ? year - 1 : year
}

return formatDateObjectToISO(new Date(`${year}-${returnCycleDates.allYear.startDate.month + 1}-${returnCycleDates.allYear.startDate.day}`))
const cycleEndDate = new Date(`${cycleStartYear}-${cycleStartMonth}-${cycleStartDay}`)

return formatDateObjectToISO(cycleEndDate)
}

module.exports = {
Expand Down
Loading

0 comments on commit 6c3beb2

Please sign in to comment.