diff --git a/lib/cycle.js b/lib/cycle.js index 5a01ae8..0f47690 100644 --- a/lib/cycle.js +++ b/lib/cycle.js @@ -89,6 +89,8 @@ export default function config(opts) { if (noBleedingDayWithinThresholdBefore(cycleDay)) return true return false + // checks that there are no relevant bleeding days before + // the input cycleDay (returns boolean) function noBleedingDayWithinThresholdBefore(cycleDay) { const localDate = LocalDate.parse(cycleDay.date) const threshold = localDate.minusDays(maxBreakInBleeding + 1).toString() @@ -101,6 +103,9 @@ export default function config(opts) { } } + // returns all bleeding days that belong to one menses directly following + // the cycle day. used to set or clear new cycle starts when the target day + // changes function getMensesDaysRightAfter(cycleDay) { const bleedingDays = bleedingDaysSortedByDate .filter(d => !d.bleeding.exclude) @@ -110,6 +115,9 @@ export default function config(opts) { }) return recurse(cycleDay, firstFollowingBleedingDayIndex, []) + // we look at the current bleeding day as well as the next, and decide + // whether they belong to one menses. if they do, we collect them, once + // they don't, we're done function recurse(day, nextIndex, mensesDays) { const next = bleedingDays[nextIndex] if (!next) return mensesDays @@ -118,10 +126,11 @@ export default function config(opts) { return recurse(next, nextIndex + 1, mensesDays) } - function isWithinThreshold(cycleDay, nextCycleDay) { - const localDate = LocalDate.parse(cycleDay.date) + // checks whether the two days belong to one menses episode + function isWithinThreshold(bleedingDay, nextBleedingDay) { + const localDate = LocalDate.parse(bleedingDay.date) const threshold = localDate.plusDays(maxBreakInBleeding + 1).toString() - return nextCycleDay.date <= threshold + return nextBleedingDay.date <= threshold } } diff --git a/test/cycle.spec.js b/test/cycle.spec.js index af07e6d..77bf379 100644 --- a/test/cycle.spec.js +++ b/test/cycle.spec.js @@ -146,7 +146,7 @@ describe('getPreviousCycle', () => { ]) }) - it('returns null when target days is not in a cyle', () => { + it('returns null when target day is not in a cyle', () => { const cycleDaysSortedByDate = [ { date: '2018-07-05', @@ -860,7 +860,7 @@ describe('isMensesStart', () => { describe('with cycle thresholds', () => { const maxBreakInBleeding = 3 - it('disregards bleeding breaks equal to max allowed bleeding break in a bleeding period', () => { + it('disregards bleeding breaks equal to maxAllowedBleedingBreak in a bleeding period', () => { const bleedingDays = [{ date: '2018-05-14', bleeding: { @@ -1121,7 +1121,7 @@ describe('getMensesDaysRightAfter', () => { describe('with cycle thresholds', () => { const maxBreakInBleeding = 3 - it('disregards bleeding breaks shorter than max allowed bleeding break in a bleeding period', () => { + it('disregards bleeding breaks shorter than maxAllowedBleedingBreak in a bleeding period', () => { const bleedingDays = [{ date: '2018-05-14', bleeding: {