fhir-dosage-utils 1.10.4 → 2.0.0

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/module.js CHANGED
@@ -1,6 +1,8 @@
1
- import $hgUW1$i18nextresourcestobackend from "i18next-resources-to-backend";
2
1
  import $hgUW1$i18next from "i18next";
3
2
  import $hgUW1$i18nextchainedbackend from "i18next-chained-backend";
3
+ import $hgUW1$i18nextresourcestobackend from "i18next-resources-to-backend";
4
+
5
+ // Export utility class
4
6
 
5
7
 
6
8
 
@@ -41,13 +43,11 @@ function $f2878500cc217687$export$f478f96b98da13a4({ code: code }) {
41
43
 
42
44
  function $a31aaee21175037d$export$30c337e25ab8e95b({ extensions: extensions }) {
43
45
  if ((0, $1c8530b981ded822$export$d9ff4a623e416a11)(extensions)) return undefined;
44
- // KIS implementation : print it as provided, without doing anything
45
- // Extensions have so many cases btw, that having a basic function does the trick
46
46
  return extensions.map((extension)=>JSON.stringify(extension)).join(" ");
47
47
  }
48
48
 
49
49
 
50
- const $c03f5f20816d8d50$export$38cd8db2db178f58 = {
50
+ const $d5ca4938758304ae$export$38cd8db2db178f58 = {
51
51
  language: "en",
52
52
  fromFHIRQuantityUnitToString: (0, $a620268c8b5a46fd$export$b1a6aed91fad1636),
53
53
  fromCodeableConceptToString: (0, $f2878500cc217687$export$f478f96b98da13a4),
@@ -80,90 +80,38 @@ const $c03f5f20816d8d50$export$38cd8db2db178f58 = {
80
80
  "patientInstruction"
81
81
  ],
82
82
  displaySeparator: " - ",
83
- dateTimeFormatOptions: {}
83
+ dateTimeFormatOptions: {},
84
+ i18nConfig: {
85
+ backend: {
86
+ backends: [
87
+ (0, $hgUW1$i18nextresourcestobackend)(async (lng, ns)=>import(`./locales/${lng}/${ns}.json`))
88
+ ]
89
+ }
90
+ }
84
91
  };
85
92
 
86
93
 
87
- class $2422495f2648c580$export$f807d9a22ff8372d {
88
- constructor(userConfig, i18nConfig){
89
- this.config = {
90
- ...(0, $c03f5f20816d8d50$export$38cd8db2db178f58),
91
- ...userConfig
92
- };
93
- this.i18nConfig = {
94
- fallbackLng: "en",
95
- lng: this.config.language,
96
- ns: [
97
- "common",
98
- "daysOfWeek",
99
- "eventTiming",
100
- "unitsOfTime",
101
- "quantityComparator"
102
- ],
103
- defaultNS: "common",
104
- ...i18nConfig
105
- };
106
- this.i18nInstance = (0, $hgUW1$i18next).createInstance();
107
- }
108
- /**
109
- * To init i18next properly according requested criteria
110
- */ async init() {
111
- // You should wait for init to complete (wait for the callback or promise resolution)
112
- // before using the t function!
113
- return await this.i18nInstance.use((0, $hgUW1$i18nextchainedbackend)).init(this.i18nConfig);
114
- }
115
- /**
116
- * To change language
117
- */ async changeLanguage(lng) {
118
- this.config = {
119
- ...this.config,
120
- language: lng
121
- };
122
- return this.i18nInstance.changeLanguage(lng);
123
- }
124
- /**
125
- * Get current language
126
- */ getLanguage() {
127
- return this.config.language;
128
- }
129
- /**
130
- * To change display order
131
- */ changeDisplayOrder(order) {
132
- this.config = {
133
- ...this.config,
134
- displayOrder: order
135
- };
136
- }
137
- /**
138
- * Get display order
139
- */ getDisplayOrder() {
140
- return this.config.displayOrder;
141
- }
142
- /**
143
- * Get display separator
144
- */ getDisplaySeparator() {
145
- return this.config.displaySeparator;
146
- }
147
- /**
148
- * Set display separator
149
- */ changeDisplaySeparator(sep) {
150
- this.config = {
151
- ...this.config,
152
- displaySeparator: sep
153
- };
154
- }
155
- /**
156
- * Get date time format options
157
- */ getDateTimeFormatOptions() {
158
- return this.config.dateTimeFormatOptions;
94
+
95
+ class $928801c629c03bf9$export$d2ca453b913dcdea {
96
+ containsOnlySequentialInstructions(dosages) {
97
+ let sequencesNumbers = dosages.map((d)=>d.sequence).filter((0, $c65c41ac1f3ea82b$export$e7321bf46d18757f));
98
+ let encounteredSequenceNumbers = new Set(sequencesNumbers);
99
+ return [
100
+ 0,
101
+ dosages.length
102
+ ].includes(encounteredSequenceNumbers.size);
159
103
  }
160
- /**
161
- * Set date time format options
162
- */ changeDateTimeFormatOptions(opts) {
163
- this.config = {
164
- ...this.config,
165
- dateTimeFormatOptions: opts
166
- };
104
+ groupBySequence(dosages) {
105
+ let sequences = new Set();
106
+ let sequencesMap = new Map();
107
+ for (let dosage of dosages){
108
+ let sequenceNr = dosage.sequence;
109
+ let localGroup = sequencesMap.get(sequenceNr) ?? [];
110
+ localGroup.push(dosage);
111
+ sequencesMap.set(sequenceNr, localGroup);
112
+ sequences.add(sequenceNr);
113
+ }
114
+ return Array.from(sequences, (sequence)=>sequencesMap.get(sequence));
167
115
  }
168
116
  }
169
117
 
@@ -171,7 +119,6 @@ class $2422495f2648c580$export$f807d9a22ff8372d {
171
119
 
172
120
  function $a8a8a2e7c5a5225c$export$76d408d587da5203(dos, key) {
173
121
  if (!(0, $c65c41ac1f3ea82b$export$e7321bf46d18757f)(dos.doseAndRate)) return undefined;
174
- // Find the first entry that match criteria
175
122
  let doseAndRate = dos.doseAndRate.find((s)=>(0, $c65c41ac1f3ea82b$export$e7321bf46d18757f)(s[key]));
176
123
  if (!(0, $c65c41ac1f3ea82b$export$e7321bf46d18757f)(doseAndRate)) return undefined;
177
124
  return doseAndRate[key];
@@ -501,9 +448,9 @@ function $f88800def2ddd9e9$var$transformOffset(i18next, offset) {
501
448
  "h",
502
449
  "min"
503
450
  ];
504
- return order.map((unit)=>time[unit] > 0 ? i18next.t(`unitsOfTime:withCount.${unit}`, {
451
+ return order.filter((unit)=>time[unit] > 0).map((unit)=>i18next.t(`unitsOfTime:withCount.${unit}`, {
505
452
  count: time[unit]
506
- }) : undefined).filter((0, $c65c41ac1f3ea82b$export$e7321bf46d18757f)).join(" ");
453
+ })).join(" ");
507
454
  }
508
455
  function $f88800def2ddd9e9$var$transformWhen(i18next, when) {
509
456
  if ((0, $1c8530b981ded822$export$d9ff4a623e416a11)(when)) return undefined;
@@ -831,20 +778,16 @@ function $252769c9f296cd6d$export$346b11a8cb220f02({ dos: dos, config: config, i
831
778
  if (!(0, $c65c41ac1f3ea82b$export$e7321bf46d18757f)(dos.maxDosePerPeriod)) return undefined;
832
779
  const maxDosePerPeriod = dos.maxDosePerPeriod;
833
780
  // In R4, it was a single object / In R5, it is an array
834
- // So better to have a generic approach
835
781
  const values = Array.isArray(maxDosePerPeriod) ? maxDosePerPeriod : [
836
782
  maxDosePerPeriod
837
783
  ];
838
- // Only consider not empty array
839
784
  if (values.length === 0) return undefined;
840
785
  // Periods are expressed as ratio (like rateRatio)
841
- const valuesAsString = values.map((period)=>{
842
- return (0, $eb371feb739abc89$export$fdc6e9cbd31555fb)({
786
+ const valuesAsString = values.map((period)=>(0, $eb371feb739abc89$export$fdc6e9cbd31555fb)({
843
787
  config: config,
844
788
  i18next: i18next,
845
789
  ratio: period
846
- });
847
- }).filter((0, $c65c41ac1f3ea82b$export$e7321bf46d18757f));
790
+ })).filter((0, $c65c41ac1f3ea82b$export$e7321bf46d18757f));
848
791
  const maxDosePerPeriodText = (0, $f475af73bad0ba43$export$826742c1df3eca39)(i18next, valuesAsString);
849
792
  return i18next.t("fields.maxDosePerPeriod", {
850
793
  count: values.length,
@@ -931,50 +874,46 @@ function $9b1af2fba509609e$export$9f330b4bf8c0975c({ entry: entry, ...args }) {
931
874
 
932
875
 
933
876
 
934
-
935
- const $8435b8d847fb3eb7$var$defaultI18NConfig = {
936
- backend: {
937
- backends: [
938
- (0, $hgUW1$i18nextresourcestobackend)(async (lng, ns)=>import(`./locales/${lng}/${ns}.json`))
939
- ]
877
+ class $8435b8d847fb3eb7$export$1c191bca55f84a03 extends (0, $928801c629c03bf9$export$d2ca453b913dcdea) {
878
+ constructor(config){
879
+ super();
880
+ this.config = {
881
+ ...(0, $d5ca4938758304ae$export$38cd8db2db178f58),
882
+ ...config
883
+ };
884
+ this.i18nInstance = (0, $hgUW1$i18next).createInstance();
940
885
  }
941
- };
942
- class $8435b8d847fb3eb7$export$1c191bca55f84a03 extends (0, $2422495f2648c580$export$f807d9a22ff8372d) {
943
- /**
944
- * Factory to create a fine-tuned instance of the utility class
945
- */ static async build(userConfig, i18nConfig = $8435b8d847fb3eb7$var$defaultI18NConfig) {
946
- const instance = new $8435b8d847fb3eb7$export$1c191bca55f84a03(userConfig, i18nConfig);
886
+ async init() {
887
+ return await this.i18nInstance.use((0, $hgUW1$i18nextchainedbackend)).init({
888
+ fallbackLng: "en",
889
+ lng: this.config.language,
890
+ ns: [
891
+ "common",
892
+ "daysOfWeek",
893
+ "eventTiming",
894
+ "unitsOfTime",
895
+ "quantityComparator"
896
+ ],
897
+ defaultNS: "common",
898
+ ...this.config.i18nConfig
899
+ });
900
+ }
901
+ static async build(config = {}) {
902
+ const instance = new $8435b8d847fb3eb7$export$1c191bca55f84a03(config);
947
903
  await instance.init();
948
904
  return instance;
949
905
  }
950
- /**
951
- * Does this array of Dosage objects contains only "sequential" instructions ?
952
- */ containsOnlySequentialInstructions(dosages) {
953
- let sequencesNumbers = dosages.map((d)=>d.sequence).filter((0, $c65c41ac1f3ea82b$export$e7321bf46d18757f));
954
- let encounteredSequenceNumbers = new Set(sequencesNumbers);
955
- return encounteredSequenceNumbers.size === 0 || encounteredSequenceNumbers.size === dosages.length;
906
+ getProperty(key) {
907
+ return this.config[key];
956
908
  }
957
- /**
958
- * Turn this array of Dosage objects into a data structure useful to handle "sequential" and "concurrent" instructions (cf. "sequence" property).
959
- * @returns {Dosage[][]} - A two-dimensional array where each inner array contains Dosage objects belonging to the same sequence numberr.
960
- */ groupBySequence(dosages) {
961
- let groups = {};
962
- let sequences = new Set();
963
- for(let idx = 0; idx < dosages.length; idx++){
964
- const dosage = dosages[idx];
965
- let sequenceNr = dosage.sequence || idx + 1;
966
- let localGroup = groups[sequenceNr] || [];
967
- localGroup.push(dosage);
968
- groups[sequenceNr] = localGroup;
969
- sequences.add(sequenceNr);
970
- }
971
- // By using the Set values, we are sure it is returned in the way Dosages were written
972
- return Array.from(sequences, (sequence)=>groups[sequence]);
909
+ setProperty(key, value) {
910
+ this.config[key] = value;
973
911
  }
974
- /**
975
- * From a single dosage, extract specific field(s) requested by user.
976
- * Some use cases could request to split part of the object for given needs (quantity and timing separately)
977
- */ getFields(dos, ...order) {
912
+ async changeLanguage(lng) {
913
+ this.config["language"] = lng;
914
+ return this.i18nInstance.changeLanguage(lng);
915
+ }
916
+ getFields(dos, ...order) {
978
917
  let parts = order.map((entry)=>(0, $9b1af2fba509609e$export$9f330b4bf8c0975c)({
979
918
  config: this.config,
980
919
  dos: dos,
@@ -983,33 +922,31 @@ class $8435b8d847fb3eb7$export$1c191bca55f84a03 extends (0, $2422495f2648c580$ex
983
922
  })).filter((0, $c65c41ac1f3ea82b$export$e7321bf46d18757f));
984
923
  return parts.join(this.config.displaySeparator);
985
924
  }
986
- /**
987
- * Turn a FHIR Dosage object into text
988
- */ fromDosageToText(dos) {
925
+ fromDosageToText(dos) {
989
926
  let order = this.config.displayOrder;
990
927
  return this.getFields(dos, ...order);
991
928
  }
992
- /**
993
- * Turn multiple FHIR Dosage objects into text
994
- */ fromMultipleDosageToText(dosages) {
995
- const hasOnlySequentialInstructions = this.containsOnlySequentialInstructions(dosages);
996
- if (hasOnlySequentialInstructions) {
997
- const dosagesAsText = dosages.map((d)=>this.fromDosageToText(d));
998
- return (0, $f475af73bad0ba43$export$826742c1df3eca39)(this.i18nInstance, dosagesAsText, "then");
999
- }
1000
- let sortedDosages = this.groupBySequence(dosages);
1001
- let sequentialInstructions = sortedDosages.map((concurrentInstructions)=>{
1002
- let concurrentInstructionsAsString = concurrentInstructions.map((dosage)=>this.fromDosageToText(dosage));
1003
- return (0, $f475af73bad0ba43$export$826742c1df3eca39)(this.i18nInstance, concurrentInstructionsAsString, "and");
1004
- });
929
+ fromMultipleDosageToText(dosages) {
930
+ if (this.containsOnlySequentialInstructions(dosages)) return this.convertSequentialDosagesToText(dosages);
931
+ return this.convertGroupedDosagesToText(dosages);
932
+ }
933
+ convertSequentialDosagesToText(dosages) {
934
+ const dosagesAsText = dosages.map((d)=>this.fromDosageToText(d));
935
+ return (0, $f475af73bad0ba43$export$826742c1df3eca39)(this.i18nInstance, dosagesAsText, "then");
936
+ }
937
+ convertConcurrentDosagesToText(dosages) {
938
+ const concurrentInstructionsAsString = dosages.map((d)=>this.fromDosageToText(d));
939
+ return (0, $f475af73bad0ba43$export$826742c1df3eca39)(this.i18nInstance, concurrentInstructionsAsString, "and");
940
+ }
941
+ convertGroupedDosagesToText(dosages) {
942
+ const sortedDosages = this.groupBySequence(dosages);
943
+ const sequentialInstructions = sortedDosages.map((d)=>this.convertConcurrentDosagesToText(d));
1005
944
  return (0, $f475af73bad0ba43$export$826742c1df3eca39)(this.i18nInstance, sequentialInstructions, "then");
1006
945
  }
1007
946
  }
1008
947
 
1009
948
 
1010
- var // Default export to make it user-friendly
1011
- $149c1bd638913645$export$2e2bcd8739ae039 = (0, $8435b8d847fb3eb7$export$1c191bca55f84a03);
1012
949
 
1013
950
 
1014
- export {$149c1bd638913645$export$2e2bcd8739ae039 as default};
951
+ export {$8435b8d847fb3eb7$export$1c191bca55f84a03 as FhirDosageUtils};
1015
952
  //# sourceMappingURL=module.js.map