@openmrs/esm-utils 3.1.15-pre.749 → 3.1.15-pre.751

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openmrs/esm-utils",
3
- "version": "3.1.15-pre.749",
3
+ "version": "3.1.15-pre.751",
4
4
  "license": "MPL-2.0",
5
5
  "description": "Helper utilities for OpenMRS",
6
6
  "browser": "dist/openmrs-esm-utils.js",
@@ -49,5 +49,5 @@
49
49
  "dependencies": {
50
50
  "semver": "^7.3.2"
51
51
  },
52
- "gitHead": "110be85fd4162b3c89f51898a8f5913bec0b15ba"
52
+ "gitHead": "de47ee691eaa1863dcd18a5b696ba785f502223e"
53
53
  }
@@ -49,20 +49,22 @@ describe("Openmrs Dates", () => {
49
49
 
50
50
  it("formats 'Today' with respect to the locale", () => {
51
51
  const testDate = new Date();
52
+ testDate.setHours(15);
53
+ testDate.setMinutes(22);
52
54
  window.i18next.language = "en";
53
- expect(formatDate(testDate)).toEqual("Today");
54
- expect(formatDate(testDate, "no day")).toEqual("Today");
55
- expect(formatDate(testDate, "no year")).toEqual("Today");
56
- expect(formatDate(testDate, "wide")).toEqual("Today");
55
+ expect(formatDate(testDate)).toEqual("Today, 03:22 PM");
56
+ expect(formatDate(testDate, "no day")).toEqual("Today, 03:22 PM");
57
+ expect(formatDate(testDate, "no year")).toEqual("Today, 03:22 PM");
58
+ expect(formatDate(testDate, "wide")).toEqual("Today, 03:22 PM");
57
59
  window.i18next.language = "sw";
58
- expect(formatDate(testDate)).toEqual("Leo");
60
+ expect(formatDate(testDate)).toEqual("Leo, 15:22");
59
61
  window.i18next.language = "ru";
60
- expect(formatDate(testDate)).toEqual("Сегодня");
62
+ expect(formatDate(testDate)).toEqual("Сегодня, 15:22");
61
63
  });
62
64
 
63
65
  it("formats dates with respect to the locale", () => {
64
66
  timezoneMock.register("UTC");
65
- const testDate = new Date("2021-12-09");
67
+ const testDate = new Date("2021-12-09T13:15:33");
66
68
  window.i18next.language = "en";
67
69
  expect(formatDate(testDate)).toEqual("09-Dec-2021");
68
70
  expect(formatDate(testDate, "no day")).toEqual("Dec 2021");
@@ -79,6 +81,32 @@ describe("Openmrs Dates", () => {
79
81
  expect(formatDate(testDate, "wide")).toEqual("09 — дек. — 2021 г.");
80
82
  });
81
83
 
84
+ it("respects the `time` option", () => {
85
+ timezoneMock.register("UTC");
86
+ const testDate = new Date("2021-12-09T13:15:33");
87
+ const today = new Date();
88
+ today.setHours(15);
89
+ today.setMinutes(22);
90
+ window.i18next.language = "en";
91
+ expect(formatDate(testDate)).toEqual("09-Dec-2021");
92
+ expect(formatDate(testDate, "standard", { time: true })).toEqual(
93
+ "09-Dec-2021, 01:15 PM"
94
+ );
95
+ expect(formatDate(testDate, "standard", { time: false })).toEqual(
96
+ "09-Dec-2021"
97
+ );
98
+ expect(formatDate(testDate, "standard", { time: "for today" })).toEqual(
99
+ "09-Dec-2021"
100
+ );
101
+ expect(formatDate(today, "standard", { time: true })).toEqual(
102
+ "Today, 03:22 PM"
103
+ );
104
+ expect(formatDate(today, "standard", { time: false })).toEqual("Today");
105
+ expect(formatDate(today, "standard", { time: "for today" })).toEqual(
106
+ "Today, 03:22 PM"
107
+ );
108
+ });
109
+
82
110
  it("formats times with respect to the locale", () => {
83
111
  timezoneMock.register("Australia/Adelaide");
84
112
  const testDate = new Date("2021-12-09T13:15:33");
package/src/omrs-dates.ts CHANGED
@@ -153,17 +153,40 @@ const DATE_FORMAT_MMM_DD = {
153
153
 
154
154
  export type FormatDateMode = "standard" | "no year" | "no day" | "wide";
155
155
 
156
+ export type FormatDateOptions = {
157
+ /**
158
+ * Whether the time should be included in the output always (`true`),
159
+ * never (`false`), or only when the input date is today (`for today`).
160
+ */
161
+ time: true | false | "for today";
162
+ };
163
+ const defaultOptions: FormatDateOptions = {
164
+ time: "for today",
165
+ };
166
+
156
167
  /**
157
168
  * Formats the input date according to the current locale and the
158
169
  * given format mode.
159
170
  *
160
- * `standard`: "13 Dec 2021"
161
- * `no year`: "13 Dec"
162
- * `no day`: "Dec 2021"
163
- * `wide`: "13 — Dec — 2021"
171
+ * - `standard`: "13 Dec 2021"
172
+ * - `no year`: "13 Dec"
173
+ * - `no day`: "Dec 2021"
174
+ * - `wide`: "13 — Dec — 2021"
175
+ *
176
+ * Regardless of the mode, if the date is today, then "Today" is produced
177
+ * (in the locale language).
178
+ *
179
+ * Can be used to format a date with time, also, by providing `options`.
180
+ * By default, the time is included only when the input date is today.
181
+ * The time is appended with a comma and a space. This agrees with the
182
+ * output of `Date.prototype.toLocaleString` for *most* locales.
164
183
  */
165
- export function formatDate(date: Date, mode: FormatDateMode = "standard") {
166
- const options = (
184
+ export function formatDate(
185
+ date: Date,
186
+ mode: FormatDateMode = "standard",
187
+ options: FormatDateOptions = defaultOptions
188
+ ) {
189
+ const formatterOptions = (
167
190
  {
168
191
  standard: DATE_FORMAT_YYYY_MMM_DD,
169
192
  wide: DATE_FORMAT_YYYY_MMM_DD,
@@ -172,20 +195,21 @@ export function formatDate(date: Date, mode: FormatDateMode = "standard") {
172
195
  } as Record<string, Intl.DateTimeFormatOptions>
173
196
  )[mode];
174
197
  let locale = getLocale();
175
- if (dayjs(date).isToday()) {
198
+ let localeString: string;
199
+ const isToday = dayjs(date).isToday();
200
+ if (isToday) {
176
201
  // This produces the word "Today" in the language of `locale`
177
202
  const rtf = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
178
- let localeString = rtf.format(0, "day");
203
+ localeString = rtf.format(0, "day");
179
204
  localeString =
180
205
  localeString[0].toLocaleUpperCase(locale) + localeString.slice(1);
181
- return localeString;
182
206
  } else {
183
207
  if (locale == "en") {
184
208
  // This locale override is here rather than in `getLocale`
185
209
  // because Americans should see AM/PM for times.
186
210
  locale = "en-GB";
187
211
  }
188
- let localeString = date.toLocaleDateString(locale, options);
212
+ localeString = date.toLocaleDateString(locale, formatterOptions);
189
213
  if (locale == "en-GB" && mode == "standard") {
190
214
  // Custom formatting for English. Use hyphens instead of spaces.
191
215
  localeString = localeString.replace(/ /g, "-");
@@ -200,8 +224,11 @@ export function formatDate(date: Date, mode: FormatDateMode = "standard") {
200
224
  localeString.slice(len - 5).replace(" — ", " ");
201
225
  }
202
226
  }
203
- return localeString;
204
227
  }
228
+ if (options.time === true || (isToday && options.time === "for today")) {
229
+ localeString += `, ${formatTime(date)}`;
230
+ }
231
+ return localeString;
205
232
  }
206
233
 
207
234
  /**
@@ -225,9 +252,7 @@ export function formatTime(date: Date) {
225
252
  * output of `Date.prototype.toLocaleString` for *most* locales.
226
253
  */
227
254
  export function formatDatetime(date: Date, mode: FormatDateMode = "standard") {
228
- const dateString = formatDate(date, mode);
229
- const timeString = formatTime(date);
230
- return `${dateString}, ${timeString}`;
255
+ return formatDate(date, mode, { time: true });
231
256
  }
232
257
 
233
258
  function getLocale() {