@openmrs/esm-utils 3.1.15-pre.889 → 3.2.0

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.889",
3
+ "version": "3.2.0",
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": "d47481bd41efded1d1627122a605f509240aa859"
52
+ "gitHead": "9c4a04089e29ff99321d6fef72294f402deb0f7b"
53
53
  }
@@ -53,9 +53,9 @@ describe("Openmrs Dates", () => {
53
53
  testDate.setMinutes(22);
54
54
  window.i18next.language = "en";
55
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");
56
+ expect(formatDate(testDate, { day: false })).toEqual("Today, 03:22 PM");
57
+ expect(formatDate(testDate, { year: false })).toEqual("Today, 03:22 PM");
58
+ expect(formatDate(testDate, { mode: "wide" })).toEqual("Today, 03:22 PM");
59
59
  window.i18next.language = "sw";
60
60
  expect(formatDate(testDate)).toEqual("Leo, 15:22");
61
61
  window.i18next.language = "ru";
@@ -67,18 +67,23 @@ describe("Openmrs Dates", () => {
67
67
  const testDate = new Date("2021-12-09T13:15:33");
68
68
  window.i18next.language = "en";
69
69
  expect(formatDate(testDate)).toEqual("09-Dec-2021");
70
- expect(formatDate(testDate, "no day")).toEqual("Dec 2021");
71
- expect(formatDate(testDate, "no year")).toEqual("09 Dec");
72
- expect(formatDate(testDate, "wide")).toEqual("09 — Dec — 2021");
70
+ expect(formatDate(testDate, { day: false })).toEqual("Dec 2021");
71
+ expect(formatDate(testDate, { year: false })).toEqual("09 Dec");
72
+ expect(formatDate(testDate, { mode: "wide" })).toEqual("09 — Dec — 2021");
73
+ expect(formatDate(testDate, { mode: "wide", year: false })).toEqual(
74
+ "09 — Dec"
75
+ );
73
76
  window.i18next.language = "fr";
74
77
  expect(formatDate(testDate)).toEqual("09 déc. 2021");
75
- expect(formatDate(testDate, "no day")).toEqual("déc. 2021");
76
- expect(formatDate(testDate, "no year")).toEqual("09 déc.");
77
- expect(formatDate(testDate, "wide")).toEqual("09 — déc. — 2021");
78
+ expect(formatDate(testDate, { day: false })).toEqual("déc. 2021");
79
+ expect(formatDate(testDate, { year: false })).toEqual("09 déc.");
80
+ expect(formatDate(testDate, { mode: "wide" })).toEqual("09 — déc. — 2021");
78
81
  window.i18next.language = "sw";
79
82
  expect(formatDate(testDate)).toEqual("09 Des 2021");
80
83
  window.i18next.language = "ru";
81
- expect(formatDate(testDate, "wide")).toEqual("09 — дек. — 2021 г.");
84
+ expect(formatDate(testDate, { mode: "wide" })).toEqual(
85
+ "09 — дек. — 2021 г."
86
+ );
82
87
  });
83
88
 
84
89
  it("respects the `time` option", () => {
@@ -89,22 +94,14 @@ describe("Openmrs Dates", () => {
89
94
  today.setMinutes(22);
90
95
  window.i18next.language = "en";
91
96
  expect(formatDate(testDate)).toEqual("09-Dec-2021");
92
- expect(formatDate(testDate, "standard", { time: true })).toEqual(
97
+ expect(formatDate(testDate, { time: true })).toEqual(
93
98
  "09-Dec-2021, 01:15 PM"
94
99
  );
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
- );
100
+ expect(formatDate(testDate, { time: false })).toEqual("09-Dec-2021");
101
+ expect(formatDate(testDate, { time: "for today" })).toEqual("09-Dec-2021");
102
+ expect(formatDate(today, { time: true })).toEqual("Today, 03:22 PM");
103
+ expect(formatDate(today, { time: false })).toEqual("Today");
104
+ expect(formatDate(today, { time: "for today" })).toEqual("Today, 03:22 PM");
108
105
  });
109
106
 
110
107
  it("formats times with respect to the locale", () => {
package/src/omrs-dates.ts CHANGED
@@ -137,63 +137,58 @@ export function parseDate(dateString: string) {
137
137
  return dayjs(dateString).toDate();
138
138
  }
139
139
 
140
- const DATE_FORMAT_YYYY_MMM_DD = {
141
- year: "numeric",
142
- month: "short",
143
- day: "2-digit",
144
- };
145
- const DATE_FORMAT_YYYY_MMM = {
146
- year: "numeric",
147
- month: "short",
148
- };
149
- const DATE_FORMAT_MMM_DD = {
150
- month: "short",
151
- day: "2-digit",
152
- };
153
-
154
- export type FormatDateMode = "standard" | "no year" | "no day" | "wide";
140
+ export type FormatDateMode = "standard" | "wide";
155
141
 
156
142
  export type FormatDateOptions = {
143
+ /**
144
+ * - `standard`: "03 Feb 2022"
145
+ * - `wide`: "03 — Feb — 2022"
146
+ */
147
+ mode: FormatDateMode;
157
148
  /**
158
149
  * Whether the time should be included in the output always (`true`),
159
150
  * never (`false`), or only when the input date is today (`for today`).
160
151
  */
161
152
  time: true | false | "for today";
153
+ /** Whether to include the day number */
154
+ day: boolean;
155
+ /** Whether to include the year */
156
+ year: boolean;
162
157
  };
158
+
163
159
  const defaultOptions: FormatDateOptions = {
160
+ mode: "standard",
164
161
  time: "for today",
162
+ day: true,
163
+ year: true,
165
164
  };
166
165
 
167
166
  /**
168
167
  * Formats the input date according to the current locale and the
169
- * given format mode.
168
+ * given options.
170
169
  *
171
- * - `standard`: "13 Dec 2021"
172
- * - `no year`: "13 Dec"
173
- * - `no day`: "Dec 2021"
174
- * - `wide`: "13 — Dec — 2021"
170
+ * Default options:
171
+ * - mode: "standard",
172
+ * - time: "for today",
173
+ * - day: true,
174
+ * - year: true
175
175
  *
176
- * Regardless of the mode, if the date is today, then "Today" is produced
177
- * (in the locale language).
176
+ * If the date is today then "Today" is produced (in the locale language).
178
177
  *
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.
178
+ * When time is included, it is appended with a comma and a space. This
179
+ * agrees with the output of `Date.prototype.toLocaleString` for *most*
180
+ * locales.
183
181
  */
184
- export function formatDate(
185
- date: Date,
186
- mode: FormatDateMode = "standard",
187
- options: FormatDateOptions = defaultOptions
188
- ) {
189
- const formatterOptions = (
190
- {
191
- standard: DATE_FORMAT_YYYY_MMM_DD,
192
- wide: DATE_FORMAT_YYYY_MMM_DD,
193
- "no year": DATE_FORMAT_MMM_DD,
194
- "no day": DATE_FORMAT_YYYY_MMM,
195
- } as Record<string, Intl.DateTimeFormatOptions>
196
- )[mode];
182
+ export function formatDate(date: Date, options?: Partial<FormatDateOptions>) {
183
+ const { mode, time, day, year }: FormatDateOptions = {
184
+ ...defaultOptions,
185
+ ...options,
186
+ };
187
+ const formatterOptions: Intl.DateTimeFormatOptions = {
188
+ year: year ? "numeric" : undefined,
189
+ month: "short",
190
+ day: day ? "2-digit" : undefined,
191
+ };
197
192
  let locale = getLocale();
198
193
  let localeString: string;
199
194
  const isToday = dayjs(date).isToday();
@@ -210,7 +205,7 @@ export function formatDate(
210
205
  locale = "en-GB";
211
206
  }
212
207
  localeString = date.toLocaleDateString(locale, formatterOptions);
213
- if (locale == "en-GB" && mode == "standard") {
208
+ if (locale == "en-GB" && mode == "standard" && year && day) {
214
209
  // Custom formatting for English. Use hyphens instead of spaces.
215
210
  localeString = localeString.replace(/ /g, "-");
216
211
  }
@@ -225,7 +220,7 @@ export function formatDate(
225
220
  }
226
221
  }
227
222
  }
228
- if (options.time === true || (isToday && options.time === "for today")) {
223
+ if (time === true || (isToday && time === "for today")) {
229
224
  localeString += `, ${formatTime(date)}`;
230
225
  }
231
226
  return localeString;
@@ -251,8 +246,11 @@ export function formatTime(date: Date) {
251
246
  * and `formatTime` with a comma and space. This agrees with the
252
247
  * output of `Date.prototype.toLocaleString` for *most* locales.
253
248
  */
254
- export function formatDatetime(date: Date, mode: FormatDateMode = "standard") {
255
- return formatDate(date, mode, { time: true });
249
+ export function formatDatetime(
250
+ date: Date,
251
+ options?: Partial<Omit<FormatDateOptions, "time">>
252
+ ) {
253
+ return formatDate(date, { ...options, time: true });
256
254
  }
257
255
 
258
256
  function getLocale() {