Change getCycleDay signature and process LocalDate
This commit is contained in:
+18
-29
@@ -1,54 +1,43 @@
|
||||
import moment from 'moment'
|
||||
import * as joda from 'js-joda'
|
||||
|
||||
export default function config(opts) {
|
||||
const LocalDate = joda.LocalDate
|
||||
|
||||
export default function config(bleedingDaysSortedByDateView, 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
|
||||
maxBreakInBleeding: 1
|
||||
}
|
||||
|
||||
return function getCycleDayNumber(unsorted, targetDate) {
|
||||
return function getCycleDayNumber(targetDateString) {
|
||||
// sort the cycle days in descending order so we travel into
|
||||
// the past as we iterate over the array
|
||||
// also, to retrieve the number, we only need the cycle days before the target day
|
||||
// TODO we can probably rely on the db to do the sorting for us
|
||||
targetDate = moment(targetDate)
|
||||
const sorted = unsorted
|
||||
const targetDate = LocalDate.parse(targetDateString)
|
||||
const withWrappedDates = bleedingDaysSortedByDateView
|
||||
.filter(day => !day.bleeding.exclude)
|
||||
.map(day => {
|
||||
day.date = moment(day.date)
|
||||
day.wrappedDate = LocalDate.parse(day.date)
|
||||
return day
|
||||
})
|
||||
.sort((a, b) => b.date.isAfter(a.date))
|
||||
const firstDayBeforeTargetDayIndex = sorted.findIndex(day => day.date.isBefore(targetDate))
|
||||
const cycleDays = sorted.slice(firstDayBeforeTargetDayIndex)
|
||||
// TODO write test for what if there is no first day before?? aka no firstbleedingdaybeforeindex
|
||||
const firstBleedingDayBeforeTargetDayIndex = withWrappedDates.findIndex(day => day.wrappedDate.isBefore(targetDate))
|
||||
const cycleDays = withWrappedDates.slice(firstBleedingDayBeforeTargetDayIndex)
|
||||
|
||||
const lastPeriodStart = cycleDays.find((day, i) => {
|
||||
if (
|
||||
isBleedingDay(day) &&
|
||||
thereIsNoPreviousBleedingDayWithinTheThreshold(day, cycleDays.slice(i + 1), opts.minCycleLengthInDays)) {
|
||||
return true
|
||||
}
|
||||
return thereIsNoPreviousBleedingDayWithinTheThreshold(day, cycleDays.slice(i + 1), opts.maxBreakInBleeding)
|
||||
})
|
||||
|
||||
if (!lastPeriodStart) return null
|
||||
|
||||
// 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))
|
||||
const diffInDays = lastPeriodStart.wrappedDate.until(targetDate, joda.ChronoUnit.DAYS)
|
||||
|
||||
// cycle starts at day 1
|
||||
return diffInDays + 1
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
function thereIsNoPreviousBleedingDayWithinTheThreshold(bleedingDay, earlierCycleDays, allowedBleedingBreak) {
|
||||
const periodThreshold = bleedingDay.wrappedDate.minusDays(allowedBleedingBreak + 1)
|
||||
return !earlierCycleDays.some(({ wrappedDate }) => wrappedDate.equals(periodThreshold) || wrappedDate.isAfter(periodThreshold))
|
||||
}
|
||||
Reference in New Issue
Block a user