extract two methods from main file

This commit is contained in:
Daniel Waxweiler 2021-04-13 21:10:28 +02:00
parent cf49fb47d6
commit 72f2c12343
3 changed files with 65 additions and 21 deletions

View File

@ -1,4 +1,4 @@
import DateTimeWrapper from './date-time-wrapper' import Formatter from './formatter'
import * as GraphqlWrapper from './graphql-wrapper' import * as GraphqlWrapper from './graphql-wrapper'
import { createAnchorElement } from './html-creator' import { createAnchorElement } from './html-creator'
@ -17,29 +17,15 @@ function displayEvents(data, list) {
const br = document.createElement('br') const br = document.createElement('br')
li.appendChild(br) li.appendChild(br)
const beginsOn = new DateTimeWrapper(events[i].beginsOn) const date = Formatter.formatDate({ start: events[i].beginsOn, end: events[i].endsOn })
const endsOn = new DateTimeWrapper(events[i].endsOn) const textnode = document.createTextNode(date)
let dateText = beginsOn.getShortDate()
dateText += ' ' + beginsOn.get24Time()
dateText += ' - '
if (!beginsOn.equalsDate(endsOn)) {
dateText += endsOn.getShortDate() + ' '
}
dateText += endsOn.get24Time()
const textnode = document.createTextNode(dateText)
li.appendChild(textnode) li.appendChild(textnode)
if (events[i].physicalAddress) { if (events[i].physicalAddress) {
let location = '' const location = Formatter.formatLocation({
if (events[i].physicalAddress.description) { description: events[i].physicalAddress.description,
location += events[i].physicalAddress.description locality: events[i].physicalAddress.locality
} })
if (location && events[i].physicalAddress.locality) {
location += ', '
}
if (events[i].physicalAddress.locality) {
location += events[i].physicalAddress.locality
}
if (location) { if (location) {
const brBeforeLocation = document.createElement('br') const brBeforeLocation = document.createElement('br')
li.appendChild(brBeforeLocation) li.appendChild(brBeforeLocation)

View File

@ -0,0 +1,27 @@
import test from 'ava'
import Formatter from './formatter'
test('#formatDate one date', t => {
const date = Formatter.formatDate({ start: '2021-04-15T10:30:00Z', end: '2021-04-15T15:30:00Z' })
t.is(date, '15/04/2021 12:30 - 17:30')
})
test('#formatDate two dates', t => {
const date = Formatter.formatDate({ start: '2021-04-15T10:30:00Z', end: '2021-04-16T15:30:00Z' })
t.is(date, '15/04/2021 12:30 - 16/04/2021 17:30')
})
test('#formatLocation both parameters', t => {
const date = Formatter.formatLocation({ description: 'a', locality: 'b' })
t.is(date, 'a, b')
})
test('#formatLocation description only', t => {
const date = Formatter.formatLocation({ description: 'a' })
t.is(date, 'a')
})
test('#formatLocation locality only', t => {
const date = Formatter.formatLocation({ locality: 'a' })
t.is(date, 'a')
})

View File

@ -0,0 +1,31 @@
import DateTimeWrapper from './date-time-wrapper'
export default class Formatter {
static formatDate({ start, end }) {
const startDateTime = new DateTimeWrapper(start)
const endDateTime = new DateTimeWrapper(end)
let dateText = startDateTime.getShortDate()
dateText += ' ' + startDateTime.get24Time()
dateText += ' - '
if (!startDateTime.equalsDate(endDateTime)) {
dateText += endDateTime.getShortDate() + ' '
}
dateText += endDateTime.get24Time()
return dateText
}
static formatLocation({ description, locality }) {
let location = ''
if (description) {
location += description
}
if (location && locality) {
location += ', '
}
if (locality) {
location += locality
}
return location
}
}