Skip to content
Snippets Groups Projects
Commit 3b2beabb authored by Julia Friesel's avatar Julia Friesel
Browse files

Get cycle day number for several happy paths

parent 6d37d451
No related branches found
No related tags found
No related merge requests found
export default function (cycleDays, targetDay) {
const lastFirstBleedingday = findLastFirstBleedingDay(cycleDays)
if (!lastFirstBleedingday) return
const diffInDays = targetDay.diff(lastFirstBleedingday.date, 'days')
// cycle starts at day 1
return diffInDays + 1
}
function findLastFirstBleedingDay(cycleDays) {
// sort the cycle days in descending order
// so we travel into the past as we iterate
// over the array
cycleDays.sort((a,b) => b.date - a.date)
let sawBleeding = false
for (let i = 0; i < cycleDays.length; i++) {
const cycleDay = cycleDays[i]
// we have detected the day before the beginning
// of a bleeding period, so the previous cycle day in the array
// was the first day of bleeding
if (sawBleeding && (!isBleedingDay(cycleDay))) {
return cycleDays[i - 1]
}
// if we get to the earliest cycle day and there was
// bleeding on that day, it's the first bleeding day for us
if (i === cycleDays.length - 1 && isBleedingDay(cycleDay)) {
return cycleDay
}
if (isBleedingDay(cycleDay)) {
sawBleeding = true
}
}
}
function isBleedingDay(cycleDay) {
return cycleDay.bleeding
&& cycleDay.bleeding.value
&& !cycleDay.exclude
}
\ No newline at end of file
import { expect } from 'chai'
it('says hello world', function () {
expect(1 + 5).to.eql(6)
})
import { expect } from 'chai'
import moment from 'moment'
import getCycleDayNumber from '../get-cycle-day-number'
describe('getCycleDay returns the cycle day', () => {
it('if the last data entered is a bleeding day', function () {
const cycleDays = [{
date: moment([2018, 5, 2])
}, {
date: moment([2018, 5, 3]),
bleeding: {
value: 2
}
}, {
date: moment([2018, 5, 4])
}, {
date: moment([2018, 5, 9]),
bleeding: {
value: 2
}
}, {
date: moment([2018, 5, 10]),
bleeding: {
value: 2
}
}]
const targetDate = moment([2018, 5, 17])
const result = getCycleDayNumber(cycleDays, targetDate)
expect(result).to.eql(9)
})
it('if the last data entered is a non-bleeding day', function () {
const cycleDays = [{
date: moment([2018, 5, 2])
}, {
date: moment([2018, 5, 3]),
bleeding: {
value: 2
}
}, {
date: moment([2018, 5, 4])
}, {
date: moment([2018, 5, 9]),
bleeding: {
value: 2
}
}, {
date: moment([2018, 5, 10]),
bleeding: {
value: 2
}
}, {
date: moment([2018, 5, 13])
}, {
date: moment([2018, 5, 14])
}]
const targetDate = moment([2018, 5, 17])
const result = getCycleDayNumber(cycleDays, targetDate)
expect(result).to.eql(9)
})
it('works if the cycle days are not sorted by date', function () {
const cycleDays = [{
date: moment([2018, 5, 13]),
bleeding: {
value: 2
}
}, {
date: moment([2018, 5, 9]),
bleeding: {
value: 2
}
}, {
date: moment([2018, 5, 3]),
bleeding: {
value: 2
}
}, {
date: moment([2018, 5, 4])
}, {
date: moment([2018, 5, 10]),
bleeding: {
value: 2
}
}, {
date: moment([2018, 5, 2])
}]
const targetDate = moment([2018, 5, 17])
const result = getCycleDayNumber(cycleDays, targetDate)
expect(result).to.eql(9)
})
it('if there are only bleeding days', function () {
const cycleDays = [{
date: moment([2018, 5, 9]),
bleeding: {
value: 2
}
}, {
date: moment([2018, 5, 10]),
bleeding: {
value: 2
}
}]
const targetDate = moment([2018, 5, 17])
const result = getCycleDayNumber(cycleDays, targetDate)
expect(result).to.eql(9)
})
})
describe('getCycleDay returns undefined', () => {
it('if there are no bleeding days', function () {
const cycleDays = [{
date: moment([2018, 5, 2])
}, {
date: moment([2018, 5, 4])
}, {
date: moment([2018, 5, 9]),
}, {
date: moment([2018, 5, 10]),
}]
const targetDate = moment([2018, 5, 17])
const result = getCycleDayNumber(cycleDays, targetDate)
expect(result).to.be.undefined
})
it('if there are no cycle days', function () {
const cycleDays = []
const targetDate = moment([2018, 5, 17])
const result = getCycleDayNumber(cycleDays, targetDate)
expect(result).to.be.undefined
})
})
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment