/** * The dates of a filter a DataTable kept in storage (`preserveFilter`). * * The stored state is JSON, so a date range comes back as the strings a `Temporal.PlainDateTime` serialises to. The * table used to restore them as they were: the header's date picker, handed a string, failed * (`base.with is not a function`) and took the table's rendering down with it - no row shown although the API had * answered - and a client-side date filter called `toZonedDateTime` on a string. * * Checked by `dev/validate-datatable-stored-filter.mjs`. */ export class StoredFilterDates { /** * The stored filter items with `DateFrom` / `DateTo` as `Temporal.PlainDateTime`. An item whose date cannot be read * is dropped - the filter is not restored rather than the table failing; anything that is not a list is no filter. */ static revive(filter: T[] | null | undefined): T[] | undefined { if (!Array.isArray(filter)) { return undefined; } const revived: T[] = []; for (const item of filter) { if (item == null || typeof item !== 'object') { continue; } const dateFrom = StoredFilterDates.toPlainDateTime(item.DateFrom); const dateTo = StoredFilterDates.toPlainDateTime(item.DateTo); if (dateFrom === null || dateTo === null) { continue; } revived.push(item.DateFrom == null && item.DateTo == null ? item : { ...item, DateFrom: dateFrom ?? item.DateFrom, DateTo: dateTo ?? item.DateTo }); } return revived; } /** * A stored bound as a `Temporal.PlainDateTime`; `undefined` when there is none, `null` when it is no date. A trailing * `Z` is dropped, as `TemporalUtils.fromString` does: the bound is the wall-clock time it names. */ private static toPlainDateTime(value: unknown): Temporal.PlainDateTime | undefined | null { if (value == null) { return undefined; } if (value instanceof Temporal.PlainDateTime) { return value; } if (typeof value !== 'string') { return null; } try { return Temporal.PlainDateTime.from(value.endsWith('Z') ? value.slice(0, -1) : value); } catch { return null; } } }