@case-framework/survey-core 0.4.1 → 0.4.2

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/build/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { $ as generateId, A as DurationUnits, B as ConstExpression, C as ReservedSurveyItemTypes, D as deserializeSurveyItemPrefill, E as SurveyItemPrefillTargetType, F as TemplateDefTypes, G as FunctionExpression, H as ContextVariableType, I as deserializeTemplateValue, J as ReferenceUsageType, K as FunctionExpressionNames, L as deserializeTemplateValues, M as ValueType, N as assertResponseValue, O as prefillTargetsEqual, P as isResponseValue, Q as generateCodingKey, R as serializeTemplateValue, S as SurveyItemKey, T as SurveyItemPrefillApplyMode, U as Expression, V as ContextVariableExpression, W as ExpressionType, X as ValueReferenceMethod, Y as ValueReference, Z as createSeededRandom, _ as toItemTypeDefinitionRegistry, a as getContentPlainText, b as builtInItemCoreRegistry, c as hasRenderableRichTextContent, d as SurveyTranslations, et as shuffleArray, f as validateLocale, g as createItemTypeDefinitionRegistry, h as createItemCore, i as getAssetUsagesFromContent, j as NumberPrecision, k as serializeSurveyItemPrefill, l as isContentEmpty, m as createFullRegistry, n as ContentType, nt as structuredCloneMethod, o as getPlainTextFromRichTextContent, p as CURRENT_SURVEY_SCHEMA, q as ResponseVariableExpression, r as createRichTextContent, s as hasRenderableRichTextBlock, t as Survey, tt as shuffleIndices, u as SurveyItemTranslations, v as GroupItemCore, w as SurveyItemCore, x as isBuiltInItemType, y as PageBreakItemCore, z as serializeTemplateValues } from "./survey-DnLtf19j.mjs";
1
+ import { $ as generateId, A as DurationUnits, B as ConstExpression, C as ReservedSurveyItemTypes, D as deserializeSurveyItemPrefill, E as SurveyItemPrefillTargetType, F as TemplateDefTypes, G as FunctionExpression, H as ContextVariableType, I as deserializeTemplateValue, J as ReferenceUsageType, K as FunctionExpressionNames, L as deserializeTemplateValues, M as ValueType, N as assertResponseValue, O as prefillTargetsEqual, P as isResponseValue, Q as generateCodingKey, R as serializeTemplateValue, S as SurveyItemKey, T as SurveyItemPrefillApplyMode, U as Expression, V as ContextVariableExpression, W as ExpressionType, X as ValueReferenceMethod, Y as ValueReference, Z as createSeededRandom, _ as toItemTypeDefinitionRegistry, a as getContentPlainText, b as builtInItemCoreRegistry, c as hasRenderableRichTextContent, d as SurveyTranslations, et as shuffleArray, f as validateLocale, g as createItemTypeDefinitionRegistry, h as createItemCore, i as getAssetUsagesFromContent, j as NumberPrecision, k as serializeSurveyItemPrefill, l as isContentEmpty, m as createFullRegistry, n as ContentType, nt as structuredCloneMethod, o as getPlainTextFromRichTextContent, p as CURRENT_SURVEY_SCHEMA, q as ResponseVariableExpression, r as createRichTextContent, s as hasRenderableRichTextBlock, t as Survey, tt as shuffleIndices, u as SurveyItemTranslations, v as GroupItemCore, w as SurveyItemCore, x as isBuiltInItemType, y as PageBreakItemCore, z as serializeTemplateValues } from "./survey-Dxt6iolo.mjs";
2
2
  import { format } from "date-fns";
3
3
  import { enUS } from "date-fns/locale";
4
4
  //#region src/to_mirgrate/data_types/legacy-types.ts
@@ -1654,6 +1654,180 @@ const flattenTree = (itemTree) => {
1654
1654
  return flatTree;
1655
1655
  };
1656
1656
  //#endregion
