Add isMensesStart

This commit is contained in:
Julia Friesel
2018-10-23 16:24:17 +02:00
parent 1b20658eed
commit 2b2e24bb5c
2 changed files with 163 additions and 1 deletions
+21 -1
View File
@@ -133,6 +133,25 @@ export default function config(opts) {
}
}
function isMensesStart(cycleDay) {
if (!cycleDay.bleeding || cycleDay.bleeding.exclude) return false
const bleedingDays = bleedingDaysSortedByDate
if (noBleedingDayWithinThreshold(cycleDay)) {
return true
}
return false
function noBleedingDayWithinThreshold(day) {
const localDate = LocalDate.parse(day.date)
const threshold = localDate.minusDays(maxBreakInBleeding + 1).toString()
const index = bleedingDays.findIndex(day => day.date === cycleDay.date)
const previousBleedingDays = bleedingDays.slice(index + 1)
return !previousBleedingDays.some(day => {
return day.date >= threshold && !day.bleeding.exclude
})
}
}
function getCycleLength(cycleStartDates) {
const cycleLengths = []
for (let i = 0; i < cycleStartDates.length - 1; i++) {
@@ -188,6 +207,7 @@ export default function config(opts) {
getCyclesBefore,
getAllMensesStarts,
getCycleLength,
getPredictedMenses
getPredictedMenses,
isMensesStart
}
}
+142
View File
@@ -686,3 +686,145 @@ describe('getAllMensesStart', () => {
expect(duration).to.be.lessThan(100)
})
})
describe('isMensesStart', () => {
it('works for simple menses start', () => {
const cycleDaysSortedByDate = [
{
date: '2018-05-04',
},
{
date: '2018-05-03',
bleeding: { value: 1 }
},
{
date: '2018-05-02',
bleeding: { value: 1 }
},
{
date: '2018-05-01',
bleeding: { value: 1 }
},
{
date: '2018-04-30',
}
]
const { isMensesStart } = cycleModule({
cycleDaysSortedByDate,
bleedingDaysSortedByDate: cycleDaysSortedByDate.filter(d => d.bleeding)
})
const start = isMensesStart(cycleDaysSortedByDate[3])
expect(start).to.be.true()
expect(isMensesStart(cycleDaysSortedByDate[0])).to.be.false()
expect(isMensesStart(cycleDaysSortedByDate[1])).to.be.false()
expect(isMensesStart(cycleDaysSortedByDate[2])).to.be.false()
expect(isMensesStart(cycleDaysSortedByDate[4])).to.be.false()
})
it('works with previous excluded value', () => {
const cycleDaysSortedByDate = [
{
date: '2018-06-01',
bleeding: { value: 2 }
},
{
date: '2018-05-01',
bleeding: { value: 2 }
},
{
date: '2018-04-30',
bleeding: { value: 2 , exclude: true}
},
]
const { isMensesStart } = cycleModule({
cycleDaysSortedByDate,
bleedingDaysSortedByDate: cycleDaysSortedByDate.filter(d => d.bleeding)
})
const start = isMensesStart(cycleDaysSortedByDate[1])
expect(start).to.be.true()
const notStart = isMensesStart(cycleDaysSortedByDate[2])
expect(notStart).to.be.false()
})
it('returns false when day has no bleeding', () => {
const cycleDaysSortedByDate = [
{
date: '2018-06-01',
},
{
date: '2018-05-01',
},
{
date: '2018-04-30',
bleeding: { value: 2 , exclude: true}
},
]
const { isMensesStart } = cycleModule({
cycleDaysSortedByDate,
bleedingDaysSortedByDate: cycleDaysSortedByDate.filter(d => d.bleeding)
})
const start = isMensesStart(cycleDaysSortedByDate[0])
expect(start).to.be.false()
})
it('returns false when there is a previous bleeding day within the threshold', () => {
const cycleDaysSortedByDate = [
{
date: '2018-06-01',
},
{
date: '2018-05-01',
},
{
date: '2018-04-30',
bleeding: { value: 2 }
},
{
date: '2018-04-29'
},
{
date: '2018-04-28',
bleeding: { value: 2 }
},
]
const { isMensesStart } = cycleModule({
cycleDaysSortedByDate,
bleedingDaysSortedByDate: cycleDaysSortedByDate.filter(d => d.bleeding)
})
const start = isMensesStart(cycleDaysSortedByDate[2])
expect(start).to.be.false()
})
it('returns true when there is a previous excluded bleeding day within the threshold', () => {
const cycleDaysSortedByDate = [
{
date: '2018-06-01',
},
{
date: '2018-05-01',
},
{
date: '2018-04-30',
bleeding: { value: 2 }
},
{
date: '2018-04-29'
},
{
date: '2018-04-28',
bleeding: { value: 2 , exclude: true}
},
]
const { isMensesStart } = cycleModule({
cycleDaysSortedByDate,
bleedingDaysSortedByDate: cycleDaysSortedByDate.filter(d => d.bleeding)
})
const start = isMensesStart(cycleDaysSortedByDate[2])
expect(start).to.be.true()
})
})