Get previous cycles before detecting fertility status
This commit is contained in:
+27
-3
@@ -1,4 +1,5 @@
|
||||
import * as joda from 'js-joda'
|
||||
|
||||
const LocalDate = joda.LocalDate
|
||||
|
||||
export default function config(opts) {
|
||||
@@ -49,14 +50,16 @@ export default function config(opts) {
|
||||
return !previousBleedingDays.some(({ wrappedDate }) => wrappedDate.equals(periodThreshold) || wrappedDate.isAfter(periodThreshold))
|
||||
}
|
||||
|
||||
withWrappedDates.forEach(day => delete day.wrappedDate)
|
||||
return lastPeriodStart
|
||||
}
|
||||
|
||||
function getCycleDayNumber(targetDateString) {
|
||||
const lastMensesStart = getLastMensesStart(targetDateString)
|
||||
if (!lastMensesStart) return null
|
||||
const targetDate = joda.LocalDate.parse(targetDateString)
|
||||
const diffInDays = lastMensesStart.wrappedDate.until(targetDate, joda.ChronoUnit.DAYS)
|
||||
const targetDate = LocalDate.parse(targetDateString)
|
||||
const lastMensesLocalDate = LocalDate.parse(lastMensesStart.date)
|
||||
const diffInDays = lastMensesLocalDate.until(targetDate, joda.ChronoUnit.DAYS)
|
||||
|
||||
// cycle starts at day 1
|
||||
return diffInDays + 1
|
||||
@@ -73,15 +76,36 @@ export default function config(opts) {
|
||||
|
||||
function getCycleDaysBeforeDay(targetDateString) {
|
||||
const firstCycleDay = getLastMensesStart(targetDateString)
|
||||
if (!firstCycleDay) return null
|
||||
return cycleDaysSortedByDate.filter(({date}) => {
|
||||
return date >= firstCycleDay.date && date <= targetDateString
|
||||
})
|
||||
}
|
||||
|
||||
function getPreviousCycles(targetCycleStartDay) {
|
||||
let previousCycleStartIndex = cycleDaysSortedByDate.indexOf(targetCycleStartDay)
|
||||
const cycles = []
|
||||
while (previousCycleStartIndex < cycleDaysSortedByDate.length - 1) {
|
||||
const prevDate = cycleDaysSortedByDate[previousCycleStartIndex + 1].date
|
||||
const cycleStart = getLastMensesStart(prevDate)
|
||||
|
||||
if (!cycleStart) break
|
||||
|
||||
const cycleStartIndex = cycleDaysSortedByDate.indexOf(cycleStart)
|
||||
const lastDayInCycle = previousCycleStartIndex + 1
|
||||
const cycle = cycleDaysSortedByDate.slice(lastDayInCycle, cycleStartIndex + 1)
|
||||
cycles.push(cycle)
|
||||
previousCycleStartIndex = cycleStartIndex
|
||||
}
|
||||
|
||||
return cycles
|
||||
}
|
||||
|
||||
return {
|
||||
getCycleDayNumber,
|
||||
getLastMensesStart,
|
||||
getPreviousTemperaturesInCycle,
|
||||
getCycleDaysBeforeDay
|
||||
getCycleDaysBeforeDay,
|
||||
getPreviousCycles
|
||||
}
|
||||
}
|
||||
|
||||
+10
-6
@@ -1,18 +1,22 @@
|
||||
import getFertilityStatus from './sympto'
|
||||
import cycleModule from './cycle'
|
||||
|
||||
const { getCycleDaysBeforeDay } = cycleModule()
|
||||
const { getCycleDaysBeforeDay, getPreviousCycles } = cycleModule()
|
||||
|
||||
export default function (dateString) {
|
||||
const cycle = getCycleDaysBeforeDay(dateString)
|
||||
if (!cycle) return `We cannot show any cycle information because no menses has been entered`
|
||||
|
||||
// we get earliest last, but sympto wants earliest first
|
||||
const cycle = getCycleDaysBeforeDay(dateString).reverse()
|
||||
// const previousCycles = getPreviousCycles()
|
||||
const status = getFertilityStatus({cycle})
|
||||
cycle.reverse()
|
||||
const previousCycles = getPreviousCycles(cycle[0])
|
||||
previousCycles.forEach(cycle => cycle.reverse())
|
||||
|
||||
const status = getFertilityStatus({cycle, previousCycles})
|
||||
|
||||
return formatStatusForApp(status)
|
||||
}
|
||||
|
||||
function formatStatusForApp(status) {
|
||||
const fertileStatus = status.assumeFertility ? 'fertile' : 'infertile'
|
||||
return `You are currently ${fertileStatus}`
|
||||
return status.assumeFertility ? 'fertile' : 'infertile'
|
||||
}
|
||||
Reference in New Issue
Block a user