Move day interval calculation to separate file and test it
This commit is contained in:
parent
160c14de2e
commit
7d312687db
|
@ -5,7 +5,8 @@
|
|||
"scripts": {
|
||||
"serve": "vue-cli-service serve",
|
||||
"build": "vue-cli-service build --modern",
|
||||
"lint": "vue-cli-service lint"
|
||||
"lint": "vue-cli-service lint",
|
||||
"test": "jest"
|
||||
},
|
||||
"dependencies": {
|
||||
"bulma": "0.9.1",
|
||||
|
@ -40,6 +41,7 @@
|
|||
"babel-eslint": "10.1.0",
|
||||
"eslint": "7.14.0",
|
||||
"eslint-plugin-vue": "7.1.0",
|
||||
"jest": "^26.6.3",
|
||||
"node-sass": "5.0.0",
|
||||
"sass-loader": "10.1.0",
|
||||
"vue-flatpickr-component": "8.1.6",
|
||||
|
|
|
@ -101,6 +101,8 @@
|
|||
import flatPickr from 'vue-flatpickr-component'
|
||||
import 'flatpickr/dist/flatpickr.css'
|
||||
|
||||
import calculateDayInterval from '@/helpers/calculateDayInterval'
|
||||
|
||||
export default {
|
||||
name: 'datepicker',
|
||||
data() {
|
||||
|
@ -151,29 +153,7 @@ export default {
|
|||
console.log(date)
|
||||
},
|
||||
getDayIntervalFromString(date) {
|
||||
|
||||
const currentDay = (new Date()).getDay()
|
||||
|
||||
switch(date) {
|
||||
case 'today':
|
||||
return 0
|
||||
case 'tomorrow':
|
||||
return 1
|
||||
case 'nextMonday':
|
||||
// Monday is 1, so we calculate the distance to the next 1
|
||||
return (7 + currentDay - 1) % 7
|
||||
case 'thisWeekend':
|
||||
// Saturday is 6 so we calculate the distance to the next 6
|
||||
return (7 + currentDay - 6) % 7
|
||||
case 'laterThisWeek':
|
||||
return 2
|
||||
case 'laterNextWeek':
|
||||
return 0
|
||||
case 'nextWeek':
|
||||
return 7
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
return calculateDayInterval(date)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
|
||||
export default function calculateDayInterval(date, currentDay = (new Date().getDay())) {
|
||||
switch(date) {
|
||||
case 'today':
|
||||
return 0
|
||||
case 'tomorrow':
|
||||
return 1
|
||||
case 'nextMonday':
|
||||
// Monday is 1, so we calculate the distance to the next 1
|
||||
return (currentDay + (8 - currentDay * 2)) % 7
|
||||
case 'thisWeekend':
|
||||
// Saturday is 6 so we calculate the distance to the next 6
|
||||
return (6 - currentDay) % 6
|
||||
case 'laterThisWeek':
|
||||
if(currentDay === 5 || currentDay === 6 || currentDay === 0) {
|
||||
return 0
|
||||
}
|
||||
|
||||
return 2
|
||||
case 'laterNextWeek':
|
||||
return calculateDayInterval('laterThisWeek', currentDay) + 7
|
||||
case 'nextWeek':
|
||||
return 7
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
import calculateDayInterval from './calculateDayInterval'
|
||||
|
||||
const days = {
|
||||
monday: 1,
|
||||
tuesday: 2,
|
||||
wednesday: 3,
|
||||
thursday: 4,
|
||||
friday: 5,
|
||||
saturday: 6,
|
||||
sunday: 0,
|
||||
}
|
||||
|
||||
for (const n in days) {
|
||||
test(`today on a ${n}`, () => {
|
||||
expect(calculateDayInterval('today', days[n])).toBe(0)
|
||||
})
|
||||
}
|
||||
|
||||
for (const n in days) {
|
||||
test(`tomorrow on a ${n}`, () => {
|
||||
expect(calculateDayInterval('tomorrow', days[n])).toBe(1)
|
||||
})
|
||||
}
|
||||
|
||||
const nextMonday = {
|
||||
monday: 0,
|
||||
tuesday: 6,
|
||||
wednesday: 5,
|
||||
thursday: 4,
|
||||
friday: 3,
|
||||
saturday: 2,
|
||||
sunday: 1,
|
||||
}
|
||||
|
||||
for (const n in nextMonday) {
|
||||
test(`next monday on a ${n}`, () => {
|
||||
expect(calculateDayInterval('nextMonday', days[n])).toBe(nextMonday[n])
|
||||
})
|
||||
}
|
||||
|
||||
const thisWeekend = {
|
||||
monday: 5,
|
||||
tuesday: 4,
|
||||
wednesday: 3,
|
||||
thursday: 2,
|
||||
friday: 1,
|
||||
saturday: 0,
|
||||
sunday: 0,
|
||||
}
|
||||
|
||||
for (const n in thisWeekend) {
|
||||
test(`this weekend on a ${n}`, () => {
|
||||
expect(calculateDayInterval('thisWeekend', days[n])).toBe(thisWeekend[n])
|
||||
})
|
||||
}
|
||||
|
||||
const laterThisWeek = {
|
||||
monday: 2,
|
||||
tuesday: 2,
|
||||
wednesday: 2,
|
||||
thursday: 2,
|
||||
friday: 0,
|
||||
saturday: 0,
|
||||
sunday: 0,
|
||||
}
|
||||
|
||||
for (const n in laterThisWeek) {
|
||||
test(`later this week on a ${n}`, () => {
|
||||
expect(calculateDayInterval('laterThisWeek', days[n])).toBe(laterThisWeek[n])
|
||||
})
|
||||
}
|
||||
|
||||
const laterNextWeek = {
|
||||
monday: 7 + 2,
|
||||
tuesday: 7 + 2,
|
||||
wednesday: 7 + 2,
|
||||
thursday: 7 + 2,
|
||||
friday: 7 + 0,
|
||||
saturday: 7 + 0,
|
||||
sunday: 7 + 0,
|
||||
}
|
||||
|
||||
for (const n in laterNextWeek) {
|
||||
test(`later next week on a ${n} (this week)`, () => {
|
||||
expect(calculateDayInterval('laterNextWeek', days[n])).toBe(laterNextWeek[n])
|
||||
})
|
||||
}
|
||||
|
||||
for (const n in days) {
|
||||
test(`next week on a ${n}`, () => {
|
||||
expect(calculateDayInterval('nextWeek', days[n])).toBe(7)
|
||||
})
|
||||
}
|
Reference in New Issue