@d4l/collect-lib 1.19.0 → 1.20.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/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,76 @@ 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
+ return { min: Math.min(...possibleValues), max: Math.max(...possibleValues) };
1052
+ }
1053
+ function calculateNumericScore(item, scoring) {
1054
+ var _a, _b;
1055
+ const scoringNumbers = (_b = (_a = scoring.scores) == null ? void 0 : _a.number) != null ? _b : {};
1056
+ const threshold = scoringNumbers[item.linkId || ""];
1057
+ if (threshold) {
1058
+ const scores = threshold.map((t) => t.score);
1059
+ return { min: Math.min(...scores), max: Math.max(...scores) };
1060
+ }
1061
+ return { min: 0, max: 0 };
1062
+ }
964
1063
 
965
1064
  // src/fhir/questionnaireResponse.ts
966
1065
  function generateFHIRResponse(id, {
@@ -1054,30 +1153,6 @@ var toQuestionnaireResponseItem = (answer) => {
1054
1153
 
1055
1154
  // src/fhir/recordingSignal.ts
1056
1155
  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
1156
  function toFHIRBundleEntries(signal) {
1082
1157
  const urn = generateUrn();
1083
1158
  const deviceUrn = generateUrn();
@@ -1584,6 +1659,148 @@ function generateFHIRTag(id, code, date, labels, stringTemplater) {
1584
1659
  };
1585
1660
  }
1586
1661
 
1662
+ // src/timefuncs/timefuncs.ts
1663
+ import { DateTime as DateTime2 } from "luxon";
1664
+
1665
+ // src/timefuncs/utils.ts
1666
+ import { DateTime } from "luxon";
1667
+ var dayFormat = (date) => DateTime.fromJSDate(date).toFormat("yyyy-MM-dd");
1668
+ var isNotAfterEndDate = (effectiveEndDate, date) => {
1669
+ return !(effectiveEndDate && date.getTime() >= effectiveEndDate.getTime());
1670
+ };
1671
+ var isNotBeforeStartDate = (startDate, date) => {
1672
+ return !isBeforeStartDate(startDate, date);
1673
+ };
1674
+ var isBeforeStartDate = (start, date) => start && new Date(start).getTime() > date.getTime();
1675
+ var overwriteTime = (date, timeComponent) => {
1676
+ if (!date || !timeComponent) {
1677
+ return void 0;
1678
+ }
1679
+ const baseDate = timezonelessDate(timeComponent);
1680
+ if (!baseDate) {
1681
+ return void 0;
1682
+ }
1683
+ const baseLuxon = DateTime.fromJSDate(baseDate);
1684
+ const startLuxon = DateTime.fromJSDate(date);
1685
+ const mergedLuxon = baseLuxon.set({
1686
+ year: startLuxon.get("year"),
1687
+ month: startLuxon.get("month"),
1688
+ day: startLuxon.get("day")
1689
+ });
1690
+ return mergedLuxon.toJSDate();
1691
+ };
1692
+ var timezonelessDate = (input) => {
1693
+ const test = DateTime.fromISO(input);
1694
+ const timezoneIndex = input.search(/[+-]\d{2}:\d{2}|Z$/);
1695
+ if (timezoneIndex > 0) {
1696
+ const withoutTimezone = input.substring(0, timezoneIndex);
1697
+ return DateTime.fromISO(withoutTimezone).toJSDate();
1698
+ }
1699
+ return test.toJSDate();
1700
+ };
1701
+
1702
+ // src/timefuncs/timefuncs.ts
1703
+ var getTimeFuncs = (parameters, startDate) => {
1704
+ var _a;
1705
+ const { type } = parameters;
1706
+ const overrideStart = overwriteTime(startDate, parameters.start);
1707
+ const params = overrideStart ? __spreadProps(__spreadValues({}, parameters), {
1708
+ // Use DateTime ISO string to retain proper time when converting to ISO (JS date automatically converts to UTC)
1709
+ start: (_a = DateTime2.fromJSDate(overrideStart).toISO()) != null ? _a : parameters.start
1710
+ }) : parameters;
1711
+ switch (type) {
1712
+ case "daily":
1713
+ return new DayFuncs(params);
1714
+ case "weekly":
1715
+ return new WeekFuncs(params);
1716
+ case "monthly":
1717
+ return new MonthFuncs(params);
1718
+ default:
1719
+ return new DayFuncs(params);
1720
+ }
1721
+ };
1722
+ var TimeFuncs = class {
1723
+ constructor(parameters) {
1724
+ this.factor = 1;
1725
+ if (parameters) {
1726
+ if (parameters.factor !== void 0 && parameters.factor > 1) {
1727
+ this.factor = parameters.factor;
1728
+ }
1729
+ if ((parameters == null ? void 0 : parameters.start) && DateTime2.fromISO(parameters.start).isValid) {
1730
+ this.start = parameters.start;
1731
+ }
1732
+ }
1733
+ }
1734
+ add(base, x) {
1735
+ return this.addFunc(base, x * this.factor);
1736
+ }
1737
+ diff(later, earlier) {
1738
+ return Math.floor(this.diffFunc(later, earlier) / this.factor);
1739
+ }
1740
+ group(date) {
1741
+ if (this.factor > 1) {
1742
+ const window2 = this.window(date);
1743
+ return `${this.formatFunc(window2.start)}|${this.formatFunc(window2.end)}`;
1744
+ }
1745
+ return this.formatFunc(date);
1746
+ }
1747
+ startOfUnit(date) {
1748
+ var _a;
1749
+ let luxonDate = DateTime2.fromJSDate(date);
1750
+ return (_a = overwriteTime(luxonDate.startOf(this.unit).toJSDate(), this.start)) != null ? _a : luxonDate.toJSDate();
1751
+ }
1752
+ endOfUnit(date) {
1753
+ let luxonDate = DateTime2.fromJSDate(date);
1754
+ return luxonDate.endOf(this.unit).toJSDate();
1755
+ }
1756
+ window(date) {
1757
+ if (this.factor > 1 && this.start !== void 0) {
1758
+ const start = this.startOfUnit(new Date(this.start));
1759
+ const current = this.startOfUnit(date);
1760
+ const offset = this.diffFunc(current, start) % this.factor;
1761
+ return {
1762
+ start: this.addFunc(current, -offset),
1763
+ end: this.endOfUnit(this.addFunc(current, this.factor - offset - 1))
1764
+ };
1765
+ }
1766
+ return {
1767
+ start: this.startOfUnit(date),
1768
+ end: this.endOfUnit(date)
1769
+ };
1770
+ }
1771
+ };
1772
+ var DayFuncs = class extends TimeFuncs {
1773
+ constructor() {
1774
+ super(...arguments);
1775
+ this.unit = "day";
1776
+ this.addFunc = (base, x) => DateTime2.fromJSDate(base).plus({ days: x }).toJSDate();
1777
+ this.diffFunc = (later, earlier) => DateTime2.fromJSDate(later).diff(DateTime2.fromJSDate(earlier), "days").days;
1778
+ this.formatFunc = dayFormat;
1779
+ }
1780
+ };
1781
+ var WeekFuncs = class extends TimeFuncs {
1782
+ constructor() {
1783
+ super(...arguments);
1784
+ this.unit = "week";
1785
+ this.addFunc = (base, x) => DateTime2.fromJSDate(base).plus({ weeks: x }).toJSDate();
1786
+ this.diffFunc = (later, earlier) => DateTime2.fromJSDate(later).diff(DateTime2.fromJSDate(earlier), "weeks").weeks;
1787
+ this.formatFunc = dayFormat;
1788
+ }
1789
+ group(date) {
1790
+ const week = this.window(date);
1791
+ return `${dayFormat(week.start)}|${dayFormat(week.end)}`;
1792
+ }
1793
+ };
1794
+ var MonthFuncs = class extends TimeFuncs {
1795
+ constructor() {
1796
+ super(...arguments);
1797
+ this.unit = "month";
1798
+ this.addFunc = (base, x) => DateTime2.fromJSDate(base).plus({ months: x }).toJSDate();
1799
+ this.diffFunc = (later, earlier) => DateTime2.fromJSDate(later).diff(DateTime2.fromJSDate(earlier), "months").months;
1800
+ this.formatFunc = (date) => DateTime2.fromJSDate(date).toFormat("yyyy-MM");
1801
+ }
1802
+ };
1803
+
1587
1804
  // src/donation/keyHandler.ts
1588
1805
  import * as secpUtils from "@noble/curves/abstract/utils";
1589
1806
  import { secp256k1 } from "@noble/curves/secp256k1";
@@ -3997,7 +4214,7 @@ var Requester = class {
3997
4214
  var requester_default = Requester;
3998
4215
 
3999
4216
  // src/donation/blurring.ts
4000
- import { DateTime } from "luxon";
4217
+ import { DateTime as DateTime3 } from "luxon";
4001
4218
  var blurDocument = (document, anonymization) => {
4002
4219
  if (!anonymization || !document) {
4003
4220
  return document;
@@ -4015,7 +4232,7 @@ var blurQuestionnaireResponse = (document, anonymization) => {
4015
4232
  const blur = anonymization == null ? void 0 : anonymization.blur;
4016
4233
  if (blur) {
4017
4234
  return __spreadValues(__spreadValues({}, document), document.authored && {
4018
- authored: blurAuthored(document.authored, blur.authored, blur.location)
4235
+ authored: blurAuthored(document.authored, blur.authored)
4019
4236
  });
4020
4237
  }
4021
4238
  return document;
@@ -4023,31 +4240,27 @@ var blurQuestionnaireResponse = (document, anonymization) => {
4023
4240
  var blurResearchSubject = (document, anonymization) => {
4024
4241
  const programBlur = anonymization == null ? void 0 : anonymization.blur;
4025
4242
  if (programBlur && programBlur.researchSubject) {
4026
- const blurredPeriod = blurPeriod(
4027
- document.period,
4028
- programBlur.researchSubject,
4029
- programBlur.location
4030
- );
4243
+ const blurredPeriod = blurPeriod(document.period, programBlur.researchSubject);
4031
4244
  return __spreadValues(__spreadValues({}, document), blurredPeriod && { period: blurredPeriod });
4032
4245
  }
4033
4246
  return document;
4034
4247
  };
4035
- var blurAuthored = (authored, blurFn, location) => {
4036
- if (!location || !blurFn) {
4248
+ var blurAuthored = (authored, blurFn) => {
4249
+ if (!blurFn) {
4037
4250
  return authored;
4038
4251
  }
4039
- const authoredDate = DateTime.fromISO(authored).setZone(location);
4252
+ const authoredDate = DateTime3.fromISO(authored);
4040
4253
  if (!authoredDate || !authoredDate.isValid) {
4041
4254
  return authored;
4042
4255
  }
4043
4256
  return blurDate(authoredDate, blurFn).toString();
4044
4257
  };
4045
- var blurPeriod = (period, blurFn, location) => {
4258
+ var blurPeriod = (period, blurFn) => {
4046
4259
  if (!period) {
4047
4260
  return void 0;
4048
4261
  }
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;
4262
+ const startDate = period.start ? DateTime3.fromISO(period.start) : void 0;
4263
+ const endDate = period.end ? DateTime3.fromISO(period.end) : void 0;
4051
4264
  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
4265
  };
4053
4266
  var blurDate = (date, blurFn) => {
@@ -4744,7 +4957,7 @@ var isScoreEligible = (step, score) => {
4744
4957
  };
4745
4958
 
4746
4959
  // src/helper/guard.ts
4747
- import { DateTime as DateTime2, Duration } from "luxon";
4960
+ import { DateTime as DateTime4, Duration } from "luxon";
4748
4961
  var evaluateOperator = (value, operator, operand) => {
4749
4962
  switch (operator) {
4750
4963
  case "exists":
@@ -4800,7 +5013,7 @@ var DateCondition = class {
4800
5013
  if (answer && !Array.isArray(answer)) {
4801
5014
  const jsDate = new Date(answer);
4802
5015
  if (!isNaN(jsDate.getTime())) {
4803
- const answerDate = DateTime2.fromJSDate(jsDate).plus(this.offset);
5016
+ const answerDate = DateTime4.fromJSDate(jsDate).plus(this.offset);
4804
5017
  return answerDate.toJSDate();
4805
5018
  }
4806
5019
  }
@@ -4889,7 +5102,7 @@ var AnswerCodeCondition = class {
4889
5102
  if (!fulfilled) {
4890
5103
  return null;
4891
5104
  }
4892
- const answerDate = DateTime2.fromJSDate(multiAnswers[this.surveyName].date).plus(this.offset);
5105
+ const answerDate = DateTime4.fromJSDate(multiAnswers[this.surveyName].date).plus(this.offset);
4893
5106
  return answerDate.toJSDate();
4894
5107
  }
4895
5108
  };
@@ -5023,7 +5236,7 @@ var Offset = class {
5023
5236
  getFulfillmentDate(multiAnswers, consentTimes) {
5024
5237
  const fulfillmentDate = this.condition.getFulfillmentDate(multiAnswers, consentTimes);
5025
5238
  if (fulfillmentDate && !isNaN(fulfillmentDate == null ? void 0 : fulfillmentDate.getTime())) {
5026
- const answerDate = DateTime2.fromJSDate(fulfillmentDate).plus(this.offset);
5239
+ const answerDate = DateTime4.fromJSDate(fulfillmentDate).plus(this.offset);
5027
5240
  return answerDate.toJSDate();
5028
5241
  }
5029
5242
  return null;
@@ -5045,7 +5258,7 @@ var ConsentCondition = class {
5045
5258
  if (!(consentTimes == null ? void 0 : consentTimes[this.consentKey])) {
5046
5259
  return null;
5047
5260
  }
5048
- return DateTime2.fromJSDate(consentTimes[this.consentKey]).plus(this.offset).toJSDate();
5261
+ return DateTime4.fromJSDate(consentTimes[this.consentKey]).plus(this.offset).toJSDate();
5049
5262
  }
5050
5263
  };
5051
5264
  var SurveyCondition = class {
@@ -5067,7 +5280,7 @@ var SurveyCondition = class {
5067
5280
  if (!date) {
5068
5281
  return null;
5069
5282
  }
5070
- const answerDate = DateTime2.fromJSDate(date).plus(this.offset);
5283
+ const answerDate = DateTime4.fromJSDate(date).plus(this.offset);
5071
5284
  if (!answerDate.isValid) {
5072
5285
  return null;
5073
5286
  }
@@ -5090,7 +5303,7 @@ var RoutineCondition = class {
5090
5303
  if (!(routineTimes == null ? void 0 : routineTimes[this.routineName])) {
5091
5304
  return null;
5092
5305
  }
5093
- return DateTime2.fromJSDate(routineTimes[this.routineName]).plus(this.offset).toJSDate();
5306
+ return DateTime4.fromJSDate(routineTimes[this.routineName]).plus(this.offset).toJSDate();
5094
5307
  }
5095
5308
  };
5096
5309
  var generateQuestionGuard = (enableWhen, enableBehavior) => {
@@ -5490,6 +5703,7 @@ export {
5490
5703
  SliderStepSizeExtension,
5491
5704
  StudyNameSystem,
5492
5705
  SurveyCondition,
5706
+ TimeFuncs,
5493
5707
  TokenNamingSystem,
5494
5708
  ULID_REGEX,
5495
5709
  ULIDtoUUID,
@@ -5500,7 +5714,10 @@ export {
5500
5714
  WebConverter,
5501
5715
  YearCondition,
5502
5716
  binaryStringToUint8Array,
5717
+ calculateQuestionnaireScoreRange,
5718
+ calculateScoreRangeForItem,
5503
5719
  capitalizeFirstLetter,
5720
+ dayFormat,
5504
5721
  extractWordsFromRecoveryPhrase,
5505
5722
  generateFHIRConsent,
5506
5723
  generateFHIRQuestionnaireScore,
@@ -5517,11 +5734,18 @@ export {
5517
5734
  getLanguageOfPassPhrase,
5518
5735
  getMovellaAnnotation,
5519
5736
  getText,
5737
+ getTimeFuncs,
5738
+ indexArrayBy,
5739
+ isBeforeStartDate,
5740
+ isNotAfterEndDate,
5741
+ isNotBeforeStartDate,
5520
5742
  isScoreEligible,
5521
5743
  isYearString,
5522
5744
  newStepDateCondtion,
5745
+ overwriteTime,
5523
5746
  stringToDate,
5524
5747
  stringToYear,
5748
+ timezonelessDate,
5525
5749
  toDeviceConfig,
5526
5750
  toQuestionnaireResponseItem,
5527
5751
  uint8ArrayToBinaryString,