Adds test and fixes getCycleByStartDate method of cycle module

This commit is contained in:
Sofiya Tepikin
2020-03-05 19:31:52 +01:00
parent e901cae0f5
commit 9e34c1b8b4
2 changed files with 98 additions and 24 deletions
+56 -25
View File
@@ -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,
)
const nextLocalDate = LocalDate.parse(nextMensesStart.date)
cycleLength = startLocalDate.until(nextLocalDate, DAYS)
} else {
cycle = cycleDaysSortedByDate.slice(0, cycleStartIndex + 1)
cycleLength = startLocalDate.until(todayAsLocalDate, DAYS)
function findIndexOfDay(day, daysSortedByDate) {
return daysSortedByDate.findIndex(d => d.date === day.date)
}
return cycleLength > maxCycleLength ? null : cycle
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
)
cycleEndIndex = nextCycleIndex + 1
cycleEndDate = nextCycleStart.date
}
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,
}
}
+43
View File
@@ -0,0 +1,43 @@
import chai from 'chai'
import dirtyChai from 'dirty-chai'
import cycleModule from '../lib/cycle'
const { expect } = chai
chai.use(dirtyChai)
const cycleStartDay = { date: '2018-05-03' }
const cycle = [
{ date: '2018-05-05' },
{ date: '2018-05-04' },
cycleStartDay,
]
const cycleDaysSortedByDate = [
{ date: '2018-07-05' },
{ date: '2018-06-05' },
...cycle,
{ date: '2018-04-05' },
{ date: '2018-04-04' },
{ date: '2018-04-03' },
{ date: '2018-04-02' },
]
const cycleStartsSortedByDate = [
{ date: '2018-07-05' },
{ date: '2018-06-05' },
{ date: '2018-05-03' },
{ date: '2018-04-02' },
]
describe('getCycleByStartDay', () => {
it('gets cycle by cycle start day', () => {
const { getCycleByStartDay } = cycleModule({
cycleDaysSortedByDate,
cycleStartsSortedByDate,
})
expect(getCycleByStartDay(cycleStartDay)).to.eql(cycle)
})
})