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 if (startFromHere < 0) return null
return cycleStartsSortedByDate return cycleStartsSortedByDate
.slice(startFromHere) .slice(startFromHere)
.map(getCycleForCycleStartDay) .map(start => getCycleForCycleStartDay(start))
// filter the ones exceeding macCycleLength // filter the ones exceeding maxCycleLength, those are null
.filter(cycle => cycle) .filter(cycle => cycle)
} }
function getCycleForCycleStartDay(startDay) { function getCycleForCycleStartDay(startDay, todayDate) {
const todayAsLocalDate = todayDate ? LocalDate.parse(todayDate) : LocalDate.now()
const cycleStartIndex = cycleDaysSortedByDate.indexOf(startDay) const cycleStartIndex = cycleDaysSortedByDate.indexOf(startDay)
const i = cycleStartsSortedByDate.indexOf(startDay) const i = cycleStartsSortedByDate.indexOf(startDay)
const startLocalDate = LocalDate.parse(startDay.date)
const nextMensesStart = cycleStartsSortedByDate[i - 1] const nextMensesStart = cycleStartsSortedByDate[i - 1]
let cycle let cycle
let cycleLength
if (nextMensesStart) { if (nextMensesStart) {
cycle = cycleDaysSortedByDate.slice( cycle = cycleDaysSortedByDate.slice(
cycleDaysSortedByDate.indexOf(nextMensesStart) + 1, cycleDaysSortedByDate.indexOf(nextMensesStart) + 1,
cycleStartIndex + 1, cycleStartIndex + 1,
) )
const nextLocalDate = LocalDate.parse(nextMensesStart.date)
cycleLength = startLocalDate.until(nextLocalDate, DAYS)
} else { } else {
cycle = cycleDaysSortedByDate.slice(0, cycleStartIndex + 1) 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 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) return getCycleForCycleStartDay(cycleStart, todayDate)
} }
function isMensesStart(cycleDay) { 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 = [ const cycleDaysSortedByDate = [
{ {
date: '2018-07-05', date: '2018-07-05',
@@ -476,15 +476,7 @@ describe('getCyclesBefore', () => {
maxCycleLength: 2 maxCycleLength: 2
}) })
const result = getCyclesBefore(cycleDaysSortedByDate[0]) const result = getCyclesBefore(cycleDaysSortedByDate[0])
expect(result.length).to.eql(1) expect(result.length).to.eql(0)
expect(result).to.eql([
[
{
date: '2018-06-05',
bleeding: { value: 2 }
}
]
])
}) })
}) })
@@ -542,7 +534,7 @@ describe('getCycleForDay', () => {
}) })
it('gets cycle that has only one day', () => { 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.length).to.eql(1)
expect(result).to.eql([ expect(result).to.eql([
{ {
@@ -588,35 +580,6 @@ describe('getCycleForDay', () => {
expect(result).to.eql(null) 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', () => { it('gets cycle for day', () => {
const result = getCycleForDay('2018-04-04') const result = getCycleForDay('2018-04-04')
expect(result.length).to.eql(4) expect(result.length).to.eql(4)