Extract maybeSetNewCycleStart into own module

This commit is contained in:
Julia Friesel
2020-02-04 11:51:07 +01:00
parent 446c5d4600
commit 7b8292ab6a
2 changed files with 46 additions and 43 deletions
+37
View File
@@ -0,0 +1,37 @@
export default function ({
val, cycleDay, mensesDaysAfter, checkIsMensesStart
}) {
// if a bleeding value is deleted or excluded, we need to check if
// there are any following bleeding days and if the
// next one of them is now a cycle start
if (bleedingValueDeletedOrExluded(val)) {
cycleDay.bleeding = val
cycleDay.isCycleStart = false
if (!mensesDaysAfter.length) return
const nextOne = mensesDaysAfter[mensesDaysAfter.length - 1]
if (checkIsMensesStart(nextOne)) {
nextOne.isCycleStart = true
}
} else if (bleedingValueAddedOrChanged(val)) {
cycleDay.bleeding = val
cycleDay.isCycleStart = checkIsMensesStart(cycleDay)
maybeClearOldCycleStarts(cycleDay)
}
function bleedingValueDeletedOrExluded(val) {
const bleedingDeleted = !val
const bleedingExcluded = val && val.exclude
return bleedingDeleted || bleedingExcluded
}
function bleedingValueAddedOrChanged(symptom, val) {
return symptom === 'bleeding' && val
}
function maybeClearOldCycleStarts() {
// 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
mensesDaysAfter.forEach(day => day.isCycleStart = false)
}
}