@d4l/collect-lib 1.19.0 → 1.20.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/index.mjs CHANGED
@@ -898,9 +898,38 @@ function generateFHIRConsent(id, consent, decision, start, end) {
898
898
  };
899
899
  }
900
900
 
901
+ // src/fhir/utils.ts
902
+ import { v4 as uuid } from "uuid";
903
+ function generateUrn() {
904
+ return `urn:uuid:${uuid()}`;
905
+ }
906
+ function capitalizeFirstLetter(string) {
907
+ return string[0].toUpperCase() + string.slice(1);
908
+ }
909
+ function getText(content, path, lng) {
910
+ const lngPath = `${path}.${lng}`;
911
+ const value = get(content, lngPath);
912
+ if (value !== void 0) {
913
+ return value;
914
+ }
915
+ console.warn(`[getText] Missing property: '${lngPath}'`);
916
+ return "";
917
+ }
918
+ function get(obj, path) {
919
+ const value = path.split(".").reduce((o, p) => o == null ? void 0 : o[p], obj);
920
+ return typeof value === "string" ? value : void 0;
921
+ }
922
+ var indexArrayBy = (array = [], keyField) => array.reduce(
923
+ (obj, item) => {
924
+ obj[item[keyField]] = item;
925
+ return obj;
926
+ },
927
+ {}
928
+ );
929
+
901
930
  // src/fhir/questionnaireScore.ts
