Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
chore(client/ts): port utils/formatters
Elian Doran0bad36b9f2f8
diff --git a/src/public/app/utils/formatters.js b/src/public/app/utils/formatters.tssimilarity index 59%rename from src/public/app/utils/formatters.jsrename to src/public/app/utils/formatters.tsindex 638404c896..398aa678b0 100644--- a/src/public/app/utils/formatters.js+++ b/src/public/app/utils/formatters.ts@@ -1,17 +1,16 @@+type DateTimeStyle = "full" | "long" | "medium" | "short" | "none" | undefined;+/*** Formats the given date and time to a string based on the current locale.- * @param {string | Date | number} date- * @param {"full" | "long" | "medium" | "short" | "none" | undefined} dateStyle- * @param {"full" | "long" | "medium" | "short" | "none" | undefined} timeStyle*/-export function formatDateTime(date, dateStyle = "medium", timeStyle = "medium") {+export function formatDateTime(date: string | Date | number, dateStyle: DateTimeStyle = "medium", timeStyle: DateTimeStyle = "medium") {const locale = navigator.language;let parsedDate;- if (typeof date === "string") {+ if (typeof date === "string" || typeof date === "number") {// Parse the given string as a dateparsedDate = new Date(date);- } else if (typeof date === "number" || date instanceof Date) {+ } else if (date instanceof Date) {// The given date is already a Date instance or a numberparsedDate = date;} else {@@ -19,16 +18,18 @@ export function formatDateTime(date, dateStyle = "medium", timeStyle = "medium")throw new TypeError(`Invalid type for the "date" argument.`);};- if (timeStyle === "none") {+ if (timeStyle !== "none" && dateStyle !== "none") {+ // Format the date and time+ const formatter = new Intl.DateTimeFormat(navigator.language, {dateStyle, timeStyle});+ return formatter.format(parsedDate);+ } else if (timeStyle === "none" && dateStyle !== "none") {// Format only the datereturn parsedDate.toLocaleDateString(locale, {dateStyle});- } else if (dateStyle === "none") {+ } else if (dateStyle === "none" && timeStyle !== "none") {// Format only the timereturn parsedDate.toLocaleTimeString(locale, {timeStyle});- } else {- // Format the date and time- const formatter = new Intl.DateTimeFormat(navigator.language, {dateStyle, timeStyle});- return formatter.format(parsedDate);}++ throw new Error("Incorrect state.");}