Skip to content
Snippets Groups Projects
Commit 2f9b08fd authored by Julia Friesel's avatar Julia Friesel
Browse files

Shorten line lengths in cycle

parent e1fa656b
No related branches found
No related tags found
No related merge requests found
import * as joda from 'js-joda' import * as joda from 'js-joda'
const LocalDate = joda.LocalDate const LocalDate = joda.LocalDate
const DAYS = joda.ChronoUnit.DAYS
export default function config(opts) { export default function config(opts) {
let bleedingDaysSortedByDate let bleedingDaysSortedByDate
...@@ -28,28 +28,31 @@ export default function config(opts) { ...@@ -28,28 +28,31 @@ export default function config(opts) {
return day return day
}) })
const firstBleedingDayBeforeTargetDayIndex = withWrappedDates.findIndex(day => { const firstBleedingBeforeTargetDayIndex = withWrappedDates.findIndex(day => {
return ( return (
day.wrappedDate.isEqual(targetDate) || day.wrappedDate.isEqual(targetDate) ||
day.wrappedDate.isBefore(targetDate) day.wrappedDate.isBefore(targetDate)
) )
}) })
if (firstBleedingDayBeforeTargetDayIndex < 0) { if (firstBleedingBeforeTargetDayIndex < 0) {
withWrappedDates.forEach(day => delete day.wrappedDate) withWrappedDates.forEach(day => delete day.wrappedDate)
return null return null
} }
const previousBleedingDays = withWrappedDates.slice(firstBleedingDayBeforeTargetDayIndex) const prevBleedingDays = withWrappedDates.slice(firstBleedingBeforeTargetDayIndex)
const lastMensesStart = previousBleedingDays.find((day, i) => { const lastMensesStart = prevBleedingDays.find((day, i) => {
return thereIsNoPreviousBleedingDayWithinTheThreshold(day, previousBleedingDays.slice(i + 1)) return noBleedingDayWithinThreshold(day, prevBleedingDays.slice(i + 1))
}) })
function thereIsNoPreviousBleedingDayWithinTheThreshold(bleedingDay, previousBleedingDays) { function noBleedingDayWithinThreshold(day, previousBleedingDays) {
const periodThreshold = bleedingDay.wrappedDate.minusDays(maxBreakInBleeding + 1) const periodThreshold = day.wrappedDate.minusDays(maxBreakInBleeding + 1)
return !previousBleedingDays.some(({ wrappedDate }) => { 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) { ...@@ -66,9 +69,11 @@ export default function config(opts) {
return day return day
}) })
const firstBleedingDayAfterTargetDay = withWrappedDates.reverse().find(day => { const firstBleedingDayAfterTargetDay = withWrappedDates
return day.wrappedDate.isAfter(targetDate) .reverse()
}) .find(day => {
return day.wrappedDate.isAfter(targetDate)
})
withWrappedDates.forEach(day => delete day.wrappedDate) withWrappedDates.forEach(day => delete day.wrappedDate)
...@@ -80,7 +85,7 @@ export default function config(opts) { ...@@ -80,7 +85,7 @@ export default function config(opts) {
if (!lastMensesStart) return null if (!lastMensesStart) return null
const targetDate = LocalDate.parse(targetDateString) const targetDate = LocalDate.parse(targetDateString)
const lastMensesLocalDate = LocalDate.parse(lastMensesStart.date) 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 // cycle starts at day 1
return diffInDays + 1 return diffInDays + 1
...@@ -100,7 +105,11 @@ export default function config(opts) { ...@@ -100,7 +105,11 @@ export default function config(opts) {
function getPreviousCycle(dateString) { function getPreviousCycle(dateString) {
const startOfCycle = getLastMensesStart(dateString) const startOfCycle = getLastMensesStart(dateString)
if (!startOfCycle) return null 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) return getCycleForDay(dateBeforeStartOfCycle)
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment