diff --git a/lib/cycle.js b/lib/cycle.js index f60a74f321d040e9937f7726d995ba24cbdec651..70401d5baa4755f5a4539092eae94f470e34bb37 100644 --- a/lib/cycle.js +++ b/lib/cycle.js @@ -1,6 +1,6 @@ import * as joda from 'js-joda' const LocalDate = joda.LocalDate - +const DAYS = joda.ChronoUnit.DAYS export default function config(opts) { let bleedingDaysSortedByDate @@ -28,28 +28,31 @@ export default function config(opts) { return day }) - const firstBleedingDayBeforeTargetDayIndex = withWrappedDates.findIndex(day => { + const firstBleedingBeforeTargetDayIndex = withWrappedDates.findIndex(day => { return ( day.wrappedDate.isEqual(targetDate) || day.wrappedDate.isBefore(targetDate) ) }) - if (firstBleedingDayBeforeTargetDayIndex < 0) { + if (firstBleedingBeforeTargetDayIndex < 0) { withWrappedDates.forEach(day => delete day.wrappedDate) return null } - const previousBleedingDays = withWrappedDates.slice(firstBleedingDayBeforeTargetDayIndex) + const prevBleedingDays = withWrappedDates.slice(firstBleedingBeforeTargetDayIndex) - const lastMensesStart = previousBleedingDays.find((day, i) => { - return thereIsNoPreviousBleedingDayWithinTheThreshold(day, previousBleedingDays.slice(i + 1)) + const lastMensesStart = prevBleedingDays.find((day, i) => { + return noBleedingDayWithinThreshold(day, prevBleedingDays.slice(i + 1)) }) - function thereIsNoPreviousBleedingDayWithinTheThreshold(bleedingDay, previousBleedingDays) { - const periodThreshold = bleedingDay.wrappedDate.minusDays(maxBreakInBleeding + 1) + function noBleedingDayWithinThreshold(day, previousBleedingDays) { + const periodThreshold = day.wrappedDate.minusDays(maxBreakInBleeding + 1) return !previousBleedingDays.some(({ wrappedDate }) => { - return wrappedDate.equals(periodThreshold) || wrappedDate.isAfter(periodThreshold) + return ( + wrappedDate.equals(periodThreshold) || + wrappedDate.isAfter(periodThreshold) + ) }) } @@ -66,9 +69,11 @@ export default function config(opts) { return day }) - const firstBleedingDayAfterTargetDay = withWrappedDates.reverse().find(day => { - return day.wrappedDate.isAfter(targetDate) - }) + const firstBleedingDayAfterTargetDay = withWrappedDates + .reverse() + .find(day => { + return day.wrappedDate.isAfter(targetDate) + }) withWrappedDates.forEach(day => delete day.wrappedDate) @@ -80,7 +85,7 @@ export default function config(opts) { if (!lastMensesStart) return null const targetDate = LocalDate.parse(targetDateString) const lastMensesLocalDate = LocalDate.parse(lastMensesStart.date) - const diffInDays = lastMensesLocalDate.until(targetDate, joda.ChronoUnit.DAYS) + const diffInDays = lastMensesLocalDate.until(targetDate, DAYS) // cycle starts at day 1 return diffInDays + 1 @@ -100,7 +105,11 @@ export default function config(opts) { function getPreviousCycle(dateString) { const startOfCycle = getLastMensesStart(dateString) if (!startOfCycle) return null - const dateBeforeStartOfCycle = LocalDate.parse(startOfCycle.date).minusDays(1).toString() + const dateBeforeStartOfCycle = LocalDate + .parse(startOfCycle.date) + .minusDays(1) + .toString() + return getCycleForDay(dateBeforeStartOfCycle) }