902
931
  function generateFHIRQuestionnaireScore(id, data, stringTemplater) {
903
- const { questionnaireName, createdAt, value, max, derivedFrom } = data;
932
+ const { questionnaireName, createdAt, value, min, max, derivedFrom } = data;
904
933
  return __spreadProps(__spreadValues({
905
934
  id,
906
935
  resourceType: "Observation",
@@ -952,7 +981,7 @@ function generateFHIRQuestionnaireScore(id, data, stringTemplater) {
952
981
  referenceRange: [
953
982
  {
954
983
  low: {
955
- value: 0
984
+ value: min
956
985
  },
957
986
  high: {
958
987
  value: max
@@ -961,6 +990,79 @@ function generateFHIRQuestionnaireScore(id, data, stringTemplater) {
961
990
  ]
962
991
  });
963
992
  }
993
+ function calculateQuestionnaireScoreRange(questionnaire, scoring) {
994
+ var _a;
995
+ if (!scoring || !((_a = questionnaire.item) == null ? void 0 : _a.length)) {
996
+ return { min: 0, max: 0 };
997
+ }
998
+ const valueSets = indexArrayBy(
999
+ questionnaire.contained || [],
1000
+ "id"
1001
+ );
1002
+ return questionnaire.item.reduce(
1003
+ (acc, curr) => {
1004
+ const { min, max } = calculateScoreRangeForItem(curr, valueSets, scoring);
1005
+ return { min: acc.min + min, max: acc.max + max };
1006
+ },
1007
+ { min: 0, max: 0 }
1008
+ );
1009
+ }
1010
+ function calculateScoreRangeForItem(item, valueSets, scoring) {
1011
+ if (item.item && item.item.length > 0) {
1012
+ return item.item.reduce(
1013
+ (acc, curr) => {
1014
+ const { min, max } = calculateScoreRangeForItem(curr, valueSets, scoring);
1015
+ return { min: acc.min + min, max: acc.max + max };
1016
+ },
1017
+ { min: 0, max: 0 }
1018
+ );
1019
+ }
1020
+ if (item.type === "coding") {
1021
+ return calculateValueSetScore(item, valueSets, scoring);
1022
+ }
1023
+ if (item.type === "decimal" || item.type === "integer") {
1024
+ return calculateNumericScore(item, scoring);
1025
+ }
1026
+ return { min: 0, max: 0 };
1027
+ }
1028
+ function calculateValueSetScore(item, valueSets, scoring) {
1029
+ var _a, _b, _c;
1030
+ if (!item.answerValueSet) {
1031
+ return { min: 0, max: 0 };
1032
+ }
1033
+ const valueSet = valueSets[item.answerValueSet.replace("#", "")];
1034
+ if (valueSet === void 0) {
1035
+ return { min: 0, max: 0 };
1036
+ }
1037
+ const uniqueCodesystems = (((_a = valueSet.expansion) == null ? void 0 : _a.contains) || []).reduce(
1038
+ (acc, { system }) => {
1039
+ if (system && !acc.includes(system)) {
1040
+ acc.push(system);
1041
+ }
1042
+ return acc;
1043
+ },
1044
+ []
1045
+ );
1046
+ const scoringSystems = (_c = (_b = scoring.scores) == null ? void 0 : _b.valueset) != null ? _c : {};
1047
+ const possibleValues = uniqueCodesystems.flatMap((system) => {
1048
+ var _a2;
1049
+ return Object.values((_a2 = scoringSystems[system]) != null ? _a2 : {});
1050
+ });
1051
+ if (possibleValues.length === 0) {
1052
+ return { min: 0, max: 0 };
1053
+ }
1054
+ return { min: Math.min(...possibleValues), max: Math.max(...possibleValues) };
1055
+ }
1056
+ function calculateNumericScore(item, scoring) {
1057
+ var _a, _b;
1058
+ const scoringNumbers = (_b = (_a = scoring.scores) == null ? void 0 : _a.number) != null ? _b : {};
1059
+ const threshold = scoringNumbers[item.linkId || ""];
1060
+ if (threshold) {
1061
+ const scores = threshold.map((t) => t.score);
1062
+ return { min: Math.min(...scores), max: Math.max(...scores) };
1063
+ }
1064
+ return { min: 0, max: 0 };
1065
+ }
964
1066
 
965
1067
  // src/fhir/questionnaireResponse.ts
966
1068
  function generateFHIRResponse(id, {
@@ -1054,30 +1156,6 @@ var toQuestionnaireResponseItem = (answer) => {
1054
1156
 
1055
1157
  // src/fhir/recordingSignal.ts
1056
1158
  import { Buffer as Buffer3 } from "buffer/";
1057
-
1058
- // src/fhir/utils.ts
1059
- import { v4 as uuid } from "uuid";
1060
- function generateUrn() {
1061
- return `urn:uuid:${uuid()}`;
1062
- }
1063
- function capitalizeFirstLetter(string) {
1064
- return string[0].toUpperCase() + string.slice(1);
1065
- }
1066
- function getText(content, path, lng) {
1067
- const lngPath = `${path}.${lng}`;
1068
- const value = get(content, lngPath);
1069
- if (value !== void 0) {
1070
- return value;
1071
- }
1072
- console.warn(`[getText] Missing property: '${lngPath}'`);
1073
- return "";
1074
- }
1075
- function get(obj, path) {
1076
- const value = path.split(".").reduce((o, p) => o == null ? void 0 : o[p], obj);
1077
- return typeof value === "string" ? value : void 0;
1078
- }
1079
-
1080
- // src/fhir/recordingSignal.ts
1081
1159
  function toFHIRBundleEntries(signal) {
1082
1160
  const urn = generateUrn();
1083
1161
  const deviceUrn = generateUrn();
@@ -1584,6 +1662,148 @@ function generateFHIRTag(id, code, date, labels, stringTemplater) {
1584
1662
  };
1585
1663
  }
1586
1664
 
1665
+ // src/timefuncs/timefuncs.ts
1666
+ import { DateTime as DateTime2 } from "luxon";
1667
+
1668
+ // src/timefuncs/utils.ts
1669
+ import { DateTime } from "luxon";
1670
+ var dayFormat = (date) => DateTime.fromJSDate(date).toFormat("yyyy-MM-dd");
1671
+ var isNotAfterEndDate = (effectiveEndDate, date) => {
1672
+ return !(effectiveEndDate && date.getTime() >= effectiveEndDate.getTime());
1673
+ };
1674
+ var isNotBeforeStartDate = (startDate, date) => {
1675
+ return !isBeforeStartDate(startDate, date);
1676
+ };
1677
+ var isBeforeStartDate = (start, date) => start && new Date(start).getTime() > date.getTime();
1678
+ var overwriteTime = (date, timeComponent) => {
1679
+ if (!date || !timeComponent) {
1680
+ return void 0;
1681
+ }
1682
+ const baseDate = timezonelessDate(timeComponent);
1683
+ if (!baseDate) {
1684
+ return void 0;
1685
+ }
1686
+ const baseLuxon = DateTime.fromJSDate(baseDate);
1687
+ const startLuxon = DateTime.fromJSDate(date);
1688
+ const mergedLuxon = baseLuxon.set({
1689
+ year: startLuxon.get("year"),
1690
+ month: startLuxon.get("month"),
1691
+ day: startLuxon.get("day")
1692
+ });
1693
+ return mergedLuxon.toJSDate();
1694
+ };
1695
+ var timezonelessDate = (input) => {
1696
+ const test = DateTime.fromISO(input);
1697
+ const timezoneIndex = input.search(/[+-]\d{2}:\d{2}|Z$/);
1698
+ if (timezoneIndex > 0) {
1699
+ const withoutTimezone = input.substring(0, timezoneIndex);
1700
+ return DateTime.fromISO(withoutTimezone).toJSDate();
1701
+ }
1702
+ return test.toJSDate();
1703
+ };
1704
+
1705
+ // src/timefuncs/timefuncs.ts
1706
+ var getTimeFuncs = (parameters, startDate) => {
1707
+ var _a;
1708
+ const { type } = parameters;
1709
+ const overrideStart = overwriteTime(startDate, parameters.start);
1710
+ const params = overrideStart ? __spreadProps(__spreadValues({}, parameters), {
1711
+ // Use DateTime ISO string to retain proper time when converting to ISO (JS date automatically converts to UTC)
1712
+ start: (_a = DateTime2.fromJSDate(overrideStart).toISO()) != null ? _a : parameters.start
1713
+ }) : parameters;
1714
+ switch (type) {
1715
+ case "daily":
1716
+ return new DayFuncs(params);
1717
+ case "weekly":
1718
+ return new WeekFuncs(params);
1719
+ case "monthly":
1720
+ return new MonthFuncs(params);
1721
+ default:
1722
+ return new DayFuncs(params);
1723
+ }
1724
+ };
1725
+ var TimeFuncs = class {
1726
+ constructor(parameters) {
1727
+ this.factor = 1;
1728
+ if (parameters) {
1729
+ if (parameters.factor !== void 0 && parameters.factor > 1) {
1730
+ this.factor = parameters.factor;
1731
+ }
1732
+ if ((parameters == null ? void 0 : parameters.start) && DateTime2.fromISO(parameters.start).isValid) {
1733
+ this.start = parameters.start;
1734
+ }
1735
+ }
1736
+ }
1737
+ add(base, x) {
1738
+ return this.addFunc(base, x * this.factor);
1739
+ }
1740
+ diff(later, earlier) {
1741
+ return Math.floor(this.diffFunc(later, earlier) / this.factor);
1742
+ }
1743
+ group(date) {
1744
+ if (this.factor > 1) {
1745
+ const window2 = this.window(date);
1746
+ return `${this.formatFunc(window2.start)}|${this.formatFunc(window2.end)}`;
1747
+ }
1748
+ return this.formatFunc(date);
1749
+ }
1750
+ startOfUnit(date) {
1751
+ var _a;
1752
+ let luxonDate = DateTime2.fromJSDate(date);
1753
+ return (_a = overwriteTime(luxonDate.startOf(this.unit).toJSDate(), this.start)) != null ? _a : luxonDate.toJSDate();
1754
+ }
1755
+ endOfUnit(date) {
1756
+ let luxonDate = DateTime2.fromJSDate(date);
1757
+ return luxonDate.endOf(this.unit).toJSDate();
1758
+ }
1759
+ window(date) {
1760
+ if (this.factor > 1 && this.start !== void 0) {
1761
+ const start = this.startOfUnit(new Date(this.start));
1762
+ const current = this.startOfUnit(date);
1763
+ const offset = this.diffFunc(current, start) % this.factor;
1764
+ return {
1765
+ start: this.addFunc(current, -offset),
1766
+ end: this.endOfUnit(this.addFunc(current, this.factor - offset - 1))
1767
+ };
1768
+ }
1769
+ return {
1770
+ start: this.startOfUnit(date),
1771
+ end: this.endOfUnit(date)
1772
+ };
1773
+ }
1774
+ };
1775
+ var DayFuncs = class extends TimeFuncs {
1776
+ constructor() {
1777
+ super(...arguments);
1778
+ this.unit = "day";
1779
+ this.addFunc = (base, x) => DateTime2.fromJSDate(base).plus({ days: x }).toJSDate();
1780
+ this.diffFunc = (later, earlier) => DateTime2.fromJSDate(later).diff(DateTime2.fromJSDate(earlier), "days").days;
1781
+ this.formatFunc = dayFormat;
1782
+ }
1783
+ };
1784
+ var WeekFuncs = class extends TimeFuncs {
1785
+ constructor() {
1786
+ super(...arguments);
1787
+ this.unit = "week";
1788
+ this.addFunc = (base, x) => DateTime2.fromJSDate(base).plus({ weeks: x }).toJSDate();
1789
+ this.diffFunc = (later, earlier) => DateTime2.fromJSDate(later).diff(DateTime2.fromJSDate(earlier), "weeks").weeks;
1790
+ this.formatFunc = dayFormat;
1791
+ }
1792
+ group(date) {
1793
+ const week = this.window(date);
1794
+ return `${dayFormat(week.start)}|${dayFormat(week.end)}`;
1795
+ }
1796
+ };
1797
+ var MonthFuncs = class extends TimeFuncs {
1798
+ constructor() {
1799
+ super(...arguments);
1800
+ this.unit = "month";
1801
+ this.addFunc = (base, x) => DateTime2.fromJSDate(base).plus({ months: x }).toJSDate();
1802
+ this.diffFunc = (later, earlier) => DateTime2.fromJSDate(later).diff(DateTime2.fromJSDate(earlier), "months").months;
1803
+ this.formatFunc = (date) => DateTime2.fromJSDate(date).toFormat("yyyy-MM");
1804
+ }
1805
+ };
1806
+
1587
1807
  // src/donation/keyHandler.ts
1588
1808
  import * as secpUtils from "@noble/curves/abstract/utils";
1589
1809
  import { secp256k1 } from "@noble/curves/secp256k1";
@@ -3997,7 +4217,7 @@ var Requester = class {
3997
4217
  var requester_default = Requester;
3998
4218
 
3999
4219
  // src/donation/blurring.ts
4000
- import { DateTime } from "luxon";
4220
+ import { DateTime as DateTime3 } from "luxon";
4001
4221
  var blurDocument = (document, anonymization) => {
4002
4222
  if (!anonymization || !document) {
4003
4223
  return document;
@@ -4015,7 +4235,7 @@ var blurQuestionnaireResponse = (document, anonymization) => {
4015
4235
  const blur = anonymization == null ? void 0 : anonymization.blur;
4016
4236
  if (blur) {
4017
4237
  return __spreadValues(__spreadValues({}, document), document.authored && {
4018
- authored: blurAuthored(document.authored, blur.authored, blur.location)
4238
+ authored: blurAuthored(document.authored, blur.authored)
4019
4239
  });
4020
4240
  }
4021
4241
  return document;
@@ -4023,31 +4243,27 @@ var blurQuestionnaireResponse = (document, anonymization) => {
4023
4243
  var blurResearchSubject = (document, anonymization) => {
4024
4244
  const programBlur = anonymization == null ? void 0 : anonymization.blur;
4025
4245
  if (programBlur && programBlur.researchSubject) {
4026
- const blurredPeriod = blurPeriod(
4027
- document.period,
4028
- programBlur.researchSubject,
4029
- programBlur.location
4030
- );
4246
+ const blurredPeriod = blurPeriod(document.period, programBlur.researchSubject);
4031
4247
  return __spreadValues(__spreadValues({}, document), blurredPeriod && { period: blurredPeriod });
4032
4248
  }
4033
4249
  return document;
4034
4250
  };
4035
- var blurAuthored = (authored, blurFn, location) => {
4036
- if (!location || !blurFn) {
4251
+ var blurAuthored = (authored, blurFn) => {
4252
+ if (!blurFn) {
4037
4253
  return authored;
4038
4254
  }
4039
- const authoredDate = DateTime.fromISO(authored).setZone(location);
4255
+ const authoredDate = DateTime3.fromISO(authored);
4040
4256
  if (!authoredDate || !authoredDate.isValid) {
4041
4257
  return authored;
4042
4258
  }
4043
4259
  return blurDate(authoredDate, blurFn).toString();
4044
4260
  };
4045
- var blurPeriod = (period, blurFn, location) => {
4261
+ var blurPeriod = (period, blurFn) => {
4046
4262
  if (!period) {
4047
4263
  return void 0;
4048
4264
  }
4049
- const startDate = period.start ? DateTime.fromISO(period.start).setZone(location) : void 0;
4050
- const endDate = period.end ? DateTime.fromISO(period.end).setZone(location) : void 0;
4265
+ const startDate = period.start ? DateTime3.fromISO(period.start) : void 0;
4266
+ const endDate = period.end ? DateTime3.fromISO(period.end) : void 0;
4051
4267
  return __spreadValues(__spreadValues({}, period.start && (startDate && startDate.isValid ? { start: blurDate(startDate, blurFn).toString() } : { start: period.start })), period.end && (endDate && endDate.isValid ? { end: blurDate(endDate, blurFn).toString() } : { end: period.end }));
4052
4268
  };
4053
4269
  var blurDate = (date, blurFn) => {
@@ -4744,7 +4960,7 @@ var isScoreEligible = (step, score) => {
4744
4960
  };
4745
4961
 
4746
4962
  // src/helper/guard.ts
4747
- import { DateTime as DateTime2, Duration } from "luxon";
4963
+ import { DateTime as DateTime4, Duration } from "luxon";
4748
4964
  var evaluateOperator = (value, operator, operand) => {
4749
4965
  switch (operator) {
4750
4966
  case "exists":
@@ -4800,7 +5016,7 @@ var DateCondition = class {
4800
5016
  if (answer && !Array.isArray(answer)) {
4801
5017
  const jsDate = new Date(answer);
4802
5018
  if (!isNaN(jsDate.getTime())) {
4803
- const answerDate = DateTime2.fromJSDate(jsDate).plus(this.offset);
5019
+ const answerDate = DateTime4.fromJSDate(jsDate).plus(this.offset);
4804
5020
  return answerDate.toJSDate();
4805
5021
  }
4806
5022
  }
@@ -4889,7 +5105,7 @@ var AnswerCodeCondition = class {
4889
5105
  if (!fulfilled) {
4890
5106
  return null;
4891
5107
  }
4892
- const answerDate = DateTime2.fromJSDate(multiAnswers[this.surveyName].date).plus(this.offset);
5108
+ const answerDate = DateTime4.fromJSDate(multiAnswers[this.surveyName].date).plus(this.offset);
4893
5109
  return answerDate.toJSDate();
4894
5110
  }
4895
5111
  };
@@ -5023,7 +5239,7 @@ var Offset = class {
5023
5239
  getFulfillmentDate(multiAnswers, consentTimes) {
5024
5240
  const fulfillmentDate = this.condition.getFulfillmentDate(multiAnswers, consentTimes);
5025
5241
  if (fulfillmentDate && !isNaN(fulfillmentDate == null ? void 0 : fulfillmentDate.getTime())) {
5026
- const answerDate = DateTime2.fromJSDate(fulfillmentDate).plus(this.offset);
5242
+ const answerDate = DateTime4.fromJSDate(fulfillmentDate).plus(this.offset);
5027
5243
  return answerDate.toJSDate();
5028
5244
  }
5029
5245
  return null;
@@ -5045,7 +5261,7 @@ var ConsentCondition = class {
5045
5261
  if (!(consentTimes == null ? void 0 : consentTimes[this.consentKey])) {
5046
5262
  return null;
5047
5263
  }
5048
- return DateTime2.fromJSDate(consentTimes[this.consentKey]).plus(this.offset).toJSDate();
5264
+ return DateTime4.fromJSDate(consentTimes[this.consentKey]).plus(this.offset).toJSDate();
5049
5265
  }
5050
5266
  };
5051
5267
  var SurveyCondition = class {
@@ -5067,7 +5283,7 @@ var SurveyCondition = class {
5067
5283
  if (!date) {
5068
5284
  return null;
5069
5285
  }
5070
- const answerDate = DateTime2.fromJSDate(date).plus(this.offset);
5286
+ const answerDate = DateTime4.fromJSDate(date).plus(this.offset);
5071
5287
  if (!answerDate.isValid) {
5072
5288
  return null;
5073
5289
  }
@@ -5090,7 +5306,7 @@ var RoutineCondition = class {
5090
5306
  if (!(routineTimes == null ? void 0 : routineTimes[this.routineName])) {
5091
5307
  return null;
5092
5308
  }
5093
- return DateTime2.fromJSDate(routineTimes[this.routineName]).plus(this.offset).toJSDate();
5309
+ return DateTime4.fromJSDate(routineTimes[this.routineName]).plus(this.offset).toJSDate();
5094
5310
  }
5095
5311
  };
5096
5312
  var generateQuestionGuard = (enableWhen, enableBehavior) => {
@@ -5490,6 +5706,7 @@ export {
5490
5706
  SliderStepSizeExtension,
5491
5707
  StudyNameSystem,
5492
5708
  SurveyCondition,
5709
+ TimeFuncs,
5493
5710
  TokenNamingSystem,
5494
5711
  ULID_REGEX,
5495
5712
  ULIDtoUUID,
@@ -5500,7 +5717,10 @@ export {
5500
5717
  WebConverter,
5501
5718
  YearCondition,
5502
5719
  binaryStringToUint8Array,
5720
+ calculateQuestionnaireScoreRange,
5721
+ calculateScoreRangeForItem,
5503
5722
  capitalizeFirstLetter,
5723
+ dayFormat,
5504
5724
  extractWordsFromRecoveryPhrase,
5505
5725
  generateFHIRConsent,
5506
5726
  generateFHIRQuestionnaireScore,
@@ -5517,11 +5737,18 @@ export {
5517
5737
  getLanguageOfPassPhrase,
5518
5738
  getMovellaAnnotation,
5519
5739
  getText,
5740
+ getTimeFuncs,
5741
+ indexArrayBy,
5742
+ isBeforeStartDate,
5743
+ isNotAfterEndDate,
5744
+ isNotBeforeStartDate,
5520
5745
  isScoreEligible,
5521
5746
  isYearString,
5522
5747
  newStepDateCondtion,
5748
+ overwriteTime,
5523
5749
  stringToDate,
5524
5750
  stringToYear,
5751
+ timezonelessDate,
5525
5752
  toDeviceConfig,
5526
5753
  toQuestionnaireResponseItem,
5527
5754
  uint8ArrayToBinaryString,