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) { ...@@ -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 = [ const BREAKPOINTS = [
{ {
// Less than 30 seconds // Less than 30 seconds
...@@ -132,22 +140,22 @@ const BREAKPOINTS = [ ...@@ -132,22 +140,22 @@ const BREAKPOINTS = [
formatFn: dayAndMonth, formatFn: dayAndMonth,
nextUpdate: null, 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 * Returns a dict that describes how to format the date based on the delta
* between date and now. * between date and now.
* *
* @param {Date} date - The date to consider as the timestamp to format. * @param {Date} date - The date to consider as the timestamp to format.
* @param {Date} now - The date to consider as the current time. * @param {Date} now - The date to consider as the current time.
* @return {Object} An object that describes how to format the date or * @return {Breakpoint} An object that describes how to format the date.
* null if no breakpoint matches.
*/ */
function getBreakpoint(date, now) { function getBreakpoint(date, now) {
for (let breakpoint of BREAKPOINTS) { for (let breakpoint of BREAKPOINTS) {
...@@ -155,7 +163,7 @@ function getBreakpoint(date, now) { ...@@ -155,7 +163,7 @@ function getBreakpoint(date, now) {
return breakpoint; 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