Don't do duplicate work in getLastMensesStart

This commit is contained in:
Julia Friesel
2018-10-14 13:08:56 +02:00
parent d01721daed
commit 1aeaac8504
2 changed files with 49 additions and 64 deletions
+40 -57
View File
@@ -26,70 +26,49 @@ export default function config(opts) {
minCyclesForPrediction = opts.minCyclesForPrediction || 3 minCyclesForPrediction = opts.minCyclesForPrediction || 3
} }
function getLastMensesStart(targetDateString) { function findLatestMensesStart(bleedingDays) {
const targetDate = LocalDate.parse(targetDateString) if (!bleedingDays.length) return null
const withWrappedDates = bleedingDaysSortedByDate
.filter(day => !day.bleeding.exclude)
.map(day => {
day.wrappedDate = LocalDate.parse(day.date)
return day
})
// the index of the first bleeding day before the target day // assumes bleeding days are ordered latest first, and
const index = withWrappedDates.findIndex(day => { // excluded values already removed
return ( const lastMensesStart = bleedingDays.find((day, i) => {
day.wrappedDate.isEqual(targetDate) || return noBleedingDayWithinThreshold(day, bleedingDays.slice(i + 1))
day.wrappedDate.isBefore(targetDate)
)
})
if (index < 0) {
withWrappedDates.forEach(day => delete day.wrappedDate)
return null
}
const prevBleedingDays = withWrappedDates.slice(index)
const lastMensesStart = prevBleedingDays.find((day, i) => {
return noBleedingDayWithinThreshold(day, prevBleedingDays.slice(i + 1))
}) })
function noBleedingDayWithinThreshold(day, previousBleedingDays) { function noBleedingDayWithinThreshold(day, previousBleedingDays) {
const periodThreshold = day.wrappedDate.minusDays(maxBreakInBleeding + 1) const localDate = LocalDate.parse(day.date)
return !previousBleedingDays.some(({ wrappedDate }) => { const threshold = localDate.minusDays(maxBreakInBleeding + 1).toString()
return ( return !previousBleedingDays.some(({ date }) => date >= threshold)
wrappedDate.equals(periodThreshold) ||
wrappedDate.isAfter(periodThreshold)
)
})
} }
withWrappedDates.forEach(day => delete day.wrappedDate)
return lastMensesStart return lastMensesStart
} }
function getFollowingMensesStart(targetDateString) { function getLastMensesStartForDay(targetDateString) {
const targetDate = LocalDate.parse(targetDateString) // the index of the first bleeding day before the target day
const withWrappedDates = bleedingDaysSortedByDate const index = bleedingDaysSortedByDate.findIndex(day => {
return day.date <= targetDateString && !day.bleeding.exclude
})
if (index < 0) return null
const prevBleedingDays = bleedingDaysSortedByDate.slice(index)
return findLatestMensesStart(prevBleedingDays)
}
function getFollowingMensesStartForDay(targetDateString) {
const followingBleedingDays = bleedingDaysSortedByDate
.filter(day => !day.bleeding.exclude) .filter(day => !day.bleeding.exclude)
.map(day => {
day.wrappedDate = LocalDate.parse(day.date)
return day
})
const firstBleedingDayAfterTargetDay = withWrappedDates
.reverse() .reverse()
.find(day => {
return day.wrappedDate.isAfter(targetDate)
})
withWrappedDates.forEach(day => delete day.wrappedDate) const firstBleedingDayAfterTargetDay = followingBleedingDays
.find(day => day.date > targetDateString)
return firstBleedingDayAfterTargetDay return firstBleedingDayAfterTargetDay
} }
function getCycleDayNumber(targetDateString) { function getCycleDayNumber(targetDateString) {
const lastMensesStart = getLastMensesStart(targetDateString) const lastMensesStart = getLastMensesStartForDay(targetDateString)
if (!lastMensesStart) return null if (!lastMensesStart) return null
const targetDate = LocalDate.parse(targetDateString) const targetDate = LocalDate.parse(targetDateString)
const lastMensesLocalDate = LocalDate.parse(lastMensesStart.date) const lastMensesLocalDate = LocalDate.parse(lastMensesStart.date)
@@ -111,7 +90,7 @@ export default function config(opts) {
} }
function getPreviousCycle(dateString) { function getPreviousCycle(dateString) {
const startOfCycle = getLastMensesStart(dateString) const startOfCycle = getLastMensesStartForDay(dateString)
if (!startOfCycle) return null if (!startOfCycle) return null
const dateBeforeStartOfCycle = LocalDate const dateBeforeStartOfCycle = LocalDate
.parse(startOfCycle.date) .parse(startOfCycle.date)
@@ -123,10 +102,10 @@ export default function config(opts) {
function getCycleForDay(dayOrDate) { function getCycleForDay(dayOrDate) {
const dateString = typeof dayOrDate === 'string' ? dayOrDate : dayOrDate.date const dateString = typeof dayOrDate === 'string' ? dayOrDate : dayOrDate.date
const cycleStart = getLastMensesStart(dateString) const cycleStart = getLastMensesStartForDay(dateString)
if (!cycleStart) return null if (!cycleStart) return null
const cycleStartIndex = cycleDaysSortedByDate.indexOf(cycleStart) const cycleStartIndex = cycleDaysSortedByDate.indexOf(cycleStart)
const nextMensesStart = getFollowingMensesStart(dateString) const nextMensesStart = getFollowingMensesStartForDay(dateString)
if (nextMensesStart) { if (nextMensesStart) {
return cycleDaysSortedByDate.slice( return cycleDaysSortedByDate.slice(
cycleDaysSortedByDate.indexOf(nextMensesStart) + 1, cycleDaysSortedByDate.indexOf(nextMensesStart) + 1,
@@ -137,16 +116,20 @@ export default function config(opts) {
} }
} }
function getAllMensesStarts(day, collectedDates) { function getAllMensesStarts(initialBleedingDays = bleedingDaysSortedByDate) {
day = day || LocalDate.now().toString() return recurse(initialBleedingDays.filter(d => !d.bleeding.exclude))
function recurse(bleedingDays, collectedDates) {
collectedDates = collectedDates || [] collectedDates = collectedDates || []
const lastStart = getLastMensesStart(day) const lastStart = findLatestMensesStart(bleedingDays)
if (!lastStart) { if (!lastStart) {
return collectedDates return collectedDates
} else { } else {
const newDay = LocalDate.parse(lastStart.date).minusDays(1).toString()
collectedDates.push(lastStart.date) collectedDates.push(lastStart.date)
return getAllMensesStarts(newDay, collectedDates) const index = bleedingDays.indexOf(lastStart)
const remainingDays = bleedingDays.slice(index + 1)
return recurse(remainingDays, collectedDates)
}
} }
} }
@@ -189,8 +172,8 @@ export default function config(opts) {
lastStart = lastStart.plusDays(periodDistance) lastStart = lastStart.plusDays(periodDistance)
const nextPredictedDates = [lastStart.toString()] const nextPredictedDates = [lastStart.toString()]
for (let j = 0; j < periodStartVariation; j++) { for (let j = 0; j < periodStartVariation; j++) {
nextPredictedDates.push(lastStart.minusDays(j+1).toString()) nextPredictedDates.push(lastStart.minusDays(j + 1).toString())
nextPredictedDates.push(lastStart.plusDays(j+1).toString()) nextPredictedDates.push(lastStart.plusDays(j + 1).toString())
} }
nextPredictedDates.sort() nextPredictedDates.sort()
predictedMenses.push(nextPredictedDates) predictedMenses.push(nextPredictedDates)
+4 -2
View File
@@ -625,7 +625,8 @@ describe('getAllMensesStart', () => {
const result = getAllMensesStarts() const result = getAllMensesStarts()
expect(result.length).to.eql(2) expect(result.length).to.eql(2)
expect(result).to.eql(['2018-06-01', '2018-05-01']) expect(result).to.eql(['2018-06-01', '2018-05-01'])
}), })
it('works for two cycle starts with excluded data', () => { it('works for two cycle starts with excluded data', () => {
const cycleDaysSortedByDate = [ const cycleDaysSortedByDate = [
{ {
@@ -649,7 +650,8 @@ describe('getAllMensesStart', () => {
const result = getAllMensesStarts() const result = getAllMensesStarts()
expect(result.length).to.eql(2) expect(result.length).to.eql(2)
expect(result).to.eql(['2018-06-01', '2018-05-01']) expect(result).to.eql(['2018-06-01', '2018-05-01'])
}), })
it('returns an empty array if no bleeding days are given', () => { it('returns an empty array if no bleeding days are given', () => {
const cycleDaysSortedByDate = [ {} ] const cycleDaysSortedByDate = [ {} ]