Gather general cycle-related functions in cycle module

This commit is contained in:
Julia Friesel
2018-06-30 13:50:01 +02:00
parent 97b8452b45
commit 28b880a40c
8 changed files with 84 additions and 75 deletions
+2 -2
View File
@@ -11,11 +11,11 @@ import Svg,{
} from 'react-native-svg' } from 'react-native-svg'
import { LocalDate } from 'js-joda' import { LocalDate } from 'js-joda'
import { getCycleDay, getOrCreateCycleDay, cycleDaysSortedByDate } from '../../db' import { getCycleDay, getOrCreateCycleDay, cycleDaysSortedByDate } from '../../db'
import getCycleDayNumberModule from '../../lib/get-cycle-day-number' import cycleModule from '../../lib/cycle'
import styles from './styles' import styles from './styles'
import config from './config' import config from './config'
const getCycleDayNumber = getCycleDayNumberModule() const getCycleDayNumber = cycleModule().getCycleDayNumber
const yAxis = makeYAxis(config) const yAxis = makeYAxis(config)
+4 -2
View File
@@ -6,14 +6,16 @@ import {
} from 'react-native' } from 'react-native'
import styles from '../styles/index' import styles from '../styles/index'
import { bleeding as labels} from '../labels/labels' import { bleeding as labels} from '../labels/labels'
import cycleDayModule from '../lib/get-cycle-day-number' import cycleModule from '../lib/cycle'
import { bleedingDaysSortedByDate } from '../db' import { bleedingDaysSortedByDate } from '../db'
const getCycleDayNumber = cycleDayModule() const getCycleDayNumber = cycleModule().getCycleDayNumber
export default class DayView extends Component { export default class DayView extends Component {
constructor(props) { constructor(props) {
super(props) super(props)
console.log('new')
console.log(props.cycleDay)
this.cycleDay = props.cycleDay this.cycleDay = props.cycleDay
this.showView = props.showView this.showView = props.showView
this.state = { this.state = {
+2 -2
View File
@@ -3,14 +3,14 @@ import {
View, View,
Text Text
} from 'react-native' } from 'react-native'
import cycleDayModule from '../lib/get-cycle-day-number' import cycleModule from '../lib/cycle'
import DayView from './cycle-day-overview' import DayView from './cycle-day-overview'
import BleedingEditView from './bleeding' import BleedingEditView from './bleeding'
import TemperatureEditView from './temperature' import TemperatureEditView from './temperature'
import { formatDateForViewHeader } from '../labels/format' import { formatDateForViewHeader } from '../labels/format'
import styles from '../styles/index' import styles from '../styles/index'
const getCycleDayNumber = cycleDayModule() const getCycleDayNumber = cycleModule().getCycleDayNumber
export default class Day extends Component { export default class Day extends Component {
constructor(props) { constructor(props) {
+2 -2
View File
@@ -6,10 +6,10 @@ import {
} from 'react-native' } from 'react-native'
import { LocalDate } from 'js-joda' import { LocalDate } from 'js-joda'
import styles from '../styles/index' import styles from '../styles/index'
import cycleDayModule from '../lib/get-cycle-day-number' import cycleModule from '../lib/cycle'
import { getOrCreateCycleDay, bleedingDaysSortedByDate, deleteAll } from '../db' import { getOrCreateCycleDay, bleedingDaysSortedByDate, deleteAll } from '../db'
const getCycleDayNumber = cycleDayModule() const getCycleDayNumber = cycleModule().getCycleDayNumber
export default class Home extends Component { export default class Home extends Component {
constructor(props) { constructor(props) {
+65
View File
@@ -0,0 +1,65 @@
import * as joda from 'js-joda'
const LocalDate = joda.LocalDate
export default function config(opts = {}) {
let bleedingDaysSortedByDate
if (!opts.bleedingDaysSortedByDate) {
// we only want to require (and run) the db module when not running the tests
bleedingDaysSortedByDate = require('../db').bleedingDaysSortedByDate
} else {
bleedingDaysSortedByDate = opts.bleedingDaysSortedByDate
}
const maxBreakInBleeding = opts.maxBreakInBleeding || 1
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)
// cycle starts at day 1
return diffInDays + 1
}
function getPreviousDaysInCycle() {
return []
}
return {
getCycleDayNumber,
getLastMensesStart,
getPreviousDaysInCycle
}
}
-23
View File
@@ -1,23 +0,0 @@
import * as joda from 'js-joda'
import getLastMensesStart from './get-last-menses-start'
export default function config(opts = {}) {
let bleedingDaysSortedByDate
if (!opts.bleedingDaysSortedByDate) {
// we only want to require (and run) the db module when not running the tests
bleedingDaysSortedByDate = require('../db').bleedingDaysSortedByDate
} else {
bleedingDaysSortedByDate = opts.bleedingDaysSortedByDate
}
const maxBreakInBleeding = opts.maxBreakInBleeding || 1
return function(targetDateString) {
const lastMensesStart = getLastMensesStart(targetDateString, bleedingDaysSortedByDate, maxBreakInBleeding)
if (!lastMensesStart) return null
const targetDate = joda.LocalDate.parse(targetDateString)
const diffInDays = lastMensesStart.wrappedDate.until(targetDate, joda.ChronoUnit.DAYS)
// cycle starts at day 1
return diffInDays + 1
}
}
-34
View File
@@ -1,34 +0,0 @@
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))
}
@@ -1,13 +1,12 @@
import chai from 'chai' import chai from 'chai'
import dirtyChai from 'dirty-chai' import dirtyChai from 'dirty-chai'
import cycleModule from '../lib/cycle'
const expect = chai.expect const expect = chai.expect
chai.use(dirtyChai) chai.use(dirtyChai)
import getCycleDayNumberModule from '../lib/get-cycle-day-number'
describe('getCycleDay', () => { describe('getCycleDay', () => {
it('works for a simple example', function () { it('works for a simple example', () => {
const bleedingDays = [{ const bleedingDays = [{
date: '2018-05-10', date: '2018-05-10',
bleeding: { bleeding: {
@@ -24,7 +23,7 @@ describe('getCycleDay', () => {
value: 2 value: 2
} }
}] }]
const getCycleDayNumber = getCycleDayNumberModule({bleedingDaysSortedByDate: bleedingDays}) 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)
@@ -50,7 +49,7 @@ describe('getCycleDay', () => {
} }
}] }]
const targetDate = '2018-05-17' const targetDate = '2018-05-17'
const getCycleDayNumber = getCycleDayNumberModule({bleedingDaysSortedByDate: bleedingDays}) const getCycleDayNumber = cycleModule({bleedingDaysSortedByDate: bleedingDays}).getCycleDayNumber
const result = getCycleDayNumber(targetDate) const result = getCycleDayNumber(targetDate)
expect(result).to.eql(15) expect(result).to.eql(15)
}) })
@@ -74,7 +73,7 @@ describe('getCycleDay', () => {
}] }]
const targetDate = '2018-04-27' const targetDate = '2018-04-27'
const getCycleDayNumber = getCycleDayNumberModule({bleedingDaysSortedByDate: bleedingDays}) const getCycleDayNumber = cycleModule({bleedingDaysSortedByDate: bleedingDays}).getCycleDayNumber
const result = getCycleDayNumber(targetDate) const result = getCycleDayNumber(targetDate)
expect(result).to.eql(18) expect(result).to.eql(18)
}) })
@@ -88,7 +87,7 @@ describe('getCycleDay', () => {
}] }]
const targetDate = '2018-05-13' const targetDate = '2018-05-13'
const getCycleDayNumber = getCycleDayNumberModule({bleedingDaysSortedByDate: bleedingDays}) const getCycleDayNumber = cycleModule({bleedingDaysSortedByDate: bleedingDays}).getCycleDayNumber
const result = getCycleDayNumber(targetDate) const result = getCycleDayNumber(targetDate)
expect(result).to.eql(1) expect(result).to.eql(1)
}) })
@@ -98,7 +97,7 @@ 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 = getCycleDayNumberModule({bleedingDaysSortedByDate: bleedingDays}) const getCycleDayNumber = cycleModule({bleedingDaysSortedByDate: bleedingDays}).getCycleDayNumber
const result = getCycleDayNumber(targetDate) const result = getCycleDayNumber(targetDate)
expect(result).to.be.null() expect(result).to.be.null()
}) })
@@ -121,7 +120,7 @@ describe('getCycleDay with cycle thresholds', () => {
}] }]
const targetDate = '2018-05-17' const targetDate = '2018-05-17'
const getCycleDayNumber = getCycleDayNumberModule({bleedingDaysSortedByDate: bleedingDays, maxBreakInBleeding }) const getCycleDayNumber = cycleModule({bleedingDaysSortedByDate: bleedingDays, maxBreakInBleeding }).getCycleDayNumber
const result = getCycleDayNumber(targetDate) const result = getCycleDayNumber(targetDate)
expect(result).to.eql(8) expect(result).to.eql(8)
}) })
@@ -139,7 +138,7 @@ describe('getCycleDay with cycle thresholds', () => {
} }
}] }]
const targetDate = '2018-05-17' const targetDate = '2018-05-17'
const getCycleDayNumber = getCycleDayNumberModule({bleedingDaysSortedByDate: bleedingDays, maxBreakInBleeding }) const getCycleDayNumber = cycleModule({bleedingDaysSortedByDate: bleedingDays, maxBreakInBleeding }).getCycleDayNumber
const result = getCycleDayNumber(targetDate) const result = getCycleDayNumber(targetDate)
expect(result).to.eql(4) expect(result).to.eql(4)
}) })