Add comments and rename things for clarity
This commit is contained in:
+12
-3
@@ -89,6 +89,8 @@ export default function config(opts) {
|
|||||||
if (noBleedingDayWithinThresholdBefore(cycleDay)) return true
|
if (noBleedingDayWithinThresholdBefore(cycleDay)) return true
|
||||||
return false
|
return false
|
||||||
|
|
||||||
|
// checks that there are no relevant bleeding days before
|
||||||
|
// the input cycleDay (returns boolean)
|
||||||
function noBleedingDayWithinThresholdBefore(cycleDay) {
|
function noBleedingDayWithinThresholdBefore(cycleDay) {
|
||||||
const localDate = LocalDate.parse(cycleDay.date)
|
const localDate = LocalDate.parse(cycleDay.date)
|
||||||
const threshold = localDate.minusDays(maxBreakInBleeding + 1).toString()
|
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) {
|
function getMensesDaysRightAfter(cycleDay) {
|
||||||
const bleedingDays = bleedingDaysSortedByDate
|
const bleedingDays = bleedingDaysSortedByDate
|
||||||
.filter(d => !d.bleeding.exclude)
|
.filter(d => !d.bleeding.exclude)
|
||||||
@@ -110,6 +115,9 @@ export default function config(opts) {
|
|||||||
})
|
})
|
||||||
return recurse(cycleDay, firstFollowingBleedingDayIndex, [])
|
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) {
|
function recurse(day, nextIndex, mensesDays) {
|
||||||
const next = bleedingDays[nextIndex]
|
const next = bleedingDays[nextIndex]
|
||||||
if (!next) return mensesDays
|
if (!next) return mensesDays
|
||||||
@@ -118,10 +126,11 @@ export default function config(opts) {
|
|||||||
return recurse(next, nextIndex + 1, mensesDays)
|
return recurse(next, nextIndex + 1, mensesDays)
|
||||||
}
|
}
|
||||||
|
|
||||||
function isWithinThreshold(cycleDay, nextCycleDay) {
|
// checks whether the two days belong to one menses episode
|
||||||
const localDate = LocalDate.parse(cycleDay.date)
|
function isWithinThreshold(bleedingDay, nextBleedingDay) {
|
||||||
|
const localDate = LocalDate.parse(bleedingDay.date)
|
||||||
const threshold = localDate.plusDays(maxBreakInBleeding + 1).toString()
|
const threshold = localDate.plusDays(maxBreakInBleeding + 1).toString()
|
||||||
return nextCycleDay.date <= threshold
|
return nextBleedingDay.date <= threshold
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
-3
@@ -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 = [
|
const cycleDaysSortedByDate = [
|
||||||
{
|
{
|
||||||
date: '2018-07-05',
|
date: '2018-07-05',
|
||||||
@@ -860,7 +860,7 @@ describe('isMensesStart', () => {
|
|||||||
describe('with cycle thresholds', () => {
|
describe('with cycle thresholds', () => {
|
||||||
const maxBreakInBleeding = 3
|
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 = [{
|
const bleedingDays = [{
|
||||||
date: '2018-05-14',
|
date: '2018-05-14',
|
||||||
bleeding: {
|
bleeding: {
|
||||||
@@ -1121,7 +1121,7 @@ describe('getMensesDaysRightAfter', () => {
|
|||||||
describe('with cycle thresholds', () => {
|
describe('with cycle thresholds', () => {
|
||||||
const maxBreakInBleeding = 3
|
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 = [{
|
const bleedingDays = [{
|
||||||
date: '2018-05-14',
|
date: '2018-05-14',
|
||||||
bleeding: {
|
bleeding: {
|
||||||
|
|||||||
Reference in New Issue
Block a user