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

Gather general cycle-related functions in cycle module

parent 97b8452b
No related branches found
No related tags found
No related merge requests found
......@@ -11,11 +11,11 @@ import Svg,{
} from 'react-native-svg'
import { LocalDate } from 'js-joda'
import { getCycleDay, getOrCreateCycleDay, cycleDaysSortedByDate } from '../../db'
import getCycleDayNumberModule from '../../lib/get-cycle-day-number'
import cycleModule from '../../lib/cycle'
import styles from './styles'
import config from './config'
const getCycleDayNumber = getCycleDayNumberModule()
const getCycleDayNumber = cycleModule().getCycleDayNumber
const yAxis = makeYAxis(config)
......
......@@ -6,14 +6,16 @@ import {
} from 'react-native'
import styles from '../styles/index'
import { bleeding as labels} from '../labels/labels'
import cycleDayModule from '../lib/get-cycle-day-number'
import cycleModule from '../lib/cycle'
import { bleedingDaysSortedByDate } from '../db'
const getCycleDayNumber = cycleDayModule()
const getCycleDayNumber = cycleModule().getCycleDayNumber
export default class DayView extends Component {
constructor(props) {
super(props)
console.log('new')
console.log(props.cycleDay)
this.cycleDay = props.cycleDay
this.showView = props.showView
this.state = {
......
......@@ -3,14 +3,14 @@ import {
View,
Text
} from 'react-native'
import cycleDayModule from '../lib/get-cycle-day-number'
import cycleModule from '../lib/cycle'
import DayView from './cycle-day-overview'
import BleedingEditView from './bleeding'
import TemperatureEditView from './temperature'
import { formatDateForViewHeader } from '../labels/format'
import styles from '../styles/index'
const getCycleDayNumber = cycleDayModule()
const getCycleDayNumber = cycleModule().getCycleDayNumber
export default class Day extends Component {
constructor(props) {
......
......@@ -6,10 +6,10 @@ import {
} from 'react-native'
import { LocalDate } from 'js-joda'
import styles from '../styles/index'
import cycleDayModule from '../lib/get-cycle-day-number'
import cycleModule from '../lib/cycle'
import { getOrCreateCycleDay, bleedingDaysSortedByDate, deleteAll } from '../db'
const getCycleDayNumber = cycleDayModule()
const getCycleDayNumber = cycleModule().getCycleDayNumber
export default class Home extends Component {
constructor(props) {
......
import * as joda from 'js-joda'
import getLastMensesStart from './get-last-menses-start'
const LocalDate = joda.LocalDate
export default function config(opts = {}) {
let bleedingDaysSortedByDate
......@@ -11,8 +12,39 @@ export default function config(opts = {}) {
}
const maxBreakInBleeding = opts.maxBreakInBleeding || 1
return function(targetDateString) {
const lastMensesStart = getLastMensesStart(targetDateString, bleedingDaysSortedByDate, maxBreakInBleeding)
function getLastMensesStart(targetDateString) {
const targetDate = LocalDate.parse(targetDateString)
const withWrappedDates = bleedingDaysSortedByDate
.filter(day => !day.bleeding.exclude)
.map(day => {
day.wrappedDate = LocalDate.parse(day.date)
return day
})
const firstBleedingDayBeforeTargetDayIndex = withWrappedDates.findIndex(day => {
return (
day.wrappedDate.isEqual(targetDate) ||
day.wrappedDate.isBefore(targetDate)
)
})
if (firstBleedingDayBeforeTargetDayIndex < 0) return null
const previousBleedingDays = withWrappedDates.slice(firstBleedingDayBeforeTargetDayIndex)
const lastPeriodStart = previousBleedingDays.find((day, i) => {
return thereIsNoPreviousBleedingDayWithinTheThreshold(day, previousBleedingDays.slice(i + 1))
})
function thereIsNoPreviousBleedingDayWithinTheThreshold(bleedingDay, previousBleedingDays) {
const periodThreshold = bleedingDay.wrappedDate.minusDays(maxBreakInBleeding + 1)
return !previousBleedingDays.some(({ wrappedDate }) => wrappedDate.equals(periodThreshold) || wrappedDate.isAfter(periodThreshold))
}
return lastPeriodStart
}
function getCycleDayNumber(targetDateString) {
const lastMensesStart = getLastMensesStart(targetDateString)
if (!lastMensesStart) return null
const targetDate = joda.LocalDate.parse(targetDateString)
const diffInDays = lastMensesStart.wrappedDate.until(targetDate, joda.ChronoUnit.DAYS)
......@@ -20,4 +52,14 @@ export default function config(opts = {}) {
// cycle starts at day 1
return diffInDays + 1
}
}
\ No newline at end of file
function getPreviousDaysInCycle() {
return []
}
return {
getCycleDayNumber,
getLastMensesStart,
getPreviousDaysInCycle
}
}
import * as joda from 'js-joda'
const LocalDate = joda.LocalDate
export default function getLastMensesStart(targetDateString, bleedingDaysSortedByDate, maxBreakInBleeding) {
const targetDate = LocalDate.parse(targetDateString)
const withWrappedDates = bleedingDaysSortedByDate
.filter(day => !day.bleeding.exclude)
.map(day => {
day.wrappedDate = LocalDate.parse(day.date)
return day
})
const firstBleedingDayBeforeTargetDayIndex = withWrappedDates.findIndex(day => {
return (
day.wrappedDate.isEqual(targetDate) ||
day.wrappedDate.isBefore(targetDate)
)
})
if (firstBleedingDayBeforeTargetDayIndex < 0) return null
const previousBleedingDays = withWrappedDates.slice(firstBleedingDayBeforeTargetDayIndex)
const lastPeriodStart = previousBleedingDays.find((day, i) => {
return thereIsNoPreviousBleedingDayWithinTheThreshold(day, previousBleedingDays.slice(i + 1), maxBreakInBleeding)
})
return lastPeriodStart
}
function thereIsNoPreviousBleedingDayWithinTheThreshold(bleedingDay, earlierCycleDays, allowedBleedingBreak) {
const periodThreshold = bleedingDay.wrappedDate.minusDays(allowedBleedingBreak + 1)
return !earlierCycleDays.some(({ wrappedDate }) => wrappedDate.equals(periodThreshold) || wrappedDate.isAfter(periodThreshold))
}
\ No newline at end of file
import chai from 'chai'
import dirtyChai from 'dirty-chai'
import cycleModule from '../lib/cycle'
const expect = chai.expect
chai.use(dirtyChai)
import getCycleDayNumberModule from '../lib/get-cycle-day-number'
describe('getCycleDay', () => {
it('works for a simple example', function () {
it('works for a simple example', () => {
const bleedingDays = [{
date: '2018-05-10',
bleeding: {
......@@ -24,7 +23,7 @@ describe('getCycleDay', () => {
value: 2
}
}]
const getCycleDayNumber = getCycleDayNumberModule({bleedingDaysSortedByDate: bleedingDays})
const getCycleDayNumber = cycleModule({bleedingDaysSortedByDate: bleedingDays}).getCycleDayNumber
const targetDate = '2018-05-17'
const result = getCycleDayNumber(targetDate)
expect(result).to.eql(9)
......@@ -50,7 +49,7 @@ describe('getCycleDay', () => {
}
}]
const targetDate = '2018-05-17'
const getCycleDayNumber = getCycleDayNumberModule({bleedingDaysSortedByDate: bleedingDays})
const getCycleDayNumber = cycleModule({bleedingDaysSortedByDate: bleedingDays}).getCycleDayNumber
const result = getCycleDayNumber(targetDate)
expect(result).to.eql(15)
})
......@@ -74,7 +73,7 @@ describe('getCycleDay', () => {
}]
const targetDate = '2018-04-27'
const getCycleDayNumber = getCycleDayNumberModule({bleedingDaysSortedByDate: bleedingDays})
const getCycleDayNumber = cycleModule({bleedingDaysSortedByDate: bleedingDays}).getCycleDayNumber
const result = getCycleDayNumber(targetDate)
expect(result).to.eql(18)
})
......@@ -88,7 +87,7 @@ describe('getCycleDay', () => {
}]
const targetDate = '2018-05-13'
const getCycleDayNumber = getCycleDayNumberModule({bleedingDaysSortedByDate: bleedingDays})
const getCycleDayNumber = cycleModule({bleedingDaysSortedByDate: bleedingDays}).getCycleDayNumber
const result = getCycleDayNumber(targetDate)
expect(result).to.eql(1)
})
......@@ -98,7 +97,7 @@ describe('getCycleDay returns null', () => {
it('if there are no bleeding days', function () {
const bleedingDays = []
const targetDate = '2018-05-17'
const getCycleDayNumber = getCycleDayNumberModule({bleedingDaysSortedByDate: bleedingDays})
const getCycleDayNumber = cycleModule({bleedingDaysSortedByDate: bleedingDays}).getCycleDayNumber
const result = getCycleDayNumber(targetDate)
expect(result).to.be.null()
})
......@@ -121,7 +120,7 @@ describe('getCycleDay with cycle thresholds', () => {
}]
const targetDate = '2018-05-17'
const getCycleDayNumber = getCycleDayNumberModule({bleedingDaysSortedByDate: bleedingDays, maxBreakInBleeding })
const getCycleDayNumber = cycleModule({bleedingDaysSortedByDate: bleedingDays, maxBreakInBleeding }).getCycleDayNumber
const result = getCycleDayNumber(targetDate)
expect(result).to.eql(8)
})
......@@ -139,7 +138,7 @@ describe('getCycleDay with cycle thresholds', () => {
}
}]
const targetDate = '2018-05-17'
const getCycleDayNumber = getCycleDayNumberModule({bleedingDaysSortedByDate: bleedingDays, maxBreakInBleeding })
const getCycleDayNumber = cycleModule({bleedingDaysSortedByDate: bleedingDays, maxBreakInBleeding }).getCycleDayNumber
const result = getCycleDayNumber(targetDate)
expect(result).to.eql(4)
})
......
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