@openmrs/esm-utils 3.1.10-pre.92 → 3.1.12-pre.598
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/README.md +2 -5
- package/dist/openmrs-esm-utils.js +1 -1
- package/dist/openmrs-esm-utils.js.map +1 -1
- package/docs/API.md +394 -0
- package/docs/interfaces/RetryOptions.md +83 -0
- package/package.json +11 -7
- package/src/age-helpers.tsx +88 -0
- package/src/index.ts +4 -0
- package/src/omrs-dates.test.ts +33 -1
- package/src/retry.ts +83 -0
- package/src/translate.ts +13 -0
- package/src/version.test.ts +53 -0
- package/src/version.ts +30 -0
- package/typedoc.json +7 -0
- package/webpack.config.js +5 -11
- package/src/set-public-path.ts +0 -3
package/docs/API.md
ADDED
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
[Back to README.md](../README.md)
|
|
2
|
+
|
|
3
|
+
# @openmrs/esm-utils
|
|
4
|
+
|
|
5
|
+
## Table of contents
|
|
6
|
+
|
|
7
|
+
### Interfaces
|
|
8
|
+
|
|
9
|
+
- [RetryOptions](interfaces/RetryOptions.md)
|
|
10
|
+
|
|
11
|
+
### Type aliases
|
|
12
|
+
|
|
13
|
+
- [DateInput](API.md#dateinput)
|
|
14
|
+
|
|
15
|
+
### Functions
|
|
16
|
+
|
|
17
|
+
- [age](API.md#age)
|
|
18
|
+
- [daysIntoYear](API.md#daysintoyear)
|
|
19
|
+
- [isOmrsDateStrict](API.md#isomrsdatestrict)
|
|
20
|
+
- [isOmrsDateToday](API.md#isomrsdatetoday)
|
|
21
|
+
- [isSameDay](API.md#issameday)
|
|
22
|
+
- [isVersionSatisfied](API.md#isversionsatisfied)
|
|
23
|
+
- [retry](API.md#retry)
|
|
24
|
+
- [toDateObjectStrict](API.md#todateobjectstrict)
|
|
25
|
+
- [toOmrsDateFormat](API.md#toomrsdateformat)
|
|
26
|
+
- [toOmrsDayDateFormat](API.md#toomrsdaydateformat)
|
|
27
|
+
- [toOmrsIsoString](API.md#toomrsisostring)
|
|
28
|
+
- [toOmrsTimeString](API.md#toomrstimestring)
|
|
29
|
+
- [toOmrsTimeString24](API.md#toomrstimestring24)
|
|
30
|
+
- [toOmrsYearlessDateFormat](API.md#toomrsyearlessdateformat)
|
|
31
|
+
- [translateFrom](API.md#translatefrom)
|
|
32
|
+
|
|
33
|
+
## Type aliases
|
|
34
|
+
|
|
35
|
+
### DateInput
|
|
36
|
+
|
|
37
|
+
Ƭ **DateInput**: `string` \| `number` \| `Date`
|
|
38
|
+
|
|
39
|
+
#### Defined in
|
|
40
|
+
|
|
41
|
+
[omrs-dates.ts:8](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-utils/src/omrs-dates.ts#L8)
|
|
42
|
+
|
|
43
|
+
## Functions
|
|
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
|
+
|
|
93
|
+
### isOmrsDateStrict
|
|
94
|
+
|
|
95
|
+
▸ **isOmrsDateStrict**(`omrsPayloadString`): `boolean`
|
|
96
|
+
|
|
97
|
+
This function is STRICT on checking whether a date string is the openmrs format.
|
|
98
|
+
The format should be YYYY-MM-DDTHH:mm:ss.SSSZZ
|
|
99
|
+
|
|
100
|
+
#### Parameters
|
|
101
|
+
|
|
102
|
+
| Name | Type |
|
|
103
|
+
| :------ | :------ |
|
|
104
|
+
| `omrsPayloadString` | `string` |
|
|
105
|
+
|
|
106
|
+
#### Returns
|
|
107
|
+
|
|
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)
|
|
113
|
+
|
|
114
|
+
___
|
|
115
|
+
|
|
116
|
+
### isOmrsDateToday
|
|
117
|
+
|
|
118
|
+
▸ **isOmrsDateToday**(`date`): `boolean`
|
|
119
|
+
|
|
120
|
+
#### Parameters
|
|
121
|
+
|
|
122
|
+
| Name | Type | Description |
|
|
123
|
+
| :------ | :------ | :------ |
|
|
124
|
+
| `date` | [`DateInput`](API.md#dateinput) | Checks if the provided date is today. |
|
|
125
|
+
|
|
126
|
+
#### Returns
|
|
127
|
+
|
|
128
|
+
`boolean`
|
|
129
|
+
|
|
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)
|
|
217
|
+
|
|
218
|
+
___
|
|
219
|
+
|
|
220
|
+
### toDateObjectStrict
|
|
221
|
+
|
|
222
|
+
▸ **toDateObjectStrict**(`omrsDateString`): `Date` \| ``null``
|
|
223
|
+
|
|
224
|
+
Converts the object to a date object if it is a valid ISO date time string.
|
|
225
|
+
|
|
226
|
+
#### Parameters
|
|
227
|
+
|
|
228
|
+
| Name | Type |
|
|
229
|
+
| :------ | :------ |
|
|
230
|
+
| `omrsDateString` | `string` |
|
|
231
|
+
|
|
232
|
+
#### Returns
|
|
233
|
+
|
|
234
|
+
`Date` \| ``null``
|
|
235
|
+
|
|
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)
|
|
239
|
+
|
|
240
|
+
___
|
|
241
|
+
|
|
242
|
+
### toOmrsDateFormat
|
|
243
|
+
|
|
244
|
+
▸ **toOmrsDateFormat**(`date`, `format?`): `string`
|
|
245
|
+
|
|
246
|
+
Formats the input as a date string. By default the format "YYYY-MMM-DD" is used.
|
|
247
|
+
|
|
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
|
|
256
|
+
|
|
257
|
+
`string`
|
|
258
|
+
|
|
259
|
+
#### Defined in
|
|
260
|
+
|
|
261
|
+
[omrs-dates.ts:112](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-utils/src/omrs-dates.ts#L112)
|
|
262
|
+
|
|
263
|
+
___
|
|
264
|
+
|
|
265
|
+
### toOmrsDayDateFormat
|
|
266
|
+
|
|
267
|
+
▸ **toOmrsDayDateFormat**(`date`): `string`
|
|
268
|
+
|
|
269
|
+
Formats the input as a date string using the format "DD - MMM - YYYY".
|
|
270
|
+
|
|
271
|
+
#### Parameters
|
|
272
|
+
|
|
273
|
+
| Name | Type |
|
|
274
|
+
| :------ | :------ |
|
|
275
|
+
| `date` | [`DateInput`](API.md#dateinput) |
|
|
276
|
+
|
|
277
|
+
#### Returns
|
|
278
|
+
|
|
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)
|
|
284
|
+
|
|
285
|
+
___
|
|
286
|
+
|
|
287
|
+
### toOmrsIsoString
|
|
288
|
+
|
|
289
|
+
▸ **toOmrsIsoString**(`date`, `toUTC?`): `string`
|
|
290
|
+
|
|
291
|
+
Formats the input as a date time string using the format "YYYY-MM-DDTHH:mm:ss.SSSZZ".
|
|
292
|
+
|
|
293
|
+
#### Parameters
|
|
294
|
+
|
|
295
|
+
| Name | Type | Default value |
|
|
296
|
+
| :------ | :------ | :------ |
|
|
297
|
+
| `date` | [`DateInput`](API.md#dateinput) | `undefined` |
|
|
298
|
+
| `toUTC` | `boolean` | `false` |
|
|
299
|
+
|
|
300
|
+
#### Returns
|
|
301
|
+
|
|
302
|
+
`string`
|
|
303
|
+
|
|
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)
|
|
307
|
+
|
|
308
|
+
___
|
|
309
|
+
|
|
310
|
+
### toOmrsTimeString
|
|
311
|
+
|
|
312
|
+
▸ **toOmrsTimeString**(`date`): `string`
|
|
313
|
+
|
|
314
|
+
Formats the input as a time string using the format "HH:mm A".
|
|
315
|
+
|
|
316
|
+
#### Parameters
|
|
317
|
+
|
|
318
|
+
| Name | Type |
|
|
319
|
+
| :------ | :------ |
|
|
320
|
+
| `date` | [`DateInput`](API.md#dateinput) |
|
|
321
|
+
|
|
322
|
+
#### Returns
|
|
323
|
+
|
|
324
|
+
`string`
|
|
325
|
+
|
|
326
|
+
#### Defined in
|
|
327
|
+
|
|
328
|
+
[omrs-dates.ts:91](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-utils/src/omrs-dates.ts#L91)
|
|
329
|
+
|
|
330
|
+
___
|
|
331
|
+
|
|
332
|
+
### toOmrsTimeString24
|
|
333
|
+
|
|
334
|
+
▸ **toOmrsTimeString24**(`date`): `string`
|
|
335
|
+
|
|
336
|
+
Formats the input as a time string using the format "HH:mm".
|
|
337
|
+
|
|
338
|
+
#### Parameters
|
|
339
|
+
|
|
340
|
+
| Name | Type |
|
|
341
|
+
| :------ | :------ |
|
|
342
|
+
| `date` | [`DateInput`](API.md#dateinput) |
|
|
343
|
+
|
|
344
|
+
#### Returns
|
|
345
|
+
|
|
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)
|
|
351
|
+
|
|
352
|
+
___
|
|
353
|
+
|
|
354
|
+
### toOmrsYearlessDateFormat
|
|
355
|
+
|
|
356
|
+
▸ **toOmrsYearlessDateFormat**(`date`): `string`
|
|
357
|
+
|
|
358
|
+
Formats the input as a date string using the format "DD-MMM".
|
|
359
|
+
|
|
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
|
|
389
|
+
|
|
390
|
+
`string`
|
|
391
|
+
|
|
392
|
+
#### Defined in
|
|
393
|
+
|
|
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,18 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openmrs/esm-utils",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.12-pre.598",
|
|
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": {
|
|
11
|
+
"document": "../../../document.sh esm-utils",
|
|
10
12
|
"test": "jest --config jest.config.js --passWithNoTests",
|
|
11
13
|
"build": "webpack --mode=production",
|
|
12
14
|
"analyze": "webpack --mode=production --env.analyze=true",
|
|
13
15
|
"typescript": "tsc",
|
|
14
|
-
"lint": "eslint src --ext ts,tsx"
|
|
15
|
-
"format": "prettier --write src/**"
|
|
16
|
+
"lint": "eslint src --ext ts,tsx"
|
|
16
17
|
},
|
|
17
18
|
"keywords": [
|
|
18
19
|
"openmrs",
|
|
@@ -37,13 +38,16 @@
|
|
|
37
38
|
"access": "public"
|
|
38
39
|
},
|
|
39
40
|
"devDependencies": {
|
|
40
|
-
"
|
|
41
|
+
"@types/semver": "^7.3.4",
|
|
42
|
+
"dayjs": "^1.10.4",
|
|
43
|
+
"rxjs": "^6.5.3"
|
|
41
44
|
},
|
|
42
45
|
"peerDependencies": {
|
|
43
|
-
"dayjs": "1.x"
|
|
46
|
+
"dayjs": "1.x",
|
|
47
|
+
"rxjs": "6.x"
|
|
44
48
|
},
|
|
45
49
|
"dependencies": {
|
|
46
|
-
"
|
|
50
|
+
"semver": "^7.3.2"
|
|
47
51
|
},
|
|
48
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "ddb157645863df8cc29a26928bb29105b8c19119"
|
|
49
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
package/src/omrs-dates.test.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
toOmrsIsoString,
|
|
3
|
+
toDateObjectStrict,
|
|
4
|
+
isOmrsDateStrict,
|
|
5
|
+
} from "./omrs-dates";
|
|
2
6
|
import dayjs from "dayjs";
|
|
3
7
|
|
|
4
8
|
describe("Openmrs Dates", () => {
|
|
@@ -9,4 +13,32 @@ describe("Openmrs Dates", () => {
|
|
|
9
13
|
).toDate();
|
|
10
14
|
expect(toOmrsIsoString(date, true)).toEqual("2018-03-18T21:05:03.999+0000");
|
|
11
15
|
});
|
|
16
|
+
|
|
17
|
+
it("checks if a string is openmrs date", () => {
|
|
18
|
+
expect(isOmrsDateStrict("2018-03-19T00:00:00.000+0300")).toEqual(true);
|
|
19
|
+
expect(isOmrsDateStrict(" 2018-03-19T00:00:00.000+0300 ")).toEqual(true);
|
|
20
|
+
// the exclusion test cases are important for strictness
|
|
21
|
+
expect(isOmrsDateStrict("2018-03-19 00:00:00.000+0300")).toEqual(false);
|
|
22
|
+
expect(isOmrsDateStrict("2018-03-19T00:00:00.000+03:00")).toEqual(false);
|
|
23
|
+
expect(isOmrsDateStrict("2018-03-19T00:00:00.000 0300")).toEqual(false);
|
|
24
|
+
expect(isOmrsDateStrict("2018-03-19T00:00:00 000+0300")).toEqual(false);
|
|
25
|
+
expect(isOmrsDateStrict("2018-03-1")).toEqual(false);
|
|
26
|
+
expect(isOmrsDateStrict("")).toEqual(false);
|
|
27
|
+
expect(isOmrsDateStrict(null as any)).toEqual(false);
|
|
28
|
+
expect(isOmrsDateStrict(undefined as any)).toEqual(false);
|
|
29
|
+
});
|
|
30
|
+
it("converts omrs date string version to js Date object", () => {
|
|
31
|
+
expect(
|
|
32
|
+
toDateObjectStrict("2018-03-19T00:00:00.000+0300")?.toUTCString()
|
|
33
|
+
).toEqual("Sun, 18 Mar 2018 21:00:00 GMT");
|
|
34
|
+
expect(toDateObjectStrict("2018-03-19")).toEqual(null);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("converts js Date object to omrs date string version", () => {
|
|
38
|
+
var date = dayjs(
|
|
39
|
+
"2018-03-19T00:05:03.999+0300",
|
|
40
|
+
"YYYY-MM-DDTHH:mm:ss.SSSZZ"
|
|
41
|
+
).toDate();
|
|
42
|
+
expect(toOmrsIsoString(date, true)).toEqual("2018-03-18T21:05:03.999+0000");
|
|
43
|
+
});
|
|
12
44
|
});
|