Make getMensesDaysAfter work for non-bleeding day too
This commit is contained in:
+47
-12
@@ -101,21 +101,23 @@ export default function config(opts) {
|
||||
}
|
||||
}
|
||||
|
||||
function getMensesDaysAfter(cycleDay) {
|
||||
const bleedingDays = bleedingDaysSortedByDate.filter(d => {
|
||||
return !d.bleeding.exclude
|
||||
function getMensesDaysRightAfter(cycleDay) {
|
||||
const bleedingDays = bleedingDaysSortedByDate
|
||||
.filter(d => {
|
||||
return !d.bleeding.exclude
|
||||
})
|
||||
.reverse()
|
||||
const firstFollowingBleedingDayIndex = bleedingDays.findIndex(day => {
|
||||
return day.date > cycleDay.date
|
||||
})
|
||||
const startIndex = bleedingDays.findIndex(day => {
|
||||
return day.date === cycleDay.date
|
||||
})
|
||||
return recurse(cycleDay, startIndex, [])
|
||||
return recurse(cycleDay, firstFollowingBleedingDayIndex, [])
|
||||
|
||||
function recurse(day, i, mensesDays) {
|
||||
if (i === 0) return mensesDays
|
||||
const next = bleedingDays[i - 1]
|
||||
function recurse(day, nextIndex, mensesDays) {
|
||||
const next = bleedingDays[nextIndex]
|
||||
if (!next) return mensesDays
|
||||
if (!isWithinThreshold(day, next)) return mensesDays
|
||||
mensesDays.unshift(next)
|
||||
return recurse(next, i - 1, mensesDays)
|
||||
return recurse(next, nextIndex + 1, mensesDays)
|
||||
}
|
||||
|
||||
function isWithinThreshold(cycleDay, nextCycleDay) {
|
||||
@@ -174,6 +176,37 @@ export default function config(opts) {
|
||||
return predictedMenses
|
||||
}
|
||||
|
||||
|
||||
// TODO this also needs a test
|
||||
function maybeSetNewCycleStart(oldCycleDay, newValue) {
|
||||
// if a bleeding value is deleted, we need to check if
|
||||
// there are any following bleeding days and if the
|
||||
// next one of them is now a cycle start
|
||||
|
||||
// in order to get the menses days, the cycle day in question still
|
||||
// has to have a bleeding value, so we get those days first and only
|
||||
// then update the cycle day
|
||||
const mensesDaysAfter = getMensesDaysRightAfter(oldCycleDay)
|
||||
oldCycleDay.bleeding = newValue
|
||||
oldCycleDay.isCycleStart = false
|
||||
|
||||
if (!mensesDaysAfter.length) return
|
||||
|
||||
const nextOne = mensesDaysAfter[mensesDaysAfter.length - 1]
|
||||
if (isMensesStart(nextOne)) {
|
||||
nextOne.isCycleStart = true
|
||||
}
|
||||
}
|
||||
|
||||
function maybeClearOldCycleStartsInThisMenses(cycleDay) {
|
||||
// if we have a new bleeding day, we need to clear the
|
||||
// menses start marker from all following days of this
|
||||
// menses that may have been marked as start before
|
||||
const mensesDaysAfter = getMensesDaysRightAfter(cycleDay)
|
||||
mensesDaysAfter.forEach(day => day.isCycleStart = false)
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
getCycleDayNumber,
|
||||
getCycleForDay,
|
||||
@@ -182,6 +215,8 @@ export default function config(opts) {
|
||||
getAllCycleLengths,
|
||||
getPredictedMenses,
|
||||
isMensesStart,
|
||||
getMensesDaysAfter
|
||||
getMensesDaysRightAfter,
|
||||
maybeSetNewCycleStart,
|
||||
maybeClearOldCycleStartsInThisMenses
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user