Make getCycleLenghts filter for max cycle length
This commit is contained in:
+9
-13
@@ -1,5 +1,5 @@
|
|||||||
import * as joda from 'js-joda'
|
import * as joda from 'js-joda'
|
||||||
import {getCycleLengthStats} from './cycle-length'
|
import { getCycleLengthStats } from './cycle-length'
|
||||||
const LocalDate = joda.LocalDate
|
const LocalDate = joda.LocalDate
|
||||||
const DAYS = joda.ChronoUnit.DAYS
|
const DAYS = joda.ChronoUnit.DAYS
|
||||||
|
|
||||||
@@ -142,24 +142,19 @@ export default function config(opts) {
|
|||||||
function getAllCycleLengths() {
|
function getAllCycleLengths() {
|
||||||
return cycleStartsSortedByDate
|
return cycleStartsSortedByDate
|
||||||
.map(day => LocalDate.parse(day.date))
|
.map(day => LocalDate.parse(day.date))
|
||||||
.reduce((lengths, cycleStart, i, startsAsLocalDates) => {
|
.map((cycleStart, i, startsAsLocalDates) => {
|
||||||
if (i === startsAsLocalDates.length - 1) return lengths
|
if (i === cycleStartsSortedByDate.length - 1) return null
|
||||||
const prevCycleStart = startsAsLocalDates[i + 1]
|
const prevCycleStart = startsAsLocalDates[i + 1]
|
||||||
const cycleLength = prevCycleStart.until(cycleStart, DAYS)
|
return prevCycleStart.until(cycleStart, DAYS)
|
||||||
if (cycleLength <= maxCycleLength) { lengths.push(cycleLength) }
|
})
|
||||||
return lengths
|
.filter(length => length && length <= maxCycleLength)
|
||||||
}, [])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPredictedMenses() {
|
function getPredictedMenses() {
|
||||||
const allMensesStarts = cycleStartsSortedByDate
|
const cycleLengths = getAllCycleLengths()
|
||||||
const atLeastOneCycle = allMensesStarts.length > 1
|
if (cycleLengths.length < minCyclesForPrediction) {
|
||||||
if (!atLeastOneCycle ||
|
|
||||||
allMensesStarts.length < minCyclesForPrediction
|
|
||||||
) {
|
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
const cycleLengths = getAllCycleLengths()
|
|
||||||
const cycleInfo = getCycleLengthStats(cycleLengths)
|
const cycleInfo = getCycleLengthStats(cycleLengths)
|
||||||
const periodDistance = Math.round(cycleInfo.mean)
|
const periodDistance = Math.round(cycleInfo.mean)
|
||||||
let periodStartVariation
|
let periodStartVariation
|
||||||
@@ -173,6 +168,7 @@ export default function config(opts) {
|
|||||||
if (periodDistance - 5 < periodStartVariation) { // otherwise predictions overlap
|
if (periodDistance - 5 < periodStartVariation) { // otherwise predictions overlap
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
const allMensesStarts = cycleStartsSortedByDate
|
||||||
let lastStart = LocalDate.parse(allMensesStarts[0].date)
|
let lastStart = LocalDate.parse(allMensesStarts[0].date)
|
||||||
const predictedMenses = []
|
const predictedMenses = []
|
||||||
for (let i = 0; i < 3; i++) {
|
for (let i = 0; i < 3; i++) {
|
||||||
|
|||||||
@@ -709,6 +709,47 @@ describe('getPredictedMenses', () => {
|
|||||||
const result = getPredictedMenses()
|
const result = getPredictedMenses()
|
||||||
expect(result).to.eql([])
|
expect(result).to.eql([])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('if number of cycles is below minCyclesForPrediction because one of them is too long', () => {
|
||||||
|
const cycleDaysSortedByDate = [
|
||||||
|
{
|
||||||
|
date: '2018-06-02',
|
||||||
|
bleeding: { value: 2 }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: '2018-06-01',
|
||||||
|
bleeding: { value: 2 }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: '2018-05-01',
|
||||||
|
bleeding: { value: 2 }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: '2018-04-03',
|
||||||
|
bleeding: { value: 2 }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: '2018-04-02',
|
||||||
|
bleeding: { value: 2 }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: '2018-04-01',
|
||||||
|
bleeding: { value: 2 }
|
||||||
|
},
|
||||||
|
]
|
||||||
|
const cycleStarts = ['2018-06-01', '2018-05-01', '2018-04-01']
|
||||||
|
|
||||||
|
const { getPredictedMenses } = cycleModule({
|
||||||
|
cycleDaysSortedByDate,
|
||||||
|
bleedingDaysSortedByDate: cycleDaysSortedByDate.filter(d => d.bleeding),
|
||||||
|
cycleStartsSortedByDate: cycleDaysSortedByDate.filter(d => {
|
||||||
|
return cycleStarts.includes(d.date)
|
||||||
|
}),
|
||||||
|
maxCycleLength: 2
|
||||||
|
})
|
||||||
|
const result = getPredictedMenses()
|
||||||
|
expect(result).to.eql([])
|
||||||
|
})
|
||||||
})
|
})
|
||||||
describe('works', () => {
|
describe('works', () => {
|
||||||
it('for one completed cycle with minCyclesForPrediction = 1', () => {
|
it('for one completed cycle with minCyclesForPrediction = 1', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user