Skip to content

Commit

Permalink
remove duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
robertparkinson committed Oct 21, 2024
1 parent 8158194 commit 944a669
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 355 deletions.
116 changes: 0 additions & 116 deletions app/lib/dates.lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* @module DatesLib
*/

const { returnCycleDates } = require('./static-lookups.lib.js')

const february = 2
const lastDayOfFebruary = 28
const lastDayOfFebruaryLeapYear = 29
Expand Down Expand Up @@ -47,114 +45,6 @@ function formatDateObjectToISO (date) {
return date.toISOString().split('T')[0]
}

/**
* Get the due date of next provided cycle, either summer or winter and all year, formatted as YYYY-MM-DD
*
* @param {boolean} summer - true for summer, false for winter and all year.
* @returns {string} - the due date of the next cycle as an ISO string.
*/
function cycleDueDateAsISO (summer) {
return formatDateObjectToISO(cycleDueDate(summer))
}

/**
* Get the due date of next provided cycle, either summer or winter and all year
*
* @param {boolean} summer - true for summer, false for winter and all year.
* @returns {Date} - the due date of the next cycle.
*/
function cycleDueDate (summer) {
const today = new Date()
const year = today.getFullYear()
const month = today.getMonth()

if (summer) {
if (month > returnCycleDates.summer.endDate.month) {
return new Date(year + 1, returnCycleDates.summer.dueDate.month, returnCycleDates.summer.dueDate.day)
}

return new Date(year, returnCycleDates.summer.dueDate.month, returnCycleDates.summer.dueDate.day)
}

if (month > returnCycleDates.allYear.endDate.month) {
return new Date(year + 1, returnCycleDates.allYear.dueDate.month, returnCycleDates.allYear.dueDate.day)
}

return new Date(year, returnCycleDates.allYear.dueDate.month, returnCycleDates.allYear.dueDate.day)
}

/**
* Get the end date of next provided cycle, either summer or winter and all year, formatted as YYYY-MM-DD
*
* @param {boolean} summer - true for summer, false for winter and all year.
* @returns {string} - the end date of the next cycle as an ISO string.
*/
function cycleEndDateAsISO (summer) {
return formatDateObjectToISO(cycleEndDate(summer))
}

/**
* Get the end date of next provided cycle, either summer and winter or all year
*
* @param {boolean} summer - true for summer, false for winter and all year.
* @returns {Date} - the end date of the next cycle.
*/
function cycleEndDate (summer) {
const today = new Date()
const year = today.getFullYear()
const month = today.getMonth()

if (summer) {
if (month > returnCycleDates.summer.endDate.month) {
return new Date(year + 1, returnCycleDates.summer.endDate.month, returnCycleDates.summer.endDate.day)
}

return new Date(year, returnCycleDates.summer.endDate.month, returnCycleDates.summer.endDate.day)
}

if (month > returnCycleDates.allYear.endDate.month) {
return new Date(year + 1, returnCycleDates.allYear.endDate.month, returnCycleDates.allYear.endDate.day)
}

return new Date(year, returnCycleDates.allYear.endDate.month, returnCycleDates.allYear.endDate.day)
}

/**
* Get the start date of next provided cycle, either summer and winter and all year, formatted as YYYY-MM-DD
*
* @param {boolean} summer - true for summer, false for winter and all year.
* @returns {string} - the start date of the next cycle as an ISO string.
*/
function cycleStartDateAsISO (summer) {
return formatDateObjectToISO(cycleStartDate(summer))
}

/**
* Get the start date of next provided cycle, either summer or winter and all year
*
* @param {boolean} summer - true for summer, false for winter and all year.
* @returns {Date} - the start date of the next cycle.
*/
function cycleStartDate (summer) {
const today = new Date()
const year = today.getFullYear()
const month = today.getMonth()

if (summer) {
if (month < returnCycleDates.summer.startDate.month) {
return new Date(year - 1, returnCycleDates.summer.startDate.month, returnCycleDates.summer.startDate.day)
}

return new Date(year, returnCycleDates.summer.startDate.month, returnCycleDates.summer.startDate.day)
}

if (month < returnCycleDates.allYear.startDate.month) {
return new Date(year - 1, returnCycleDates.allYear.startDate.month, returnCycleDates.allYear.startDate.day)
}

return new Date(year, returnCycleDates.allYear.startDate.month, returnCycleDates.allYear.startDate.day)
}

