@openmrs/esm-utils 3.1.10-pre.97 → 3.1.11-pre.595

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/docs/API.md CHANGED
@@ -4,14 +4,23 @@
4
4
 
5
5
  ## Table of contents
6
6
 
7
+ ### Interfaces
8
+
9
+ - [RetryOptions](interfaces/RetryOptions.md)
10
+
7
11
  ### Type aliases
8
12
 
9
13
  - [DateInput](API.md#dateinput)
10
14
 
11
15
  ### Functions
12
16
 
17
+ - [age](API.md#age)
18
+ - [daysIntoYear](API.md#daysintoyear)
13
19
  - [isOmrsDateStrict](API.md#isomrsdatestrict)
14
20
  - [isOmrsDateToday](API.md#isomrsdatetoday)
21
+ - [isSameDay](API.md#issameday)
22
+ - [isVersionSatisfied](API.md#isversionsatisfied)
23
+ - [retry](API.md#retry)
15
24
  - [toDateObjectStrict](API.md#todateobjectstrict)
16
25
  - [toOmrsDateFormat](API.md#toomrsdateformat)
17
26
  - [toOmrsDayDateFormat](API.md#toomrsdaydateformat)
@@ -19,174 +28,367 @@
19
28
  - [toOmrsTimeString](API.md#toomrstimestring)
20
29
  - [toOmrsTimeString24](API.md#toomrstimestring24)
21
30
  - [toOmrsYearlessDateFormat](API.md#toomrsyearlessdateformat)
31
+ - [translateFrom](API.md#translatefrom)
22
32
 
23
33
  ## Type aliases
24
34
 
25
35
  ### DateInput
26
36
 
27
- Ƭ **DateInput**: *string* \| *number* \| Date
37
+ Ƭ **DateInput**: `string` \| `number` \| `Date`
38
+
39
+ #### Defined in
28
40
 
29
- Defined in: [omrs-dates.ts:8](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/esm-utils/src/omrs-dates.ts#L8)
41
+ [omrs-dates.ts:8](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-utils/src/omrs-dates.ts#L8)
30
42
 
31
43
  ## Functions
32
44
 
45
+ ### age
46
+
47
+ ▸ **age**(`dateString`): `string`
48
+
49
+ Gets a human readable age represention of the provided date string.
50
+
51
+ #### Parameters
52
+
53
+ | Name | Type | Description |
54
+ | :------ | :------ | :------ |
55
+ | `dateString` | `string` | The stringified date. |
56
+
57
+ #### Returns
58
+
59
+ `string`
60
+
61
+ A human-readable string version of the age.
62
+
63
+ #### Defined in
64
+
65
+ [age-helpers.tsx:37](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-utils/src/age-helpers.tsx#L37)
66
+
67
+ ___
68
+
69
+ ### daysIntoYear
70
+
71
+ ▸ **daysIntoYear**(`date`): `number`
72
+
73
+ Gets the number of days in the year of the given date.
74
+
75
+ #### Parameters
76
+
77
+ | Name | Type | Description |
78
+ | :------ | :------ | :------ |
79
+ | `date` | `Date` | The date to compute the days within the year. |
80
+
81
+ #### Returns
82
+
83
+ `number`
84
+
85
+ The number of days.
86
+
87
+ #### Defined in
88
+
89
+ [age-helpers.tsx:6](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-utils/src/age-helpers.tsx#L6)
90
+
91
+ ___
92
+
33
93
  ### isOmrsDateStrict
34
94
 
35
- ▸ **isOmrsDateStrict**(`omrsPayloadString`: *string*): *boolean*
95
+ ▸ **isOmrsDateStrict**(`omrsPayloadString`): `boolean`
36
96
 
37
97
  This function is STRICT on checking whether a date string is the openmrs format.
38
98
  The format should be YYYY-MM-DDTHH:mm:ss.SSSZZ
39
99
 
40
- #### Parameters:
100
+ #### Parameters
41
101
 
42
- Name | Type |
43
- :------ | :------ |
44
- `omrsPayloadString` | *string* |
102
+ | Name | Type |
103
+ | :------ | :------ |
104
+ | `omrsPayloadString` | `string` |
45
105
 
46
- **Returns:** *boolean*
106
+ #### Returns
47
107
 
48
- Defined in: [omrs-dates.ts:16](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/esm-utils/src/omrs-dates.ts#L16)
108
+ `boolean`
109
+
110
+ #### Defined in
111
+
112
+ [omrs-dates.ts:16](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-utils/src/omrs-dates.ts#L16)
49
113
 
50
114
  ___
51
115
 
52
116
  ### isOmrsDateToday
53
117
 
54
- ▸ **isOmrsDateToday**(`date`: [*DateInput*](API.md#dateinput)): *boolean*
118
+ ▸ **isOmrsDateToday**(`date`): `boolean`
119
+
120
+ #### Parameters
55
121
 
56
- #### Parameters:
122
+ | Name | Type | Description |
123
+ | :------ | :------ | :------ |
124
+ | `date` | [`DateInput`](API.md#dateinput) | Checks if the provided date is today. |
57
125
 
58
- Name | Type | Description |
59
- :------ | :------ | :------ |
60
- `date` | [*DateInput*](API.md#dateinput) | Checks if the provided date is today. |
126
+ #### Returns
61
127
 
62
- **Returns:** *boolean*
128
+ `boolean`
63
129
 
64
- Defined in: [omrs-dates.ts:53](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/esm-utils/src/omrs-dates.ts#L53)
130
+ #### Defined in
131
+
132
+ [omrs-dates.ts:53](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-utils/src/omrs-dates.ts#L53)
133
+
134
+ ___
135
+
136
+ ### isSameDay
137
+
138
+ ▸ **isSameDay**(`firstDate`, `secondDate`): `boolean`
139
+
140
+ Checks if two dates are representing the same day.
141
+
142
+ #### Parameters
143
+
144
+ | Name | Type | Description |
145
+ | :------ | :------ | :------ |
146
+ | `firstDate` | `Date` | The first date. |
147
+ | `secondDate` | `Date` | The second date. |
148
+
149
+ #### Returns
150
+
151
+ `boolean`
152
+
153
+ True if both are located on the same day.
154
+
155
+ #### Defined in
156
+
157
+ [age-helpers.tsx:23](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-utils/src/age-helpers.tsx#L23)
158
+
159
+ ___
160
+
161
+ ### isVersionSatisfied
162
+
163
+ ▸ **isVersionSatisfied**(`requiredVersion`, `installedVersion`): `boolean`
164
+
165
+ #### Parameters
166
+
167
+ | Name | Type |
168
+ | :------ | :------ |
169
+ | `requiredVersion` | `string` |
170
+ | `installedVersion` | `string` |
171
+
172
+ #### Returns
173
+
174
+ `boolean`
175
+
176
+ #### Defined in
177
+
178
+ [version.ts:21](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-utils/src/version.ts#L21)
179
+
180
+ ___
181
+
182
+ ### retry
183
+
184
+ ▸ **retry**<`T`\>(`fn`, `options?`): `Promise`<`T`\>
185
+
186
+ Executes the specified function and retries executing on failure with a custom backoff strategy
187
+ defined by the options.
188
+
189
+ If not configured otherwise, this function uses the following default options:
190
+ * Retries 5 times beyond the initial attempt.
191
+ * Uses an exponential backoff starting with an initial delay of 1000ms.
192
+
193
+ **`throws`** Rethrows the final error of running `fn` when the function stops retrying.
194
+
195
+ #### Type parameters
196
+
197
+ | Name |
198
+ | :------ |
199
+ | `T` |
200
+
201
+ #### Parameters
202
+
203
+ | Name | Type | Description |
204
+ | :------ | :------ | :------ |
205
+ | `fn` | () => `Promise`<`T`\> | The function to be executed and retried on failure. |
206
+ | `options` | [`RetryOptions`](interfaces/RetryOptions.md) | Additional options which configure the retry behavior. |
207
+
208
+ #### Returns
209
+
210
+ `Promise`<`T`\>
211
+
212
+ The result of successfully executing `fn`.
213
+
214
+ #### Defined in
215
+
216
+ [retry.ts:38](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-utils/src/retry.ts#L38)
65
217
 
66
218
  ___
67
219
 
68
220
  ### toDateObjectStrict
69
221
 
70
- ▸ **toDateObjectStrict**(`omrsDateString`: *string*): Date \| *null*
222
+ ▸ **toDateObjectStrict**(`omrsDateString`): `Date` \| ``null``
71
223
 
72
224
  Converts the object to a date object if it is a valid ISO date time string.
73
225
 
74
- #### Parameters:
226
+ #### Parameters
227
+
228
+ | Name | Type |
229
+ | :------ | :------ |
230
+ | `omrsDateString` | `string` |
75
231
 
76
- Name | Type |
77
- :------ | :------ |
78
- `omrsDateString` | *string* |
232
+ #### Returns
79
233
 
80
- **Returns:** Date \| *null*
234
+ `Date` \| ``null``
81
235
 
82
- Defined in: [omrs-dates.ts:60](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/esm-utils/src/omrs-dates.ts#L60)
236
+ #### Defined in
237
+
238
+ [omrs-dates.ts:60](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-utils/src/omrs-dates.ts#L60)
83
239
 
84
240
  ___
85
241
 
86
242
  ### toOmrsDateFormat
87
243
 
88
- ▸ **toOmrsDateFormat**(`date`: [*DateInput*](API.md#dateinput), `format?`: *string*): *string*
244
+ ▸ **toOmrsDateFormat**(`date`, `format?`): `string`
89
245
 
90
246
  Formats the input as a date string. By default the format "YYYY-MMM-DD" is used.
91
247
 
92
- #### Parameters:
248
+ #### Parameters
249
+
250
+ | Name | Type | Default value |
251
+ | :------ | :------ | :------ |
252
+ | `date` | [`DateInput`](API.md#dateinput) | `undefined` |
253
+ | `format` | `string` | `"YYYY-MMM-DD"` |
254
+
255
+ #### Returns
93
256
 
94
- Name | Type | Default value |
95
- :------ | :------ | :------ |
96
- `date` | [*DateInput*](API.md#dateinput) | - |
97
- `format` | *string* | "YYYY-MMM-DD" |
257
+ `string`
98
258
 
99
- **Returns:** *string*
259
+ #### Defined in
100
260
 
101
- Defined in: [omrs-dates.ts:112](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/esm-utils/src/omrs-dates.ts#L112)
261
+ [omrs-dates.ts:112](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-utils/src/omrs-dates.ts#L112)
102
262
 
103
263
  ___
104
264
 
105
265
  ### toOmrsDayDateFormat
106
266
 
107
- ▸ **toOmrsDayDateFormat**(`date`: [*DateInput*](API.md#dateinput)): *string*
267
+ ▸ **toOmrsDayDateFormat**(`date`): `string`
108
268
 
109
269
  Formats the input as a date string using the format "DD - MMM - YYYY".
110
270
 
111
- #### Parameters:
271
+ #### Parameters
112
272
 
113
- Name | Type |
114
- :------ | :------ |
115
- `date` | [*DateInput*](API.md#dateinput) |
273
+ | Name | Type |
274
+ | :------ | :------ |
275
+ | `date` | [`DateInput`](API.md#dateinput) |
116
276
 
117
- **Returns:** *string*
277
+ #### Returns
118
278
 
119
- Defined in: [omrs-dates.ts:98](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/esm-utils/src/omrs-dates.ts#L98)
279
+ `string`
280
+
281
+ #### Defined in
282
+
283
+ [omrs-dates.ts:98](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-utils/src/omrs-dates.ts#L98)
120
284
 
121
285
  ___
122
286
 
123
287
  ### toOmrsIsoString
124
288
 
125
- ▸ **toOmrsIsoString**(`date`: [*DateInput*](API.md#dateinput), `toUTC?`: *boolean*): *string*
289
+ ▸ **toOmrsIsoString**(`date`, `toUTC?`): `string`
126
290
 
127
291
  Formats the input as a date time string using the format "YYYY-MM-DDTHH:mm:ss.SSSZZ".
128
292
 
129
- #### Parameters:
293
+ #### Parameters
294
+
295
+ | Name | Type | Default value |
296
+ | :------ | :------ | :------ |
297
+ | `date` | [`DateInput`](API.md#dateinput) | `undefined` |
298
+ | `toUTC` | `boolean` | `false` |
130
299
 
131
- Name | Type | Default value |
132
- :------ | :------ | :------ |
133
- `date` | [*DateInput*](API.md#dateinput) | - |
134
- `toUTC` | *boolean* | false |
300
+ #### Returns
135
301
 
136
- **Returns:** *string*
302
+ `string`
137
303
 
138
- Defined in: [omrs-dates.ts:71](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/esm-utils/src/omrs-dates.ts#L71)
304
+ #### Defined in
305
+
306
+ [omrs-dates.ts:71](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-utils/src/omrs-dates.ts#L71)
139
307
 
140
308
  ___
141
309
 
142
310
  ### toOmrsTimeString
143
311
 
144
- ▸ **toOmrsTimeString**(`date`: [*DateInput*](API.md#dateinput)): *string*
312
+ ▸ **toOmrsTimeString**(`date`): `string`
145
313
 
146
314
  Formats the input as a time string using the format "HH:mm A".
147
315
 
148
- #### Parameters:
316
+ #### Parameters
317
+
318
+ | Name | Type |
319
+ | :------ | :------ |
320
+ | `date` | [`DateInput`](API.md#dateinput) |
321
+
322
+ #### Returns
149
323
 
150
- Name | Type |
151
- :------ | :------ |
152
- `date` | [*DateInput*](API.md#dateinput) |
324
+ `string`
153
325
 
154
- **Returns:** *string*
326
+ #### Defined in
155
327
 
156
- Defined in: [omrs-dates.ts:91](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/esm-utils/src/omrs-dates.ts#L91)
328
+ [omrs-dates.ts:91](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-utils/src/omrs-dates.ts#L91)
157
329
 
158
330
  ___
159
331
 
160
332
  ### toOmrsTimeString24
161
333
 
162
- ▸ **toOmrsTimeString24**(`date`: [*DateInput*](API.md#dateinput)): *string*
334
+ ▸ **toOmrsTimeString24**(`date`): `string`
163
335
 
164
336
  Formats the input as a time string using the format "HH:mm".
165
337
 
166
- #### Parameters:
338
+ #### Parameters
167
339
 
168
- Name | Type |
169
- :------ | :------ |
170
- `date` | [*DateInput*](API.md#dateinput) |
340
+ | Name | Type |
341
+ | :------ | :------ |
342
+ | `date` | [`DateInput`](API.md#dateinput) |
171
343
 
172
- **Returns:** *string*
344
+ #### Returns
173
345
 
174
- Defined in: [omrs-dates.ts:84](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/esm-utils/src/omrs-dates.ts#L84)
346
+ `string`
347
+
348
+ #### Defined in
349
+
350
+ [omrs-dates.ts:84](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-utils/src/omrs-dates.ts#L84)
175
351
 
176
352
  ___
177
353
 
178
354
  ### toOmrsYearlessDateFormat
179
355
 
180
- ▸ **toOmrsYearlessDateFormat**(`date`: [*DateInput*](API.md#dateinput)): *string*
356
+ ▸ **toOmrsYearlessDateFormat**(`date`): `string`
181
357
 
182
358
  Formats the input as a date string using the format "DD-MMM".
183
359
 
184
- #### Parameters:
360
+ #### Parameters
361
+
362
+ | Name | Type |
363
+ | :------ | :------ |
364
+ | `date` | [`DateInput`](API.md#dateinput) |
365
+
366
+ #### Returns
367
+
368
+ `string`
369
+
370
+ #### Defined in
371
+
372
+ [omrs-dates.ts:105](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-utils/src/omrs-dates.ts#L105)
373
+
374
+ ___
375
+
376
+ ### translateFrom
377
+
378
+ ▸ **translateFrom**(`moduleName`, `key`, `fallback?`): `string`
379
+
380
+ #### Parameters
381
+
382
+ | Name | Type |
383
+ | :------ | :------ |
384
+ | `moduleName` | `string` |
385
+ | `key` | `string` |
386
+ | `fallback?` | `string` |
387
+
388
+ #### Returns
185
389
 
186
- Name | Type |
187
- :------ | :------ |
188
- `date` | [*DateInput*](API.md#dateinput) |
390
+ `string`
189
391
 
190
- **Returns:** *string*
392
+ #### Defined in
191
393
 
192
- Defined in: [omrs-dates.ts:105](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/esm-utils/src/omrs-dates.ts#L105)
394
+ [translate.ts:3](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-utils/src/translate.ts#L3)
@@ -0,0 +1,83 @@
1
+ [@openmrs/esm-utils](../API.md) / RetryOptions
2
+
3
+ # Interface: RetryOptions
4
+
5
+ Options for configuring the behavior of the [retry](../API.md#retry) function.
6
+
7
+ ## Table of contents
8
+
9
+ ### Methods
10
+
11
+ - [getDelay](RetryOptions.md#getdelay)
12
+ - [onError](RetryOptions.md#onerror)
13
+ - [shouldRetry](RetryOptions.md#shouldretry)
14
+
15
+ ## Methods
16
+
17
+ ### getDelay
18
+
19
+ ▸ `Optional` **getDelay**(`attempt`): `number`
20
+
21
+ Calculates the next delay (in milliseconds) before a retry attempt.
22
+ Returning a value for the inital attempt (`0`) delays the initial function invocation.
23
+
24
+ #### Parameters
25
+
26
+ | Name | Type | Description |
27
+ | :------ | :------ | :------ |
28
+ | `attempt` | `number` | The current (zero-based) retry attempt. `0` indicates the initial attempt. |
29
+
30
+ #### Returns
31
+
32
+ `number`
33
+
34
+ #### Defined in
35
+
36
+ [retry.ts:16](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-utils/src/retry.ts#L16)
37
+
38
+ ___
39
+
40
+ ### onError
41
+
42
+ ▸ `Optional` **onError**(`e`, `attempt`): `void`
43
+
44
+ Called when invoking the function resulted in an error.
45
+ Allows running side-effects on errors, e.g. logging.
46
+
47
+ #### Parameters
48
+
49
+ | Name | Type | Description |
50
+ | :------ | :------ | :------ |
51
+ | `e` | `any` | The error thrown by the function. |
52
+ | `attempt` | `number` | The current (zero-based) retry attempt. `0` indicates the initial attempt. |
53
+
54
+ #### Returns
55
+
56
+ `void`
57
+
58
+ #### Defined in
59
+
60
+ [retry.ts:23](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-utils/src/retry.ts#L23)
61
+
62
+ ___
63
+
64
+ ### shouldRetry
65
+
66
+ ▸ `Optional` **shouldRetry**(`attempt`): `any`
67
+
68
+ Determines whether the retry function should retry executing the function after it failed
69
+ with an error on the current attempt.
70
+
71
+ #### Parameters
72
+
73
+ | Name | Type | Description |
74
+ | :------ | :------ | :------ |
75
+ | `attempt` | `number` | The current (zero-based) retry attempt. `0` indicates the initial attempt. |
76
+
77
+ #### Returns
78
+
79
+ `any`
80
+
81
+ #### Defined in
82
+
83
+ [retry.ts:10](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-utils/src/retry.ts#L10)
package/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "name": "@openmrs/esm-utils",
3
- "version": "3.1.10-pre.97",
3
+ "version": "3.1.11-pre.595",
4
4
  "license": "MPL-2.0",
5
5
  "description": "Helper utilities for OpenMRS",
6
6
  "browser": "dist/openmrs-esm-utils.js",
7
7
  "main": "src/index.ts",
8
8
  "source": true,
9
+ "sideEffects": false,
9
10
  "scripts": {
10
- "document": "../../document.sh esm-utils",
11
+ "document": "../../../document.sh esm-utils",
11
12
  "test": "jest --config jest.config.js --passWithNoTests",
12
13
  "build": "webpack --mode=production",
13
14
  "analyze": "webpack --mode=production --env.analyze=true",
14
15
  "typescript": "tsc",
15
- "lint": "eslint src --ext ts,tsx",
16
- "format": "prettier --write src/**"
16
+ "lint": "eslint src --ext ts,tsx"
17
17
  },
18
18
  "keywords": [
19
19
  "openmrs",
@@ -38,13 +38,16 @@
38
38
  "access": "public"
39
39
  },
40
40
  "devDependencies": {
41
- "dayjs": "^1.10.4"
41
+ "@types/semver": "^7.3.4",
42
+ "dayjs": "^1.10.4",
43
+ "rxjs": "^6.5.3"
42
44
  },
43
45
  "peerDependencies": {
44
- "dayjs": "1.x"
46
+ "dayjs": "1.x",
47
+ "rxjs": "6.x"
45
48
  },
46
49
  "dependencies": {
47
- "systemjs-webpack-interop": "^2.1.2"
50
+ "semver": "^7.3.2"
48
51
  },
49
- "gitHead": "23be3c2711bccfde941c0fa823544c72ac6dfc08"
52
+ "gitHead": "c2141b8223409e20051338ee5109eb4e40cd19f0"
50
53
  }
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Gets the number of days in the year of the given date.
3
+ * @param date The date to compute the days within the year.
4
+ * @returns The number of days.
5
+ */
6
+ export function daysIntoYear(date: Date) {
7
+ return (
8
+ (Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()) -
9
+ Date.UTC(date.getUTCFullYear(), 0, 0)) /
10
+ 24 /
11
+ 60 /
12
+ 60 /
13
+ 1000
14
+ );
15
+ }
16
+
17
+ /**
18
+ * Checks if two dates are representing the same day.
19
+ * @param firstDate The first date.
20
+ * @param secondDate The second date.
21
+ * @returns True if both are located on the same day.
22
+ */
23
+ export function isSameDay(firstDate: Date, secondDate: Date) {
24
+ const firstISO = firstDate.toISOString();
25
+ const secondISO = secondDate.toISOString();
26
+ return (
27
+ firstISO.slice(0, firstISO.indexOf("T")) ===
28
+ secondISO.slice(0, secondISO.indexOf("T"))
29
+ );
30
+ }
31
+
32
+ /**
33
+ * Gets a human readable age represention of the provided date string.
34
+ * @param dateString The stringified date.
35
+ * @returns A human-readable string version of the age.
36
+ */
37
+ export function age(dateString: string): string {
38
+ // Based on http://www.gregoryschmidt.ca/writing/patient-age-display-ehr-conventions,
39
+ // which is different from npm packages such as https://www.npmjs.com/package/timeago
40
+
41
+ // First calculate the age in years
42
+ const today = new Date();
43
+ const birthDate = new Date(dateString);
44
+ const monthDifference = today.getUTCMonth() - birthDate.getUTCMonth();
45
+ const dateDifference = today.getUTCDate() - birthDate.getUTCDate();
46
+ let age = today.getUTCFullYear() - birthDate.getUTCFullYear();
47
+ if (monthDifference < 0 || (monthDifference === 0 && dateDifference < 0)) {
48
+ age--;
49
+ }
50
+
51
+ // Now calculate the number of months in addition to the year's age
52
+ let monthsAgo = monthDifference >= 0 ? monthDifference : monthDifference + 12;
53
+ if (dateDifference < 0) {
54
+ monthsAgo--;
55
+ }
56
+ const monthsAgoStr = monthsAgo > 0 ? `${monthsAgo} mo` : "";
57
+
58
+ // For patients less than a year old, we calculate the number of days/weeks they have been alive
59
+ let totalDaysAgo = daysIntoYear(today) - daysIntoYear(birthDate);
60
+ if (totalDaysAgo < 0) {
61
+ totalDaysAgo += 365;
62
+ }
63
+ const weeksAgo = Math.floor(totalDaysAgo / 7);
64
+
65
+ // The "remainder" number of days after the weeksAgo number of weeks
66
+ const remainderDaysInWeek = totalDaysAgo - weeksAgo * 7;
67
+
68
+ // Depending on their age, return a different representation of their age.
69
+ if (age === 0) {
70
+ if (isSameDay(today, birthDate)) {
71
+ return `Today`;
72
+ } else if (weeksAgo < 4) {
73
+ return `${totalDaysAgo} day`;
74
+ } else if (weeksAgo === 0) {
75
+ return `${totalDaysAgo} day`;
76
+ } else if (remainderDaysInWeek > 0) {
77
+ return `${weeksAgo} wk ${remainderDaysInWeek} d`;
78
+ } else {
79
+ return `${weeksAgo} week`;
80
+ }
81
+ } else if (age < 2) {
82
+ return `${monthsAgoStr} ${age} yr`.trim();
83
+ } else if (age < 18) {
84
+ return `${age} yr ${monthsAgoStr}`.trim();
85
+ } else {
86
+ return `${age} years`;
87
+ }
88
+ }
package/src/index.ts CHANGED
@@ -1 +1,5 @@
1
+ export * from "./age-helpers";
1
2
  export * from "./omrs-dates";
3
+ export * from "./translate";
4
+ export * from "./version";
5
+ export * from "./retry";