From 446c5d46006093dc44ab34d02c20ff5a6e265ee5 Mon Sep 17 00:00:00 2001 From: Julia Friesel Date: Mon, 3 Feb 2020 11:04:20 +0100 Subject: [PATCH 1/6] Set new cycle start when bleeding value excluded --- db/index.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/db/index.js b/db/index.js index b8ea002..2b7aa7f 100644 --- a/db/index.js +++ b/db/index.js @@ -77,7 +77,7 @@ export function saveSymptom(symptom, date, val) { if (!cycleDay) cycleDay = createCycleDay(date) db.write(() => { - if (bleedingValueDeleted(symptom, val)) { + if (bleedingValueDeletedOrExluded(symptom, val)) { cycleDay.bleeding = val cycleDay.isCycleStart = false maybeSetNewCycleStart(cycleDay, val) @@ -90,19 +90,23 @@ export function saveSymptom(symptom, date, val) { } }) - function bleedingValueDeleted(symptom, val) { - return symptom === 'bleeding' && !val + function bleedingValueDeletedOrExluded(symptom, val) { + if (symptom !== 'bleeding') return + + const bleedingDeleted = !val + const bleedingExcluded = val && val.exclude + return bleedingDeleted || bleedingExcluded } function bleedingValueAddedOrChanged(symptom, val) { return symptom === 'bleeding' && val } - function maybeSetNewCycleStart(dayWithDeletedBleeding) { - // if a bleeding value is deleted, we need to check if + function maybeSetNewCycleStart(dayWithRemovedBleeding) { + // 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 - const mensesDaysAfter = getMensesDaysRightAfter(dayWithDeletedBleeding) + const mensesDaysAfter = getMensesDaysRightAfter(dayWithRemovedBleeding) if (!mensesDaysAfter.length) return const nextOne = mensesDaysAfter[mensesDaysAfter.length - 1] if (isMensesStart(nextOne)) { From 7b8292ab6aab5a7687c125d764c1cc8e7694c05d Mon Sep 17 00:00:00 2001 From: Julia Friesel Date: Tue, 4 Feb 2020 11:51:07 +0100 Subject: [PATCH 2/6] Extract maybeSetNewCycleStart into own module --- db/index.js | 52 +++++++------------------------------- lib/set-new-cycle-start.js | 37 +++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 43 deletions(-) create mode 100644 lib/set-new-cycle-start.js diff --git a/db/index.js b/db/index.js index 2b7aa7f..16f584e 100644 --- a/db/index.js +++ b/db/index.js @@ -5,9 +5,10 @@ import fs from 'react-native-fs' import restart from 'react-native-restart' import schemas from './schemas' import cycleModule from '../lib/cycle' +import maybeSetNewCycleStart from '../lib/set-new-cycle-start' let db -let isMensesStart +let checkIsMensesStart let getMensesDaysRightAfter export async function openDb (hash) { @@ -50,7 +51,7 @@ export async function openDb (hash) { db = connection const cycle = cycleModule() - isMensesStart = cycle.isMensesStart + checkIsMensesStart = cycle.isMensesStart getMensesDaysRightAfter = cycle.getMensesDaysRightAfter return true } @@ -77,56 +78,21 @@ export function saveSymptom(symptom, date, val) { if (!cycleDay) cycleDay = createCycleDay(date) db.write(() => { - if (bleedingValueDeletedOrExluded(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) + if (symptom === 'bleeding') { + const mensesDaysAfter = getMensesDaysRightAfter(cycleDay) + maybeSetNewCycleStart({ + val, cycleDay, mensesDaysAfter, checkIsMensesStart + }) } else { cycleDay[symptom] = val } }) - - function bleedingValueDeletedOrExluded(symptom, val) { - if (symptom !== 'bleeding') return - - const bleedingDeleted = !val - const bleedingExcluded = val && val.exclude - return bleedingDeleted || bleedingExcluded - } - - function bleedingValueAddedOrChanged(symptom, val) { - return symptom === 'bleeding' && val - } - - function maybeSetNewCycleStart(dayWithRemovedBleeding) { - // 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 - const mensesDaysAfter = getMensesDaysRightAfter(dayWithRemovedBleeding) - if (!mensesDaysAfter.length) return - const nextOne = mensesDaysAfter[mensesDaysAfter.length - 1] - if (isMensesStart(nextOne)) { - nextOne.isCycleStart = true - } - } - - 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 = getMensesDaysRightAfter(cycleDay) - mensesDaysAfter.forEach(day => day.isCycleStart = false) - } } export function updateCycleStartsForAllCycleDays() { db.write(() => { getBleedingDaysSortedByDate().forEach(day => { - if (isMensesStart(day)) { + if (checkIsMensesStart(day)) { day.isCycleStart = true } }) diff --git a/lib/set-new-cycle-start.js b/lib/set-new-cycle-start.js new file mode 100644 index 0000000..34ec016 --- /dev/null +++ b/lib/set-new-cycle-start.js @@ -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) + } +} \ No newline at end of file From 009c8fa0f28d5a220d2923fd73395034ff7a36a2 Mon Sep 17 00:00:00 2001 From: Julia Friesel Date: Tue, 4 Feb 2020 11:51:35 +0100 Subject: [PATCH 3/6] Add test for deleted bleeding value --- test/set-new-cycle-start.spec.js | 78 ++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 test/set-new-cycle-start.spec.js diff --git a/test/set-new-cycle-start.spec.js b/test/set-new-cycle-start.spec.js new file mode 100644 index 0000000..973415c --- /dev/null +++ b/test/set-new-cycle-start.spec.js @@ -0,0 +1,78 @@ +import chai from 'chai' +import dirtyChai from 'dirty-chai' +import maybeSetNewCycleStart from '../lib/set-new-cycle-start' + +const expect = chai.expect +chai.use(dirtyChai) + +describe('maybeSetNewCycleStart', () => { + function getFixtures() { + const cycleDay = { + date: '2020-01-01', + isCycleStart: true, + bleeding: { + value: 2, + exclude: false + } + } + + const mensesDaysAfter = [ + { + date: '2020-01-04', + isCycleStart: false, + value: { + bleeding: { + value: 2, + exclude: false + } + } + }, + { + date: '2020-01-03', + isCycleStart: false, + value: { + bleeding: { + value: 2, + exclude: false + } + } + }, + { + date: '2020-01-02', + isCycleStart: false, + value: { + bleeding: { + value: 2, + exclude: false + } + } + } + ] + return [cycleDay, mensesDaysAfter] + } + + const checkIsMensesStart = cycleDay => { + if (cycleDay.date === '2020-01-02') return true + } + + it('sets new cycle start when first day of period deleted', () => { + const deletedBleedingValue = null + + const [dayWithDeletedBleeding, mensesDaysAfter] = getFixtures() + + maybeSetNewCycleStart({ + val: deletedBleedingValue, + cycleDay: dayWithDeletedBleeding, + mensesDaysAfter, + checkIsMensesStart + }) + expect(dayWithDeletedBleeding.isCycleStart).to.be.false() + expect(dayWithDeletedBleeding.bleeding).to.be.null() + expect(mensesDaysAfter[2].isCycleStart).to.be.true() + }) + it('sets new cycle start when first day of period excluded', () => {}) + it('does not set new cycle start when other day of period deleted', () => {}) + it('does not set new cycle start when other day of period excluded', () => {}) + it('works when there are no followiing bleeding days', () => {}) + it('works when the following bleeding days are already the start of a new cycle', () => {}) +}) \ No newline at end of file From 963e0ffea03b594cb5ddf2475c63381e3846b7c9 Mon Sep 17 00:00:00 2001 From: Julia Friesel Date: Tue, 4 Feb 2020 12:11:10 +0100 Subject: [PATCH 4/6] Add remaining tests for maybeSetNewCycleStart --- test/set-new-cycle-start.spec.js | 99 +++++++++++++++++++++++++++----- 1 file changed, 86 insertions(+), 13 deletions(-) diff --git a/test/set-new-cycle-start.spec.js b/test/set-new-cycle-start.spec.js index 973415c..f797510 100644 --- a/test/set-new-cycle-start.spec.js +++ b/test/set-new-cycle-start.spec.js @@ -6,8 +6,16 @@ const expect = chai.expect chai.use(dirtyChai) describe('maybeSetNewCycleStart', () => { + const deletedBleedingValue = null + + const excludedBleedingValue = { + value: 2, + exclude: true + } + + function getFixtures() { - const cycleDay = { + const cycleStartDay = { date: '2020-01-01', isCycleStart: true, bleeding: { @@ -48,7 +56,17 @@ describe('maybeSetNewCycleStart', () => { } } ] - return [cycleDay, mensesDaysAfter] + + const notCycleStartDay = { + date: '2020-01-02', + isCycleStart: false, + bleeding: { + value: 2, + exclude: false + } + } + + return [cycleStartDay, mensesDaysAfter, notCycleStartDay] } const checkIsMensesStart = cycleDay => { @@ -56,23 +74,78 @@ describe('maybeSetNewCycleStart', () => { } it('sets new cycle start when first day of period deleted', () => { - const deletedBleedingValue = null - - const [dayWithDeletedBleeding, mensesDaysAfter] = getFixtures() + const [cycleStartDay, mensesDaysAfter] = getFixtures() maybeSetNewCycleStart({ val: deletedBleedingValue, - cycleDay: dayWithDeletedBleeding, + cycleDay: cycleStartDay, mensesDaysAfter, checkIsMensesStart }) - expect(dayWithDeletedBleeding.isCycleStart).to.be.false() - expect(dayWithDeletedBleeding.bleeding).to.be.null() + expect(cycleStartDay.isCycleStart).to.be.false() + expect(cycleStartDay.bleeding).to.be.null() expect(mensesDaysAfter[2].isCycleStart).to.be.true() }) - it('sets new cycle start when first day of period excluded', () => {}) - it('does not set new cycle start when other day of period deleted', () => {}) - it('does not set new cycle start when other day of period excluded', () => {}) - it('works when there are no followiing bleeding days', () => {}) - it('works when the following bleeding days are already the start of a new cycle', () => {}) + + it('sets new cycle start when first day of period excluded', () => { + const [cycleStartDay, mensesDaysAfter] = getFixtures() + + maybeSetNewCycleStart({ + val: excludedBleedingValue, + cycleDay: cycleStartDay, + mensesDaysAfter, + checkIsMensesStart + }) + + expect(cycleStartDay.isCycleStart).to.be.false() + expect(cycleStartDay.bleeding).to.equal(excludedBleedingValue) + expect(mensesDaysAfter[2].isCycleStart).to.be.true() + }) + + it('does not set new cycle start when other day of period deleted', () => { + const [cycleStartDay, mensesDaysAfter, notCycleStartDay] = getFixtures() + + maybeSetNewCycleStart({ + val: deletedBleedingValue, + cycleDay: notCycleStartDay, + mensesDaysAfter, + checkIsMensesStart + }) + + expect(cycleStartDay.isCycleStart).to.be.true() + expect(notCycleStartDay.isCycleStart).to.be.false() + expect(notCycleStartDay.bleeding).to.equal(deletedBleedingValue) + }) + it('does not set new cycle start when other day of period excluded', () => { + const excludedBleedingValue = { + value: 2, + exclude: true + } + + const [cycleStartDay, mensesDaysAfter, notCycleStartDay] = getFixtures() + + maybeSetNewCycleStart({ + val: excludedBleedingValue, + cycleDay: notCycleStartDay, + mensesDaysAfter, + checkIsMensesStart + }) + + expect(cycleStartDay.isCycleStart).to.be.true() + expect(notCycleStartDay.isCycleStart).to.be.false() + expect(notCycleStartDay.bleeding).to.equal(excludedBleedingValue) + }) + it('works when there are no following bleeding days', () => { + const [cycleStartDay] = getFixtures() + + maybeSetNewCycleStart({ + val: deletedBleedingValue, + cycleDay: cycleStartDay, + mensesDaysAfter: [], + checkIsMensesStart + }) + + expect(cycleStartDay.isCycleStart).to.be.false() + expect(cycleStartDay.bleeding).to.equal(deletedBleedingValue) + }) }) \ No newline at end of file From ae0d9094070e5f533e0628bda0a6121de2d61532 Mon Sep 17 00:00:00 2001 From: Julia Friesel Date: Tue, 4 Feb 2020 12:18:24 +0100 Subject: [PATCH 5/6] Remove now superfluous check for bleeding symptom --- lib/set-new-cycle-start.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/set-new-cycle-start.js b/lib/set-new-cycle-start.js index 34ec016..bdc6872 100644 --- a/lib/set-new-cycle-start.js +++ b/lib/set-new-cycle-start.js @@ -12,7 +12,7 @@ export default function ({ if (checkIsMensesStart(nextOne)) { nextOne.isCycleStart = true } - } else if (bleedingValueAddedOrChanged(val)) { + } else { cycleDay.bleeding = val cycleDay.isCycleStart = checkIsMensesStart(cycleDay) maybeClearOldCycleStarts(cycleDay) @@ -24,10 +24,6 @@ export default function ({ 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 From 62df30de8ae365cb50637b88b282d56a1a9d78d5 Mon Sep 17 00:00:00 2001 From: Julia Friesel Date: Fri, 7 Feb 2020 09:53:11 +0100 Subject: [PATCH 6/6] De-duplicate line --- lib/set-new-cycle-start.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/set-new-cycle-start.js b/lib/set-new-cycle-start.js index bdc6872..34b5a69 100644 --- a/lib/set-new-cycle-start.js +++ b/lib/set-new-cycle-start.js @@ -1,11 +1,13 @@ 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 + + cycleDay.bleeding = val + + // 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] @@ -13,7 +15,6 @@ export default function ({ nextOne.isCycleStart = true } } else { - cycleDay.bleeding = val cycleDay.isCycleStart = checkIsMensesStart(cycleDay) maybeClearOldCycleStarts(cycleDay) }