fhir-dosage-utils 1.0.0 → 1.1.1

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/dist/main.js CHANGED
@@ -72,43 +72,77 @@ function $5ab57a471c359130$export$7de3722258b87db9({ config: config, dos: dos, i
72
72
  }
73
73
 
74
74
 
75
- // types
76
- function $9a073aa5b09aa778$export$4cd16590995531a9({ dos: dos, config: config, i18next: i18next }) {
77
- // If empty, return undefined
78
- if (dos.doseAndRate === undefined) return undefined;
79
- // Find the first entry that match criteria
80
- let doseRange = dos.doseAndRate.find((s)=>s.doseRange !== undefined);
81
- // If not found, skip
82
- if (doseRange === undefined) return undefined;
83
- let low = doseRange.doseRange.low?.value;
84
- let high = doseRange.doseRange.high?.value;
85
- let quantityUnit = doseRange.doseRange.high !== undefined ? doseRange.doseRange.high : doseRange.doseRange.low;
86
- // quantity unit
87
- let unit = config.fromFHIRQuantityUnitToString({
75
+ // Utility function
76
+ // Quantity unit to string
77
+ function $9ee880701f47723b$var$transformQuantityUnitToString(i18next, quantity, config) {
78
+ let quantityValue = quantity.value;
79
+ // If common units from HL7, do the job
80
+ if (quantity.system === "http://hl7.org/fhir/ValueSet/duration-units") {
81
+ let code = quantity.code;
82
+ return i18next.t(`unitsOfTime:withoutCount.${code}`, {
83
+ count: quantityValue
84
+ });
85
+ } else // otherwise, it is UCUM, ... so let the user do the job
86
+ return config.fromFHIRQuantityUnitToString({
88
87
  language: config.language,
89
- quantity: quantityUnit
88
+ quantity: quantity
90
89
  });
91
- // Three cases
92
- // 1. Both low & high are present
93
- if (high !== undefined && low !== undefined) return i18next.t("fields.doseRange.lowAndHigh", {
94
- low: low,
95
- high: high,
90
+ }
91
+ function $9ee880701f47723b$export$be17d167ed50d870({ range: range, config: config, i18next: i18next }) {
92
+ // Extract params
93
+ const { low: low, high: high } = range;
94
+ const lowValue = low?.value;
95
+ const highValue = high?.value;
96
+ // prepare unit display
97
+ let quantityUnit = high || low;
98
+ let hasUnit = quantityUnit?.unit !== undefined || quantityUnit?.code !== undefined;
99
+ // Four cases
100
+ // 1. If we have a empty object, return undefined
101
+ if (lowValue === undefined && highValue === undefined) return undefined;
102
+ // quantity unit
103
+ let unit = hasUnit ? $9ee880701f47723b$var$transformQuantityUnitToString(i18next, quantityUnit, config) : "";
104
+ let technicalKey = hasUnit ? "withUnit" : "withoutUnit";
105
+ // 2. Both low & high are present
106
+ if (lowValue !== undefined && highValue !== undefined) return i18next.t(`amount.range.${technicalKey}.lowAndHigh`, {
107
+ low: lowValue,
108
+ high: highValue,
96
109
  unit: unit
97
110
  });
98
- // 2. Only high is present
99
- if (high !== undefined) return i18next.t("fields.doseRange.onlyHigh", {
100
- high: high,
111
+ // 3. Only high is present
112
+ if (highValue !== undefined) return i18next.t(`amount.range.${technicalKey}.onlyHigh`, {
113
+ high: highValue,
101
114
  unit: unit
102
115
  });
103
- // 3. Only low is present
116
+ // 4. Only low is present
104
117
  // Warning, this case is kind dangerous and clinically unsafe so minimal effort on this ...
105
- return i18next.t("fields.doseRange.onlyLow", {
106
- low: low,
118
+ return i18next.t(`amount.range.${technicalKey}.onlyLow`, {
119
+ low: lowValue,
107
120
  unit: unit
108
121
  });
109
122
  }
110
123
 
111
124
 
125
+ function $9a073aa5b09aa778$export$4cd16590995531a9({ dos: dos, config: config, i18next: i18next }) {
126
+ // If empty, return undefined
127
+ if (dos.doseAndRate === undefined) return undefined;
128
+ // Find the first entry that match criteria
129
+ let doseRange = dos.doseAndRate.find((s)=>s.doseRange !== undefined);
130
+ // If not found, skip
131
+ if (doseRange === undefined) return undefined;
132
+ // Turn range into a text
133
+ const text = (0, $9ee880701f47723b$export$be17d167ed50d870)({
134
+ range: doseRange.doseRange,
135
+ config: config,
136
+ i18next: i18next
137
+ });
138
+ // Reject if empty
139
+ if (text === undefined) return undefined;
140
+ return i18next.t("fields.doseRange", {
141
+ rangeText: text
142
+ });
143
+ }
144
+
145
+
112
146
  // types
113
147
  function $052c79abb28cba29$export$4e24ef8c7997cc56({ dos: dos, config: config, i18next: i18next }) {
114
148
  // If empty, return undefined
@@ -131,72 +165,91 @@ function $052c79abb28cba29$export$4e24ef8c7997cc56({ dos: dos, config: config, i
131
165
  }
132
166
 
133
167
 
134
- // types
168
+ // Utility function
169
+
135
170
  function $703b645b55481398$export$d3dd7d3522271dba({ dos: dos, config: config, i18next: i18next }) {
136
171
  // If empty, return undefined
137
172
  if (dos.doseAndRate === undefined) return undefined;
138
173
  // Find the first entry that match criteria
139
- let rateRange = dos.doseAndRate.find((s)=>s.rateRange !== undefined);
174
+ let doseAndRate = dos.doseAndRate.find((s)=>s.rateRange !== undefined);
140
175
  // If not found, skip
141
- if (rateRange === undefined) return undefined;
142
- // low / high
143
- let low = rateRange.rateRange.low;
144
- let high = rateRange.rateRange.high;
145
- let quantityLow = low?.value;
146
- let quantityHigh = high?.value;
147
- // quantity unit
148
- let unit = config.fromFHIRQuantityUnitToString({
149
- language: config.language,
150
- quantity: high || low
151
- });
152
- // Three cases
153
- // 1. Both low & high are present
154
- if (quantityHigh !== undefined && quantityLow !== undefined) return i18next.t("fields.rateRange.lowAndHigh", {
155
- low: quantityLow,
156
- high: quantityHigh,
157
- unit: unit
158
- });
159
- // 2. Only high is present
160
- if (quantityHigh !== undefined) return i18next.t("fields.rateRange.onlyHigh", {
161
- high: quantityHigh,
162
- unit: unit
176
+ if (doseAndRate === undefined) return undefined;
177
+ // Turn range into a text
178
+ const rangeText = (0, $9ee880701f47723b$export$be17d167ed50d870)({
179
+ range: doseAndRate.rateRange,
180
+ config: config,
181
+ i18next: i18next
163
182
  });
164
- // 3. Only low is present
165
- // Warning, this case is kind dangerous and clinically unsafe so minimal effort on this ...
166
- return i18next.t("fields.rateRange.onlyLow", {
167
- low: quantityLow,
168
- unit: unit
183
+ // Reject if empty
184
+ if (rangeText === undefined) return undefined;
185
+ // return the final string
186
+ return i18next.t("fields.rateRange", {
187
+ rangeText: rangeText
169
188
  });
170
189
  }
171
190
 
172
191
 
173
- // types
174
- function $301791f33da707fa$export$b71cfd2510242de2({ dos: dos, config: config, i18next: i18next }) {
175
- // If empty, return undefined
176
- if (dos.doseAndRate === undefined) return undefined;
177
- // Find the first entry that match criteria
178
- let rateRatio = dos.doseAndRate.find((s)=>s.rateRatio !== undefined);
179
- // If not found, skip
180
- if (rateRatio === undefined) return undefined;
181
- // num / dem
182
- let numerator = rateRatio.rateRatio.numerator;
183
- let denominator = rateRatio.rateRatio.denominator;
184
- let quantityNum = numerator?.value || 1;
185
- let quantityDenom = denominator?.value || 1;
192
+ // Quantity has an unit ?
193
+ function $7895e9944cc835ce$var$hasUnit(quantity) {
194
+ return (quantity?.unit || quantity?.code) !== undefined;
195
+ }
196
+ function $7895e9944cc835ce$export$fdc6e9cbd31555fb({ ratio: ratio, config: config, i18next: i18next }) {
197
+ // Extract params
198
+ const { denominator: denominator, numerator: numerator } = ratio;
186
199
  // units as text
187
- let numeratorUnit = numerator !== undefined ? config.fromFHIRQuantityUnitToString({
200
+ let numeratorUnit = $7895e9944cc835ce$var$hasUnit(numerator) ? config.fromFHIRQuantityUnitToString({
188
201
  language: config.language,
189
202
  quantity: numerator
190
- }) : "";
191
- let denominatorUnit = denominator !== undefined ? config.fromFHIRQuantityUnitToString({
203
+ }) : undefined;
204
+ let denominatorUnit = $7895e9944cc835ce$var$hasUnit(denominator) ? config.fromFHIRQuantityUnitToString({
192
205
  language: config.language,
193
206
  quantity: denominator
194
- }) : "";
195
- return i18next.t("fields.rateRatio.rateRatio", {
196
- count: quantityDenom,
197
- quantityNumerator: quantityNum,
198
- numeratorUnit: numeratorUnit,
199
- denominatorUnit: denominatorUnit
207
+ }) : undefined;
208
+ // quantity
209
+ let quantityNumerator = numerator?.value;
210
+ let quantityDenominator = denominator?.value;
211
+ // Collect
212
+ const parts = [];
213
+ // Deal with numerator first
214
+ if (quantityNumerator !== undefined) {
215
+ let technicalKey = numeratorUnit !== undefined ? "withUnit" : "withoutUnit";
216
+ const numeratorString = i18next.t(`amount.ratio.${technicalKey}.numerator`, {
217
+ count: quantityNumerator,
218
+ numeratorUnit: numeratorUnit
219
+ });
220
+ parts.push(numeratorString);
221
+ }
222
+ // Deal with denominator
223
+ if (quantityDenominator !== undefined) {
224
+ let technicalKey = denominatorUnit !== undefined ? "withUnit" : "withoutUnit";
225
+ const denominatorString = i18next.t(`amount.ratio.${technicalKey}.denominator`, {
226
+ count: quantityDenominator,
227
+ denominatorUnit: denominatorUnit
228
+ });
229
+ parts.push(denominatorString);
230
+ }
231
+ // Concatenate the result
232
+ if (parts.length === 0) return undefined;
233
+ else return parts.join(" ");
234
+ }
235
+
236
+
237
+ function $301791f33da707fa$export$b71cfd2510242de2({ dos: dos, config: config, i18next: i18next }) {
238
+ // If empty, return undefined
239
+ if (dos.doseAndRate === undefined) return undefined;
240
+ // Find the first entry that match criteria
241
+ let doseAndRate = dos.doseAndRate.find((s)=>s.rateRatio !== undefined);
242
+ // If not found, skip
243
+ if (doseAndRate === undefined) return undefined;
244
+ // Turn ratio to text
245
+ const ratioText = (0, $7895e9944cc835ce$export$fdc6e9cbd31555fb)({
246
+ config: config,
247
+ i18next: i18next,
248
+ ratio: doseAndRate.rateRatio
249
+ });
250
+ if (ratioText === undefined) return undefined;
251
+ return i18next.t("fields.rateRatio", {
252
+ ratioText: ratioText
200
253
  });
201
254
  }
202
255
 
@@ -496,21 +549,7 @@ function $9d632517596957df$export$b927a06bc51aea32({ dos: dos, config: config, i
496
549
 
497
550
 
498
551
  // types
499
- // Quantity unit to string
500
- function $2da391a8a8345a3d$var$transformQuantityUnitToString(i18next, quantity, config) {
501
- let quantityValue = quantity.value;
502
- // If common units from HL7, do the job
503
- if (quantity.system === "http://hl7.org/fhir/ValueSet/duration-units") {
504
- let code = quantity.code;
505
- return i18next.t(`unitsOfTime:withoutCount.${code}`, {
506
- count: quantityValue
507
- });
508
- } else // otherwise, it is UCUM, ... so let the user do the job
509
- return config.fromFHIRQuantityUnitToString({
510
- language: config.language,
511
- quantity: quantity
512
- });
513
- }
552
+
514
553
  function $2da391a8a8345a3d$export$8c667cbf7bebaa93({ dos: dos, config: config, i18next: i18next }) {
515
554
  // If empty, return undefined
516
555
  if (dos.timing === undefined || dos.timing.repeat === undefined) return undefined;
@@ -520,26 +559,17 @@ function $2da391a8a8345a3d$export$8c667cbf7bebaa93({ dos: dos, config: config, i
520
559
  // Do nothing if no boundsRange, I am not a wizard
521
560
  if (boundsRange === undefined) return undefined;
522
561
  else {
523
- let low = boundsRange.low;
524
- let high = boundsRange.high;
525
- // quantity unit
526
- let unit = $2da391a8a8345a3d$var$transformQuantityUnitToString(i18next, high || low, config);
527
- // Three cases
528
- // 1. Both low & high are present
529
- if (high !== undefined && low !== undefined) return i18next.t("fields.boundsRange.lowAndHigh", {
530
- low: low.value,
531
- high: high.value,
532
- unit: unit
562
+ // Turn range into a text
563
+ const rangeText = (0, $9ee880701f47723b$export$be17d167ed50d870)({
564
+ range: boundsRange,
565
+ config: config,
566
+ i18next: i18next
533
567
  });
534
- // 2. Only high is present
535
- if (high !== undefined) return i18next.t("fields.boundsRange.onlyHigh", {
536
- high: high.value,
537
- unit: unit
538
- });
539
- // 3. Only low is present
540
- return i18next.t("fields.boundsRange.onlyLow", {
541
- low: low.value,
542
- unit: unit
568
+ // Reject if empty
569
+ if (rangeText === undefined) return undefined;
570
+ // return the final string
571
+ return i18next.t("fields.boundsRange", {
572
+ rangeText: rangeText
543
573
  });
544
574
  }
545
575
  }
@@ -764,6 +794,7 @@ function $45256d2a188dcbd7$export$75a89431d80a701a({ dos: dos, config: config, i
764
794
 
765
795
 
766
796
 
797
+
767
798
  function $39ffdeadc450f558$export$346b11a8cb220f02({ dos: dos, config: config, i18next: i18next }) {
768
799
  // If empty, return undefined
769
800
  if (dos.maxDosePerPeriod === undefined) return undefined;
@@ -778,29 +809,14 @@ function $39ffdeadc450f558$export$346b11a8cb220f02({ dos: dos, config: config, i
778
809
  if (values.length === 0) return undefined;
779
810
  // Periods are expressed as ratio (like rateRatio)
780
811
  const valuesAsString = values.map((period)=>{
781
- // num / dem
782
- let numerator = period.numerator;
783
- let denominator = period.denominator;
784
- let quantityNum = numerator?.value || 1;
785
- let quantityDenom = denominator?.value || 1;
786
- // units as text
787
- let numeratorUnit = numerator !== undefined ? config.fromFHIRQuantityUnitToString({
788
- language: config.language,
789
- quantity: numerator
790
- }) : "";
791
- let denominatorUnit = denominator !== undefined ? config.fromFHIRQuantityUnitToString({
792
- language: config.language,
793
- quantity: denominator
794
- }) : "";
795
- return i18next.t("fields.maxDosePerPeriod.maxDosePerPeriod", {
796
- count: quantityDenom,
797
- quantityNumerator: quantityNum,
798
- numeratorUnit: numeratorUnit,
799
- denominatorUnit: denominatorUnit
812
+ return (0, $7895e9944cc835ce$export$fdc6e9cbd31555fb)({
813
+ config: config,
814
+ i18next: i18next,
815
+ ratio: period
800
816
  });
801
- });
817
+ }).filter((s)=>s !== undefined);
802
818
  const maxDosePerPeriodText = (0, $93b58b7d2ead3b95$export$826742c1df3eca39)(i18next, valuesAsString);
803
- return i18next.t("fields.maxDosePerPeriod.general", {
819
+ return i18next.t("fields.maxDosePerPeriod", {
804
820
  count: values.length,
805
821
  maxDosePerPeriodText: maxDosePerPeriodText
806
822
  });
@@ -955,6 +971,11 @@ class $74b0cf9d3dbd5b46$export$1c191bca55f84a03 {
955
971
  return this.i18nInstance.changeLanguage(lng);
956
972
  }
957
973
  /**
974
+ * Get current language
975
+ */ getLanguage() {
976
+ return this.config.language;
977
+ }
978
+ /**
958
979
  * To change display order
959
980
  */ changeDisplayOrder(order) {
960
981
  this.config = {
@@ -963,10 +984,41 @@ class $74b0cf9d3dbd5b46$export$1c191bca55f84a03 {
963
984
  };
964
985
  }
965
986
  /**
966
- * Turn a FHIR Dosage object into text
967
- */ fromDosageToText(dos) {
987
+ * Get display order
988
+ */ getDisplayOrder() {
989
+ return this.config.displayOrder;
990
+ }
991
+ /**
992
+ * Get display separator
993
+ */ getDisplaySeparator() {
994
+ return this.config.displaySeparator;
995
+ }
996
+ /**
997
+ * Set display separator
998
+ */ changeDisplaySeparator(sep) {
999
+ this.config = {
1000
+ ...this.config,
1001
+ displaySeparator: sep
1002
+ };
1003
+ }
1004
+ /**
1005
+ * Get date time format options
1006
+ */ getDateTimeFormatOptions() {
1007
+ return this.config.dateTimeFormatOptions;
1008
+ }
1009
+ /**
1010
+ * Set date time format options
1011
+ */ changeDateTimeFormatOptions(opts) {
1012
+ this.config = {
1013
+ ...this.config,
1014
+ dateTimeFormatOptions: opts
1015
+ };
1016
+ }
1017
+ /**
1018
+ * From a single dosage, extract specific field(s) requested by user.
1019
+ * Some use cases could request to split part of the object for given needs (quantity and timing separately)
1020
+ */ getFields(dos, ...order) {
968
1021
  // iterate on each key and generate a string from each part
969
- let order = this.config.displayOrder;
970
1022
  let parts = order.map((entry)=>(0, $2c21ace90ecdbde6$export$9f330b4bf8c0975c)({
971
1023
  config: this.config,
972
1024
  dos: dos,
@@ -977,6 +1029,13 @@ class $74b0cf9d3dbd5b46$export$1c191bca55f84a03 {
977
1029
  return parts.join(this.config.displaySeparator);
978
1030
  }
979
1031
  /**
1032
+ * Turn a FHIR Dosage object into text
1033
+ */ fromDosageToText(dos) {
1034
+ // iterate on each key and generate a string from each part
1035
+ let order = this.config.displayOrder;
1036
+ return this.getFields(dos, ...order);
1037
+ }
1038
+ /**
980
1039
  * Turn multiple FHIR Dosage object into text
981
1040
  */ fromMultipleDosageToText(dosages) {
982
1041
  // As we can have concurrent / sequential instructions, we need a generic algorithm to do the job