Throw if args are wrong
This commit is contained in:
+24
-1
@@ -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')
|
||||
}
|
||||
Reference in New Issue
Block a user