Adds test and fixes getCycleByStartDate method of cycle module
This commit is contained in:
+55
-24
@@ -51,7 +51,7 @@ export default function config(opts) {
|
||||
const i = cycleStartsSortedByDate.indexOf(cycleStart)
|
||||
const earlierCycleStart = cycleStartsSortedByDate[i + 1]
|
||||
if (!earlierCycleStart) return null
|
||||
return getCycleForCycleStartDay(earlierCycleStart)
|
||||
return getCycleByStartDay(earlierCycleStart)
|
||||
}
|
||||
|
||||
function getCyclesBefore(targetCycleStartDay) {
|
||||
@@ -61,40 +61,70 @@ export default function config(opts) {
|
||||
if (startFromHere < 0) return null
|
||||
return cycleStartsSortedByDate
|
||||
.slice(startFromHere)
|
||||
.map(start => getCycleForCycleStartDay(start))
|
||||
.map(start => getCycleByStartDay(start))
|
||||
// filter the ones exceeding maxCycleLength, those are null
|
||||
.filter(cycle => cycle)
|
||||
}
|
||||
|
||||
function getCycleForCycleStartDay(startDay, todayDate) {
|
||||
const todayAsLocalDate = todayDate
|
||||
? LocalDate.parse(todayDate)
|
||||
: LocalDate.now()
|
||||
const cycleStartIndex = cycleDaysSortedByDate.indexOf(startDay)
|
||||
const i = cycleStartsSortedByDate.indexOf(startDay)
|
||||
const startLocalDate = LocalDate.parse(startDay.date)
|
||||
const nextMensesStart = cycleStartsSortedByDate[i - 1]
|
||||
let cycle
|
||||
let cycleLength
|
||||
if (nextMensesStart) {
|
||||
cycle = cycleDaysSortedByDate.slice(
|
||||
cycleDaysSortedByDate.indexOf(nextMensesStart) + 1,
|
||||
cycleStartIndex + 1,
|
||||
function findIndexOfDay(day, daysSortedByDate) {
|
||||
return daysSortedByDate.findIndex(d => d.date === day.date)
|
||||
}
|
||||
|
||||
function getNextCycleStartDay(startDay, cycleStartsSortedByDate) {
|
||||
const cycleStartIndex = findIndexOfDay(
|
||||
startDay,
|
||||
cycleStartsSortedByDate)
|
||||
return cycleStartsSortedByDate[cycleStartIndex - 1]
|
||||
}
|
||||
|
||||
function getTodayDate() {
|
||||
return new Date().toISOString().slice(0, 10)
|
||||
}
|
||||
|
||||
function getCycleLength(startDate, endDate) {
|
||||
return LocalDate.parse(startDate)
|
||||
.until(LocalDate.parse(endDate), DAYS)
|
||||
}
|
||||
|
||||
function isValidCycle(startDate, endDate) {
|
||||
return getCycleLength(startDate, endDate) <= maxCycleLength
|
||||
}
|
||||
|
||||
function getCycleByStartDay(startDay, todayDate) {
|
||||
let cycleEndDate = todayDate || getTodayDate()
|
||||
let cycleEndIndex = 0
|
||||
|
||||
const nextCycleStart = getNextCycleStartDay(
|
||||
startDay,
|
||||
cycleStartsSortedByDate
|
||||
)
|
||||
|
||||
if (nextCycleStart) {
|
||||
const nextCycleIndex = findIndexOfDay(
|
||||
nextCycleStart,
|
||||
cycleDaysSortedByDate
|
||||
)
|
||||
const nextLocalDate = LocalDate.parse(nextMensesStart.date)
|
||||
cycleLength = startLocalDate.until(nextLocalDate, DAYS)
|
||||
} else {
|
||||
cycle = cycleDaysSortedByDate.slice(0, cycleStartIndex + 1)
|
||||
cycleLength = startLocalDate.until(todayAsLocalDate, DAYS)
|
||||
cycleEndIndex = nextCycleIndex + 1
|
||||
cycleEndDate = nextCycleStart.date
|
||||
}
|
||||
return cycleLength > maxCycleLength ? null : cycle
|
||||
|
||||
if (isValidCycle(startDay.date, cycleEndDate)) {
|
||||
const cycleStartIndex = findIndexOfDay(
|
||||
startDay,
|
||||
cycleDaysSortedByDate
|
||||
)
|
||||
return cycleDaysSortedByDate
|
||||
.slice(cycleEndIndex, cycleStartIndex + 1)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
function getCycleForDay(dayOrDate, todayDate) {
|
||||
const dateString = typeof dayOrDate === 'string' ? dayOrDate : dayOrDate.date
|
||||
const cycleStart = getLastMensesStartForDay(dateString)
|
||||
if (!cycleStart) return null
|
||||
return getCycleForCycleStartDay(cycleStart, todayDate)
|
||||
return getCycleByStartDay(cycleStart, todayDate)
|
||||
}
|
||||
|
||||
function isMensesStart(cycleDay) {
|
||||
@@ -201,6 +231,7 @@ export default function config(opts) {
|
||||
getAllCycleLengths,
|
||||
getPredictedMenses,
|
||||
isMensesStart,
|
||||
getMensesDaysRightAfter
|
||||
getMensesDaysRightAfter,
|
||||
getCycleByStartDay,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user