moves getCycleLength to corresponding files, adds function to get next menses (not finished) with first tests

This commit is contained in:
tina
2018-08-22 18:21:57 +02:00
parent b80c91cb46
commit ea21fc92a2
5 changed files with 171 additions and 15 deletions
+32 -1
View File
@@ -1,4 +1,5 @@
import * as joda from 'js-joda'
import {getCycleLengthStats, getCycleLength} from './cycle-length'
const LocalDate = joda.LocalDate
const DAYS = joda.ChronoUnit.DAYS
@@ -143,11 +144,41 @@ export default function config(opts) {
}
}
function getPredictedMenses(maxCycleLength, minCyclesForPrediction) {
maxCycleLength = maxCycleLength || 99
minCyclesForPrediction = minCyclesForPrediction || 3
const allMensesStarts = getAllMensesStarts()
const atLeastOneCycle = allMensesStarts.length > 1
if (!atLeastOneCycle ||
allMensesStarts.length < minCyclesForPrediction ||
getCycleDayNumber(LocalDate.now().toString()) > maxCycleLength
) {
return {}
}
const cycleLengths = getCycleLength(allMensesStarts)
const cycleInfo = getCycleLengthStats(cycleLengths)
const periodDistance = Math.round(cycleInfo.mean)
const periodStartVariation = (cycleInfo.stdDeviation < 1.5) ? 1 : 2 // threshold is choosen a little arbitrarily
var lastStart = allMensesStarts[0]
const predictedMenses = []
for (let i = 0; i < 3; i++) {
lastStart = LocalDate.parse(lastStart).plusDays(periodDistance).toString()
const nextPredictedRange = {
'startDate': LocalDate.parse(lastStart).minusDays(periodStartVariation).toString(),
'endDate': LocalDate.parse(lastStart).plusDays(periodStartVariation).toString()
}
predictedMenses.push(nextPredictedRange)
}
return predictedMenses
}
return {
getCycleDayNumber,
getCycleForDay,
getPreviousCycle,
getCyclesBefore,
getAllMensesStarts
getAllMensesStarts,
getPredictedMenses
}
}