From 72f2c123431ce19332b86e0f2d65c12f4f6a6940 Mon Sep 17 00:00:00 2001 From: Daniel Waxweiler Date: Tue, 13 Apr 2021 21:10:28 +0200 Subject: [PATCH] extract two methods from main file --- .../front/events-loader.js | 28 +++++------------ .../front/formatter-test.js | 27 ++++++++++++++++ source/connector-mobilizon/front/formatter.js | 31 +++++++++++++++++++ 3 files changed, 65 insertions(+), 21 deletions(-) create mode 100644 source/connector-mobilizon/front/formatter-test.js create mode 100644 source/connector-mobilizon/front/formatter.js diff --git a/source/connector-mobilizon/front/events-loader.js b/source/connector-mobilizon/front/events-loader.js index 7fdb0b4..d2cf881 100644 --- a/source/connector-mobilizon/front/events-loader.js +++ b/source/connector-mobilizon/front/events-loader.js @@ -1,4 +1,4 @@ -import DateTimeWrapper from './date-time-wrapper' +import Formatter from './formatter' import * as GraphqlWrapper from './graphql-wrapper' import { createAnchorElement } from './html-creator' @@ -17,29 +17,15 @@ function displayEvents(data, list) { const br = document.createElement('br') li.appendChild(br) - const beginsOn = new DateTimeWrapper(events[i].beginsOn) - const endsOn = new DateTimeWrapper(events[i].endsOn) - let dateText = beginsOn.getShortDate() - dateText += ' ' + beginsOn.get24Time() - dateText += ' - ' - if (!beginsOn.equalsDate(endsOn)) { - dateText += endsOn.getShortDate() + ' ' - } - dateText += endsOn.get24Time() - const textnode = document.createTextNode(dateText) + const date = Formatter.formatDate({ start: events[i].beginsOn, end: events[i].endsOn }) + const textnode = document.createTextNode(date) li.appendChild(textnode) if (events[i].physicalAddress) { - let location = '' - if (events[i].physicalAddress.description) { - location += events[i].physicalAddress.description - } - if (location && events[i].physicalAddress.locality) { - location += ', ' - } - if (events[i].physicalAddress.locality) { - location += events[i].physicalAddress.locality - } + const location = Formatter.formatLocation({ + description: events[i].physicalAddress.description, + locality: events[i].physicalAddress.locality + }) if (location) { const brBeforeLocation = document.createElement('br') li.appendChild(brBeforeLocation) diff --git a/source/connector-mobilizon/front/formatter-test.js b/source/connector-mobilizon/front/formatter-test.js new file mode 100644 index 0000000..a4f2acd --- /dev/null +++ b/source/connector-mobilizon/front/formatter-test.js @@ -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') +}) diff --git a/source/connector-mobilizon/front/formatter.js b/source/connector-mobilizon/front/formatter.js new file mode 100644 index 0000000..d2306c5 --- /dev/null +++ b/source/connector-mobilizon/front/formatter.js @@ -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 + } +}