Accept previousCycles as array
This commit is contained in:
+11
-6
@@ -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)
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user