@openmrs/esm-utils 6.3.1-pre.3175 → 6.3.1-pre.3199

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.
@@ -1,3 +1,3 @@
1
- [0] Successfully compiled: 12 files with swc (184.05ms)
1
+ [0] Successfully compiled: 12 files with swc (125.32ms)
2
2
  [0] swc --strip-leading-paths src -d dist exited with code 0
3
3
  [1] tsc --project tsconfig.build.json exited with code 0
@@ -3,6 +3,7 @@
3
3
  * @category Date and Time
4
4
  */
5
5
  import { type CalendarDate, type CalendarDateTime, type CalendarIdentifier, type ZonedDateTime } from '@internationalized/date';
6
+ import type { DurationFormatOptions, DurationInput } from '@formatjs/intl-durationformat/src/types';
6
7
  export type DateInput = string | number | Date;
7
8
  /**
8
9
  * This function checks whether a date string is the OpenMRS ISO format.
@@ -142,3 +143,11 @@ export declare function formatDatetime(date: Date, options?: Partial<Omit<Format
142
143
  * @returns CalendarDate
143
144
  */
144
145
  export declare function convertToLocaleCalendar(date: CalendarDate | CalendarDateTime | ZonedDateTime, locale: string | Intl.Locale): CalendarDate | CalendarDateTime | ZonedDateTime;
146
+ /**
147
+ * Formats the input duration according to the current locale.
148
+ *
149
+ * @param duration The duration to format (DurationInput object).
150
+ * @param options Optional options for formatting.
151
+ * @returns The formatted duration string.
152
+ */
153
+ export declare function formatDuration(duration: DurationInput, options?: DurationFormatOptions): string;
@@ -27,6 +27,7 @@ function _class_private_field_init(obj, privateMap, value) {
27
27
  privateMap.set(obj, value);
28
28
  }
29
29
  import { createCalendar, toCalendar } from "@internationalized/date";
30
+ import { DurationFormat } from "@formatjs/intl-durationformat";
30
31
  import { attempt } from "any-date-parser";
31
32
  import dayjs from "dayjs";
32
33
  import isToday from "dayjs/plugin/isToday.js";
@@ -333,3 +334,13 @@ const formatParts = (separator)=>{
333
334
  const localCalendarName = getDefaultCalendar(locale_);
334
335
  return localCalendarName ? toCalendar(date, createCalendar(localCalendarName)) : date;
335
336
  }
337
+ /**
338
+ * Formats the input duration according to the current locale.
339
+ *
340
+ * @param duration The duration to format (DurationInput object).
341
+ * @param options Optional options for formatting.
342
+ * @returns The formatted duration string.
343
+ */ export function formatDuration(duration, options) {
344
+ const formatter = new DurationFormat(getLocale(), options);
345
+ return formatter.format(duration);
346
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openmrs/esm-utils",
3
- "version": "6.3.1-pre.3175",
3
+ "version": "6.3.1-pre.3199",
4
4
  "license": "MPL-2.0",
5
5
  "description": "Helper utilities for OpenMRS",
6
6
  "type": "module",
@@ -58,7 +58,7 @@
58
58
  "rxjs": "6.x"
59
59
  },
60
60
  "devDependencies": {
61
- "@openmrs/esm-globals": "6.3.1-pre.3175",
61
+ "@openmrs/esm-globals": "6.3.1-pre.3199",
62
62
  "@swc/cli": "^0.7.7",
63
63
  "@swc/core": "^1.11.29",
64
64
  "@types/lodash-es": "^4.17.12",
@@ -11,6 +11,7 @@ import {
11
11
  formatTime,
12
12
  registerDefaultCalendar,
13
13
  formatPartialDate,
14
+ formatDuration,
14
15
  } from './date-util';
15
16
 
16
17
  window.i18next = { language: 'en' } as i18n;
@@ -196,4 +197,16 @@ describe('Openmrs Dates', () => {
196
197
  .replace(/ /g, '-') + ', 03:22 PM';
197
198
  expect(formatDate(testDate, { noToday: true }).replaceAll(/[\u202F]/g, ' ')).toEqual(expected);
198
199
  });
200
+
201
+ it('formats duration with respect to the locale', () => {
202
+ window.i18next.language = 'en';
203
+ const duration = { hours: 1, minutes: 1, seconds: 1, days: 1, months: 1, years: 1 };
204
+ expect(formatDuration(duration, { style: 'narrow' })).toMatch(/1y.*1m.*1d.*1h.*1m.*1s/);
205
+ expect(formatDuration(duration, { style: 'short' })).toMatch(
206
+ /1\s+yr,.*1\s+mth,.*1\s+day,.*1\s+hr,.*1\s+min,.*1\s+sec/,
207
+ );
208
+ expect(formatDuration(duration, { style: 'long' })).toMatch(
209
+ /1\s+year,.*1\s+month,.*1\s+day,.*1\s+hour,.*1\s+minute,.*1\s+second/,
210
+ );
211
+ });
199
212
  });
@@ -10,6 +10,7 @@ import {
10
10
  createCalendar,
11
11
  toCalendar,
12
12
  } from '@internationalized/date';
13
+ import { DurationFormat } from '@formatjs/intl-durationformat';
13
14
  import { attempt } from 'any-date-parser';
14
15
  import dayjs from 'dayjs';
15
16
  import isToday from 'dayjs/plugin/isToday.js';
@@ -17,6 +18,7 @@ import objectSupport from 'dayjs/plugin/objectSupport.js';
17
18
  import utc from 'dayjs/plugin/utc.js';
18
19
  import { isNil, omit } from 'lodash-es';
19
20
  import { getLocale } from '../';
21
+ import type { DurationFormatOptions, DurationInput } from '@formatjs/intl-durationformat/src/types';
20
22
 
21
23
  dayjs.extend(isToday);
22
24
  dayjs.extend(utc);
@@ -413,3 +415,15 @@ export function convertToLocaleCalendar(
413
415
 
414
416
  return localCalendarName ? toCalendar(date, createCalendar(localCalendarName)) : date;
415
417
  }
418
+
419
+ /**
420
+ * Formats the input duration according to the current locale.
421
+ *
422
+ * @param duration The duration to format (DurationInput object).
423
+ * @param options Optional options for formatting.
424
+ * @returns The formatted duration string.
425
+ */
426
+ export function formatDuration(duration: DurationInput, options?: DurationFormatOptions) {
427
+ const formatter = new DurationFormat(getLocale(), options);
428
+ return formatter.format(duration);
429
+ }