From 9cdf69443781203c86256dea85918960b1f17c9c Mon Sep 17 00:00:00 2001 From: Lukas Obermann Date: Thu, 12 Mar 2026 23:14:49 +0100 Subject: [PATCH] fix!: require external date formatter --- scripts/testOutput.ts | 2 ++ src/creator.ts | 2 +- src/helpers/locale.ts | 1 + test/helpers/locale.ts | 3 ++- test/helpers/translate.ts | 21 +++++++++------------ 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/scripts/testOutput.ts b/scripts/testOutput.ts index 1dab757..c9964a3 100644 --- a/scripts/testOutput.ts +++ b/scripts/testOutput.ts @@ -67,11 +67,13 @@ const unitListFormat = new Intl.ListFormat(localeId, { }) const collator = new Intl.Collator(localeId, { usage: "sort" }) +const dateFormatter = new Intl.DateTimeFormat(localeId) const localeEnv: LocaleEnvironment = { id: localeId, format: (text, args) => new MessageFormat(localeId, text, { bidiIsolation: "none" }).format(args), compare: collator.compare.bind(collator), + formatDate: dateFormatter.format.bind(dateFormatter), translate: (key, ...rest) => new MessageFormat(localeId, localeInstance.translations?.[key] ?? key, { bidiIsolation: "none", diff --git a/src/creator.ts b/src/creator.ts index ce0c59e..a0cb93f 100644 --- a/src/creator.ts +++ b/src/creator.ts @@ -114,7 +114,7 @@ export const createEntityDescriptionCreator = ...rawEntry, body: rawEntry.body.filter(isNotNullish).map(mapRawSection), errata: rawEntry.errata?.map(({ date, description }) => ({ - date: date.toLocaleDateString(locale.id), + date: locale.formatDate(date), description: description.trim(), })), references: diff --git a/src/helpers/locale.ts b/src/helpers/locale.ts index a6be57e..9ccc080 100644 --- a/src/helpers/locale.ts +++ b/src/helpers/locale.ts @@ -13,6 +13,7 @@ export type LocaleJoinType = "conjunction" | "disjunction" | "unit" export type LocaleEnvironment = { id: string format: Format + formatDate: (date: Date) => string translate: Translate translateMap: TranslateMap compare: LocaleCompare diff --git a/test/helpers/locale.ts b/test/helpers/locale.ts index cf088d1..d3f3469 100644 --- a/test/helpers/locale.ts +++ b/test/helpers/locale.ts @@ -1,6 +1,6 @@ import { assertExhaustive } from "@elyukai/utils/typeSafety" import { LocaleEnvironment } from "../../src/helpers/locale.js" -import { formatMock, translateMapMock, translateMock } from "./translate.js" +import { formatDateMock, formatMock, translateMapMock, translateMock } from "./translate.js" const localeId = "en-US" @@ -22,6 +22,7 @@ const unitListFormat = new Intl.ListFormat(localeId, { export const defaultLocaleEnvironment: LocaleEnvironment = { id: "en-US", format: formatMock, + formatDate: formatDateMock, translate: translateMock, translateMap: translateMapMock, compare: (x, y) => collator.compare(x, y), diff --git a/test/helpers/translate.ts b/test/helpers/translate.ts index 4389740..09e6ecf 100644 --- a/test/helpers/translate.ts +++ b/test/helpers/translate.ts @@ -1,26 +1,23 @@ import { MessageFormat } from "messageformat" -import { - Translate, - TranslateMap, - type Format, -} from "../../src/helpers/translate.js" +import { Translate, TranslateMap, type Format } from "../../src/helpers/translate.js" /** * A mocked format function. */ -export const formatMock: Format = (text, args) => - new MessageFormat("en", text).format(args) +export const formatMock: Format = (text, args) => new MessageFormat("en", text).format(args) + +/** + * A mocked date format function that formats a date as YYYY-MM-DD. + */ +export const formatDateMock = (date: Date): string => date.toISOString().split("T")[0] ?? "" /** * A mocked translate function. */ export const translateMock: Translate = (key, ...rest) => - new MessageFormat("en", key).format( - rest[0] as Record | undefined, - ) + new MessageFormat("en", key).format(rest[0] as Record | undefined) /** * A mocked translate map function. */ -export const translateMapMock: TranslateMap = map => - map?.["en-US"] ?? map?.["de-DE"] +export const translateMapMock: TranslateMap = map => map?.["en-US"] ?? map?.["de-DE"]