From 591684bba1cc4d8aec6c505094e4a4d07e11e7a5 Mon Sep 17 00:00:00 2001
From: Alexander Polyankin <alexander.polyankin@metabase.com>
Date: Mon, 23 Aug 2021 15:32:18 +0300
Subject: [PATCH] Add day of week formatting with formatting options (#17549)

---
 frontend/src/metabase/lib/formatting.js          |  2 +-
 frontend/src/metabase/lib/time.js                |  7 +++++++
 .../test/metabase/lib/formatting.unit.spec.js    | 16 +++++++++++++++-
 3 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/frontend/src/metabase/lib/formatting.js b/frontend/src/metabase/lib/formatting.js
index 9cec63dc54e..c462d4b44ab 100644
--- a/frontend/src/metabase/lib/formatting.js
+++ b/frontend/src/metabase/lib/formatting.js
@@ -532,7 +532,7 @@ export function formatDateTimeWithUnit(
     );
   }
 
-  return formatDateTimeWithFormats(value, dateFormat, timeFormat, options);
+  return formatDateTimeWithFormats(m, dateFormat, timeFormat, options);
 }
 
 export function formatTime(value: Value) {
diff --git a/frontend/src/metabase/lib/time.js b/frontend/src/metabase/lib/time.js
index 25bbd18c93d..c277cc0bbc8 100644
--- a/frontend/src/metabase/lib/time.js
+++ b/frontend/src/metabase/lib/time.js
@@ -33,6 +33,10 @@ function addAbbreviatedLocale() {
   moment.locale(initialLocale);
 }
 
+const TEXT_UNIT_FORMATS = {
+  "day-of-week": value => moment.parseZone(value, "ddd").startOf("day"),
+};
+
 const NUMERIC_UNIT_FORMATS = {
   // workaround for https://github.com/metabase/metabase/issues/1992
   year: value =>
@@ -81,6 +85,8 @@ export function parseTimestamp(value, unit = null, local = false) {
     m = value;
   } else if (typeof value === "string" && /(Z|[+-]\d\d:?\d\d)$/.test(value)) {
     m = moment.parseZone(value);
+  } else if (unit in TEXT_UNIT_FORMATS && typeof value === "string") {
+    m = TEXT_UNIT_FORMATS[unit](value);
   } else if (unit in NUMERIC_UNIT_FORMATS && typeof value == "number") {
     m = NUMERIC_UNIT_FORMATS[unit](value);
   } else {
@@ -119,6 +125,7 @@ export function formatHourAMPM(hour) {
   }
 }
 
+// @deprecated use formatDateTimeWithUnit(day, "day-of-week")
 export function formatDay(day) {
   switch (day) {
     case "mon":
diff --git a/frontend/test/metabase/lib/formatting.unit.spec.js b/frontend/test/metabase/lib/formatting.unit.spec.js
index a34ceba10f1..81a1583cb8b 100644
--- a/frontend/test/metabase/lib/formatting.unit.spec.js
+++ b/frontend/test/metabase/lib/formatting.unit.spec.js
@@ -464,9 +464,23 @@ describe("formatting", () => {
         ).toEqual("julio 7, 2019 – julio 13, 2019");
       } finally {
         // globally reset locale
-        moment.locale(false);
+        moment.locale("en");
       }
     });
+
+    it("should format days of week with default options", () => {
+      expect(formatDateTimeWithUnit("mon", "day-of-week")).toEqual("Monday");
+    });
+
+    it("should format days of week with compact option", () => {
+      const options = {
+        compact: true,
+      };
+
+      expect(formatDateTimeWithUnit("sun", "day-of-week", options)).toEqual(
+        "Sun",
+      );
+    });
   });
 
   describe("formatTimeWithUnit", () => {
-- 
GitLab