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