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

Extract getLastMensesStart

parent ecbd00a3
No related branches found
No related tags found
No related merge requests found
import * as joda from 'js-joda'
const LocalDate = joda.LocalDate
import getLastMensesStart from './get-last-menses-start'
export default function config(opts = {}) {
let bleedingDaysSortedByDate
......@@ -12,37 +11,13 @@ export default function config(opts = {}) {
}
const maxBreakInBleeding = opts.maxBreakInBleeding || 1
return function getCycleDayNumber(targetDateString) {
const targetDate = LocalDate.parse(targetDateString)
const withWrappedDates = bleedingDaysSortedByDate
.filter(day => !day.bleeding.exclude)
.map(day => {
day.wrappedDate = LocalDate.parse(day.date)
return day
})
const firstBleedingDayBeforeTargetDayIndex = withWrappedDates.findIndex(day => {
return (
day.wrappedDate.isEqual(targetDate) ||
day.wrappedDate.isBefore(targetDate)
)
})
if (firstBleedingDayBeforeTargetDayIndex < 0) return null
const previousBleedingDays = withWrappedDates.slice(firstBleedingDayBeforeTargetDayIndex)
const lastPeriodStart = previousBleedingDays.find((day, i) => {
return thereIsNoPreviousBleedingDayWithinTheThreshold(day, previousBleedingDays.slice(i + 1), maxBreakInBleeding)
})
const diffInDays = lastPeriodStart.wrappedDate.until(targetDate, joda.ChronoUnit.DAYS)
return function(targetDateString) {
const lastMensesStart = getLastMensesStart(targetDateString, bleedingDaysSortedByDate, maxBreakInBleeding)
if (!lastMensesStart) return null
const targetDate = joda.LocalDate.parse(targetDateString)
const diffInDays = lastMensesStart.wrappedDate.until(targetDate, joda.ChronoUnit.DAYS)
// cycle starts at day 1
return diffInDays + 1
}
}
function thereIsNoPreviousBleedingDayWithinTheThreshold(bleedingDay, earlierCycleDays, allowedBleedingBreak) {
const periodThreshold = bleedingDay.wrappedDate.minusDays(allowedBleedingBreak + 1)
return !earlierCycleDays.some(({ wrappedDate }) => wrappedDate.equals(periodThreshold) || wrappedDate.isAfter(periodThreshold))
}
\ No newline at end of file
import * as joda from 'js-joda'
const LocalDate = joda.LocalDate
export default function getLastMensesStart(targetDateString, bleedingDaysSortedByDate, maxBreakInBleeding) {
const targetDate = LocalDate.parse(targetDateString)
const withWrappedDates = bleedingDaysSortedByDate
.filter(day => !day.bleeding.exclude)
.map(day => {
day.wrappedDate = LocalDate.parse(day.date)
return day
})
const firstBleedingDayBeforeTargetDayIndex = withWrappedDates.findIndex(day => {
return (
day.wrappedDate.isEqual(targetDate) ||
day.wrappedDate.isBefore(targetDate)
)
})
if (firstBleedingDayBeforeTargetDayIndex < 0) return null
const previousBleedingDays = withWrappedDates.slice(firstBleedingDayBeforeTargetDayIndex)
const lastPeriodStart = previousBleedingDays.find((day, i) => {
return thereIsNoPreviousBleedingDayWithinTheThreshold(day, previousBleedingDays.slice(i + 1), maxBreakInBleeding)
})
return lastPeriodStart
}
function thereIsNoPreviousBleedingDayWithinTheThreshold(bleedingDay, earlierCycleDays, allowedBleedingBreak) {
const periodThreshold = bleedingDay.wrappedDate.minusDays(allowedBleedingBreak + 1)
return !earlierCycleDays.some(({ wrappedDate }) => wrappedDate.equals(periodThreshold) || wrappedDate.isAfter(periodThreshold))
}
\ No newline at end of file
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