Pass in dates as JS dates, not moment-wrapped

This commit is contained in:
Julia Friesel
2018-06-07 11:22:29 +02:00
parent 3bc85d74fc
commit 1430734874
2 changed files with 56 additions and 51 deletions
+7 -1
View File
@@ -12,7 +12,13 @@ export default function config(opts) {
// the past as we iterate over the array // the past as we iterate over the array
// also, to retrieve the number, we only need the cycle days before the target day // also, to retrieve the number, we only need the cycle days before the target day
// TODO we can probably rely on the db to do the sorting for us // TODO we can probably rely on the db to do the sorting for us
const sorted = [...unsorted].sort((a, b) => b.date.isAfter(a.date)) targetDate = moment(targetDate)
const sorted = unsorted
.map(day => {
day.date = moment(day.date)
return day
})
.sort((a, b) => b.date.isAfter(a.date))
const firstDayBeforeTargetDayIndex = sorted.findIndex(day => day.date.isBefore(targetDate)) const firstDayBeforeTargetDayIndex = sorted.findIndex(day => day.date.isBefore(targetDate))
const cycleDays = sorted.slice(firstDayBeforeTargetDayIndex) const cycleDays = sorted.slice(firstDayBeforeTargetDayIndex)
+49 -50
View File
@@ -1,6 +1,5 @@
import chai from 'chai' import chai from 'chai'
import dirtyChai from 'dirty-chai' import dirtyChai from 'dirty-chai'
import moment from 'moment'
const expect = chai.expect const expect = chai.expect
chai.use(dirtyChai) chai.use(dirtyChai)
@@ -11,163 +10,163 @@ describe('getCycleDay', () => {
const getCycleDayNumber = getCycleDayNumberModule() const getCycleDayNumber = getCycleDayNumberModule()
it('works if the last data entered is a bleeding day', function () { it('works if the last data entered is a bleeding day', function () {
const cycleDays = [{ const cycleDays = [{
date: moment([2018, 5, 2]) date: new Date(2018, 5, 2)
}, { }, {
date: moment([2018, 5, 3]), date: new Date(2018, 5, 3),
bleeding: { bleeding: {
value: 2 value: 2
} }
}, { }, {
date: moment([2018, 5, 4]) date: new Date(2018, 5, 4)
}, { }, {
date: moment([2018, 5, 9]), date: new Date(2018, 5, 9),
bleeding: { bleeding: {
value: 2 value: 2
} }
}, { }, {
date: moment([2018, 5, 10]), date: new Date(2018, 5, 10),
bleeding: { bleeding: {
value: 2 value: 2
} }
}] }]
const targetDate = moment([2018, 5, 17]) const targetDate = new Date(2018, 5, 17)
const result = getCycleDayNumber(cycleDays, targetDate) const result = getCycleDayNumber(cycleDays, targetDate)
expect(result).to.eql(9) expect(result).to.eql(9)
}) })
it('works if the last data entered is a non-bleeding day', function () { it('works if the last data entered is a non-bleeding day', function () {
const cycleDays = [{ const cycleDays = [{
date: moment([2018, 5, 2]) date: new Date(2018, 5, 2)
}, { }, {
date: moment([2018, 5, 3]), date: new Date(2018, 5, 3),
bleeding: { bleeding: {
value: 2 value: 2
} }
}, { }, {
date: moment([2018, 5, 4]) date: new Date(2018, 5, 4)
}, { }, {
date: moment([2018, 5, 9]), date: new Date(2018, 5, 9),
bleeding: { bleeding: {
value: 2 value: 2
} }
}, { }, {
date: moment([2018, 5, 10]), date: new Date(2018, 5, 10),
bleeding: { bleeding: {
value: 2 value: 2
} }
}, { }, {
date: moment([2018, 5, 13]) date: new Date(2018, 5, 13)
}, { }, {
date: moment([2018, 5, 14]) date: new Date(2018, 5, 14)
}] }]
const targetDate = moment([2018, 5, 17]) const targetDate = new Date(2018, 5, 17)
const result = getCycleDayNumber(cycleDays, targetDate) const result = getCycleDayNumber(cycleDays, targetDate)
expect(result).to.eql(9) expect(result).to.eql(9)
}) })
it('works if the cycle days are not sorted by date', function () { it('works if the cycle days are not sorted by date', function () {
const cycleDays = [{ const cycleDays = [{
date: moment([2018, 5, 13]), date: new Date(2018, 5, 13),
bleeding: { bleeding: {
value: 2 value: 2
} }
}, { }, {
date: moment([2018, 5, 9]), date: new Date(2018, 5, 9),
bleeding: { bleeding: {
value: 2 value: 2
} }
}, { }, {
date: moment([2018, 5, 3]), date: new Date(2018, 5, 3),
bleeding: { bleeding: {
value: 2 value: 2
} }
}, { }, {
date: moment([2018, 5, 4]) date: new Date(2018, 5, 4)
}, { }, {
date: moment([2018, 5, 10]), date: new Date(2018, 5, 10),
bleeding: { bleeding: {
value: 2 value: 2
} }
}, { }, {
date: moment([2018, 5, 2]) date: new Date(2018, 5, 2)
}] }]
const targetDate = moment([2018, 5, 17]) const targetDate = new Date(2018, 5, 17)
const result = getCycleDayNumber(cycleDays, targetDate) const result = getCycleDayNumber(cycleDays, targetDate)
expect(result).to.eql(5) expect(result).to.eql(5)
}) })
it('works when there are only bleeding days', function () { it('works when there are only bleeding days', function () {
const cycleDays = [{ const cycleDays = [{
date: moment([2018, 5, 9]), date: new Date(2018, 5, 9),
bleeding: { bleeding: {
value: 2 value: 2
} }
}, { }, {
date: moment([2018, 5, 10]), date: new Date(2018, 5, 10),
bleeding: { bleeding: {
value: 2 value: 2
} }
}] }]
const targetDate = moment([2018, 5, 17]) const targetDate = new Date(2018, 5, 17)
const result = getCycleDayNumber(cycleDays, targetDate) const result = getCycleDayNumber(cycleDays, targetDate)
expect(result).to.eql(9) expect(result).to.eql(9)
}) })
it('works if some bleedings are exluded', function () { it('works if some bleedings are exluded', function () {
const cycleDays = [{ const cycleDays = [{
date: moment([2018, 5, 2]) date: new Date(2018, 5, 2)
}, { }, {
date: moment([2018, 5, 3]), date: new Date(2018, 5, 3),
bleeding: { bleeding: {
value: 2 value: 2
} }
}, { }, {
date: moment([2018, 5, 4]) date: new Date(2018, 5, 4)
}, { }, {
date: moment([2018, 5, 9]), date: new Date(2018, 5, 9),
bleeding: { bleeding: {
value: 2, value: 2,
exclude: true exclude: true
} }
}, { }, {
date: moment([2018, 5, 10]), date: new Date(2018, 5, 10),
bleeding: { bleeding: {
value: 2, value: 2,
exclude: true exclude: true
} }
}] }]
const targetDate = moment([2018, 5, 17]) const targetDate = new Date(2018, 5, 17)
const result = getCycleDayNumber(cycleDays, targetDate) const result = getCycleDayNumber(cycleDays, targetDate)
expect(result).to.eql(15) expect(result).to.eql(15)
}) })
it('gets the correct number if the target day is not in the current cycle', () => { it('gets the correct number if the target day is not in the current cycle', () => {
const cycleDays = [{ const cycleDays = [{
date: moment([2018, 5, 14]), date: new Date(2018, 5, 14),
}, { }, {
date: moment([2018, 5, 13]), date: new Date(2018, 5, 13),
bleeding: { bleeding: {
value: 2 value: 2
} }
}, { }, {
date: moment([2018, 4, 12]), date: new Date(2018, 4, 12),
}, { }, {
date: moment([2018, 4, 11]), date: new Date(2018, 4, 11),
bleeding: { bleeding: {
value: 2 value: 2
} }
}, { }, {
date: moment([2018, 4, 10]), date: new Date(2018, 4, 10),
bleeding: { bleeding: {
value: 2 value: 2
} }
}, { }, {
date: moment([2018, 4, 9]), date: new Date(2018, 4, 9),
}] }]
const targetDate = moment([2018, 4, 27]) const targetDate = new Date(2018, 4, 27)
const result = getCycleDayNumber(cycleDays, targetDate) const result = getCycleDayNumber(cycleDays, targetDate)
expect(result).to.eql(18) expect(result).to.eql(18)
}) })
@@ -177,22 +176,22 @@ describe('getCycleDay returns null', () => {
const getCycleDayNumber = getCycleDayNumberModule() const getCycleDayNumber = getCycleDayNumberModule()
it('if there are no bleeding days', function () { it('if there are no bleeding days', function () {
const cycleDays = [{ const cycleDays = [{
date: moment([2018, 5, 2]) date: new Date(2018, 5, 2)
}, { }, {
date: moment([2018, 5, 4]) date: new Date(2018, 5, 4)
}, { }, {
date: moment([2018, 5, 9]), date: new Date(2018, 5, 9),
}, { }, {
date: moment([2018, 5, 10]), date: new Date(2018, 5, 10),
}] }]
const targetDate = moment([2018, 5, 17]) const targetDate = new Date(2018, 5, 17)
const result = getCycleDayNumber(cycleDays, targetDate) const result = getCycleDayNumber(cycleDays, targetDate)
expect(result).to.be.null() expect(result).to.be.null()
}) })
it('if there are no cycle days', function () { it('if there are no cycle days', function () {
const cycleDays = [] const cycleDays = []
const targetDate = moment([2018, 5, 17]) const targetDate = new Date(2018, 5, 17)
const result = getCycleDayNumber(cycleDays, targetDate) const result = getCycleDayNumber(cycleDays, targetDate)
expect(result).to.be.null() expect(result).to.be.null()
}) })
@@ -205,35 +204,35 @@ describe('getCycleDay with cycle thresholds', () => {
it('disregards bleeding breaks shorter than the min cycle threshold in a bleeding period', () => { it('disregards bleeding breaks shorter than the min cycle threshold in a bleeding period', () => {
const cycleDays = [{ const cycleDays = [{
date: moment([2018, 5, 10]), date: new Date(2018, 5, 10),
bleeding: { bleeding: {
value: 2 value: 2
} }
}, { }, {
date: moment([2018, 5, 14]), date: new Date(2018, 5, 14),
bleeding: { bleeding: {
value: 2 value: 2
} }
}] }]
const targetDate = moment([2018, 5, 17]) const targetDate = new Date(2018, 5, 17)
const result = getCycleDayNumber(cycleDays, targetDate) const result = getCycleDayNumber(cycleDays, targetDate)
expect(result).to.eql(8) expect(result).to.eql(8)
}) })
it('counts bleeding breaks longer than the min cycle threshold in a bleeding period', () => { it('counts bleeding breaks longer than the min cycle threshold in a bleeding period', () => {
const cycleDays = [{ const cycleDays = [{
date: moment([2018, 5, 9]), date: new Date(2018, 5, 9),
bleeding: { bleeding: {
value: 2 value: 2
} }
}, { }, {
date: moment([2018, 5, 14]), date: new Date(2018, 5, 14),
bleeding: { bleeding: {
value: 2 value: 2
} }
}] }]
const targetDate = moment([2018, 5, 17]) const targetDate = new Date(2018, 5, 17)
const result = getCycleDayNumber(cycleDays, targetDate) const result = getCycleDayNumber(cycleDays, targetDate)
expect(result).to.eql(4) expect(result).to.eql(4)
}) })