|
// Match up time periods for the tickler file and show items that haven't been tickled yet. |
|
// |
|
// My tickler notes have two properties set up: |
|
// - tickler: [YYYY-MM-DD, YYYY-MM-DD,...] |
|
// - tickled: [YYYY-MM-DD, ...] |
|
// |
|
// I want a note to appear in my tickler file whenever the current date is larger than the largest element |
|
// in tickler smaller than the largest element in tickled. That will allow me to have multiple tickler |
|
// dates in the future. |
|
// @param { dv.page } page - page fed through a dv.page query. |
|
// @param { dv.luxon.DateTime } date - date passed as as luxon.DateTime object, |
|
// the same as the DataView dates. Makes it easier to do |
|
// date comparisons. |
|
function tickler_file(page, date) { |
|
// Helper function for comparing Date objects. |
|
function isSameOrBeforeDay(d1, d2) { |
|
return d1.hasSame(d2, "day") || d1 < d2; |
|
} |
|
|
|
debugger; |
|
|
|
if (!date.isLuxonDateTime) { |
|
throw new Error(`You passed an invalid date into the query. |
|
Make sure to wrap the date in dv.luxon.DateTime.fromISO('YYYY-MM-DD')`); |
|
} |
|
|
|
if (!page.tickler) { |
|
// No tickler field, probably a bad query. Simply return false. |
|
return false; |
|
} |
|
|
|
const tickler_dates = Array.isArray(page.tickler) |
|
? page.tickler |
|
: [page.tickler]; |
|
|
|
// If it hasn't been tickled yet, find the minimum date in the tickler array. |
|
if (!page.tickled) { |
|
// Find the min |
|
const min_tickler_date = tickler_dates.reduce((latest, current) => |
|
latest < current ? latest : current, |
|
); |
|
return isSameOrBeforeDay(min_tickler_date, date); |
|
} |
|
|
|
// If it has been tickled in the past, check whether the next tickler date is the same or before |
|
// the current date. |
|
const tickled_dates = Array.isArray(page.tickled) |
|
? page.tickled |
|
: [page.tickled]; |
|
const latest_tickled = tickled_dates.reduce((a, b) => (b > a ? b : a)); |
|
const remaining_ticklers = tickler_dates.filter((d) => d > latest_tickled); |
|
|
|
// If there are no remaining ticklers, we are done. |
|
if (remaining_ticklers.length === 0) return false; |
|
|
|
const next_tickler = remaining_ticklers.reduce((a, b) => (a <= b ? a : b)); |
|
|
|
return isSameOrBeforeDay(next_tickler, date); |
|
} |
|
|
|
exports.tickler_file = tickler_file; |