Get previous cycles before detecting fertility status
This commit is contained in:
@@ -43,10 +43,9 @@ export default class Day extends Component {
|
|||||||
Cycle day {cycleDayNumber}
|
Cycle day {cycleDayNumber}
|
||||||
</Text> }
|
</Text> }
|
||||||
|
|
||||||
{ cycleDayNumber &&
|
|
||||||
<Text style={styles.cycleDayNumber} >
|
<Text style={styles.cycleDayNumber} >
|
||||||
{fertilityStatus}
|
{fertilityStatus}
|
||||||
</Text> }
|
</Text>
|
||||||
</View >
|
</View >
|
||||||
<View style={ styles.cycleDaySymptomsView }>
|
<View style={ styles.cycleDaySymptomsView }>
|
||||||
{
|
{
|
||||||
|
|||||||
+27
-3
@@ -1,4 +1,5 @@
|
|||||||
import * as joda from 'js-joda'
|
import * as joda from 'js-joda'
|
||||||
|
|
||||||
const LocalDate = joda.LocalDate
|
const LocalDate = joda.LocalDate
|
||||||
|
|
||||||
export default function config(opts) {
|
export default function config(opts) {
|
||||||
@@ -49,14 +50,16 @@ export default function config(opts) {
|
|||||||
return !previousBleedingDays.some(({ wrappedDate }) => wrappedDate.equals(periodThreshold) || wrappedDate.isAfter(periodThreshold))
|
return !previousBleedingDays.some(({ wrappedDate }) => wrappedDate.equals(periodThreshold) || wrappedDate.isAfter(periodThreshold))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
withWrappedDates.forEach(day => delete day.wrappedDate)
|
||||||
return lastPeriodStart
|
return lastPeriodStart
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCycleDayNumber(targetDateString) {
|
function getCycleDayNumber(targetDateString) {
|
||||||
const lastMensesStart = getLastMensesStart(targetDateString)
|
const lastMensesStart = getLastMensesStart(targetDateString)
|
||||||
if (!lastMensesStart) return null
|
if (!lastMensesStart) return null
|
||||||
const targetDate = joda.LocalDate.parse(targetDateString)
|
const targetDate = LocalDate.parse(targetDateString)
|
||||||
const diffInDays = lastMensesStart.wrappedDate.until(targetDate, joda.ChronoUnit.DAYS)
|
const lastMensesLocalDate = LocalDate.parse(lastMensesStart.date)
|
||||||
|
const diffInDays = lastMensesLocalDate.until(targetDate, joda.ChronoUnit.DAYS)
|
||||||
|
|
||||||
// cycle starts at day 1
|
// cycle starts at day 1
|
||||||
return diffInDays + 1
|
return diffInDays + 1
|
||||||
@@ -73,15 +76,36 @@ export default function config(opts) {
|
|||||||
|
|
||||||
function getCycleDaysBeforeDay(targetDateString) {
|
function getCycleDaysBeforeDay(targetDateString) {
|
||||||
const firstCycleDay = getLastMensesStart(targetDateString)
|
const firstCycleDay = getLastMensesStart(targetDateString)
|
||||||
|
if (!firstCycleDay) return null
|
||||||
return cycleDaysSortedByDate.filter(({date}) => {
|
return cycleDaysSortedByDate.filter(({date}) => {
|
||||||
return date >= firstCycleDay.date && date <= targetDateString
|
return date >= firstCycleDay.date && date <= targetDateString
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getPreviousCycles(targetCycleStartDay) {
|
||||||
|
let previousCycleStartIndex = cycleDaysSortedByDate.indexOf(targetCycleStartDay)
|
||||||
|
const cycles = []
|
||||||
|
while (previousCycleStartIndex < cycleDaysSortedByDate.length - 1) {
|
||||||
|
const prevDate = cycleDaysSortedByDate[previousCycleStartIndex + 1].date
|
||||||
|
const cycleStart = getLastMensesStart(prevDate)
|
||||||
|
|
||||||
|
if (!cycleStart) break
|
||||||
|
|
||||||
|
const cycleStartIndex = cycleDaysSortedByDate.indexOf(cycleStart)
|
||||||
|
const lastDayInCycle = previousCycleStartIndex + 1
|
||||||
|
const cycle = cycleDaysSortedByDate.slice(lastDayInCycle, cycleStartIndex + 1)
|
||||||
|
cycles.push(cycle)
|
||||||
|
previousCycleStartIndex = cycleStartIndex
|
||||||
|
}
|
||||||
|
|
||||||
|
return cycles
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
getCycleDayNumber,
|
getCycleDayNumber,
|
||||||
getLastMensesStart,
|
getLastMensesStart,
|
||||||
getPreviousTemperaturesInCycle,
|
getPreviousTemperaturesInCycle,
|
||||||
getCycleDaysBeforeDay
|
getCycleDaysBeforeDay,
|
||||||
|
getPreviousCycles
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-6
@@ -1,18 +1,22 @@
|
|||||||
import getFertilityStatus from './sympto'
|
import getFertilityStatus from './sympto'
|
||||||
import cycleModule from './cycle'
|
import cycleModule from './cycle'
|
||||||
|
|
||||||
const { getCycleDaysBeforeDay } = cycleModule()
|
const { getCycleDaysBeforeDay, getPreviousCycles } = cycleModule()
|
||||||
|
|
||||||
export default function (dateString) {
|
export default function (dateString) {
|
||||||
|
const cycle = getCycleDaysBeforeDay(dateString)
|
||||||
|
if (!cycle) return `We cannot show any cycle information because no menses has been entered`
|
||||||
|
|
||||||
// we get earliest last, but sympto wants earliest first
|
// we get earliest last, but sympto wants earliest first
|
||||||
const cycle = getCycleDaysBeforeDay(dateString).reverse()
|
cycle.reverse()
|
||||||
// const previousCycles = getPreviousCycles()
|
const previousCycles = getPreviousCycles(cycle[0])
|
||||||
const status = getFertilityStatus({cycle})
|
previousCycles.forEach(cycle => cycle.reverse())
|
||||||
|
|
||||||
|
const status = getFertilityStatus({cycle, previousCycles})
|
||||||
|
|
||||||
return formatStatusForApp(status)
|
return formatStatusForApp(status)
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatStatusForApp(status) {
|
function formatStatusForApp(status) {
|
||||||
const fertileStatus = status.assumeFertility ? 'fertile' : 'infertile'
|
return status.assumeFertility ? 'fertile' : 'infertile'
|
||||||
return `You are currently ${fertileStatus}`
|
|
||||||
}
|
}
|
||||||
+137
-49
@@ -23,7 +23,7 @@ describe('getCycleDay', () => {
|
|||||||
value: 2
|
value: 2
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
const getCycleDayNumber = cycleModule({bleedingDaysSortedByDate: bleedingDays}).getCycleDayNumber
|
const getCycleDayNumber = cycleModule({ bleedingDaysSortedByDate: bleedingDays }).getCycleDayNumber
|
||||||
const targetDate = '2018-05-17'
|
const targetDate = '2018-05-17'
|
||||||
const result = getCycleDayNumber(targetDate)
|
const result = getCycleDayNumber(targetDate)
|
||||||
expect(result).to.eql(9)
|
expect(result).to.eql(9)
|
||||||
@@ -49,7 +49,7 @@ describe('getCycleDay', () => {
|
|||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
const targetDate = '2018-05-17'
|
const targetDate = '2018-05-17'
|
||||||
const getCycleDayNumber = cycleModule({bleedingDaysSortedByDate: bleedingDays}).getCycleDayNumber
|
const getCycleDayNumber = cycleModule({ bleedingDaysSortedByDate: bleedingDays }).getCycleDayNumber
|
||||||
const result = getCycleDayNumber(targetDate)
|
const result = getCycleDayNumber(targetDate)
|
||||||
expect(result).to.eql(15)
|
expect(result).to.eql(15)
|
||||||
})
|
})
|
||||||
@@ -73,7 +73,7 @@ describe('getCycleDay', () => {
|
|||||||
}]
|
}]
|
||||||
|
|
||||||
const targetDate = '2018-04-27'
|
const targetDate = '2018-04-27'
|
||||||
const getCycleDayNumber = cycleModule({bleedingDaysSortedByDate: bleedingDays}).getCycleDayNumber
|
const getCycleDayNumber = cycleModule({ bleedingDaysSortedByDate: bleedingDays }).getCycleDayNumber
|
||||||
const result = getCycleDayNumber(targetDate)
|
const result = getCycleDayNumber(targetDate)
|
||||||
expect(result).to.eql(18)
|
expect(result).to.eql(18)
|
||||||
})
|
})
|
||||||
@@ -87,59 +87,147 @@ describe('getCycleDay', () => {
|
|||||||
}]
|
}]
|
||||||
|
|
||||||
const targetDate = '2018-05-13'
|
const targetDate = '2018-05-13'
|
||||||
const getCycleDayNumber = cycleModule({bleedingDaysSortedByDate: bleedingDays}).getCycleDayNumber
|
const getCycleDayNumber = cycleModule({ bleedingDaysSortedByDate: bleedingDays }).getCycleDayNumber
|
||||||
const result = getCycleDayNumber(targetDate)
|
const result = getCycleDayNumber(targetDate)
|
||||||
expect(result).to.eql(1)
|
expect(result).to.eql(1)
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
|
||||||
describe('getCycleDay returns null', () => {
|
describe('getCycleDay returns null', () => {
|
||||||
it('if there are no bleeding days', function () {
|
it('if there are no bleeding days', function () {
|
||||||
const bleedingDays = []
|
const bleedingDays = []
|
||||||
const targetDate = '2018-05-17'
|
const targetDate = '2018-05-17'
|
||||||
const getCycleDayNumber = cycleModule({bleedingDaysSortedByDate: bleedingDays}).getCycleDayNumber
|
const getCycleDayNumber = cycleModule({ bleedingDaysSortedByDate: bleedingDays }).getCycleDayNumber
|
||||||
const result = getCycleDayNumber(targetDate)
|
const result = getCycleDayNumber(targetDate)
|
||||||
expect(result).to.be.null()
|
expect(result).to.be.null()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('getCycleDay with cycle thresholds', () => {
|
||||||
|
const maxBreakInBleeding = 3
|
||||||
|
|
||||||
|
it('disregards bleeding breaks shorter than max allowed bleeding break in a bleeding period', () => {
|
||||||
|
const bleedingDays = [{
|
||||||
|
date: '2018-05-14',
|
||||||
|
bleeding: {
|
||||||
|
value: 2
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
date: '2018-05-10',
|
||||||
|
bleeding: {
|
||||||
|
value: 2
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
|
||||||
|
const targetDate = '2018-05-17'
|
||||||
|
const getCycleDayNumber = cycleModule({ bleedingDaysSortedByDate: bleedingDays, maxBreakInBleeding }).getCycleDayNumber
|
||||||
|
const result = getCycleDayNumber(targetDate)
|
||||||
|
expect(result).to.eql(8)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('counts bleeding breaks longer than maxAllowedBleedingBreak in a bleeding period', () => {
|
||||||
|
const bleedingDays = [{
|
||||||
|
date: '2018-05-14',
|
||||||
|
bleeding: {
|
||||||
|
value: 2
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
date: '2018-05-09',
|
||||||
|
bleeding: {
|
||||||
|
value: 2
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
const targetDate = '2018-05-17'
|
||||||
|
const getCycleDayNumber = cycleModule({ bleedingDaysSortedByDate: bleedingDays, maxBreakInBleeding }).getCycleDayNumber
|
||||||
|
const result = getCycleDayNumber(targetDate)
|
||||||
|
expect(result).to.eql(4)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('getCycleDay with cycle thresholds', () => {
|
describe('getPreviousCycles', () => {
|
||||||
const maxBreakInBleeding = 3
|
it('gets previous cycles', () => {
|
||||||
|
const cycleDaysSortedByDate = [
|
||||||
|
{
|
||||||
|
date: '2018-07-05',
|
||||||
|
bleeding: { value: 2 }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: '2018-06-05',
|
||||||
|
bleeding: { value: 2 }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: '2018-05-05',
|
||||||
|
mucus: { value: 2 }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: '2018-05-04',
|
||||||
|
bleeding: { value: 2 }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: '2018-05-03',
|
||||||
|
bleeding: { value: 2 }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: '2018-04-05',
|
||||||
|
mucus: { value: 2 }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: '2018-04-04',
|
||||||
|
mucus: { value: 2 }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: '2018-04-03',
|
||||||
|
mucus: { value: 2 }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: '2018-04-02',
|
||||||
|
bleeding: { value: 2 }
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
it('disregards bleeding breaks shorter than max allowed bleeding break in a bleeding period', () => {
|
const { getPreviousCycles } = cycleModule({
|
||||||
const bleedingDays = [{
|
cycleDaysSortedByDate,
|
||||||
date: '2018-05-14',
|
bleedingDaysSortedByDate: cycleDaysSortedByDate.filter(d => d.bleeding)
|
||||||
bleeding: {
|
})
|
||||||
value: 2
|
const result = getPreviousCycles(cycleDaysSortedByDate[0])
|
||||||
}
|
expect(result.length).to.eql(3)
|
||||||
}, {
|
expect(result).to.eql([
|
||||||
date: '2018-05-10',
|
[
|
||||||
bleeding: {
|
{
|
||||||
value: 2
|
date: '2018-06-05',
|
||||||
}
|
bleeding: { value: 2 }
|
||||||
}]
|
}
|
||||||
|
], [
|
||||||
const targetDate = '2018-05-17'
|
{
|
||||||
const getCycleDayNumber = cycleModule({bleedingDaysSortedByDate: bleedingDays, maxBreakInBleeding }).getCycleDayNumber
|
date: '2018-05-05',
|
||||||
const result = getCycleDayNumber(targetDate)
|
mucus: { value: 2 }
|
||||||
expect(result).to.eql(8)
|
},
|
||||||
})
|
{
|
||||||
|
date: '2018-05-04',
|
||||||
it('counts bleeding breaks longer than maxAllowedBleedingBreak in a bleeding period', () => {
|
bleeding: { value: 2 }
|
||||||
const bleedingDays = [{
|
},
|
||||||
date: '2018-05-14',
|
{
|
||||||
bleeding: {
|
date: '2018-05-03',
|
||||||
value: 2
|
bleeding: { value: 2 }
|
||||||
}
|
}
|
||||||
}, {
|
], [
|
||||||
date: '2018-05-09',
|
{
|
||||||
bleeding: {
|
date: '2018-04-05',
|
||||||
value: 2
|
mucus: { value: 2 }
|
||||||
}
|
},
|
||||||
}]
|
{
|
||||||
const targetDate = '2018-05-17'
|
date: '2018-04-04',
|
||||||
const getCycleDayNumber = cycleModule({bleedingDaysSortedByDate: bleedingDays, maxBreakInBleeding }).getCycleDayNumber
|
mucus: { value: 2 }
|
||||||
const result = getCycleDayNumber(targetDate)
|
},
|
||||||
expect(result).to.eql(4)
|
{
|
||||||
|
date: '2018-04-03',
|
||||||
|
mucus: { value: 2 }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: '2018-04-02',
|
||||||
|
bleeding: { value: 2 }
|
||||||
|
},
|
||||||
|
]
|
||||||
|
])
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
Reference in New Issue
Block a user