1657
+ //#region src/json-patch.ts
1658
+ var JsonPatchError = class extends Error {
1659
+ constructor(message, code, path, operationIndex) {
1660
+ super(message);
1661
+ this.code = code;
1662
+ this.path = path;
1663
+ this.operationIndex = operationIndex;
1664
+ this.name = "JsonPatchError";
1665
+ }
1666
+ };
1667
+ const hasOwn = (value, key) => Object.prototype.hasOwnProperty.call(value, key);
1668
+ const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
1669
+ const assertValidPointerSegment = (segment, pointer) => {
1670
+ if (/~(?![01])/.test(segment)) throw new JsonPatchError(`Invalid JSON pointer segment "${segment}" in "${pointer}". Only "~0" and "~1" escapes are allowed.`, "invalid-json-pointer", pointer);
1671
+ };
1672
+ const decodeJsonPointerSegment = (segment) => {
1673
+ assertValidPointerSegment(segment, segment);
1674
+ return segment.replace(/~1/g, "/").replace(/~0/g, "~");
1675
+ };
1676
+ const encodeJsonPointerSegment = (segment) => segment.replace(/~/g, "~0").replace(/\//g, "~1");
1677
+ const parseJsonPointer = (pointer) => {
1678
+ if (pointer === "") return [];
1679
+ if (!pointer.startsWith("/")) throw new JsonPatchError(`Invalid JSON pointer "${pointer}". It must be empty or start with "/".`, "invalid-json-pointer", pointer);
1680
+ return pointer.slice(1).split("/").map((segment) => {
1681
+ assertValidPointerSegment(segment, pointer);
1682
+ return segment.replace(/~1/g, "/").replace(/~0/g, "~");
1683
+ });
1684
+ };
1685
+ const parseArrayIndex = ({ segment, arrayLength, allowAppend, allowEnd, path }) => {
1686
+ if (segment === "-") {
1687
+ if (allowAppend) return arrayLength;
1688
+ throw new JsonPatchError(`JSON pointer "${path}" uses "-" where an existing array index is required.`, "invalid-array-index", path);
1689
+ }
1690
+ if (!/^(0|[1-9][0-9]*)$/.test(segment)) throw new JsonPatchError(`JSON pointer "${path}" uses invalid array index "${segment}".`, "invalid-array-index", path);
1691
+ const index = Number(segment);
1692
+ const maximum = allowEnd ? arrayLength : arrayLength - 1;
1693
+ if (index < 0 || index > maximum) throw new JsonPatchError(`JSON pointer "${path}" array index ${index} is out of bounds.`, "array-index-out-of-bounds", path);
1694
+ return index;
1695
+ };
1696
+ const getValueAtSegments = (document, segments, path) => {
1697
+ let current = document;
1698
+ for (const segment of segments) {
1699
+ if (Array.isArray(current)) {
1700
+ const index = parseArrayIndex({
1701
+ segment,
1702
+ arrayLength: current.length,
1703
+ allowAppend: false,
1704
+ allowEnd: false,
1705
+ path
1706
+ });
1707
+ current = current[index];
1708
+ continue;
1709
+ }
1710
+ if (!isRecord(current) || !hasOwn(current, segment)) throw new JsonPatchError(`JSON pointer "${path}" does not exist.`, "path-not-found", path);
1711
+ current = current[segment];
1712
+ }
1713
+ return current;
1714
+ };
1715
+ const getValueAtJsonPointer = (document, pointer) => {
1716
+ return getValueAtSegments(document, parseJsonPointer(pointer), pointer);
1717
+ };
1718
+ const deepEqualJson = (left, right) => {
1719
+ if (left === right) return true;
1720
+ if (Array.isArray(left) || Array.isArray(right)) {
1721
+ if (!Array.isArray(left) || !Array.isArray(right) || left.length !== right.length) return false;
1722
+ return left.every((entry, index) => deepEqualJson(entry, right[index]));
1723
+ }
1724
+ if (isRecord(left) || isRecord(right)) {
1725
+ if (!isRecord(left) || !isRecord(right)) return false;
1726
+ const leftKeys = Object.keys(left).sort();
1727
+ const rightKeys = Object.keys(right).sort();
1728
+ if (leftKeys.length !== rightKeys.length) return false;
1729
+ return leftKeys.every((key, index) => key === rightKeys[index] && deepEqualJson(left[key], right[key]));
1730
+ }
1731
+ return false;
1732
+ };
1733
+ const applyAdd = (document, path, value) => {
1734
+ const segments = parseJsonPointer(path);
1735
+ if (segments.length === 0) return structuredCloneMethod(value);
1736
+ const parentSegments = segments.slice(0, -1);
1737
+ const key = segments[segments.length - 1];
1738
+ const parent = getValueAtSegments(document, parentSegments, path);
1739
+ if (Array.isArray(parent)) {
1740
+ const index = parseArrayIndex({
1741
+ segment: key,
1742
+ arrayLength: parent.length,
1743
+ allowAppend: true,
1744
+ allowEnd: true,
1745
+ path
1746
+ });
1747
+ parent.splice(index, 0, structuredCloneMethod(value));
1748
+ return document;
1749
+ }
1750
+ if (!isRecord(parent)) throw new JsonPatchError(`JSON pointer "${path}" parent is not an object or array.`, "invalid-target-parent", path);
1751
+ parent[key] = structuredCloneMethod(value);
1752
+ return document;
1753
+ };
1754
+ const applyRemove = (document, path) => {
1755
+ const segments = parseJsonPointer(path);
1756
+ if (segments.length === 0) throw new JsonPatchError("Removing the document root is not supported.", "remove-root", path);
1757
+ const parentSegments = segments.slice(0, -1);
1758
+ const key = segments[segments.length - 1];
1759
+ const parent = getValueAtSegments(document, parentSegments, path);
1760
+ if (Array.isArray(parent)) {
1761
+ const index = parseArrayIndex({
1762
+ segment: key,
1763
+ arrayLength: parent.length,
1764
+ allowAppend: false,
1765
+ allowEnd: false,
1766
+ path
1767
+ });
1768
+ const [removedValue] = parent.splice(index, 1);
1769
+ return {
1770
+ document,
1771
+ removedValue
1772
+ };
1773
+ }
1774
+ if (!isRecord(parent) || !hasOwn(parent, key)) throw new JsonPatchError(`JSON pointer "${path}" does not exist.`, "path-not-found", path);
1775
+ const removedValue = parent[key];
1776
+ delete parent[key];
1777
+ return {
1778
+ document,
1779
+ removedValue
1780
+ };
1781
+ };
1782
+ const applyReplace = (document, path, value) => {
1783
+ const segments = parseJsonPointer(path);
1784
+ if (segments.length === 0) return structuredCloneMethod(value);
1785
+ getValueAtSegments(document, segments, path);
1786
+ applyRemove(document, path);
1787
+ return applyAdd(document, path, value);
1788
+ };
1789
+ const assertMoveTargetIsNotDescendant = (from, path) => {
1790
+ if (from === "" || path === from || !path.startsWith(`${from}/`)) return;
1791
+ throw new JsonPatchError(`JSON Patch cannot move "${from}" into its own descendant "${path}".`, "move-into-descendant", path);
1792
+ };
1793
+ const applyJsonPatch = (document, operations) => {
1794
+ let patchedDocument = structuredCloneMethod(document);
1795
+ operations.forEach((operation, operationIndex) => {
1796
+ try {
1797
+ switch (operation.op) {
1798
+ case "add":
1799
+ patchedDocument = applyAdd(patchedDocument, operation.path, operation.value);
1800
+ break;
1801
+ case "remove":
1802
+ patchedDocument = applyRemove(patchedDocument, operation.path).document;
1803
+ break;
1804
+ case "replace":
1805
+ patchedDocument = applyReplace(patchedDocument, operation.path, operation.value);
1806
+ break;
1807
+ case "move": {
1808
+ assertMoveTargetIsNotDescendant(operation.from, operation.path);
1809
+ if (operation.from === operation.path) break;
1810
+ const { document: withoutValue, removedValue } = applyRemove(patchedDocument, operation.from);
1811
+ patchedDocument = applyAdd(withoutValue, operation.path, removedValue);
1812
+ break;
1813
+ }
1814
+ case "copy": {
1815
+ const value = getValueAtJsonPointer(patchedDocument, operation.from);
1816
+ patchedDocument = applyAdd(patchedDocument, operation.path, value);
1817
+ break;
1818
+ }
1819
+ case "test":
1820
+ if (!deepEqualJson(getValueAtJsonPointer(patchedDocument, operation.path), operation.value)) throw new JsonPatchError(`JSON Patch test failed at "${operation.path}".`, "test-failed", operation.path);
1821
+ break;
1822
+ }
1823
+ } catch (error) {
1824
+ if (error instanceof JsonPatchError) throw new JsonPatchError(error.message, error.code, error.path, operationIndex);
1825
+ throw error;
1826
+ }
1827
+ });
1828
+ return patchedDocument;
1829
+ };
1830
+ //#endregion
1657
1831
  //#region src/response-exporter/types.ts
1658
1832
  /** Meta column keys in fixed order (common attributes first). */
1659
1833
  const META_COLUMN_ORDER = [
@@ -2006,6 +2180,6 @@ function generateCodebook(survey, options) {
2006
2180
  };
2007
2181
  }
2008
2182
  //#endregion
2009
- export { AndExpressionEditor, CURRENT_SURVEY_SCHEMA, ConstBooleanEditor, ConstDateArrayEditor, ConstDateEditor, ConstExpression, ConstNumberArrayEditor, ConstNumberEditor, ConstStringArrayEditor, ConstStringEditor, ContentType, ContextVariableExpression, ContextVariableType, CtxCustomExpressionEditor, CtxCustomValueEditor, CtxLocaleEditor, CtxPFlagDateEditor, CtxPFlagIsDefinedEditor, CtxPFlagNumEditor, CtxPFlagStringEditor, DEFAULT_TRANSFORM, DurationUnits, EXPORT_COLUMN_SLOT_SEPARATOR, EqExpressionEditor, Expression, ExpressionEditor, ExpressionEvaluator, ExpressionType, FunctionExpression, FunctionExpressionNames, GroupItemCore, GtExpressionEditor, GteExpressionEditor, InRangeExpressionEditor, LtExpressionEditor, LteExpressionEditor, META_COLUMN_ORDER, MaxExpressionEditor, MinExpressionEditor, NumberPrecision, OrExpressionEditor, PageBreakItemCore, ReferenceUsageType, ReservedSurveyItemTypes, ResponseItem, ResponseVariableEditor, ResponseVariableExpression, SURVEY_RESPONSE_SCHEMA_VERSION, SlotTransformMode, StrEqExpressionEditor, StrListContainsExpressionEditor, SumExpressionEditor, Survey, SurveyEngineCore, SurveyEventTypes, SurveyItemCore, SurveyItemKey, SurveyItemPrefillApplyMode, SurveyItemPrefillTargetType, SurveyItemResponse, SurveyItemTranslations, SurveyResponse, SurveyResponseExporter, SurveyTranslations, TemplateDefTypes, ValueReference, ValueReferenceMethod, ValueType, and, assertResponseValue, buildItemExpression, builtInItemCoreRegistry, const_boolean, const_date, const_date_array, const_number, const_number_array, const_string, const_string_array, createFullRegistry, createItemCore, createItemTypeDefinitionRegistry, createRichTextContent, createSeededRandom, ctx_custom_expression, ctx_custom_value, ctx_locale, ctx_pflag_date, ctx_pflag_is_defined, ctx_pflag_num, ctx_pflag_string, deserializeSurveyItemPrefill, deserializeTemplateValue, deserializeTemplateValues, eq, escapeCsvCell, exportSingleResponseSlot, flattenTree, generateCodebook, generateCodingKey, generateId, getAssetUsagesFromContent, getContentPlainText, getItemExpressionDefinition, getPlainTextFromRichTextContent, gt, gte, hasRenderableRichTextBlock, hasRenderableRichTextContent, in_range, initValueForType, isBuiltInItemType, isContentEmpty, isLegacyItemGroupComponent, isLegacySurveyGroupItem, isResponseValue, lt, lte, max, min, or, prefillTargetsEqual, response_boolean, response_date, response_date_array, response_number, response_number_array, response_string, response_string_array, serializeSurveyItemPrefill, serializeTemplateValue, serializeTemplateValues, serializeToCsv, shuffleArray, shuffleIndices, str_eq, str_list_contains, structuredCloneMethod, sum, toItemTypeDefinitionRegistry, validateLocale };
2183
+ export { AndExpressionEditor, CURRENT_SURVEY_SCHEMA, ConstBooleanEditor, ConstDateArrayEditor, ConstDateEditor, ConstExpression, ConstNumberArrayEditor, ConstNumberEditor, ConstStringArrayEditor, ConstStringEditor, ContentType, ContextVariableExpression, ContextVariableType, CtxCustomExpressionEditor, CtxCustomValueEditor, CtxLocaleEditor, CtxPFlagDateEditor, CtxPFlagIsDefinedEditor, CtxPFlagNumEditor, CtxPFlagStringEditor, DEFAULT_TRANSFORM, DurationUnits, EXPORT_COLUMN_SLOT_SEPARATOR, EqExpressionEditor, Expression, ExpressionEditor, ExpressionEvaluator, ExpressionType, FunctionExpression, FunctionExpressionNames, GroupItemCore, GtExpressionEditor, GteExpressionEditor, InRangeExpressionEditor, JsonPatchError, LtExpressionEditor, LteExpressionEditor, META_COLUMN_ORDER, MaxExpressionEditor, MinExpressionEditor, NumberPrecision, OrExpressionEditor, PageBreakItemCore, ReferenceUsageType, ReservedSurveyItemTypes, ResponseItem, ResponseVariableEditor, ResponseVariableExpression, SURVEY_RESPONSE_SCHEMA_VERSION, SlotTransformMode, StrEqExpressionEditor, StrListContainsExpressionEditor, SumExpressionEditor, Survey, SurveyEngineCore, SurveyEventTypes, SurveyItemCore, SurveyItemKey, SurveyItemPrefillApplyMode, SurveyItemPrefillTargetType, SurveyItemResponse, SurveyItemTranslations, SurveyResponse, SurveyResponseExporter, SurveyTranslations, TemplateDefTypes, ValueReference, ValueReferenceMethod, ValueType, and, applyJsonPatch, assertResponseValue, buildItemExpression, builtInItemCoreRegistry, const_boolean, const_date, const_date_array, const_number, const_number_array, const_string, const_string_array, createFullRegistry, createItemCore, createItemTypeDefinitionRegistry, createRichTextContent, createSeededRandom, ctx_custom_expression, ctx_custom_value, ctx_locale, ctx_pflag_date, ctx_pflag_is_defined, ctx_pflag_num, ctx_pflag_string, decodeJsonPointerSegment, deserializeSurveyItemPrefill, deserializeTemplateValue, deserializeTemplateValues, encodeJsonPointerSegment, eq, escapeCsvCell, exportSingleResponseSlot, flattenTree, generateCodebook, generateCodingKey, generateId, getAssetUsagesFromContent, getContentPlainText, getItemExpressionDefinition, getPlainTextFromRichTextContent, getValueAtJsonPointer, gt, gte, hasRenderableRichTextBlock, hasRenderableRichTextContent, in_range, initValueForType, isBuiltInItemType, isContentEmpty, isLegacyItemGroupComponent, isLegacySurveyGroupItem, isResponseValue, lt, lte, max, min, or, parseJsonPointer, prefillTargetsEqual, response_boolean, response_date, response_date_array, response_number, response_number_array, response_string, response_string_array, serializeSurveyItemPrefill, serializeTemplateValue, serializeTemplateValues, serializeToCsv, shuffleArray, shuffleIndices, str_eq, str_list_contains, structuredCloneMethod, sum, toItemTypeDefinitionRegistry, validateLocale };
2010
2184
 
2011
2185
  //# sourceMappingURL=index.mjs.map