mirror of
https://github.com/zadam/trilium.git
synced 2025-10-26 07:46:30 +01:00
fix(client): crash if locale is incorrect
This commit is contained in:
13
apps/client/src/utils/formatters.spec.ts
Normal file
13
apps/client/src/utils/formatters.spec.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import options from "../services/options";
|
||||
import { formatDateTime } from "./formatters";
|
||||
|
||||
describe("formatDateTime", () => {
|
||||
it("tolerates incorrect locale", () => {
|
||||
options.set("formattingLocale", "cn_TW");
|
||||
|
||||
expect(formatDateTime(new Date())).toBeTruthy();
|
||||
expect(formatDateTime(new Date(), "full", "none")).toBeTruthy();
|
||||
expect(formatDateTime(new Date(), "none", "full")).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -26,14 +26,27 @@ export function formatDateTime(date: string | Date | number | null | undefined,
|
||||
|
||||
if (timeStyle !== "none" && dateStyle !== "none") {
|
||||
// Format the date and time
|
||||
const formatter = new Intl.DateTimeFormat(locale, { dateStyle, timeStyle });
|
||||
return formatter.format(parsedDate);
|
||||
try {
|
||||
const formatter = new Intl.DateTimeFormat(locale, { dateStyle, timeStyle });
|
||||
return formatter.format(parsedDate);
|
||||
} catch (e) {
|
||||
const formatter = new Intl.DateTimeFormat(undefined, { dateStyle, timeStyle });
|
||||
return formatter.format(parsedDate);
|
||||
}
|
||||
} else if (timeStyle === "none" && dateStyle !== "none") {
|
||||
// Format only the date
|
||||
return parsedDate.toLocaleDateString(locale, { dateStyle });
|
||||
try {
|
||||
return parsedDate.toLocaleDateString(locale, { dateStyle });
|
||||
} catch (e) {
|
||||
return parsedDate.toLocaleDateString(undefined, { dateStyle });
|
||||
}
|
||||
} else if (dateStyle === "none" && timeStyle !== "none") {
|
||||
// Format only the time
|
||||
return parsedDate.toLocaleTimeString(locale, { timeStyle });
|
||||
try {
|
||||
return parsedDate.toLocaleTimeString(locale, { timeStyle });
|
||||
} catch (e) {
|
||||
return parsedDate.toLocaleTimeString(undefined, { timeStyle });
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error("Incorrect state.");
|
||||
|
||||
Reference in New Issue
Block a user