Add getMensesDaysAfter
This commit is contained in:
+32
-9
@@ -135,23 +135,45 @@ export default function config(opts) {
|
||||
|
||||
function isMensesStart(cycleDay) {
|
||||
if (!cycleDay.bleeding || cycleDay.bleeding.exclude) return false
|
||||
const bleedingDays = bleedingDaysSortedByDate
|
||||
if (noBleedingDayWithinThreshold(cycleDay)) {
|
||||
return true
|
||||
}
|
||||
if (noBleedingDayWithinThresholdBefore(cycleDay)) return true
|
||||
return false
|
||||
|
||||
function noBleedingDayWithinThreshold(day) {
|
||||
const localDate = LocalDate.parse(day.date)
|
||||
function noBleedingDayWithinThresholdBefore(cycleDay) {
|
||||
const localDate = LocalDate.parse(cycleDay.date)
|
||||
const threshold = localDate.minusDays(maxBreakInBleeding + 1).toString()
|
||||
const bleedingDays = bleedingDaysSortedByDate
|
||||
const index = bleedingDays.findIndex(day => day.date === cycleDay.date)
|
||||
const previousBleedingDays = bleedingDays.slice(index + 1)
|
||||
return !previousBleedingDays.some(day => {
|
||||
const candidates = bleedingDays.slice(index + 1)
|
||||
return !candidates.some(day => {
|
||||
return day.date >= threshold && !day.bleeding.exclude
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function getMensesDaysAfter(bleedingDay) {
|
||||
const bleedingDays = bleedingDaysSortedByDate.filter(d => {
|
||||
return !d.bleeding.exclude
|
||||
})
|
||||
const startIndex = bleedingDays.findIndex(day => {
|
||||
return day.date === bleedingDay.date
|
||||
})
|
||||
return recurse(bleedingDay, startIndex, [])
|
||||
|
||||
function recurse(day, i, mensesDays) {
|
||||
if (i === 0) return mensesDays
|
||||
const next = bleedingDays[i - 1]
|
||||
if (!isWithinThreshold(day, next)) return mensesDays
|
||||
mensesDays.unshift(next)
|
||||
return recurse(next, i - 1, mensesDays)
|
||||
}
|
||||
|
||||
function isWithinThreshold(cycleDay, nextCycleDay) {
|
||||
const localDate = LocalDate.parse(cycleDay.date)
|
||||
const threshold = localDate.plusDays(maxBreakInBleeding + 1).toString()
|
||||
return nextCycleDay.date <= threshold
|
||||
}
|
||||
}
|
||||
|
||||
function getCycleLength(cycleStartDates) {
|
||||
const cycleLengths = []
|
||||
for (let i = 0; i < cycleStartDates.length - 1; i++) {
|
||||
@@ -208,6 +230,7 @@ export default function config(opts) {
|
||||
getAllMensesStarts,
|
||||
getCycleLength,
|
||||
getPredictedMenses,
|
||||
isMensesStart
|
||||
isMensesStart,
|
||||
getMensesDaysAfter
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user