From a2c428e9913a8db8a3ac2c68dfca71a833a7cb71 Mon Sep 17 00:00:00 2001 From: Julia Friesel <julia.friesel@gmail.com> Date: Thu, 2 Aug 2018 10:47:06 +0200 Subject: [PATCH] Shorten line lengths in cycle --- lib/cycle.js | 7 ++++--- test/cycle.spec.js | 24 +++++++++++++++++------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/lib/cycle.js b/lib/cycle.js index 70401d5b..f350ff82 100644 --- a/lib/cycle.js +++ b/lib/cycle.js @@ -28,19 +28,20 @@ export default function config(opts) { return day }) - const firstBleedingBeforeTargetDayIndex = withWrappedDates.findIndex(day => { + // the index of the first bleeding day before the target day + const index = withWrappedDates.findIndex(day => { return ( day.wrappedDate.isEqual(targetDate) || day.wrappedDate.isBefore(targetDate) ) }) - if (firstBleedingBeforeTargetDayIndex < 0) { + if (index < 0) { withWrappedDates.forEach(day => delete day.wrappedDate) return null } - const prevBleedingDays = withWrappedDates.slice(firstBleedingBeforeTargetDayIndex) + const prevBleedingDays = withWrappedDates.slice(index) const lastMensesStart = prevBleedingDays.find((day, i) => { return noBleedingDayWithinThreshold(day, prevBleedingDays.slice(i + 1)) diff --git a/test/cycle.spec.js b/test/cycle.spec.js index 3b82c40d..a0efa7d4 100644 --- a/test/cycle.spec.js +++ b/test/cycle.spec.js @@ -5,6 +5,10 @@ import cycleModule from '../lib/cycle' const expect = chai.expect chai.use(dirtyChai) +function useBleedingDays(days) { + return cycleModule({ bleedingDaysSortedByDate: days }).getCycleDayNumber +} + describe('getCycleDay', () => { it('works for a simple example', () => { const bleedingDays = [{ @@ -23,7 +27,7 @@ describe('getCycleDay', () => { value: 2 } }] - const getCycleDayNumber = cycleModule({ bleedingDaysSortedByDate: bleedingDays }).getCycleDayNumber + const getCycleDayNumber = useBleedingDays(bleedingDays) const targetDate = '2018-05-17' const result = getCycleDayNumber(targetDate) expect(result).to.eql(9) @@ -49,7 +53,7 @@ describe('getCycleDay', () => { } }] const targetDate = '2018-05-17' - const getCycleDayNumber = cycleModule({ bleedingDaysSortedByDate: bleedingDays }).getCycleDayNumber + const getCycleDayNumber = useBleedingDays(bleedingDays) const result = getCycleDayNumber(targetDate) expect(result).to.eql(15) }) @@ -73,7 +77,7 @@ describe('getCycleDay', () => { }] const targetDate = '2018-04-27' - const getCycleDayNumber = cycleModule({ bleedingDaysSortedByDate: bleedingDays }).getCycleDayNumber + const getCycleDayNumber = useBleedingDays(bleedingDays) const result = getCycleDayNumber(targetDate) expect(result).to.eql(18) }) @@ -87,7 +91,7 @@ describe('getCycleDay', () => { }] const targetDate = '2018-05-13' - const getCycleDayNumber = cycleModule({ bleedingDaysSortedByDate: bleedingDays }).getCycleDayNumber + const getCycleDayNumber = useBleedingDays(bleedingDays) const result = getCycleDayNumber(targetDate) expect(result).to.eql(1) }) @@ -96,7 +100,7 @@ describe('getCycleDay', () => { it('if there are no bleeding days', function () { const bleedingDays = [] const targetDate = '2018-05-17' - const getCycleDayNumber = cycleModule({ bleedingDaysSortedByDate: bleedingDays }).getCycleDayNumber + const getCycleDayNumber = useBleedingDays(bleedingDays) const result = getCycleDayNumber(targetDate) expect(result).to.be.null() }) @@ -119,7 +123,10 @@ describe('getCycleDay', () => { }] const targetDate = '2018-05-17' - const getCycleDayNumber = cycleModule({ bleedingDaysSortedByDate: bleedingDays, maxBreakInBleeding }).getCycleDayNumber + const getCycleDayNumber = cycleModule({ + bleedingDaysSortedByDate: bleedingDays, + maxBreakInBleeding + }).getCycleDayNumber const result = getCycleDayNumber(targetDate) expect(result).to.eql(8) }) @@ -137,7 +144,10 @@ describe('getCycleDay', () => { } }] const targetDate = '2018-05-17' - const getCycleDayNumber = cycleModule({ bleedingDaysSortedByDate: bleedingDays, maxBreakInBleeding }).getCycleDayNumber + const getCycleDayNumber = cycleModule({ + bleedingDaysSortedByDate: bleedingDays, + maxBreakInBleeding + }).getCycleDayNumber const result = getCycleDayNumber(targetDate) expect(result).to.eql(4) }) -- GitLab