From e5dfa5210dd0eef99cb079beef528e8cc4825823 Mon Sep 17 00:00:00 2001 From: Yousif Yassi Date: Mon, 29 Jan 2024 14:10:32 -0500 Subject: [PATCH] tweaks to support unexpected types gracefully --- src/components/CellViews/DateTimeCell.js | 4 ++-- src/components/CellViews/StringCell.js | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/components/CellViews/DateTimeCell.js b/src/components/CellViews/DateTimeCell.js index 9aecaff3..7c5d1ff3 100644 --- a/src/components/CellViews/DateTimeCell.js +++ b/src/components/CellViews/DateTimeCell.js @@ -1,12 +1,12 @@ import { format, isValid } from "date-fns"; +export const dateTimeFormat = "MMM dd yyyy, HH:mm:ss"; export const DateTimeCell = (column) => { const date = new Date(column.value); - const dateFormat = "MMM dd yyyy, HH:mm:ss"; return column.value ? (
- {isValid(date) ? format(date, dateFormat) : ""} + {isValid(date) ? format(date, dateTimeFormat) : ""}
) : ( "" diff --git a/src/components/CellViews/StringCell.js b/src/components/CellViews/StringCell.js index d0bfdc18..d4c7a3bc 100644 --- a/src/components/CellViews/StringCell.js +++ b/src/components/CellViews/StringCell.js @@ -1,7 +1,11 @@ -const valueToString = (value) => { +import { format, isValid } from "date-fns"; +import { dateTimeFormat } from "./DateTimeCell"; + +export const valueToString = (value) => { if (typeof value === "string") return value; /* if undefined or null we'll treat it as empty string */ if (value === undefined || value === null) return ""; + if (value instanceof Date && isValid(value)) return format(value, dateTimeFormat); try { /* JSON.stringify will handle JSON and non-strings, non-null, non-undefined */