1
0

feat: move the calculation of the current salutation to a different function

This commit is contained in:
kolaente
2021-12-26 18:06:33 +01:00
committed by Gitea
parent dd450263fb
commit de77393905
4 changed files with 57 additions and 58 deletions

View File

@ -0,0 +1,34 @@
import {describe, it, expect} from 'vitest'
import {hourToSalutation} from './hourToSalutation'
const dateWithHour = (hours: number): Date => {
const date = new Date()
date.setHours(hours)
return date
}
describe('Salutation', () => {
it('shows the right salutation in the night', () => {
const salutation = hourToSalutation(dateWithHour(4))
expect(salutation).toBe('Night')
})
it('shows the right salutation in the morning', () => {
const salutation = hourToSalutation(dateWithHour(8))
expect(salutation).toBe('Morning')
})
it('shows the right salutation in the day', () => {
const salutation = hourToSalutation(dateWithHour(13))
expect(salutation).toBe('Day')
})
it('shows the right salutation in the night', () => {
const salutation = hourToSalutation(dateWithHour(20))
expect(salutation).toBe('Evening')
})
it('shows the right salutation in the night again', () => {
const salutation = hourToSalutation(dateWithHour(23))
expect(salutation).toBe('Night')
})
})

View File

@ -0,0 +1,21 @@
export function hourToSalutation(now: Date = new Date()): String {
const hours = new Date(now).getHours()
if (hours < 5) {
return 'Night'
}
if (hours < 11) {
return 'Morning'
}
if (hours < 18) {
return 'Day'
}
if (hours < 23) {
return 'Evening'
}
return 'Night'
}