From 77ad491d300551ae4d5ee133f4dd84679bd2df7e Mon Sep 17 00:00:00 2001 From: Julia Friesel Date: Tue, 10 Jul 2018 10:55:33 +0200 Subject: [PATCH] Return evaluation completed day from mucus eval --- lib/sympto/mucus.js | 34 ++++++++++++++++++++-------------- test/sympto/mucus.spec.js | 6 +++++- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/lib/sympto/mucus.js b/lib/sympto/mucus.js index ada8127..f848881 100644 --- a/lib/sympto/mucus.js +++ b/lib/sympto/mucus.js @@ -2,31 +2,37 @@ 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)) - const mucusPeak = cycleDays.find((day, i) => { - if (!mucusDays.includes(day) || day.mucus.value !== bestQuality) return false + for (let i = 0; i < mucusDays.length; i++) { + const day = mucusDays[i] + if (day.mucus.value !== bestQuality) continue // sensiplan says the three following days must be of lower quality // AND no best quality day may occur until temperature evaluation has // been completed - const mucusDaysIndex = mucusDays.indexOf(day) - const threeFollowingDays = mucusDays.slice(mucusDaysIndex + 1, mucusDaysIndex + 4) - if (threeFollowingDays.length < 3) return false + const threeFollowingDays = mucusDays.slice(i + 1, i + 4) + if (threeFollowingDays.length < 3) continue const bestQualityOccurringIn3FollowingDays = threeFollowingDays.some(day => day.mucus.value >= bestQuality) - if (bestQualityOccurringIn3FollowingDays) return false + if (bestQualityOccurringIn3FollowingDays) continue + // FIXME mucus peak can be same day as first higher measurement + const cycleDayIndex = cycleDays.indexOf(day) const relevantDays = cycleDays - .slice(i + 1, tempEvalEndIndex + 1) + .slice(cycleDayIndex + 1, tempEvalEndIndex + 1) .filter(day => day.mucus && !day.mucus.exclude) - return relevantDays.every(day => day.mucus.value < bestQuality) - }) + const evaluationCompleteDay = relevantDays.length > 3 ? + relevantDays[relevantDays.length - 1] : threeFollowingDays[threeFollowingDays.length - 1] - if (!mucusPeak) return { detected: false } - - return { - detected: true, - mucusPeak + if (relevantDays.every(day => day.mucus.value < bestQuality)) { + return { + detected: true, + mucusPeak: day, + evaluationCompleteDay + } + } } + + return { detected: false } } diff --git a/test/sympto/mucus.spec.js b/test/sympto/mucus.spec.js index 7e9b704..f8b83d2 100644 --- a/test/sympto/mucus.spec.js +++ b/test/sympto/mucus.spec.js @@ -16,12 +16,16 @@ describe('sympto', () => { it('detects mucus shift correctly', function () { const values = [0, 0, 0, 1, 1, 2, 2, 2, 3, 3, 3, 2, 2, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0] .map(turnIntoCycleDayObject) - const status = getMucusStatus(values, 30) + const status = getMucusStatus(values, 12) expect(status).to.eql({ detected: true, mucusPeak: { date: 10, mucus: { value: 3 } + }, + evaluationCompleteDay: { + date: 13, + mucus: { value: 0 } } }) })