Add option for minimum cycle length

This commit is contained in:
Julia Friesel
2018-06-05 11:00:21 +02:00
parent 2b8569d6d0
commit 1cf4087039
4 changed files with 99 additions and 47 deletions
+34 -36
View File
@@ -1,46 +1,44 @@
export default function (cycleDays, targetDay) {
const lastFirstBleedingday = findLastFirstBleedingDay(cycleDays)
import moment from 'moment'
if (!lastFirstBleedingday) return
export default function config(opts) {
opts = opts || {
// at the very minimum, a cycle can be a bleeding day
// followed by a non-bleeding day, thus a length of 2
minCycleLengthInDays: 2
}
const diffInDays = targetDay.diff(lastFirstBleedingday.date, 'days')
return function getCycleDayNumber(unsorted, targetDate) {
// sort the cycle days in descending order so we travel into
// the past as we iterate over the array
const cycleDays = [...unsorted].sort((a, b) => b.date.isAfter(a.date))
// cycle starts at day 1
return diffInDays + 1
}
const lastPeriodStart = cycleDays.find((day, i) => {
if (
isBleedingDay(day) &&
thereIsNoPreviousBleedingDayWithinTheThreshold(day, cycleDays.slice(i + 1), opts.minCycleLengthInDays)) {
return true
}
})
function findLastFirstBleedingDay(cycleDays) {
// sort the cycle days in descending order
// so we travel into the past as we iterate
// over the array
cycleDays.sort((a,b) => b.date - a.date)
if (!lastPeriodStart) return null
let sawBleeding = false
// by default, moment.diff rounds down, so we get the decimal number
// and round it ourselves
const diffInDays = Math.round(targetDate.diff(lastPeriodStart.date, 'days', true))
for (let i = 0; i < cycleDays.length; i++) {
const cycleDay = cycleDays[i]
// we have detected the day before the beginning
// of a bleeding period, so the previous cycle day in the array
// was the first day of bleeding
if (sawBleeding && (!isBleedingDay(cycleDay))) {
return cycleDays[i - 1]
}
// if we get to the earliest cycle day and there was
// bleeding on that day, it's the first bleeding day for us
if (i === cycleDays.length - 1 && isBleedingDay(cycleDay)) {
return cycleDay
}
if (isBleedingDay(cycleDay)) {
sawBleeding = true
}
// cycle starts at day 1
return diffInDays + 1
}
}
function isBleedingDay(cycleDay) {
return cycleDay.bleeding
&& cycleDay.bleeding.value
&& !cycleDay.bleeding.exclude
function thereIsNoPreviousBleedingDayWithinTheThreshold(bleedingDay, earlierCycleDays, threshold) {
// the bleeding day itself is included in the threshold, thus we subtract 1
const minCycleThresholdDate = moment(bleedingDay.date).subtract(threshold - 1, 'days')
return !earlierCycleDays.some(day => {
return day.date.isSameOrAfter(minCycleThresholdDate, 'day') && isBleedingDay(day)
})
}
function isBleedingDay(day) {
return day.bleeding && day.bleeding.value && !day.bleeding.exclude
}