Commit d4846a9d authored by Robert Knight's avatar Robert Knight

Make it clearer that `getBreakpoint` cannot return `null`

Reading the code it appeared that `getBreakpoint` could return `null`,
but this was not actually the case because the last item in
`BREAKPOINTS` always matches.

Revise the way `getBreakpoint` works to make it more obvious that it
will always return a breakpoint.
parent ba433e28
......@@ -101,6 +101,14 @@ function dayAndMonthAndYear(date, now, Intl) {
);
}
/**
* @typedef Breakpoint
* @prop {(date: Date, now: Date) => boolean} test
* @prop {(date: Date, now: Date, Intl: typeof window.Intl) => string} formatFn
* @prop {number|null} nextUpdate
*/
/** @type {Breakpoint[]} */
const BREAKPOINTS = [
{
// Less than 30 seconds
......@@ -132,22 +140,22 @@ const BREAKPOINTS = [
formatFn: dayAndMonth,
nextUpdate: null,
},
{
// everything else (default case)
test: () => true,
formatFn: dayAndMonthAndYear,
nextUpdate: null,
},
];
/** @type {Breakpoint} */
const DEFAULT_BREAKPOINT = {
test: () => true,
formatFn: dayAndMonthAndYear,
nextUpdate: null,
};
/**
* Returns a dict that describes how to format the date based on the delta
* between date and now.
*
* @param {Date} date - The date to consider as the timestamp to format.
* @param {Date} now - The date to consider as the current time.
* @return {Object} An object that describes how to format the date or
* null if no breakpoint matches.
* @return {Breakpoint} An object that describes how to format the date.
*/
function getBreakpoint(date, now) {
for (let breakpoint of BREAKPOINTS) {
......@@ -155,7 +163,7 @@ function getBreakpoint(date, now) {
return breakpoint;
}
}
return null;
return DEFAULT_BREAKPOINT;
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment