From 39b023bbda3bbabb39ba499ce6a30993ded8526a Mon Sep 17 00:00:00 2001 From: Chris Pymm Date: Thu, 23 Jan 2025 12:23:45 +0000 Subject: [PATCH] refactor(date picker): refactor setExcludedDates for readability and to appease eslint The large map function in the setExcldedDates method was hard to read, and eslint didn't like it didn't have an obvious return value. This commit refactors part of the map into a parseDateRangeString function making the code much more readable and control flow more obvious. --- src/moj/components/date-picker/date-picker.js | 49 +++++++++++-------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/src/moj/components/date-picker/date-picker.js b/src/moj/components/date-picker/date-picker.js index 28e9941f2..1711661c5 100644 --- a/src/moj/components/date-picker/date-picker.js +++ b/src/moj/components/date-picker/date-picker.js @@ -361,29 +361,38 @@ Datepicker.prototype.setExcludedDates = function () { .replace(/\s+/, " ") .split(" ") .map((item) => { - if (item.includes("-")) { - // parse the date range from the format "dd/mm/yyyy-dd/mm/yyyy" - const [startDate, endDate] = item - .split("-") - .map((d) => this.formattedDateFromString(d, null)); - if (startDate && endDate) { - const date = new Date(startDate.getTime()); - const dates = []; - while (date <= endDate) { - dates.push(new Date(date)); - date.setDate(date.getDate() + 1); - } - return dates; - } - } else { - return this.formattedDateFromString(item, null); - } + return item.includes("-") + ? this.parseDateRangeString(item) + : this.formattedDateFromString(item); }) .flat() .filter((item) => item); } }; +/* + * Parses a daterange string into an array of dates + * @param {String} datestring - A daterange string in the format "dd/mm/yyyy-dd/mm/yyyy" + * @returns {Date[]} + */ +Datepicker.prototype.parseDateRangeString = function (datestring) { + const dates = []; + const [startDate, endDate] = datestring + .split("-") + .map((d) => this.formattedDateFromString(d, null)); + + if (startDate && endDate) { + const date = new Date(startDate.getTime()); + /* eslint-disable no-unmodified-loop-condition */ + while (date <= endDate) { + dates.push(new Date(date)); + date.setDate(date.getDate() + 1); + } + /* eslint-enable no-unmodified-loop-condition */ + } + return dates; +}; + Datepicker.prototype.setExcludedDays = function () { if (this.config.excludedDays) { // lowercase and arrange dayLabels to put indexOf sunday == 0 for comparison @@ -788,7 +797,7 @@ Datepicker.prototype.focusPreviousYear = function (event, focus = true) { Datepicker.prototype.parseDataset = function (schema, dataset) { const parsed = {}; - for (const [field, attributes] of Object.entries(schema.properties)) { + for (const [field, ,] of Object.entries(schema.properties)) { if (field in dataset) { parsed[field] = dataset[field]; } @@ -860,12 +869,12 @@ DSCalendarDay.prototype.init = function () { * @param {boolean} disabled - is the day selectable or excluded */ DSCalendarDay.prototype.update = function (day, hidden, disabled) { - let label = day.getDate(); + const label = day.getDate(); let accessibleLabel = this.picker.formattedDateHuman(day); if (disabled) { this.button.setAttribute("aria-disabled", true); - accessibleLabel = "Excluded date, " + accessibleLabel; + accessibleLabel = `Excluded date, ${accessibleLabel}`; } else { this.button.removeAttribute("aria-disabled"); }