Make getCycleLenghts filter for max cycle length

This commit is contained in:
Julia Friesel
2018-12-21 15:08:32 +01:00
parent 9fd0105506
commit 1f2801a83b
2 changed files with 50 additions and 13 deletions
+9 -13
View File
@@ -1,5 +1,5 @@
import * as joda from 'js-joda'
import {getCycleLengthStats} from './cycle-length'
import { getCycleLengthStats } from './cycle-length'
const LocalDate = joda.LocalDate
const DAYS = joda.ChronoUnit.DAYS
@@ -142,24 +142,19 @@ export default function config(opts) {
function getAllCycleLengths() {
return cycleStartsSortedByDate
.map(day => LocalDate.parse(day.date))
.reduce((lengths, cycleStart, i, startsAsLocalDates) => {
if (i === startsAsLocalDates.length - 1) return lengths
.map((cycleStart, i, startsAsLocalDates) => {
if (i === cycleStartsSortedByDate.length - 1) return null
const prevCycleStart = startsAsLocalDates[i + 1]
const cycleLength = prevCycleStart.until(cycleStart, DAYS)
if (cycleLength <= maxCycleLength) { lengths.push(cycleLength) }
return lengths
}, [])
return prevCycleStart.until(cycleStart, DAYS)
})
.filter(length => length && length <= maxCycleLength)
}
function getPredictedMenses() {
const allMensesStarts = cycleStartsSortedByDate
const atLeastOneCycle = allMensesStarts.length > 1
if (!atLeastOneCycle ||
allMensesStarts.length < minCyclesForPrediction
) {
const cycleLengths = getAllCycleLengths()
if (cycleLengths.length < minCyclesForPrediction) {
return []
}
const cycleLengths = getAllCycleLengths()
const cycleInfo = getCycleLengthStats(cycleLengths)
const periodDistance = Math.round(cycleInfo.mean)
let periodStartVariation
@@ -173,6 +168,7 @@ export default function config(opts) {
if (periodDistance - 5 < periodStartVariation) { // otherwise predictions overlap
return []
}
const allMensesStarts = cycleStartsSortedByDate
let lastStart = LocalDate.parse(allMensesStarts[0].date)
const predictedMenses = []
for (let i = 0; i < 3; i++) {
+41
View File
@@ -709,6 +709,47 @@ describe('getPredictedMenses', () => {
const result = getPredictedMenses()
expect(result).to.eql([])
})
it('if number of cycles is below minCyclesForPrediction because one of them is too long', () => {
const cycleDaysSortedByDate = [
{
date: '2018-06-02',
bleeding: { value: 2 }
},
{
date: '2018-06-01',
bleeding: { value: 2 }
},
{
date: '2018-05-01',
bleeding: { value: 2 }
},
{
date: '2018-04-03',
bleeding: { value: 2 }
},
{
date: '2018-04-02',
bleeding: { value: 2 }
},
{
date: '2018-04-01',
bleeding: { value: 2 }
},
]
const cycleStarts = ['2018-06-01', '2018-05-01', '2018-04-01']
const { getPredictedMenses } = cycleModule({
cycleDaysSortedByDate,
bleedingDaysSortedByDate: cycleDaysSortedByDate.filter(d => d.bleeding),
cycleStartsSortedByDate: cycleDaysSortedByDate.filter(d => {
return cycleStarts.includes(d.date)
}),
maxCycleLength: 2
})
const result = getPredictedMenses()
expect(result).to.eql([])
})
})
describe('works', () => {
it('for one completed cycle with minCyclesForPrediction = 1', () => {