Skip to content

Commit

Permalink
One shot query
Browse files Browse the repository at this point in the history
Signed-off-by: Yarden Shoham <hrsi88@gmail.com>
  • Loading branch information
yardenshoham committed Oct 15, 2022
1 parent f354132 commit 184ea65
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions web_src/js/features/formatting.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,22 @@ export function initFormattingReplacements() {
}

function formatAllTimeElements() {
const formats = ['date', 'short-date', 'date-time'];
for (const f of formats) {
formatTimeElements(f);
const allTimeElements = document.querySelectorAll('time[data-format]');
for (const timeElement of allTimeElements) {
const formatter = getFormatter(timeElement.dataset.format);
timeElement.textContent = formatter.format(new Date(timeElement.dateTime));
}
}

function formatTimeElements(format) {
let formatter;
function getFormatter(format) {
switch (format) {
case 'date':
formatter = dateFormatter;
break;
return dateFormatter;
case 'short-date':
formatter = shortDateFormatter;
break;
return shortDateFormatter;
case 'date-time':
formatter = dateTimeFormatter;
break;
return dateTimeFormatter;
default:
throw new Error('Unknown format');
}
for (const timeElement of document.querySelectorAll(`time[data-format="${format}"]`)) {
timeElement.textContent = formatter.format(new Date(timeElement.dateTime));
}
}

0 comments on commit 184ea65

Please sign in to comment.