Skip to content

Commit e9c9d03

Browse files
Add documentation for utils and filters (#264)
1 parent 6abc03c commit e9c9d03

19 files changed

Lines changed: 18988 additions & 183 deletions

app/filters/form-attributes.js

Lines changed: 0 additions & 121 deletions
This file was deleted.

app/filters/formatting.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ const formatAnswer = (value, options = {}) => {
3838
return yesValue ? `${yesPrefix} - ${yesValue}` : yesPrefix
3939
}
4040

41-
// Convert an integer to its ordinal name (first, second, third, etc)
41+
/**
42+
* Convert a 1-based integer to its ordinal name
43+
*
44+
* @param {number} integer - Integer between 1 and 10
45+
* @returns {string} Ordinal name
46+
* @example
47+
* getOrdinalName(1) // 'first'
48+
* getOrdinalName(3) // 'third'
49+
*/
4250
const getOrdinalName = (integer) =>
4351
{
4452
const ordinals = [
@@ -66,7 +74,15 @@ const getOrdinalName = (integer) =>
6674
return ordinals[parsedInteger]
6775
}
6876

69-
// Convert a zero-based index to its ordinal name (0 => first, 1 => second, etc)
77+
/**
78+
* Convert a 0-based index to its ordinal name (0 → 'first', 1 → 'second', etc)
79+
*
80+
* @param {number} integer - 0-based index
81+
* @returns {string} Ordinal name
82+
* @example
83+
* getOrdinalNameIndex0(0) // 'first'
84+
* getOrdinalNameIndex0(2) // 'third'
85+
*/
7086
const getOrdinalNameIndex0 = (integer) => getOrdinalName(Number(integer) + 1)
7187

7288
module.exports = {

app/filters/nunjucks.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
const { safe: nunjucksSafe } = require('nunjucks/src/filters')
44

5+
/**
6+
* Render a value to the browser console via an inline script tag (for template debugging)
7+
*
8+
* @param {*} a - Value to log
9+
* @param {string | null} [description] - Optional label shown before the value
10+
* @returns {string} Safe HTML containing an inline script
11+
*/
512
const log = (a, description = null) => {
613
if (description) {
714
description = `console.log("${description}:");`
@@ -127,8 +134,9 @@ const getUsername = function (userId, options = {}) {
127134
}
128135

129136
/**
137+
* Return the full Nunjucks template context — useful for debugging
130138
*
131-
* @returns {object} The context data
139+
* @returns {object} The current Nunjucks context object
132140
*/
133141
const getContext = function () {
134142
return this.ctx

app/lib/utils/arrays.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ const findById = (array, id) => {
1414
return array.find((item) => item.id === id)
1515
}
1616

17+
/**
18+
* Append an item to an array, returning a new array (deep clones the item)
19+
*
20+
* @param {Array} array - Array to append to
21+
* @param {*} item - Item to append (will be deep cloned)
22+
* @returns {Array} New array with item appended
23+
*/
1724
const push = (array, item) => {
1825
const newArray = [...array]
1926
newArray.push(_.cloneDeep(item)) // clone needed to stop this mutating original

app/lib/utils/clinics.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const config = require('../../config')
88
* Get today's clinics
99
*
1010
* @param {Array} clinics - Array of all clinics
11+
* @returns {Array} Clinics scheduled for today
1112
*/
1213
const getTodaysClinics = (clinics) => {
1314
const today = dayjs().startOf('day')
@@ -19,6 +20,7 @@ const getTodaysClinics = (clinics) => {
1920
*
2021
* @param {Array} events - Array of all events
2122
* @param {string} clinicId - Clinic ID to filter by
23+
* @returns {Array} Events belonging to the given clinic
2224
*/
2325
const getClinicEvents = (events, clinicId) => {
2426
if (!events || !clinicId) return []
@@ -31,6 +33,7 @@ const getClinicEvents = (events, clinicId) => {
3133
* Format clinic time slot
3234
*
3335
* @param {string} dateTime - ISO date string
36+
* @returns {string} Time formatted as HH:MM
3437
*/
3538
const formatTimeSlot = (dateTime) => {
3639
const date = new Date(dateTime)
@@ -74,7 +77,8 @@ const getClinicHours = (clinic) => {
7477
* Get clinics filtered by time period
7578
*
7679
* @param {Array} clinics - Array of all clinics
77-
* @param {string} filter - Filter to apply (today, upcoming, completed, all)
80+
* @param {string} [filter='all'] - Filter to apply: 'today', 'upcoming', 'completed', or 'all'; excludes clinics older than 2 weeks
81+
* @returns {Array} Filtered and sorted clinics
7882
*/
7983
const getFilteredClinics = (clinics, filter = 'all') => {
8084
const today = dayjs().startOf('day')

app/lib/utils/dates.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ const isValidDate = (dateInput) => {
146146
* Format a date in UK format
147147
*
148148
* @param {string | Array | object} dateString - ISO date string, array [day, month, year], [month, year], or object {day, month, year}, {month, year}
149-
* @param {string} format - Optional format string
149+
* @param {string} [format='D MMMM YYYY'] - Optional format string
150+
* @returns {string} Formatted date string, or empty string if invalid
150151
*/
151152
const formatDate = (dateString, format = 'D MMMM YYYY') => {
152153
if (!dateString) return ''
@@ -262,7 +263,8 @@ const formatMonthYear = (input, format = 'MMMM YYYY') => {
262263
* Format a time in UK format
263264
*
264265
* @param {string} dateString - ISO date string
265-
* @param {string} format - Optional format string
266+
* @param {string} [format='H:mm'] - Optional format string
267+
* @returns {string} Formatted time string
266268
*/
267269
const formatTime = (dateString, format = 'H:mm') => {
268270
if (!dateString) return ''
@@ -312,7 +314,8 @@ const formatTimeRange = (times) => {
312314
* Format a date and time
313315
*
314316
* @param {string} dateString - ISO date string
315-
* @param {string} format - Optional format string
317+
* @param {string} [format='D MMMM YYYY, HH:mm'] - Optional format string
318+
* @returns {string} Formatted date-time string
316319
*/
317320
const formatDateTime = (dateString, format = 'D MMMM YYYY, HH:mm') => {
318321
if (!dateString) return ''
@@ -385,8 +388,12 @@ const formatRelativeDate = (dateInput, withoutSuffix = false) => {
385388

386389
/**
387390
* Format a year as relative to the current year
391+
*
388392
* @param {string|number|object} yearInput - Year as number, string, or object {year: 2024}
389-
* @returns {string} Relative year description (e.g., "this year", "last year", "3 years ago")
393+
* @returns {string} Relative year description
394+
* @example
395+
* relativeYear(2025) // 'last year' (if current year is 2026)
396+
* relativeYear(2020) // '6 years ago'
390397
*/
391398
const relativeYear = (yearInput) => {
392399
if (!yearInput) return ''
@@ -436,10 +443,14 @@ const relativeYear = (yearInput) => {
436443
}
437444

438445
/**
439-
* Calculate the number of days since a given date
440-
* @param {string | object | array } dateInput - Input date in one of ISO date string, keyed object, array of [day, month, year], [month, year], or {month, year}
446+
* Calculate the number of days since a given date (positive = past, negative = future)
447+
*
448+
* @param {string | object | Array} dateInput - ISO date string, array [day, month, year], or object {day, month, year}
441449
* @param {string | Dayjs | null} [compareDate] - Optional reference date (defaults to today)
442-
* @returns {number} Number of days since the date (positive integer for past dates)
450+
* @returns {number} Days since the date (positive for past dates)
451+
* @example
452+
* daysSince('2026-03-05') // 7 (if today is 2026-03-12)
453+
* daysSince('2026-03-15') // -3 (future date)
443454
*/
444455
const daysSince = (dateInput, compareDate = null) => {
445456
if (!dateInput) return 0
@@ -592,10 +603,11 @@ const now = () => {
592603
}
593604

594605
/**
595-
* Format a date range
606+
* Format a date range, collapsing shared day/month/year as appropriate
596607
*
597-
* @param {string} startDate - ISO date string
598-
* @param {string} endDate - ISO date string
608+
* @param {string} startDate - ISO date string for range start
609+
* @param {string} endDate - ISO date string for range end
610+
* @returns {string} Formatted range, e.g. '1 - 5 March 2026' or '1 March - 5 April 2026'
599611
*/
600612
const formatDateRange = (startDate, endDate) => {
601613
if (!startDate || !endDate) return ''

app/lib/utils/event-data.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ const getEvent = (data, eventId) => {
1212
return data.events.find((e) => e.id === eventId) || null
1313
}
1414

15+
/**
16+
* Get event data bundle for a given clinic and event ID
17+
*
18+
* @param {object} data - Session data
19+
* @param {string} clinicId - Clinic ID
20+
* @param {string} eventId - Event ID
21+
* @returns {object | null} Bundle of {clinic, event, participant, location, unit} or null if not found
22+
*/
1523
const getEventData = (data, clinicId, eventId) => {
1624
const clinic = data.clinics.find((c) => c.id === clinicId)
1725
if (!clinic) return null

app/lib/utils/participants.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ const getParticipant = (data, participantId) => {
1515
}
1616

1717
/**
18-
* Get full name of participant
18+
* Get full name (first, middle, last) of a participant as a Nunjucks-safe string
1919
*
2020
* @param {object} participant - Participant object
21+
* @returns {string} Full name, or empty string if unavailable
2122
*/
2223
const getFullName = (participant) => {
2324
if (!participant?.demographicInformation) return ''
@@ -28,9 +29,12 @@ const getFullName = (participant) => {
2829
}
2930

3031
/**
31-
* Get full name of participant
32+
* Get full name in reversed 'Last, First Middle' format
3233
*
3334
* @param {object} participant - Participant object
35+
* @returns {string} Reversed full name, or empty string if unavailable
36+
* @example
37+
* getFullNameReversed(participant) // 'Smith, Jane Louise'
3438
*/
3539
const getFullNameReversed = (participant) => {
3640
if (!participant?.demographicInformation) return ''
@@ -39,9 +43,10 @@ const getFullNameReversed = (participant) => {
3943
}
4044

4145
/**
42-
* Get short name (first + last) of participant
46+
* Get short name (first + last only) of participant as a Nunjucks-safe string
4347
*
4448
* @param {object} participant - Participant object
49+
* @returns {string} Short name, or empty string if unavailable
4550
*/
4651
const getShortName = (participant) => {
4752
if (!participant?.demographicInformation) return ''
@@ -54,6 +59,7 @@ const getShortName = (participant) => {
5459
*
5560
* @param {Array} participants - Array of all participants
5661
* @param {string} sxNumber - SX number to search for
62+
* @returns {object | undefined} Matching participant or undefined
5763
*/
5864
const findBySXNumber = (participants, sxNumber) => {
5965
return participants.find((p) => p.sxNumber === sxNumber)
@@ -157,12 +163,14 @@ const getParticipantClinicHistory = (data, participantId, options = {}) => {
157163
}
158164

159165
// Helper functions for common use cases
166+
/** Get the most recent historic clinic/event pair for a participant */
160167
const getParticipantMostRecentClinic = (data, participantId) =>
161168
getParticipantClinicHistory(data, participantId, {
162169
filter: 'historic',
163170
mostRecent: true
164171
})
165172

173+
/** Get the start time of the participant's most recent clinic, or false if none */
166174
const getParticipantMostRecentClinicDate = (data, participantId) => {
167175
const clinic = getParticipantClinicHistory(data, participantId, {
168176
filter: 'historic',
@@ -173,9 +181,11 @@ const getParticipantMostRecentClinicDate = (data, participantId) => {
173181
} else return false
174182
}
175183

184+
/** Get all past clinic/event pairs for a participant */
176185
const getParticipantHistoricClinics = (data, participantId) =>
177186
getParticipantClinicHistory(data, participantId, { filter: 'historic' })
178187

188+
/** Get all upcoming clinic/event pairs for a participant */
179189
const getParticipantUpcomingClinics = (data, participantId) =>
180190
getParticipantClinicHistory(data, participantId, { filter: 'upcoming' })
181191

0 commit comments

Comments
 (0)