Skip to content
Snippets Groups Projects
Unverified Commit 591684bb authored by Alexander Polyankin's avatar Alexander Polyankin Committed by GitHub
Browse files

Add day of week formatting with formatting options (#17549)

parent 5a057ea9
No related branches found
No related tags found
No related merge requests found
......@@ -532,7 +532,7 @@ export function formatDateTimeWithUnit(
);
}
return formatDateTimeWithFormats(value, dateFormat, timeFormat, options);
return formatDateTimeWithFormats(m, dateFormat, timeFormat, options);
}
export function formatTime(value: Value) {
......
......@@ -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":
......
......@@ -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", () => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment