diff --git a/lib/cycle.js b/lib/cycle.js index 8de6723..b1fcf72 100644 --- a/lib/cycle.js +++ b/lib/cycle.js @@ -62,6 +62,8 @@ export default function config(opts) { return cycleStartsSortedByDate .slice(startFromHere) .map(getCycleForCycleStartDay) + // filter the ones exceeding macCycleLength + .filter(cycle => cycle) } function getCycleForCycleStartDay(startDay) { diff --git a/test/cycle.spec.js b/test/cycle.spec.js index 8c746b0..90ccaf8 100644 --- a/test/cycle.spec.js +++ b/test/cycle.spec.js @@ -265,6 +265,64 @@ describe('getPreviousCycle', () => { const result = getPreviousCycle('2018-04-18') expect(result).to.eql(null) }) + + it('returns null when the previous cycle > maxcyclelength', () => { + const cycleDaysSortedByDate = [ + { + date: '2018-07-05', + bleeding: { value: 2 } + }, + { + date: '2018-06-05', + bleeding: { value: 2 } + }, + { + date: '2018-05-05', + mucus: { value: 2 } + }, + { + date: '2018-05-04', + bleeding: { value: 2 } + }, + { + date: '2018-05-03', + bleeding: { value: 2 } + }, + { + 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 } + }, + ] + + const cycleStarts = [ + '2018-07-05', + '2018-06-05', + '2018-05-03', + '2018-04-02' + ] + + const { getPreviousCycle } = cycleModule({ + cycleDaysSortedByDate, + cycleStartsSortedByDate: cycleDaysSortedByDate.filter(d => { + return cycleStarts.includes(d.date) + }), + maxCycleLength: 2 + }) + const result = getPreviousCycle('2018-06-08') + expect(result).to.eql(null) + }) }) describe('getCyclesBefore', () => { @@ -362,6 +420,72 @@ describe('getCyclesBefore', () => { ] ]) }) + + it('skips cycle that are longer than max', () => { + const cycleDaysSortedByDate = [ + { + date: '2018-07-05', + bleeding: { value: 2 } + }, + { + date: '2018-06-05', + bleeding: { value: 2 } + }, + { + date: '2018-05-05', + mucus: { value: 2 } + }, + { + date: '2018-05-04', + bleeding: { value: 2 } + }, + { + date: '2018-05-03', + bleeding: { value: 2 } + }, + { + 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 } + }, + ] + + const cycleStarts = [ + '2018-07-05', + '2018-06-05', + '2018-05-03', + '2018-04-02' + ] + + const { getCyclesBefore } = cycleModule({ + cycleDaysSortedByDate, + cycleStartsSortedByDate: cycleDaysSortedByDate.filter(d => { + return cycleStarts.includes(d.date) + }), + maxCycleLength: 2 + }) + const result = getCyclesBefore(cycleDaysSortedByDate[0]) + expect(result.length).to.eql(1) + expect(result).to.eql([ + [ + { + date: '2018-06-05', + bleeding: { value: 2 } + } + ] + ]) + }) }) describe('getCycleForDay', () => {