/**
* Check if a date is valid or not by creating a date and checking it gives the time
*
Expand Down Expand Up @@ -226,12 +116,6 @@ function _isLeapYear (year) {
module.exports = {
formatDateObjectToISO,
formatStandardDateToISO,
cycleDueDate,
cycleDueDateAsISO,
cycleEndDate,
cycleEndDateAsISO,
cycleStartDate,
cycleStartDateAsISO,
isISODateFormat,
isValidDate
}
240 changes: 1 addition & 239 deletions test/lib/dates.lib.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,13 @@
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')

const { describe, it, beforeEach } = exports.lab = Lab.script()
const { describe, it } = exports.lab = Lab.script()
const { expect } = Code

const { returnCycleDates } = require('../../app/lib/static-lookups.lib.js')

// Thing under test
const DateLib = require('../../app/lib/dates.lib.js')

describe('Dates lib', () => {
const today = new Date()
const month = today.getMonth()

let expectedDate
let summer

describe('formatStandardDateToISO', () => {
it('returns null if the date is null ', () => {
const result = DateLib.formatStandardDateToISO(null)
Expand Down Expand Up @@ -94,234 +86,4 @@ describe('Dates lib', () => {
expect(result).to.be.true()
})
})

describe('cycleDueDate', () => {
describe('when the requested cycle is "summer"', () => {
beforeEach(() => {
summer = true

if (month > returnCycleDates.summer.endDate.month) {
expectedDate = new Date(new Date().getFullYear() + 1, 10, 28)
} else {
expectedDate = new Date(new Date().getFullYear(), 10, 28)
}
})

it('should return the due date for the current summer cycle', () => {
const result = DateLib.cycleDueDate(summer)

expect(result).to.equal(expectedDate)
})
})

describe('when the requested cycle is "winter and all year"', () => {
beforeEach(() => {
summer = false

if (month > returnCycleDates.allYear.endDate.month) {
expectedDate = new Date(new Date().getFullYear() + 1, 3, 28)
} else {
expectedDate = new Date(new Date().getFullYear(), 3, 28)
}
})

it('should return the due date of the current winter and all year cycle', () => {
const result = DateLib.cycleDueDate(summer)

expect(result).to.equal(expectedDate)
})
})
})

describe('cycleDueDateAsISO', () => {
describe('when the requested cycle is "summer"', () => {
beforeEach(() => {
summer = true

if (month > returnCycleDates.summer.endDate.month) {
expectedDate = new Date(new Date().getFullYear() + 1, 10, 28).toISOString().split('T')[0]
} else {
expectedDate = new Date(new Date().getFullYear(), 10, 28).toISOString().split('T')[0]
}
})

it('should return the due date for the current summer cycle formatted as YYYY-MM-DD', () => {
const result = DateLib.cycleDueDateAsISO(summer)

expect(result).to.equal(expectedDate)
})
})

describe('when the requested cycle is "winter and all year"', () => {
beforeEach(() => {
summer = false

if (month > returnCycleDates.allYear.endDate.month) {
expectedDate = new Date(new Date().getFullYear() + 1, 3, 28).toISOString().split('T')[0]
} else {
expectedDate = new Date(new Date().getFullYear(), 3, 28).toISOString().split('T')[0]
}
})

it('should return the due date of the current winter and all year cycle formatted as YYYY-MM-DD', () => {
const result = DateLib.cycleDueDateAsISO(summer)

expect(result).to.equal(expectedDate)
})
})
})

describe('cycleEndDate', () => {
describe('when the requested cycle is "summer"', () => {
beforeEach(() => {
summer = true

if (month > returnCycleDates.summer.endDate.month) {
expectedDate = new Date(new Date().getFullYear() + 1, 9, 31)
} else {
expectedDate = new Date(new Date().getFullYear(), 9, 31)
}
})

it('should return the due date for the current summer cycle', () => {
const result = DateLib.cycleEndDate(summer)

expect(result).to.equal(expectedDate)
})
})

describe('when the requested cycle is "winter and all year"', () => {
beforeEach(() => {
summer = false

if (month > returnCycleDates.allYear.endDate.month) {
expectedDate = new Date(new Date().getFullYear() + 1, 2, 31)
} else {
expectedDate = new Date(new Date().getFullYear(), 2, 31)
}
})

it('should return the due date of the current winter and all year cycle', () => {
const result = DateLib.cycleEndDate(summer)

expect(result).to.equal(expectedDate)
})
})
})

describe('cycleEndDateAsISO', () => {
describe('when the requested cycle is "summer"', () => {
beforeEach(() => {
summer = true

if (month > returnCycleDates.summer.endDate.month) {
expectedDate = new Date(new Date().getFullYear() + 1, 9, 31).toISOString().split('T')[0]
} else {
expectedDate = new Date(new Date().getFullYear(), 9, 31).toISOString().split('T')[0]
}
})

it('should return the due date for the current summer cycle formatted as YYYY-MM-DD', () => {
const result = DateLib.cycleEndDateAsISO(summer)

expect(result).to.equal(expectedDate)
})
})

describe('when the requested cycle is "winter and all year"', () => {
beforeEach(() => {
summer = false

if (month > returnCycleDates.allYear.endDate.month) {
expectedDate = new Date(new Date().getFullYear() + 1, 2, 31).toISOString().split('T')[0]
} else {
expectedDate = new Date(new Date().getFullYear(), 2, 31).toISOString().split('T')[0]
}
})

it('should return the due date of the current winter and all year cycle formatted as YYYY-MM-DD', () => {
const result = DateLib.cycleEndDateAsISO(summer)

expect(result).to.equal(expectedDate)
})
})
})

describe('cycleStartDate', () => {
describe('when the requested cycle is "summer"', () => {
beforeEach(() => {
summer = true

if (month < returnCycleDates.summer.startDate.month) {
expectedDate = new Date(new Date().getFullYear() - 1, 10, 1)
} else {
expectedDate = new Date(new Date().getFullYear(), 10, 1)
}
})

it('should return the start date for the current summer cycle', () => {
const result = DateLib.cycleStartDate(summer)

expect(result).to.equal(expectedDate)
})
})

describe('when the requested cycle is "winter and all year"', () => {
beforeEach(() => {
summer = false

if (month < returnCycleDates.allYear.startDate.month) {
expectedDate = new Date(new Date().getFullYear() - 1, 3, 1)
} else {
expectedDate = new Date(new Date().getFullYear(), 3, 1)
}
})

it('should return the due date of the current winter and all year cycle', () => {
const result = DateLib.cycleStartDate(summer)

expect(result).to.equal(expectedDate)
})
})
})

describe('cycleStartDateAsISO', () => {
describe('when the requested cycle is "summer"', () => {
beforeEach(() => {
summer = true

if (month < returnCycleDates.summer.startDate.month) {
expectedDate = new Date(new Date().getFullYear() - 1, 10, 1).toISOString().split('T')[0]
} else {
expectedDate = new Date(new Date().getFullYear(), 10, 1).toISOString().split('T')[0]
}
})

it('should return the start date for the current summer cycle formatted as YYYY-MM-DD', () => {
const result = DateLib.cycleStartDateAsISO(summer)

expect(result).to.equal(expectedDate)
})
})

describe('when the requested cycle is "winter and all year"', () => {
beforeEach(() => {
summer = false

if (month < returnCycleDates.allYear.startDate.month) {
expectedDate = new Date(new Date().getFullYear() - 1, 3, 1).toISOString().split('T')[0]
} else {
expectedDate = new Date(new Date().getFullYear(), 3, 1).toISOString().split('T')[0]
}

expectedDate = new Date(new Date().getFullYear(), 3, 1).toISOString().split('T')[0]
})

it('should return the due date of the current winter and all year cycle formatted as YYYY-MM-DD', () => {
const result = DateLib.cycleStartDateAsISO(summer)

expect(result).to.equal(expectedDate)
})
})
})
})

0 comments on commit 944a669

Please sign in to comment.