From 1f2801a83b265fb5a75b51abf4910fd87ab3d633 Mon Sep 17 00:00:00 2001 From: Julia Friesel Date: Fri, 21 Dec 2018 15:08:32 +0100 Subject: [PATCH] Make getCycleLenghts filter for max cycle length --- lib/cycle.js | 22 +++++++++------------- test/cycle.spec.js | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/lib/cycle.js b/lib/cycle.js index b1fcf72..7236d85 100644 --- a/lib/cycle.js +++ b/lib/cycle.js @@ -1,5 +1,5 @@ import * as joda from 'js-joda' -import {getCycleLengthStats} from './cycle-length' +import { getCycleLengthStats } from './cycle-length' const LocalDate = joda.LocalDate const DAYS = joda.ChronoUnit.DAYS @@ -142,24 +142,19 @@ export default function config(opts) { function getAllCycleLengths() { return cycleStartsSortedByDate .map(day => LocalDate.parse(day.date)) - .reduce((lengths, cycleStart, i, startsAsLocalDates) => { - if (i === startsAsLocalDates.length - 1) return lengths + .map((cycleStart, i, startsAsLocalDates) => { + if (i === cycleStartsSortedByDate.length - 1) return null const prevCycleStart = startsAsLocalDates[i + 1] - const cycleLength = prevCycleStart.until(cycleStart, DAYS) - if (cycleLength <= maxCycleLength) { lengths.push(cycleLength) } - return lengths - }, []) + return prevCycleStart.until(cycleStart, DAYS) + }) + .filter(length => length && length <= maxCycleLength) } function getPredictedMenses() { - const allMensesStarts = cycleStartsSortedByDate - const atLeastOneCycle = allMensesStarts.length > 1 - if (!atLeastOneCycle || - allMensesStarts.length < minCyclesForPrediction - ) { + const cycleLengths = getAllCycleLengths() + if (cycleLengths.length < minCyclesForPrediction) { return [] } - const cycleLengths = getAllCycleLengths() const cycleInfo = getCycleLengthStats(cycleLengths) const periodDistance = Math.round(cycleInfo.mean) let periodStartVariation @@ -173,6 +168,7 @@ export default function config(opts) { if (periodDistance - 5 < periodStartVariation) { // otherwise predictions overlap return [] } + const allMensesStarts = cycleStartsSortedByDate let lastStart = LocalDate.parse(allMensesStarts[0].date) const predictedMenses = [] for (let i = 0; i < 3; i++) { diff --git a/test/cycle.spec.js b/test/cycle.spec.js index 90ccaf8..ec4fccc 100644 --- a/test/cycle.spec.js +++ b/test/cycle.spec.js @@ -709,6 +709,47 @@ describe('getPredictedMenses', () => { const result = getPredictedMenses() 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', () => { it('for one completed cycle with minCyclesForPrediction = 1', () => {