Fix case for no previous bleeding day. Use new getCycleDay API

This commit is contained in:
Julia Friesel
2018-06-09 12:47:57 +02:00
parent 5b406848cb
commit a56ee7df38
4 changed files with 32 additions and 23 deletions
+5 -11
View File
@@ -4,15 +4,10 @@ const LocalDate = joda.LocalDate
export default function config(bleedingDaysSortedByDateView, opts) {
opts = opts || {
// at the very minimum, a cycle can be a bleeding day
// followed by a non-bleeding day, thus a length of 2
maxBreakInBleeding: 1
}
return function getCycleDayNumber(targetDateString) {
// sort the cycle days in descending order so we travel into
// the past as we iterate over the array
// also, to retrieve the number, we only need the cycle days before the target day
const targetDate = LocalDate.parse(targetDateString)
const withWrappedDates = bleedingDaysSortedByDateView
.filter(day => !day.bleeding.exclude)
@@ -20,16 +15,15 @@ export default function config(bleedingDaysSortedByDateView, opts) {
day.wrappedDate = LocalDate.parse(day.date)
return day
})
// TODO write test for what if there is no first day before?? aka no firstbleedingdaybeforeindex
const firstBleedingDayBeforeTargetDayIndex = withWrappedDates.findIndex(day => day.wrappedDate.isBefore(targetDate))
const cycleDays = withWrappedDates.slice(firstBleedingDayBeforeTargetDayIndex)
if (firstBleedingDayBeforeTargetDayIndex < 0) return null
const previousBleedingDays = withWrappedDates.slice(firstBleedingDayBeforeTargetDayIndex)
const lastPeriodStart = cycleDays.find((day, i) => {
return thereIsNoPreviousBleedingDayWithinTheThreshold(day, cycleDays.slice(i + 1), opts.maxBreakInBleeding)
const lastPeriodStart = previousBleedingDays.find((day, i) => {
return thereIsNoPreviousBleedingDayWithinTheThreshold(day, previousBleedingDays.slice(i + 1), opts.maxBreakInBleeding)
})
if (!lastPeriodStart) return null
const diffInDays = lastPeriodStart.wrappedDate.until(targetDate, joda.ChronoUnit.DAYS)
// cycle starts at day 1