From 8d7fa268de7edf7d2b33d78a0b56d56d221c63af Mon Sep 17 00:00:00 2001 From: Julia Friesel Date: Fri, 4 Jan 2019 14:33:13 +0100 Subject: [PATCH] Fix cycle length in getCycle --- lib/cycle.js | 19 +++++++++++++------ test/cycle.spec.js | 43 +++---------------------------------------- 2 files changed, 16 insertions(+), 46 deletions(-) diff --git a/lib/cycle.js b/lib/cycle.js index 7236d85..bf300ab 100644 --- a/lib/cycle.js +++ b/lib/cycle.js @@ -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) { diff --git a/test/cycle.spec.js b/test/cycle.spec.js index b55471c..105263c 100644 --- a/test/cycle.spec.js +++ b/test/cycle.spec.js @@ -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)