diff --git a/lib/sympto/cervix.js b/lib/sympto/cervix.js index 25783817463491651b3b3d411149888f7ce92426..1f012b3251bfafd12405a9a42213fdf404a77a69 100644 --- a/lib/sympto/cervix.js +++ b/lib/sympto/cervix.js @@ -2,7 +2,6 @@ export default function (cycleDays, tempEvalEndIndex) { const notDetected = { detected: false } const cervixDays = cycleDays .filter(day => day.cervix && !day.cervix.exclude) - .filter(day => typeof day.cervix.opening === 'number' && typeof day.cervix.firmness === 'number') // we search for the day of cervix peak, which must: // * have fertile cervix values @@ -19,14 +18,19 @@ export default function (cycleDays, tempEvalEndIndex) { const threeFollowingDays = cervixDays.slice(i + 1, i + 4) if (threeFollowingDays.length < 3) continue - // no other fertile cervix value may occur until temperature evaluation has - // been completed const fertileCervixOccursIn3FollowingDays = threeFollowingDays.some(day => { return !isClosedAndHard(day.cervix) }) if (fertileCervixOccursIn3FollowingDays) continue const cycleDayIndex = cycleDays.indexOf(day) + + // if temperature evaluation has been completed an we still haven't found + // a candidate, there is no cervix shift + if (cycleDayIndex > tempEvalEndIndex) return notDetected + + // no other fertile cervix value may occur until temperature evaluation has + // been completed const relevantDays = cycleDays .slice(cycleDayIndex + 1, tempEvalEndIndex + 1) .filter(day => day.cervix && !day.cervix.exclude) diff --git a/lib/sympto/mucus.js b/lib/sympto/mucus.js index 12e23b8e96c6595af539a1ffea9de2ebbafd59d0..727f76a91ba593e1de5576d4c20408fc484ff230 100644 --- a/lib/sympto/mucus.js +++ b/lib/sympto/mucus.js @@ -16,8 +16,6 @@ export default function (cycleDays, tempEvalEndIndex) { if (day.mucus.value !== currentBestQuality) continue // the three following days must be of lower quality - // AND no best quality day may occur until temperature evaluation has - // been completed const threeFollowingDays = mucusDays.slice(i + 1, i + 4) if (threeFollowingDays.length < 3) continue @@ -27,6 +25,13 @@ export default function (cycleDays, tempEvalEndIndex) { if (bestQualityOccursIn3FollowingDays) continue const cycleDayIndex = cycleDays.indexOf(day) + + // if temperature evaluation has been completed an we still haven't found + // a candidate, there is no cervix shift + if (cycleDayIndex > tempEvalEndIndex) return notDetected + + // no best quality day may occur until temperature evaluation has + // been completed const relevantDays = cycleDays .slice(cycleDayIndex + 1, tempEvalEndIndex + 1) .filter(day => day.mucus && !day.mucus.exclude) diff --git a/test/sympto/cervix-temp-fixtures.js b/test/sympto/cervix-temp-fixtures.js index 18adfe63c38a37fff5ddf9750117591475a3838c..89242497dc49ac0f7c3609c79419ea042e2b3337 100644 --- a/test/sympto/cervix-temp-fixtures.js +++ b/test/sympto/cervix-temp-fixtures.js @@ -102,15 +102,15 @@ export const longAndComplicatedCycle = [ { date: '2018-06-07', temperature: 36.5, cervix: { opening: 0, firmness: 0 } }, { date: '2018-06-08', temperature: 36.52, cervix: { opening: 0, firmness: 0 } }, { date: '2018-06-09', temperature: 36.5, cervix: { opening: 2, firmness: 1 } }, - { date: '2018-06-10', temperature: 36.4, cervix: { opening: 2, firmness: 1 } }, + { date: '2018-06-10', temperature: 36.7, cervix: { opening: 2, firmness: 1 } }, { date: '2018-06-13', temperature: 36.45, cervix: { opening: 1, firmness: 1 } }, { date: '2018-06-14', temperature: 36.5, cervix: { opening: 1, firmness: 1 } }, { date: '2018-06-15', temperature: 36.55, cervix: { opening: 1, firmness: 1 } }, - { date: '2018-06-16', temperature: 36.7, cervix: { opening: 2, firmness: 1 } }, - { date: '2018-06-17', temperature: 36.65, cervix: { opening: 2, firmness: 1 } }, - { date: '2018-06-18', temperature: 36.75, cervix: { opening: 1, firmness: 1 } }, + { date: '2018-06-16', temperature: 36.5, cervix: { opening: 2, firmness: 1 } }, + { date: '2018-06-17', temperature: 36.5, cervix: { opening: 2, firmness: 1 } }, + { date: '2018-06-18', temperature: 36.5, cervix: { opening: 1, firmness: 1 } }, { date: '2018-06-19', temperature: 36.8, cervix: { opening: 0, firmness: 0 } }, - { date: '2018-06-20', temperature: 36.85, cervix: { opening: 1, firmness: 1 } }, + { date: '2018-06-20', temperature: 36.85, cervix: { opening: 0, firmness: 0 } }, { date: '2018-06-21', temperature: 36.8, cervix: { opening: 1, firmness: 1 } }, { date: '2018-06-22', temperature: 36.9, cervix: { opening: 0, firmness: 0 } }, { date: '2018-06-25', temperature: 36.9, cervix: { opening: 0, firmness: 0 } }, @@ -265,3 +265,28 @@ export const cycleWithCervixOnFirstDay = [ { date: '2018-06-26', temperature: 36.8, cervix: { opening: 1, firmness: 1 } }, { date: '2018-06-27', temperature: 36.9, cervix: { opening: 1, firmness: 1 } } ].map(convertToSymptoFormat) + +export const fertileCervixOnlyAfterEndOfTempEval = [ + { date: '2018-06-01', temperature: 36.6, bleeding: 2 }, + { date: '2018-06-02', temperature: 36.65 }, + { date: '2018-06-04', temperature: 36.6 }, + { date: '2018-06-05', temperature: 36.55 }, + { date: '2018-06-06', temperature: 36.7 }, + { date: '2018-06-09', temperature: 36.5 }, + { date: '2018-06-10', temperature: 36.4 }, + { date: '2018-06-13', temperature: 36.45 }, + { date: '2018-06-14', temperature: 36.5 }, + { date: '2018-06-15', temperature: 36.55 }, + { date: '2018-06-16', temperature: 36.7 }, + { date: '2018-06-17', temperature: 36.65 }, + { date: '2018-06-18', temperature: 36.60 }, + { date: '2018-06-19', temperature: 36.8 }, + { date: '2018-06-20', temperature: 36.85 }, + { date: '2018-06-21', temperature: 36.8 }, + { date: '2018-06-22', temperature: 36.9 }, + { date: '2018-06-25', temperature: 36.9, cervix: { opening: 1, firmness: 1 }}, + { date: '2018-06-26', temperature: 36.8, cervix: { opening: 0, firmness: 0 }}, + { date: '2018-06-30', temperature: 36.9, cervix: { opening: 0, firmness: 0 }}, + { date: '2018-07-01', temperature: 36.9, cervix: { opening: 0, firmness: 0 }}, + { date: '2018-07-02', temperature: 36.9, cervix: { opening: 0, firmness: 0 }} +].map(convertToSymptoFormat) diff --git a/test/sympto/cervix-temp.spec.js b/test/sympto/cervix-temp.spec.js index 964b1d6d007dc1fd455ec0a68aa6e4bf2450933b..b7c599b4703542cbdaec52a95c84fce7ef24d3f6 100644 --- a/test/sympto/cervix-temp.spec.js +++ b/test/sympto/cervix-temp.spec.js @@ -14,7 +14,8 @@ import { fhmOnDay12, fhmOnDay15, cycleWithEarlyCervix, - cycleWithCervixOnFirstDay + cycleWithCervixOnFirstDay, + fertileCervixOnlyAfterEndOfTempEval } from './cervix-temp-fixtures' const expect = chai.expect @@ -222,7 +223,34 @@ describe('sympto', () => { start: { date: '2018-03-13' } }) }) + + it('with fertile cervix only occurring after end of temperature evaluation ignores it', () => { + const status = getSensiplanStatus({ + cycle: fertileCervixOnlyAfterEndOfTempEval, + previousCycle: cervixShiftAndFhmOnSameDay, + secondarySymptom: 'cervix' + }) + + expect(status.temperatureShift).to.be.undefined() + expect(status.mucusShift).to.be.undefined() + + expect(Object.keys(status.phases).length).to.eql(2) + expect(status.phases.preOvulatory).to.eql({ + start: { date: '2018-06-01' }, + end: { date: '2018-06-05' }, + cycleDays: fertileCervixOnlyAfterEndOfTempEval + .filter(({date}) => date <= '2018-06-05') + }) + expect(status.phases.periOvulatory).to.eql({ + start: { date: '2018-06-06' }, + cycleDays: fertileCervixOnlyAfterEndOfTempEval + .filter(({date}) => { + return date > '2018-06-05' + }) + }) + }) }) + describe('applying the minus-8 rule', () => { it('shortens the pre-ovu phase if there is a previous < 13 fhm', () => { const status = getSensiplanStatus({ diff --git a/test/sympto/cervix.spec.js b/test/sympto/cervix.spec.js index f8bbc3ebb25e1848c3dfc0965bd7ce36625c5aa7..a328d6cd15877207d8118820a0362dc8f9d5b831 100644 --- a/test/sympto/cervix.spec.js +++ b/test/sympto/cervix.spec.js @@ -105,30 +105,6 @@ describe('sympto', () => { } }) }) - it('when the cervix shift is happening after tempEvalEnd', () => { - const values = [1,1,1,1,1,2,3,3,3,3,1,1,1,1,0,0,0,0,0,0,0] - .map(turnIntoCycleDayObject) - const status = getCervixStatus(values, 10) - expect(status).to.eql({ - detected: true, - cervixPeakBeforeShift: { - date: 13, - cervix: { - opening: 1, - firmness: 0, - exclude: false - } - }, - evaluationCompleteDay: { - date: 16, - cervix: { - opening: 0, - firmness: 0, - exclude: false - } - } - }) - }) }) describe('detects no cervix shift', () => { @@ -144,6 +120,12 @@ describe('sympto', () => { const status = getCervixStatus(values, 17) expect(status).to.eql({ detected: false }) }) + it('when the cervix shift is happening after tempEvalEnd', () => { + const values = [1,1,1,1,1,2,3,3,3,3,1,1,1,1,0,0,0,0,0,0,0] + .map(turnIntoCycleDayObject) + const status = getCervixStatus(values, 10) + expect(status).to.eql({ detected: false }) + }) it('if no days indicate fertile cervix which could be cervix peak', () => { const values = [1, 3, 2, 1, 3, 2, 1, 3, 2, 1, 3, 2, 1, 3, 2, 1] .map(turnIntoCycleDayObject) diff --git a/test/sympto/mucus-temp-fixtures.js b/test/sympto/mucus-temp-fixtures.js index d6463c018886c957fdd7019085b219569e9a74e4..29e0cf31eef3c8190b7d2ecfac185c4252e861ef 100644 --- a/test/sympto/mucus-temp-fixtures.js +++ b/test/sympto/mucus-temp-fixtures.js @@ -169,7 +169,7 @@ export const mucusPeakAndFhmOnSameDay = [ { date: '2018-06-27', temperature: 36.9, mucus: 1 } ].map(convertToSymptoFormat) -export const fhmTwoDaysBeforeMucusPeak = [ +export const mucusPeakOnLastDayOfTempEval = [ { date: '2018-06-01', temperature: 36.6, bleeding: 2 }, { date: '2018-06-02', temperature: 36.65 }, { date: '2018-06-04', temperature: 36.6 }, @@ -183,9 +183,9 @@ export const fhmTwoDaysBeforeMucusPeak = [ { date: '2018-06-16', temperature: 36.7, mucus: 2 }, { date: '2018-06-17', temperature: 36.65, mucus: 2 }, { date: '2018-06-18', temperature: 36.75, mucus: 2 }, - { date: '2018-06-19', temperature: 36.8, mucus: 3 }, - { date: '2018-06-20', temperature: 36.85, mucus: 2 }, - { date: '2018-06-21', temperature: 36.8, mucus: 4 }, + { date: '2018-06-19', temperature: 36.8, mucus: 2 }, + { date: '2018-06-20', temperature: 36.85, mucus: 3 }, + { date: '2018-06-21', temperature: 36.8, mucus: 2 }, { date: '2018-06-22', temperature: 36.9, mucus: 2 }, { date: '2018-06-25', temperature: 36.9, mucus: 1 }, { date: '2018-06-26', temperature: 36.8, mucus: 1 }, @@ -332,3 +332,28 @@ export const mucusPeakSlightlyBeforeTempShift = [ { date: '2018-06-21', temperature: 36.8, mucus: 1}, { date: '2018-06-22', temperature: 36.8, mucus: 1} ].map(convertToSymptoFormat) + +export const mucusOnlyAfterEndOfTempEval = [ + { date: '2018-06-01', temperature: 36.6, bleeding: 2 }, + { date: '2018-06-02', temperature: 36.65 }, + { date: '2018-06-04', temperature: 36.6 }, + { date: '2018-06-05', temperature: 36.55 }, + { date: '2018-06-06', temperature: 36.7 }, + { date: '2018-06-09', temperature: 36.5 }, + { date: '2018-06-10', temperature: 36.4 }, + { date: '2018-06-13', temperature: 36.45 }, + { date: '2018-06-14', temperature: 36.5 }, + { date: '2018-06-15', temperature: 36.55 }, + { date: '2018-06-16', temperature: 36.7 }, + { date: '2018-06-17', temperature: 36.65 }, + { date: '2018-06-18', temperature: 36.60 }, + { date: '2018-06-19', temperature: 36.8 }, + { date: '2018-06-20', temperature: 36.85 }, + { date: '2018-06-21', temperature: 36.8 }, + { date: '2018-06-22', temperature: 36.9 }, + { date: '2018-06-25', temperature: 36.9, mucus: 4 }, + { date: '2018-06-26', temperature: 36.8, mucus: 1 }, + { date: '2018-06-30', temperature: 36.9, mucus: 1 }, + { date: '2018-07-01', temperature: 36.9, mucus: 1 }, + { date: '2018-07-02', temperature: 36.9, mucus: 1 } +].map(convertToSymptoFormat) diff --git a/test/sympto/mucus-temp.spec.js b/test/sympto/mucus-temp.spec.js index c2a2563c3075f083afbf10dcfdfd2beaf1df50a5..ddc0bcb7aae81a02641d4a09716af17050681d03 100644 --- a/test/sympto/mucus-temp.spec.js +++ b/test/sympto/mucus-temp.spec.js @@ -11,13 +11,14 @@ import { cycleWithEarlyMucus, cycleWithMucusOnFirstDay, mucusPeakAndFhmOnSameDay, - fhmTwoDaysBeforeMucusPeak, + mucusPeakOnLastDayOfTempEval, fhm5DaysAfterMucusPeak, mucusPeak5DaysAfterFhm, mucusPeakTwoDaysBeforeFhm, fhmOnDay12, fhmOnDay15, - mucusPeakSlightlyBeforeTempShift + mucusPeakSlightlyBeforeTempShift, + mucusOnlyAfterEndOfTempEval } from './mucus-temp-fixtures' const expect = chai.expect @@ -172,9 +173,10 @@ describe('sympto', () => { .filter(({date}) => date >= '2018-06-21') }) }) + it('with fhM 2 days before mucus peak waits for end of mucus eval', () => { const status = getSensiplanStatus({ - cycle: fhmTwoDaysBeforeMucusPeak, + cycle: mucusPeakOnLastDayOfTempEval, previousCycle: cycleWithFhm }) @@ -185,24 +187,24 @@ describe('sympto', () => { expect(status.phases.preOvulatory).to.eql({ start: { date: '2018-06-01' }, end: { date: '2018-06-05' }, - cycleDays: fhmTwoDaysBeforeMucusPeak + cycleDays: mucusPeakOnLastDayOfTempEval .filter(({date}) => date <= '2018-06-05') }) expect(status.phases.periOvulatory).to.eql({ start: { date: '2018-06-06' }, - end: { date: '2018-06-26', time: '18:00' }, - cycleDays: fhmTwoDaysBeforeMucusPeak + end: { date: '2018-06-25', time: '18:00' }, + cycleDays: mucusPeakOnLastDayOfTempEval .filter(({date}) => { - return date > '2018-06-05' && date <= '2018-06-26' + return date > '2018-06-05' && date <= '2018-06-25' }) }) expect(status.phases.postOvulatory).to.eql({ start: { - date: '2018-06-26', + date: '2018-06-25', time: '18:00' }, - cycleDays: fhmTwoDaysBeforeMucusPeak - .filter(({date}) => date >= '2018-06-26') + cycleDays: mucusPeakOnLastDayOfTempEval + .filter(({date}) => date >= '2018-06-25') }) }) it('another example for mucus peak before temp shift', () => { @@ -334,6 +336,31 @@ describe('sympto', () => { .filter(({date}) => date >= '2018-06-21') }) }) + + it('with mucus only occurring after end of temperature evaluation ignores it', () => { + const status = getSensiplanStatus({ + cycle: mucusOnlyAfterEndOfTempEval, + previousCycle: cycleWithFhm + }) + + expect(status.temperatureShift).to.be.undefined() + expect(status.mucusShift).to.be.undefined() + + expect(Object.keys(status.phases).length).to.eql(2) + expect(status.phases.preOvulatory).to.eql({ + start: { date: '2018-06-01' }, + end: { date: '2018-06-05' }, + cycleDays: fhm5DaysAfterMucusPeak + .filter(({date}) => date <= '2018-06-05') + }) + expect(status.phases.periOvulatory).to.eql({ + start: { date: '2018-06-06' }, + cycleDays: mucusOnlyAfterEndOfTempEval + .filter(({date}) => { + return date > '2018-06-05' + }) + }) + }) }) describe('applying the minus-8 rule', () => { it('shortens the pre-ovu phase if there is a previous <13 fhm', () => {