diff --git a/lib/sympto/mucus.js b/lib/sympto/mucus.js index 9eb37efb6d5af600a24a05d91c373492bc44521b..cc788f87a53f3488050d0b8144eafe2c5b0d982d 100644 --- a/lib/sympto/mucus.js +++ b/lib/sympto/mucus.js @@ -1,10 +1,15 @@ export default function (cycleDays, tempEvalEndIndex) { const mucusDays = cycleDays.filter(day => day.mucus && !day.mucus.exclude) - const bestQuality = Math.max(...mucusDays.map(day => day.mucus.value)) + let currentBestQuality = 0 for (let i = 0; i < mucusDays.length; i++) { const day = mucusDays[i] - if (day.mucus.value !== bestQuality) continue + + if (day.mucus.value > currentBestQuality) { + currentBestQuality = day.mucus.value + } + + 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 @@ -13,7 +18,7 @@ export default function (cycleDays, tempEvalEndIndex) { if (threeFollowingDays.length < 3) continue const bestQualityOccursIn3FollowingDays = threeFollowingDays.some(day => { - return day.mucus.value >= bestQuality + return day.mucus.value >= currentBestQuality }) if (bestQualityOccursIn3FollowingDays) continue @@ -23,7 +28,7 @@ export default function (cycleDays, tempEvalEndIndex) { .filter(day => day.mucus && !day.mucus.exclude) const noBestQualityUntilEndOfTempEval = relevantDays.every(day => { - return day.mucus.value < bestQuality + return day.mucus.value < currentBestQuality }) if (noBestQualityUntilEndOfTempEval) { diff --git a/test/periode-length.spec.js b/test/periode-length.spec.js index 0b9b200b479b828e526295682b756c87065208a9..f10fb1854441b9d3578b7c6ed7384ab357269817 100644 --- a/test/periode-length.spec.js +++ b/test/periode-length.spec.js @@ -5,7 +5,7 @@ import periodInfo from '../lib/period-length' const expect = chai.expect -describe.only('getPeriodLengthStats', () => { +describe('getPeriodLengthStats', () => { it('works for a simple odd-numbered array', () => { const periodLengths = [99, 5, 1, 2, 100] const result = periodInfo(periodLengths) diff --git a/test/sympto/fixtures.js b/test/sympto/fixtures.js index 6b61492812c28970ff89c1a7cc0971acd3f85412..0bb4f9fbc96dd7144657e809e3d350bd330560ee 100644 --- a/test/sympto/fixtures.js +++ b/test/sympto/fixtures.js @@ -298,4 +298,27 @@ export const mucusPeakSlightlyBeforeTempShift = [ { date: '2018-06-20', temperature: 36.75, mucus: 1}, { date: '2018-06-21', temperature: 36.8, mucus: 1}, { date: '2018-06-22', temperature: 36.8, mucus: 1} +].map(convertToSymptoFormat) + + +export const highestMucusQualityAfterEndOfEval = [ + { 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-07', temperature: 36.4, mucus: 1 }, + { date: '2018-06-08', temperature: 36.35, mucus: 2}, + { date: '2018-06-09', temperature: 36.4, mucus: 2}, + { date: '2018-06-10', temperature: 36.45, mucus: 2}, + { date: '2018-06-11', temperature: 36.4, mucus: 2}, + { date: '2018-06-12', temperature: 36.45, mucus: 2}, + { date: '2018-06-13', temperature: 36.45, mucus: 3}, + { date: '2018-06-14', temperature: 36.55, mucus: 2}, + { date: '2018-06-15', temperature: 36.6, mucus: 2}, + { date: '2018-06-16', temperature: 36.6, mucus: 2}, + { date: '2018-06-17', temperature: 36.55, mucus: 2}, + { date: '2018-06-18', temperature: 36.6, mucus: 1}, + { date: '2018-06-19', temperature: 36.7, mucus: 4}, + { date: '2018-06-20', temperature: 36.75, mucus: 1}, + { date: '2018-06-21', temperature: 36.8, mucus: 1}, + { date: '2018-06-22', temperature: 36.8, mucus: 1} ].map(convertToSymptoFormat) \ No newline at end of file diff --git a/test/sympto/index.spec.js b/test/sympto/index.spec.js index 7f653191d39d7c3d43aeda2c14a30c0a5a1c74a1..8f1df47118972349981f056acfdb45721204aec7 100644 --- a/test/sympto/index.spec.js +++ b/test/sympto/index.spec.js @@ -17,7 +17,8 @@ import { mucusPeakTwoDaysBeforeFhm, fhmOnDay12, fhmOnDay15, - mucusPeakSlightlyBeforeTempShift + mucusPeakSlightlyBeforeTempShift, + highestMucusQualityAfterEndOfEval } from './fixtures' const expect = chai.expect @@ -360,6 +361,40 @@ describe('sympto', () => { .filter(({date}) => date >= '2018-06-21') }) }) + + it('with highest quality after end of eval', () => { + const status = getSensiplanStatus({ + cycle: highestMucusQualityAfterEndOfEval, + previousCycle: cycleWithFhm + }) + + expect(status.temperatureShift).to.be.an('object') + expect(status.mucusShift).to.be.an('object') + + expect(Object.keys(status.phases).length).to.eql(3) + expect(status.phases.preOvulatory).to.eql({ + start: { date: '2018-06-01' }, + end: { date: '2018-06-05' }, + cycleDays: highestMucusQualityAfterEndOfEval + .filter(({date}) => date <= '2018-06-05') + }) + expect(status.phases.periOvulatory).to.eql({ + start: { date: '2018-06-06' }, + end: { date: '2018-06-17', time: '18:00' }, + cycleDays: highestMucusQualityAfterEndOfEval + .filter(({date}) => { + return date > '2018-06-05' && date <= '2018-06-17' + }) + }) + expect(status.phases.postOvulatory).to.eql({ + start: { + date: '2018-06-17', + time: '18:00' + }, + cycleDays: highestMucusQualityAfterEndOfEval + .filter(({date}) => date >= '2018-06-17') + }) + }) }) describe('applying the minus-8 rule', () => {