Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

fix: format week of year ordinal string safely (#66667)

Riley Thompson632b5f96a9ff
diff --git a/frontend/src/metabase/lib/formatting/date.tsx b/frontend/src/metabase/lib/formatting/date.tsx
index 2dc456fb93a..76b78c0c91a 100644
--- a/frontend/src/metabase/lib/formatting/date.tsx
+++ b/frontend/src/metabase/lib/formatting/date.tsx
@@ -1110,7 +1110,11 @@ export function formatDateTimeWithUnit(
}
if (unit === "week-of-year") {
- return dayjs().localeData().ordinal(m.isoWeek()).slice(1, -1);
+ const ordinal = String(dayjs().localeData().ordinal(m.isoWeek()));
+ if (ordinal.startsWith("[") && ordinal.endsWith("]")) {
+ return ordinal.slice(1, -1);
+ }
+ return ordinal;
}
// expand "week" into a range in specific contexts
diff --git a/frontend/test/metabase/lib/formatting.unit.spec.js b/frontend/test/metabase/lib/formatting.unit.spec.js
index fc10db1dd86..9c4ba01470c 100644
--- a/frontend/test/metabase/lib/formatting.unit.spec.js
+++ b/frontend/test/metabase/lib/formatting.unit.spec.js
@@ -789,6 +789,25 @@ describe("formatting", () => {
const endYear = formatDateTimeWithUnit("2023-12-25", "week-of-year");
expect(endYear).toMatch(/^5[0-3][a-z]+$/);
});
+
+ it("should remove square brackets from English ordinals", () => {
+ expect(formatDateTimeWithUnit(1, "week-of-year")).toEqual("1st");
+ expect(formatDateTimeWithUnit(2, "week-of-year")).toEqual("2nd");
+ expect(formatDateTimeWithUnit(3, "week-of-year")).toEqual("3rd");
+ });
+
+ it("should handle non-English locales where ordinals are not wrapped in brackets (#66658)", () => {
+ const originalLocale = dayjs.locale();
+ try {
+ require("dayjs/locale/fr");
+ dayjs.locale("fr");
+ expect(formatDateTimeWithUnit(1, "week-of-year")).toEqual("1er");
+ expect(formatDateTimeWithUnit(2, "week-of-year")).toEqual("2");
+ expect(formatDateTimeWithUnit(3, "week-of-year")).toEqual("3");
+ } finally {
+ dayjs.locale(originalLocale);
+ }
+ });
});
});