Skip to content

Commit

Permalink
(feat) O3-4013: Add support for partial (estimated) dates
Browse files Browse the repository at this point in the history
This leverages the any-date-parser library to parse partial date
representations. Note that there are some serious edge cases with
estimated birth dates less than a day old, but hopefully that's not
something we need to deal with.
  • Loading branch information
ibacher committed Feb 12, 2025
1 parent 25ce1c3 commit e2410cf
Show file tree
Hide file tree
Showing 9 changed files with 372 additions and 134 deletions.
86 changes: 63 additions & 23 deletions packages/framework/esm-framework/docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
- [convertToLocaleCalendar](API.md#converttolocalecalendar)
- [formatDate](API.md#formatdate)
- [formatDatetime](API.md#formatdatetime)
- [formatPartialDate](API.md#formatpartialdate)
- [formatTime](API.md#formattime)
- [getDefaultCalendar](API.md#getdefaultcalendar)
- [getLocale](API.md#getlocale)
Expand Down Expand Up @@ -382,7 +383,7 @@ ___

#### Defined in

[packages/framework/esm-utils/src/omrs-dates.ts:26](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L26)
[packages/framework/esm-utils/src/omrs-dates.ts:23](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L23)

___

Expand All @@ -392,7 +393,7 @@ ___

#### Defined in

[packages/framework/esm-utils/src/omrs-dates.ts:140](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L140)
[packages/framework/esm-utils/src/omrs-dates.ts:208](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L208)

___

Expand All @@ -416,7 +417,7 @@ ___

#### Defined in

[packages/framework/esm-utils/src/omrs-dates.ts:142](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L142)
[packages/framework/esm-utils/src/omrs-dates.ts:210](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L210)

___

Expand Down Expand Up @@ -3947,7 +3948,7 @@ CalendarDate

#### Defined in

[packages/framework/esm-utils/src/omrs-dates.ts:404](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L404)
[packages/framework/esm-utils/src/omrs-dates.ts:462](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L462)

___

Expand Down Expand Up @@ -3986,7 +3987,7 @@ locales.

#### Defined in

[packages/framework/esm-utils/src/omrs-dates.ts:278](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L278)
[packages/framework/esm-utils/src/omrs-dates.ts:336](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L336)

___

Expand Down Expand Up @@ -4015,7 +4016,46 @@ output of `Date.prototype.toLocaleString` for *most* locales.

#### Defined in

[packages/framework/esm-utils/src/omrs-dates.ts:381](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L381)
[packages/framework/esm-utils/src/omrs-dates.ts:439](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L439)

___

### formatPartialDate

▸ **formatPartialDate**(`dateString`, `options?`): ``null`` \| `string`

Formats the string representing a date, including partial representations of dates, eaccording to the current
locale and the given options.

Default options:
- mode: "standard",
- time: "for today",
- day: true,
- month: true,
- year: true
- noToday: false

If the date is today then "Today" is produced (in the locale language).
This behavior can be disabled with `noToday: true`.

When time is included, it is appended with a comma and a space. This
agrees with the output of `Date.prototype.toLocaleString` for *most*
locales.

#### Parameters

| Name | Type |
| :------ | :------ |
| `dateString` | `string` |
| `options` | `Partial`<[`FormatDateOptions`](API.md#formatdateoptions)\> |

#### Returns

``null`` \| `string`

#### Defined in

[packages/framework/esm-utils/src/omrs-dates.ts:275](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L275)

___

Expand All @@ -4038,7 +4078,7 @@ Formats the input as a time, according to the current locale.

#### Defined in

[packages/framework/esm-utils/src/omrs-dates.ts:365](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L365)
[packages/framework/esm-utils/src/omrs-dates.ts:423](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L423)

___

Expand All @@ -4060,7 +4100,7 @@ Retrieves the default calendar for the specified locale if any.

#### Defined in

[packages/framework/esm-utils/src/omrs-dates.ts:252](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L252)
[packages/framework/esm-utils/src/omrs-dates.ts:202](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L202)

___

Expand All @@ -4078,7 +4118,7 @@ string

#### Defined in

[packages/framework/esm-utils/src/omrs-dates.ts:389](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L389)
[packages/framework/esm-utils/src/omrs-dates.ts:447](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L447)

___

Expand All @@ -4101,7 +4141,7 @@ The format should be YYYY-MM-DDTHH:mm:ss.SSSZZ

#### Defined in

[packages/framework/esm-utils/src/omrs-dates.ts:34](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L34)
[packages/framework/esm-utils/src/omrs-dates.ts:31](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L31)

___

Expand All @@ -4121,7 +4161,7 @@ ___

#### Defined in

[packages/framework/esm-utils/src/omrs-dates.ts:63](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L63)
[packages/framework/esm-utils/src/omrs-dates.ts:60](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L60)

___

Expand All @@ -4144,7 +4184,7 @@ Uses `dayjs(dateString)`.

#### Defined in

[packages/framework/esm-utils/src/omrs-dates.ts:136](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L136)
[packages/framework/esm-utils/src/omrs-dates.ts:133](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L133)

___

Expand Down Expand Up @@ -4172,7 +4212,7 @@ registerDefaultCalendar('en', 'buddhist') // sets the default calendar for the '

#### Defined in

[packages/framework/esm-utils/src/omrs-dates.ts:243](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L243)
[packages/framework/esm-utils/src/omrs-dates.ts:193](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L193)

___

Expand All @@ -4195,7 +4235,7 @@ Otherwise returns null.

#### Defined in

[packages/framework/esm-utils/src/omrs-dates.ts:71](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L71)
[packages/framework/esm-utils/src/omrs-dates.ts:68](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L68)

___

Expand All @@ -4219,7 +4259,7 @@ Formats the input as a date string. By default the format "YYYY-MMM-DD" is used.

#### Defined in

[packages/framework/esm-utils/src/omrs-dates.ts:128](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L128)
[packages/framework/esm-utils/src/omrs-dates.ts:125](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L125)

___

Expand All @@ -4242,7 +4282,7 @@ Formats the input as a date string using the format "DD - MMM - YYYY".

#### Defined in

[packages/framework/esm-utils/src/omrs-dates.ts:112](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L112)
[packages/framework/esm-utils/src/omrs-dates.ts:109](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L109)

___

Expand All @@ -4265,7 +4305,7 @@ Formats the input to OpenMRS ISO format: "YYYY-MM-DDTHH:mm:ss.SSSZZ".

#### Defined in

[packages/framework/esm-utils/src/omrs-dates.ts:82](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L82)
[packages/framework/esm-utils/src/omrs-dates.ts:79](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L79)

___

Expand All @@ -4288,7 +4328,7 @@ Formats the input as a time string using the format "HH:mm A".

#### Defined in

[packages/framework/esm-utils/src/omrs-dates.ts:104](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L104)
[packages/framework/esm-utils/src/omrs-dates.ts:101](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L101)

___

Expand All @@ -4311,7 +4351,7 @@ Formats the input as a time string using the format "HH:mm".

#### Defined in

[packages/framework/esm-utils/src/omrs-dates.ts:96](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L96)
[packages/framework/esm-utils/src/omrs-dates.ts:93](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L93)

___

Expand All @@ -4334,7 +4374,7 @@ Formats the input as a date string using the format "DD-MMM".

#### Defined in

[packages/framework/esm-utils/src/omrs-dates.ts:120](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L120)
[packages/framework/esm-utils/src/omrs-dates.ts:117](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/omrs-dates.ts#L117)

___

Expand Down Expand Up @@ -7765,8 +7805,8 @@ https://webarchive.nationalarchives.gov.uk/ukgwa/20160921162509mp_/http://system

| Name | Type | Description |
| :------ | :------ | :------ |
| `birthDate` | `undefined` \| ``null`` \| `string` \| `number` \| `Date` \| `Dayjs` | The birthDate. If birthDate is null, returns null. |
| `currentDate` | `undefined` \| ``null`` \| `string` \| `number` \| `Date` \| `Dayjs` | Optional. If provided, calculates the age of the person at the provided currentDate (instead of now). |
| `birthDate` | `ConfigType` | The birthDate. If birthDate is null, returns null. |
| `currentDate` | `ConfigType` | Optional. If provided, calculates the age of the person at the provided currentDate (instead of now). |

#### Returns

Expand All @@ -7776,7 +7816,7 @@ A human-readable string version of the age.

#### Defined in

[packages/framework/esm-utils/src/age-helpers.ts:17](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/age-helpers.ts#L17)
[packages/framework/esm-utils/src/age-helpers.ts:22](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-utils/src/age-helpers.ts#L22)

___

Expand Down
3 changes: 3 additions & 0 deletions packages/framework/esm-routes/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ module.exports = {
transform: {
'^.+\\.(m?j|t)sx?$': ['@swc/jest'],
},
moduleNameMapper: {
'lodash-es': 'lodash',
},
testEnvironment: 'jsdom',
testEnvironmentOptions: {
url: 'http://localhost/',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { useMemo } from 'react';
import classNames from 'classnames';
import { ExtensionSlot } from '@openmrs/esm-react-utils';
import { getCoreTranslation } from '@openmrs/esm-translations';
import { age, formatDate, parseDate } from '@openmrs/esm-utils';
import { age, formatPartialDate } from '@openmrs/esm-utils';
import { GenderFemaleIcon, GenderMaleIcon, GenderOtherIcon, GenderUnknownIcon } from '../../icons';
import PatientBannerPatientIdentifiers from './patient-banner-patient-identifiers.component';
import styles from './patient-banner-patient-info.module.scss';
Expand Down Expand Up @@ -78,7 +78,7 @@ export function PatientBannerPatientInfo({ patient }: PatientBannerPatientInfoPr
<>
<span>{age(patient.birthDate)}</span>
<span className={styles.separator}>&middot;</span>
<span>{formatDate(parseDate(patient.birthDate), { time: false })}</span>
<span>{formatPartialDate(patient.birthDate, { time: false })}</span>
<span className={styles.separator}>&middot;</span>
</>
)}
Expand Down
3 changes: 3 additions & 0 deletions packages/framework/esm-utils/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ module.exports = {
transform: {
'^.+\\.tsx?$': ['@swc/jest'],
},
moduleNameMapper: {
'lodash-es': 'lodash',
},
testEnvironment: 'jsdom',
testEnvironmentOptions: {
url: 'http://localhost/',
Expand Down
6 changes: 5 additions & 1 deletion packages/framework/esm-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@
},
"devDependencies": {
"@openmrs/esm-globals": "workspace:*",
"@types/lodash-es": "^4.17.12",
"@types/semver": "^7.3.4",
"dayjs": "^1.10.4",
"jest": "^29.7.0",
"rxjs": "^6.5.3"
},
"peerDependencies": {
Expand All @@ -51,8 +53,10 @@
"rxjs": "6.x"
},
"dependencies": {
"@formatjs/intl-durationformat": "^0.2.4",
"@formatjs/intl-durationformat": "^0.7.3",
"@internationalized/date": "^3.5.5",
"any-date-parser": "^2.0.3",
"lodash-es": "^4.17.21",
"semver": "7.3.2"
}
}
Loading

0 comments on commit e2410cf

Please sign in to comment.