From 918d383163ab5c5308550c860953632ed7b4b822 Mon Sep 17 00:00:00 2001 From: Julia Friesel Date: Thu, 25 Oct 2018 15:17:26 +0200 Subject: [PATCH] Simplify cycle start detection funtions --- db/index.js | 47 ++++++++++++++++++++++------------------------- lib/cycle.js | 34 +--------------------------------- 2 files changed, 23 insertions(+), 58 deletions(-) diff --git a/db/index.js b/db/index.js index a9322ff..99436bb 100644 --- a/db/index.js +++ b/db/index.js @@ -8,7 +8,7 @@ import cycleModule from '../lib/cycle' let db let isMensesStart -let getMensesDaysAfter +let getMensesDaysRightAfter export async function openDb ({ hash, persistConnection }) { const realmConfig = {} @@ -37,7 +37,7 @@ export async function openDb ({ hash, persistConnection }) { if (persistConnection) db = connection const cycle = cycleModule() isMensesStart = cycle.isMensesStart - getMensesDaysAfter = cycle.getMensesDaysAfter + getMensesDaysRightAfter = cycle.getMensesDaysRightAfter } @@ -57,51 +57,48 @@ export function getCycleStartsSortedByDate() { export function saveSymptom(symptom, cycleDay, val) { db.write(() => { - if (symptom === 'bleeding') { - saveBleeding(cycleDay, val) + if (bleedingValueDeleted(symptom, val)) { + cycleDay.bleeding = val + cycleDay.isCycleStart = false + maybeSetNewCycleStart(cycleDay, val) + } else if (bleedingValueAddedOrChanged(symptom, val)) { + cycleDay.bleeding = val + cycleDay.isCycleStart = isMensesStart(cycleDay) + maybeClearOldCycleStarts(cycleDay) } else { cycleDay[symptom] = val } }) -} -// TODO this also needs a test -export function saveBleeding(cycleDay, bleeding) { - if (!bleeding) { - updateCycleDayAndMaybeSetNewCycleStart(cycleDay, bleeding) - } else { - cycleDay.bleeding = bleeding - cycleDay.isCycleStart = isMensesStart(cycleDay) - maybeClearOldCycleStartsInThisMenses(cycleDay) + function bleedingValueDeleted(symptom, val) { + return symptom === 'bleeding' && !val } - function updateCycleDayAndMaybeSetNewCycleStart(oldCycleDay, newValue) { + function bleedingValueAddedOrChanged(symptom, val) { + return symptom === 'bleeding' && val + } + + function maybeSetNewCycleStart(dayWithDeletedBleeding) { // 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 = getMensesDaysAfter(oldCycleDay) - oldCycleDay.bleeding = newValue - oldCycleDay.isCycleStart = false - + const mensesDaysAfter = getMensesDaysRightAfter(dayWithDeletedBleeding) if (!mensesDaysAfter.length) return - const nextOne = mensesDaysAfter[mensesDaysAfter.length - 1] if (isMensesStart(nextOne)) { nextOne.isCycleStart = true } } - function maybeClearOldCycleStartsInThisMenses(cycleDay) { + function maybeClearOldCycleStarts(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 = getMensesDaysAfter(cycleDay) + const mensesDaysAfter = getMensesDaysRightAfter(cycleDay) mensesDaysAfter.forEach(day => day.isCycleStart = false) } + + } export function getOrCreateCycleDay(localDate) { diff --git a/lib/cycle.js b/lib/cycle.js index a5bd7d3..1be36ab 100644 --- a/lib/cycle.js +++ b/lib/cycle.js @@ -177,36 +177,6 @@ export default function config(opts) { } - // 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, @@ -215,8 +185,6 @@ export default function config(opts) { getAllCycleLengths, getPredictedMenses, isMensesStart, - getMensesDaysRightAfter, - maybeSetNewCycleStart, - maybeClearOldCycleStartsInThisMenses + getMensesDaysRightAfter } } \ No newline at end of file