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)
})
})
}