diff --git a/bleeding.js b/bleeding.js index 359a94f..f7e4e55 100644 --- a/bleeding.js +++ b/bleeding.js @@ -10,7 +10,10 @@ import styles from './styles' import { saveBleeding } from './db' import { formatDateForViewHeader } from './format' import { bleeding as labels } from './labels' -import getCycleDay from './get-cycle-day' +import cycleDayModule from './get-cycle-day-number' +import { getCycleDaysSortedByDateView } from './db' + +const getCycleDay = cycleDayModule(getCycleDaysSortedByDateView()) export default class Bleeding extends Component { constructor(props) { @@ -40,7 +43,7 @@ export default class Bleeding extends Component { return ( {formatDateForViewHeader(day.date)} - Cycle day {getCycleDay()} + Cycle day {getCycleDay(day.date)} Bleeding {formatDateForViewHeader(day.date)} - Cycle day {getCycleDay()} + Cycle day {getCycleDay(day.date)} {bleedingLabel} + ) } -} \ No newline at end of file +} diff --git a/index.js b/index.js index f1543b7..408a2f0 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,4 @@ import { AppRegistry } from 'react-native' -import Home from './app' -import { openDatabase } from './db' +import App from './app' -// TODO error handling -openDatabase() - .then(() => { - AppRegistry.registerComponent('home', () => Home) - }) \ No newline at end of file +AppRegistry.registerComponent('home', () => App) \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index c531614..21bda8c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4693,6 +4693,11 @@ "merge-stream": "^1.0.1" } }, + "js-joda": { + "version": "1.8.2", + "resolved": "https://registry.npmjs.org/js-joda/-/js-joda-1.8.2.tgz", + "integrity": "sha512-3w+3TnKqiplQyG/YQk31cBhJ/sg2Xb/fX7lneiK+z+nEjTzdfvHqJquJhtzyEA1NyLJKNpIeOQSBr3Q4nY+O8Q==" + }, "js-tokens": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-3.0.2.tgz", diff --git a/package.json b/package.json index 28310a9..ff64b52 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "lint": "eslint app test" }, "dependencies": { + "js-joda": "^1.8.2", "moment": "^2.22.1", "react": "16.3.1", "react-native": "0.55.4", diff --git a/test/get-cycle-day.spec.js b/test/get-cycle-day.spec.js index 64ee0b9..60b4fa5 100644 --- a/test/get-cycle-day.spec.js +++ b/test/get-cycle-day.spec.js @@ -7,233 +7,126 @@ chai.use(dirtyChai) import getCycleDayNumberModule from '../get-cycle-day-number' describe('getCycleDay', () => { - const getCycleDayNumber = getCycleDayNumberModule() - it('works if the last data entered is a bleeding day', function () { - const cycleDays = [{ - date: new Date(2018, 5, 2) - }, { - date: new Date(2018, 5, 3), + it('works for a simple example', function () { + const bleedingDays = [{ + date: '2018-05-10', bleeding: { value: 2 } }, { - date: new Date(2018, 5, 4) - }, { - date: new Date(2018, 5, 9), + date: '2018-05-09', bleeding: { value: 2 } }, { - date: new Date(2018, 5, 10), + date: '2018-05-03', bleeding: { value: 2 } }] - const targetDate = new Date(2018, 5, 17) - const result = getCycleDayNumber(cycleDays, targetDate) - expect(result).to.eql(9) - }) - - it('works if the last data entered is a non-bleeding day', function () { - const cycleDays = [{ - date: new Date(2018, 5, 2) - }, { - date: new Date(2018, 5, 3), - bleeding: { - value: 2 - } - }, { - date: new Date(2018, 5, 4) - }, { - date: new Date(2018, 5, 9), - bleeding: { - value: 2 - } - }, { - date: new Date(2018, 5, 10), - bleeding: { - value: 2 - } - }, { - date: new Date(2018, 5, 13) - }, { - date: new Date(2018, 5, 14) - }] - - const targetDate = new Date(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: new Date(2018, 5, 13), - bleeding: { - value: 2 - } - }, { - date: new Date(2018, 5, 9), - bleeding: { - value: 2 - } - }, { - date: new Date(2018, 5, 3), - bleeding: { - value: 2 - } - }, { - date: new Date(2018, 5, 4) - }, { - date: new Date(2018, 5, 10), - bleeding: { - value: 2 - } - }, { - date: new Date(2018, 5, 2) - }] - - const targetDate = new Date(2018, 5, 17) - const result = getCycleDayNumber(cycleDays, targetDate) - expect(result).to.eql(5) - }) - - it('works when there are only bleeding days', function () { - const cycleDays = [{ - date: new Date(2018, 5, 9), - bleeding: { - value: 2 - } - }, { - date: new Date(2018, 5, 10), - bleeding: { - value: 2 - } - }] - const targetDate = new Date(2018, 5, 17) - const result = getCycleDayNumber(cycleDays, targetDate) + const getCycleDayNumber = getCycleDayNumberModule(bleedingDays) + const targetDate = '2018-05-17' + const result = getCycleDayNumber(targetDate) expect(result).to.eql(9) }) it('works if some bleedings are exluded', function () { - const cycleDays = [{ - date: new Date(2018, 5, 2) + const bleedingDays = [{ + date: '2018-05-10', + bleeding: { + value: 2, + exclude: true + } }, { - date: new Date(2018, 5, 3), + date: '2018-05-09', + bleeding: { + value: 2, + exclude: true + } + }, { + date: '2018-05-03', bleeding: { value: 2 } - }, { - date: new Date(2018, 5, 4) - }, { - date: new Date(2018, 5, 9), - bleeding: { - value: 2, - exclude: true - } - }, { - date: new Date(2018, 5, 10), - bleeding: { - value: 2, - exclude: true - } }] - const targetDate = new Date(2018, 5, 17) - const result = getCycleDayNumber(cycleDays, targetDate) + const targetDate = '2018-05-17' + const getCycleDayNumber = getCycleDayNumberModule(bleedingDays) + const result = getCycleDayNumber(targetDate) expect(result).to.eql(15) }) it('gets the correct number if the target day is not in the current cycle', () => { - const cycleDays = [{ - date: new Date(2018, 5, 14), - }, { - date: new Date(2018, 5, 13), + const bleedingDays = [{ + date: '2018-05-13', bleeding: { value: 2 } }, { - date: new Date(2018, 4, 12), - }, { - date: new Date(2018, 4, 11), + date: '2018-04-11', bleeding: { value: 2 } }, { - date: new Date(2018, 4, 10), + date: '2018-04-10', bleeding: { value: 2 } - }, { - date: new Date(2018, 4, 9), }] - const targetDate = new Date(2018, 4, 27) - const result = getCycleDayNumber(cycleDays, targetDate) + const targetDate = '2018-04-27' + const getCycleDayNumber = getCycleDayNumberModule(bleedingDays) + const result = getCycleDayNumber(targetDate) expect(result).to.eql(18) }) }) describe('getCycleDay returns null', () => { - const getCycleDayNumber = getCycleDayNumberModule() it('if there are no bleeding days', function () { - const cycleDays = [{ - date: new Date(2018, 5, 2) - }, { - date: new Date(2018, 5, 4) - }, { - date: new Date(2018, 5, 9), - }, { - date: new Date(2018, 5, 10), - }] - const targetDate = new Date(2018, 5, 17) - const result = getCycleDayNumber(cycleDays, targetDate) - expect(result).to.be.null() - }) - - it('if there are no cycle days', function () { - const cycleDays = [] - const targetDate = new Date(2018, 5, 17) - const result = getCycleDayNumber(cycleDays, targetDate) + const bleedingDays = [] + const targetDate = '2018-05-17' + const getCycleDayNumber = getCycleDayNumberModule(bleedingDays) + const result = getCycleDayNumber(targetDate) expect(result).to.be.null() }) }) describe('getCycleDay with cycle thresholds', () => { - const getCycleDayNumber = getCycleDayNumberModule({ - minCycleLengthInDays: 5 - }) + const opts = { maxBreakInBleeding: 3 } - it('disregards bleeding breaks shorter than the min cycle threshold in a bleeding period', () => { - const cycleDays = [{ - date: new Date(2018, 5, 10), + it('disregards bleeding breaks shorter than max allowed bleeding break in a bleeding period', () => { + const bleedingDays = [{ + date: '2018-05-14', bleeding: { value: 2 } }, { - date: new Date(2018, 5, 14), + date: '2018-05-10', bleeding: { value: 2 } }] - const targetDate = new Date(2018, 5, 17) - const result = getCycleDayNumber(cycleDays, targetDate) + const targetDate = '2018-05-17' + const getCycleDayNumber = getCycleDayNumberModule(bleedingDays, opts) + const result = getCycleDayNumber(targetDate) expect(result).to.eql(8) }) - it('counts bleeding breaks longer than the min cycle threshold in a bleeding period', () => { - const cycleDays = [{ - date: new Date(2018, 5, 9), + it('counts bleeding breaks longer than maxAllowedBleedingBreak in a bleeding period', () => { + const bleedingDays = [{ + date: '2018-05-14', bleeding: { value: 2 } }, { - date: new Date(2018, 5, 14), + date: '2018-05-09', bleeding: { value: 2 } }] - const targetDate = new Date(2018, 5, 17) - const result = getCycleDayNumber(cycleDays, targetDate) + const targetDate = '2018-05-17' + const getCycleDayNumber = getCycleDayNumberModule(bleedingDays, opts) + const result = getCycleDayNumber(targetDate) expect(result).to.eql(4) }) }) \ No newline at end of file