Fix cycle length in getCycle

This commit is contained in:
Julia Friesel
2019-01-04 14:33:13 +01:00
parent c28e32a953
commit 8d7fa268de
2 changed files with 16 additions and 46 deletions
+13 -6
View File
@@ -61,32 +61,39 @@ export default function config(opts) {
if (startFromHere < 0) return null
return cycleStartsSortedByDate
.slice(startFromHere)
.map(getCycleForCycleStartDay)
// filter the ones exceeding macCycleLength
.map(start => getCycleForCycleStartDay(start))
// filter the ones exceeding maxCycleLength, those are null
.filter(cycle => cycle)
}
function getCycleForCycleStartDay(startDay) {
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)
}
return cycle.length > maxCycleLength ? null : cycle
console.log(cycleLength, maxCycleLength)
return cycleLength > maxCycleLength ? null : cycle
}
function getCycleForDay(dayOrDate) {
function getCycleForDay(dayOrDate, todayDate) {
const dateString = typeof dayOrDate === 'string' ? dayOrDate : dayOrDate.date
const cycleStart = getLastMensesStartForDay(dateString)
if (!cycleStart) return null
return getCycleForCycleStartDay(cycleStart)
return getCycleForCycleStartDay(cycleStart, todayDate)
}
function isMensesStart(cycleDay) {
+3 -40
View File
@@ -421,7 +421,7 @@ describe('getCyclesBefore', () => {
])
})
it('skips cycle that are longer than max', () => {
it('skips cycles that are longer than max', () => {
const cycleDaysSortedByDate = [
{
date: '2018-07-05',
@@ -476,15 +476,7 @@ describe('getCyclesBefore', () => {
maxCycleLength: 2
})
const result = getCyclesBefore(cycleDaysSortedByDate[0])
expect(result.length).to.eql(1)
expect(result).to.eql([
[
{
date: '2018-06-05',
bleeding: { value: 2 }
}
]
])
expect(result.length).to.eql(0)
})
})
@@ -542,7 +534,7 @@ describe('getCycleForDay', () => {
})
it('gets cycle that has only one day', () => {
const result = getCycleForDay('2018-07-05')
const result = getCycleForDay('2018-07-05', '2018-08-01')
expect(result.length).to.eql(1)
expect(result).to.eql([
{
@@ -588,35 +580,6 @@ describe('getCycleForDay', () => {
expect(result).to.eql(null)
})
it('returns the cycle if the cycle is shorter or equal max', () => {
const { getCycleForDay } = cycleModule({
cycleDaysSortedByDate,
cycleStartsSortedByDate: cycleDaysSortedByDate.filter(d => {
return cycleStarts.includes(d.date)
}),
maxCycleLength: 4
})
const result = getCycleForDay('2018-04-04')
expect(result).to.eql([
{
date: '2018-04-05',
mucus: { value: 2 }
},
{
date: '2018-04-04',
mucus: { value: 2 }
},
{
date: '2018-04-03',
mucus: { value: 2 }
},
{
date: '2018-04-02',
bleeding: { value: 2 }
},
])
})
it('gets cycle for day', () => {
const result = getCycleForDay('2018-04-04')
expect(result.length).to.eql(4)