Accept previousCycles as array

This commit is contained in:
Julia Friesel
2018-07-11 14:14:42 +02:00
parent 9e645ddd8e
commit 19e880183e
3 changed files with 55 additions and 32 deletions
+11 -6
View File
@@ -4,18 +4,20 @@ import getPreOvulatoryPhase from './pre-ovulatory'
import { LocalDate } from 'js-joda'
import assert from 'assert'
export default function ({ cycle, previousCycle }) {
throwIfArgsAreNotInRequiredFormat(cycle, previousCycle)
export default function ({ cycle, previousCycles = [] }) {
throwIfArgsAreNotInRequiredFormat(cycle, previousCycles)
const status = {
assumeFertility: true,
phases: {}
}
// TODO handle no previous cycles
// if there was no first higher measurement in the previous cycle,
// no infertile pre-ovulatory phase may be assumed
if (getTemperatureShift(previousCycle).detected && !cycle[0].mucus) {
status.phases.preOvulatory = getPreOvulatoryPhase(cycle)
const lastCycle = previousCycles[previousCycles.length - 1]
if (getTemperatureShift(lastCycle).detected && !cycle[0].mucus) {
status.phases.preOvulatory = getPreOvulatoryPhase(cycle, previousCycles)
if (status.phases.preOvulatory.cycleDays.length === cycle.length) {
status.assumeFertility = false
return status
@@ -70,9 +72,10 @@ export default function ({ cycle, previousCycle }) {
return status
}
function throwIfArgsAreNotInRequiredFormat(cycle, previousCycle) {
[cycle, previousCycle].forEach(cycle => {
function throwIfArgsAreNotInRequiredFormat(cycle, previousCycles) {
[cycle, ...previousCycles].forEach(cycle => {
assert.ok(Array.isArray(cycle))
// TODO handle case of no previous cycles
assert.ok(cycle.length > 0)
assert.equal(typeof cycle[0].bleeding, 'object')
assert.equal(typeof cycle[0].bleeding.value, 'number')
@@ -81,6 +84,8 @@ function throwIfArgsAreNotInRequiredFormat(cycle, previousCycle) {
assert.doesNotThrow(() => LocalDate.parse(day.date))
if (day.temperature) assert.equal(typeof day.temperature.value, 'number')
if (day.mucus) assert.equal(typeof day.mucus.value, 'number')
if (day.mucus) assert.ok(day.mucus.value >= 0)
if (day.mucus) assert.ok(day.mucus.value < 5)
})
})
}
+16 -7
View File
@@ -1,13 +1,20 @@
import { LocalDate } from "js-joda"
export default function(cycle) {
export default function(cycle, previousCycles) {
// TODO handle no previous cycles
let preOvuPhaseLength = 5
//TODO make sure it handles weird cases like fhm < 9
const minus8DayRuleResult = apply8DayRule(previousCycles)
if (minus8DayRuleResult) preOvuPhaseLength = minus8DayRuleResult
const startDate = LocalDate.parse(cycle[0].date)
const fiveDayEndDate = startDate.plusDays(4).toString()
const fiveDayRuleDays = cycle.slice(0, 5).filter(d => d.date <= fiveDayEndDate)
const preOvulatoryDays = getDaysUntilFertileMucus(fiveDayRuleDays)
const preOvuPhaseEndDate = startDate.plusDays(preOvuPhaseLength - 1).toString()
const maybePreOvuDays = cycle.slice(0, 5).filter(d => d.date <= preOvuPhaseEndDate)
const preOvulatoryDays = getDaysUntilFertileMucus(maybePreOvuDays)
let endDate
if (preOvulatoryDays.length === fiveDayRuleDays.length) {
endDate = fiveDayEndDate
if (preOvulatoryDays.length === maybePreOvuDays.length) {
endDate = preOvuPhaseEndDate
} else {
endDate = preOvulatoryDays[preOvulatoryDays.length - 1].date
}
@@ -29,4 +36,6 @@ function getDaysUntilFertileMucus(days) {
return days.slice(0, firstFertileMucusDayIndex)
}
return days
}
}
function apply8DayRule() {}