Get cycle day for day in any cycle, not just the current

This commit is contained in:
Julia Friesel
2018-06-07 11:14:07 +02:00
parent 1cf4087039
commit 3bc85d74fc
2 changed files with 34 additions and 1 deletions
+5 -1
View File
@@ -10,7 +10,11 @@ export default function config(opts) {
return function getCycleDayNumber(unsorted, targetDate) {
// sort the cycle days in descending order so we travel into
// the past as we iterate over the array
const cycleDays = [...unsorted].sort((a, b) => b.date.isAfter(a.date))
// also, to retrieve the number, we only need the cycle days before the target day
// TODO we can probably rely on the db to do the sorting for us
const sorted = [...unsorted].sort((a, b) => b.date.isAfter(a.date))
const firstDayBeforeTargetDayIndex = sorted.findIndex(day => day.date.isBefore(targetDate))
const cycleDays = sorted.slice(firstDayBeforeTargetDayIndex)
const lastPeriodStart = cycleDays.find((day, i) => {
if (