Throw if args are wrong

This commit is contained in:
Julia Friesel
2018-07-10 16:37:38 +02:00
parent 81b7b46b23
commit d027c6e9c4
3 changed files with 119 additions and 39 deletions
+24 -1
View File
@@ -4,7 +4,8 @@ import getPreOvulatoryPhase from './pre-ovulatory'
import { LocalDate } from 'js-joda'
export default function ({ cycle, previousCycle }) {
// TODO check for basic stuff, throw if nonexistent
throwIfArgsAreNotInRequiredFormat(cycle, previousCycle)
const status = {
assumeFertility: true,
phases: {}
@@ -66,4 +67,26 @@ export default function ({ cycle, previousCycle }) {
status.assumeFertility = false
return status
}
function throwIfArgsAreNotInRequiredFormat(cycle, previousCycle) {
if (!Array.isArray(cycle) || !cycle.length) throw new Error('Please provide all cycle days as array')
if (!Array.isArray(previousCycle) || !previousCycle.length) throw new Error('Please provide previous cycle days as array')
if (
!cycle[0].bleeding || typeof cycle[0].bleeding.value != 'number' ||
!previousCycle[0].bleeding || typeof previousCycle[0].bleeding.value != 'number'
) throw new Error('Cycle must start with bleeding')
if ([cycle, previousCycle].some(cycle => {
return cycle.some(day => {
if (!day.date) return true
try {
LocalDate.parse(day.date)
} catch(err) {
throw new Error('Please provide dates in ISO8601 format')
}
if (day.temperature && typeof day.temperature.value != 'number') return true
if (day.mucus && typeof day.mucus.value != 'number') return true
})
})) throw new Error('Cycle days are not in correct format')
}