feat: move the calculation of the current salutation to a different function
This commit is contained in:
34
src/helpers/hourToSalutation.test.ts
Normal file
34
src/helpers/hourToSalutation.test.ts
Normal 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')
|
||||
})
|
||||
})
|
||||
|
||||
|
21
src/helpers/hourToSalutation.ts
Normal file
21
src/helpers/hourToSalutation.ts
Normal 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'
|
||||
}
|
Reference in New Issue
Block a user