diff --git a/lib/cycle.js b/lib/cycle.js index 1418eb448aefd1b0694620275adeb26d78674f9a..d171398af7ef295fa8b1945aa59fea9521452473 100644 --- a/lib/cycle.js +++ b/lib/cycle.js @@ -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, + function findIndexOfDay(day, daysSortedByDate) { + return daysSortedByDate.findIndex(d => d.date === day.date) + } + + 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 ) - const nextLocalDate = LocalDate.parse(nextMensesStart.date) - cycleLength = startLocalDate.until(nextLocalDate, DAYS) - } else { - cycle = cycleDaysSortedByDate.slice(0, cycleStartIndex + 1) - cycleLength = startLocalDate.until(todayAsLocalDate, DAYS) + cycleEndIndex = nextCycleIndex + 1 + cycleEndDate = nextCycleStart.date } - 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) { 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, } } diff --git a/test/get-cycle-by-start-day.spec.js b/test/get-cycle-by-start-day.spec.js new file mode 100644 index 0000000000000000000000000000000000000000..6a3adbe472c219dbda57f99a75fa1379bcd421e9 --- /dev/null +++ b/test/get-cycle-by-start-day.spec.js @@ -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) + }) +})