@medplum/core 0.9.7 → 0.9.10

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/cjs/index.js CHANGED
@@ -55,9 +55,17 @@
55
55
  __classPrivateFieldSet(this, _LRUCache_max, max, "f");
56
56
  __classPrivateFieldSet(this, _LRUCache_cache, new Map(), "f");
57
57
  }
58
+ /**
59
+ * Deletes all values from the cache.
60
+ */
58
61
  clear() {
59
62
  __classPrivateFieldGet(this, _LRUCache_cache, "f").clear();
60
63
  }
64
+ /**
65
+ * Returns the value for the given key.
66
+ * @param key The key to retrieve.
67
+ * @returns The value if found; undefined otherwise.
68
+ */
61
69
  get(key) {
62
70
  const item = __classPrivateFieldGet(this, _LRUCache_cache, "f").get(key);
63
71
  if (item) {
@@ -66,6 +74,11 @@
66
74
  }
67
75
  return item;
68
76
  }
77
+ /**
78
+ * Sets the value for the given key.
79
+ * @param key The key to set.
80
+ * @param val The value to set.
81
+ */
69
82
  set(key, val) {
70
83
  if (__classPrivateFieldGet(this, _LRUCache_cache, "f").has(key)) {
71
84
  __classPrivateFieldGet(this, _LRUCache_cache, "f").delete(key);
@@ -75,9 +88,20 @@
75
88
  }
76
89
  __classPrivateFieldGet(this, _LRUCache_cache, "f").set(key, val);
77
90
  }
91
+ /**
92
+ * Deletes the value for the given key.
93
+ * @param key The key to delete.
94
+ */
78
95
  delete(key) {
79
96
  __classPrivateFieldGet(this, _LRUCache_cache, "f").delete(key);
80
97
  }
98
+ /**
99
+ * Returns the list of all keys in the cache.
100
+ * @returns The array of keys in the cache.
101
+ */
102
+ keys() {
103
+ return __classPrivateFieldGet(this, _LRUCache_cache, "f").keys();
104
+ }
81
105
  }
82
106
  _LRUCache_max = new WeakMap(), _LRUCache_cache = new WeakMap(), _LRUCache_instances = new WeakSet(), _LRUCache_first = function _LRUCache_first() {
83
107
  // This works because the Map class maintains ordered keys.
@@ -786,6 +810,106 @@
786
810
  }
787
811
  }
788
812
 
813
+ /*
814
+ * This file attempts a unified "generatePdf" function that works both client-side and server-side.
815
+ * On client-side, it checks for a global "pdfMake" variable.
816
+ * On server-side, it dynamically loads "pdfmake" from the node_modules.
817
+ */
818
+ function generatePdf(docDefinition, tableLayouts, fonts) {
819
+ return __awaiter(this, void 0, void 0, function* () {
820
+ // Setup sane defaults
821
+ // See: https://pdfmake.github.io/docs/0.1/document-definition-object/styling/
822
+ docDefinition.pageSize = docDefinition.pageSize || 'LETTER';
823
+ docDefinition.pageMargins = docDefinition.pageMargins || [60, 60, 60, 60];
824
+ docDefinition.pageOrientation = docDefinition.pageOrientation || 'portrait';
825
+ docDefinition.defaultStyle = docDefinition.defaultStyle || {};
826
+ docDefinition.defaultStyle.font = docDefinition.defaultStyle.font || 'Helvetica';
827
+ docDefinition.defaultStyle.fontSize = docDefinition.defaultStyle.fontSize || 11;
828
+ docDefinition.defaultStyle.lineHeight = docDefinition.defaultStyle.lineHeight || 2.0;
829
+ if (typeof window !== 'undefined' && typeof pdfMake !== 'undefined') {
830
+ return generatePdfClientSide(docDefinition, tableLayouts, fonts);
831
+ }
832
+ if (typeof process !== 'undefined' && typeof require !== 'undefined') {
833
+ return generatePdfServerSide(docDefinition, tableLayouts, fonts);
834
+ }
835
+ throw new Error('Unable to determine PDF environment');
836
+ });
837
+ }
838
+ function generatePdfServerSide(docDefinition, tableLayouts, fonts) {
839
+ return __awaiter(this, void 0, void 0, function* () {
840
+ if (!fonts) {
841
+ fonts = {
842
+ Helvetica: {
843
+ normal: 'Helvetica',
844
+ bold: 'Helvetica-Bold',
845
+ italics: 'Helvetica-Oblique',
846
+ bolditalics: 'Helvetica-BoldOblique',
847
+ },
848
+ };
849
+ }
850
+ return new Promise((resolve, reject) => {
851
+ try {
852
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
853
+ const PdfPrinter = require('pdfmake');
854
+ const printer = new PdfPrinter(fonts);
855
+ const pdfDoc = printer.createPdfKitDocument(docDefinition, { tableLayouts });
856
+ const chunks = [];
857
+ pdfDoc.on('data', (chunk) => chunks.push(chunk));
858
+ pdfDoc.on('end', () => resolve(concat(chunks)));
859
+ pdfDoc.on('error', reject);
860
+ pdfDoc.end();
861
+ }
862
+ catch (err) {
863
+ reject(err);
864
+ }
865
+ });
866
+ });
867
+ }
868
+ function generatePdfClientSide(docDefinition, tableLayouts, fonts) {
869
+ return __awaiter(this, void 0, void 0, function* () {
870
+ if (!fonts) {
871
+ fonts = {
872
+ Helvetica: {
873
+ normal: 'https://static.medplum.com/fonts/Helvetica.ttf',
874
+ bold: 'https://static.medplum.com/fonts/Helvetica-bold.ttf',
875
+ italics: 'https://static.medplum.com/fonts/Helvetica-italic.ttf',
876
+ bolditalics: 'https://static.medplum.com/fonts/Helvetica-bold-italic.ttf',
877
+ },
878
+ Roboto: {
879
+ normal: 'https://static.medplum.com/fonts/Roboto-Regular.ttf',
880
+ bold: 'https://static.medplum.com/fonts/Roboto-Medium.ttf',
881
+ italics: 'https://static.medplum.com/fonts/Roboto-Italic.ttf',
882
+ bolditalics: 'https://static.medplum.com/fonts/Roboto-MediumItalic.ttf',
883
+ },
884
+ Avenir: {
885
+ normal: 'https://static.medplum.com/fonts/avenir.ttf',
886
+ },
887
+ };
888
+ }
889
+ return new Promise((resolve) => {
890
+ pdfMake.createPdf(docDefinition, tableLayouts, fonts).getBlob(resolve);
891
+ });
892
+ });
893
+ }
894
+ /**
895
+ * Concatenates an array of Uint8Arrays into a single Uint8Array.
896
+ * @param arrays An array of arrays of bytes.
897
+ * @returns A single array of bytes.
898
+ */
899
+ function concat(arrays) {
900
+ let len = 0;
901
+ for (const array of arrays) {
902
+ len += array.length;
903
+ }
904
+ const result = new Uint8Array(len);
905
+ let index = 0;
906
+ for (const array of arrays) {
907
+ result.set(array, index);
908
+ index += array.length;
909
+ }
910
+ return result;
911
+ }
912
+
789
913
  var _ReadablePromise_suspender, _ReadablePromise_status, _ReadablePromise_response, _ReadablePromise_error, _a;
790
914
  /**
791
915
  * The ReadablePromise class wraps a request promise suitable for React Suspense.
@@ -1472,6 +1596,26 @@
1472
1596
  __classPrivateFieldSet(this, _MedplumClient_config, undefined, "f");
1473
1597
  this.dispatchEvent({ type: 'change' });
1474
1598
  }
1599
+ /**
1600
+ * Invalidates any cached values or cached requests for the given URL.
1601
+ * @param url The URL to invalidate.
1602
+ */
1603
+ invalidateUrl(url) {
1604
+ url = url.toString();
1605
+ __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").delete(url);
1606
+ }
1607
+ /**
1608
+ * Invalidates all cached search results or cached requests for the given resourceType.
1609
+ * @param resourceType The resource type to invalidate.
1610
+ */
1611
+ invalidateSearches(resourceType) {
1612
+ const url = 'fhir/R4/' + resourceType;
1613
+ for (const key of __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").keys()) {
1614
+ if (key.endsWith(url) || key.includes(url + '?')) {
1615
+ __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").delete(key);
1616
+ }
1617
+ }
1618
+ }
1475
1619
  /**
1476
1620
  * Makes an HTTP GET request to the specified URL.
1477
1621
  *
@@ -1516,7 +1660,7 @@
1516
1660
  if (contentType) {
1517
1661
  __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_setRequestContentType).call(this, options, contentType);
1518
1662
  }
1519
- __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").delete(url);
1663
+ this.invalidateUrl(url);
1520
1664
  return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_request).call(this, 'POST', url, options);
1521
1665
  }
1522
1666
  /**
@@ -1540,7 +1684,7 @@
1540
1684
  if (contentType) {
1541
1685
  __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_setRequestContentType).call(this, options, contentType);
1542
1686
  }
1543
- __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").delete(url);
1687
+ this.invalidateUrl(url);
1544
1688
  return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_request).call(this, 'PUT', url, options);
1545
1689
  }
1546
1690
  /**
@@ -1559,7 +1703,7 @@
1559
1703
  url = url.toString();
1560
1704
  __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_setRequestBody).call(this, options, operations);
1561
1705
  __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_setRequestContentType).call(this, options, PATCH_CONTENT_TYPE);
1562
- __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").delete(url);
1706
+ this.invalidateUrl(url);
1563
1707
  return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_request).call(this, 'PATCH', url, options);
1564
1708
  }
1565
1709
  /**
@@ -1575,7 +1719,7 @@
1575
1719
  */
1576
1720
  delete(url, options = {}) {
1577
1721
  url = url.toString();
1578
- __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").delete(url);
1722
+ this.invalidateUrl(url);
1579
1723
  return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_request).call(this, 'DELETE', url, options);
1580
1724
  }
1581
1725
  /**
@@ -1653,6 +1797,19 @@
1653
1797
  fhirUrl(...path) {
1654
1798
  return new URL(__classPrivateFieldGet(this, _MedplumClient_baseUrl, "f") + 'fhir/R4/' + path.join('/'));
1655
1799
  }
1800
+ /**
1801
+ * Builds a FHIR search URL from a search query or structured query object.
1802
+ * @param query The FHIR search query or structured query object.
1803
+ * @returns The well-formed FHIR URL.
1804
+ */
1805
+ fhirSearchUrl(query) {
1806
+ if (typeof query === 'string') {
1807
+ return this.fhirUrl(query);
1808
+ }
1809
+ const url = this.fhirUrl(query.resourceType);
1810
+ url.search = formatSearchQuery(query);
1811
+ return url;
1812
+ }
1656
1813
  /**
1657
1814
  * Sends a FHIR search request.
1658
1815
  *
@@ -1708,7 +1865,7 @@
1708
1865
  * @returns Promise to the search result bundle.
1709
1866
  */
1710
1867
  search(query, options = {}) {
1711
- return this.get(typeof query === 'string' ? 'fhir/R4/' + query : this.fhirUrl(query.resourceType) + formatSearchQuery(query), options);
1868
+ return this.get(this.fhirSearchUrl(query), options);
1712
1869
  }
1713
1870
  /**
1714
1871
  * Sends a FHIR search request for a single resource.
@@ -1732,7 +1889,16 @@
1732
1889
  searchOne(query, options = {}) {
1733
1890
  const search = typeof query === 'string' ? parseSearchDefinition(query) : query;
1734
1891
  search.count = 1;
1735
- return new ReadablePromise(this.search(search, options).then((bundle) => { var _a, _b; return (_b = (_a = bundle.entry) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.resource; }));
1892
+ const cacheKey = this.fhirSearchUrl(query).toString() + '-searchOne';
1893
+ if (!(options === null || options === void 0 ? void 0 : options.cache)) {
1894
+ const cached = __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").get(cacheKey);
1895
+ if (cached) {
1896
+ return cached;
1897
+ }
1898
+ }
1899
+ const promise = new ReadablePromise(this.search(search, options).then((b) => { var _a, _b; return (_b = (_a = b.entry) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.resource; }));
1900
+ __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").set(cacheKey, promise);
1901
+ return promise;
1736
1902
  }
1737
1903
  /**
1738
1904
  * Sends a FHIR search request for an array of resources.
@@ -1754,7 +1920,16 @@
1754
1920
  * @returns Promise to the search result bundle.
1755
1921
  */
1756
1922
  searchResources(query, options = {}) {
1757
- return new ReadablePromise(this.search(query, options).then((bundle) => { var _a, _b; return (_b = (_a = bundle.entry) === null || _a === void 0 ? void 0 : _a.map((entry) => entry.resource)) !== null && _b !== void 0 ? _b : []; }));
1923
+ const cacheKey = this.fhirSearchUrl(query).toString() + '-searchResources';
1924
+ if (!(options === null || options === void 0 ? void 0 : options.cache)) {
1925
+ const cached = __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").get(cacheKey);
1926
+ if (cached) {
1927
+ return cached;
1928
+ }
1929
+ }
1930
+ const promise = new ReadablePromise(this.search(query, options).then((b) => { var _a, _b; return (_b = (_a = b.entry) === null || _a === void 0 ? void 0 : _a.map((e) => e.resource)) !== null && _b !== void 0 ? _b : []; }));
1931
+ __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").set(cacheKey, promise);
1932
+ return promise;
1758
1933
  }
1759
1934
  /**
1760
1935
  * Searches a ValueSet resource using the "expand" operation.
@@ -1809,27 +1984,6 @@
1809
1984
  readResource(resourceType, id) {
1810
1985
  return this.get(this.fhirUrl(resourceType, id));
1811
1986
  }
1812
- /**
1813
- * Reads a resource by resource type and ID using the in-memory resource cache.
1814
- *
1815
- * If the resource is not available in the cache, it will be read from the server.
1816
- *
1817
- * Example:
1818
- *
1819
- * ```typescript
1820
- * const patient = await medplum.readCached('Patient', '123');
1821
- * console.log(patient);
1822
- * ```
1823
- *
1824
- * See the FHIR "read" operation for full details: https://www.hl7.org/fhir/http.html#read
1825
- *
1826
- * @param resourceType The FHIR resource type.
1827
- * @param id The resource ID.
1828
- * @returns The resource if available; undefined otherwise.
1829
- */
1830
- readCached(resourceType, id) {
1831
- return this.get(this.fhirUrl(resourceType, id));
1832
- }
1833
1987
  /**
1834
1988
  * Reads a resource by `Reference`.
1835
1989
  *
@@ -1856,34 +2010,6 @@
1856
2010
  const [resourceType, id] = refString.split('/');
1857
2011
  return this.readResource(resourceType, id);
1858
2012
  }
1859
- /**
1860
- * Reads a resource by `Reference` using the in-memory resource cache.
1861
- *
1862
- * This is a convenience method for `readResource()` that accepts a `Reference` object.
1863
- *
1864
- * If the resource is not available in the cache, it will be read from the server.
1865
- *
1866
- * Example:
1867
- *
1868
- * ```typescript
1869
- * const serviceRequest = await medplum.readResource('ServiceRequest', '123');
1870
- * const patient = await medplum.readCachedReference(serviceRequest.subject);
1871
- * console.log(patient);
1872
- * ```
1873
- *
1874
- * See the FHIR "read" operation for full details: https://www.hl7.org/fhir/http.html#read
1875
- *
1876
- * @param reference The FHIR reference object.
1877
- * @returns The resource if available; undefined otherwise.
1878
- */
1879
- readCachedReference(reference) {
1880
- const refString = reference === null || reference === void 0 ? void 0 : reference.reference;
1881
- if (!refString) {
1882
- return new ReadablePromise(Promise.reject(new Error('Missing reference')));
1883
- }
1884
- const [resourceType, id] = refString.split('/');
1885
- return this.readCached(resourceType, id);
1886
- }
1887
2013
  /**
1888
2014
  * Returns a cached schema for a resource type.
1889
2015
  * If the schema is not cached, returns undefined.
@@ -2014,6 +2140,7 @@
2014
2140
  if (!resource.resourceType) {
2015
2141
  throw new Error('Missing resourceType');
2016
2142
  }
2143
+ this.invalidateSearches(resource.resourceType);
2017
2144
  return this.post(this.fhirUrl(resource.resourceType), resource);
2018
2145
  }
2019
2146
  /**
@@ -2108,15 +2235,14 @@
2108
2235
  *
2109
2236
  * See the pdfmake document definition for full details: https://pdfmake.github.io/docs/0.1/document-definition-object/
2110
2237
  *
2111
- * @param docDefinition The FHIR resource to create.
2238
+ * @param docDefinition The PDF document definition.
2112
2239
  * @returns The result of the create operation.
2113
2240
  */
2114
- createPdf(docDefinition, filename) {
2115
- const url = this.fhirUrl('Binary', '$pdf');
2116
- if (filename) {
2117
- url.searchParams.set('_filename', filename);
2118
- }
2119
- return this.post(url, docDefinition, 'application/json');
2241
+ createPdf(docDefinition, filename, tableLayouts, fonts) {
2242
+ return __awaiter(this, void 0, void 0, function* () {
2243
+ const blob = yield generatePdf(docDefinition, tableLayouts, fonts);
2244
+ return this.createBinary(blob, filename, 'application/pdf');
2245
+ });
2120
2246
  }
2121
2247
  /**
2122
2248
  * Creates a FHIR `Communication` resource with the provided data content.
@@ -2183,6 +2309,7 @@
2183
2309
  if (!resource.id) {
2184
2310
  throw new Error('Missing id');
2185
2311
  }
2312
+ this.invalidateSearches(resource.resourceType);
2186
2313
  return this.put(this.fhirUrl(resource.resourceType, resource.id), resource);
2187
2314
  }
2188
2315
  /**
@@ -2209,6 +2336,7 @@
2209
2336
  * @returns The result of the patch operations.
2210
2337
  */
2211
2338
  patchResource(resourceType, id, operations) {
2339
+ this.invalidateSearches(resourceType);
2212
2340
  return this.patch(this.fhirUrl(resourceType, id), operations);
2213
2341
  }
2214
2342
  /**
@@ -2227,6 +2355,7 @@
2227
2355
  * @returns The result of the delete operation.
2228
2356
  */
2229
2357
  deleteResource(resourceType, id) {
2358
+ this.invalidateSearches(resourceType);
2230
2359
  return this.delete(this.fhirUrl(resourceType, id));
2231
2360
  }
2232
2361
  /**
@@ -2442,7 +2571,10 @@
2442
2571
  const headers = options.headers;
2443
2572
  headers['Content-Type'] = contentType;
2444
2573
  }, _MedplumClient_setRequestBody = function _MedplumClient_setRequestBody(options, data) {
2445
- if (typeof data === 'string' || (typeof File !== 'undefined' && data instanceof File)) {
2574
+ if (typeof data === 'string' ||
2575
+ (typeof Blob !== 'undefined' && data instanceof Blob) ||
2576
+ (typeof File !== 'undefined' && data instanceof File) ||
2577
+ (typeof Uint8Array !== 'undefined' && data instanceof Uint8Array)) {
2446
2578
  options.body = data;
2447
2579
  }
2448
2580
  else if (data) {
@@ -2557,47 +2689,38 @@
2557
2689
  }
2558
2690
 
2559
2691
  /**
2560
- * Ensures that the value is wrapped in an array.
2561
- * @param input The input as a an array or a value.
2562
- * @returns The input as an array.
2692
+ * Returns a single element array with a typed boolean value.
2693
+ * @param value The primitive boolean value.
2694
+ * @returns Single element array with a typed boolean value.
2563
2695
  */
2564
- function ensureArray(input) {
2565
- if (input === null || input === undefined) {
2566
- return [];
2567
- }
2568
- return Array.isArray(input) ? input : [input];
2696
+ function booleanToTypedValue(value) {
2697
+ return [{ type: exports.PropertyType.boolean, value }];
2569
2698
  }
2570
2699
  /**
2571
- * Applies a function to single value or an array of values.
2572
- * @param context The context which will be passed to the function.
2573
- * @param fn The function to apply.
2574
- * @returns The result of the function.
2700
+ * Returns a "best guess" TypedValue for a given value.
2701
+ * @param value The unknown value to check.
2702
+ * @returns A "best guess" TypedValue for the given value.
2575
2703
  */
2576
- function applyMaybeArray(context, fn) {
2577
- if (context === undefined) {
2578
- return undefined;
2704
+ function toTypedValue(value) {
2705
+ if (Number.isSafeInteger(value)) {
2706
+ return { type: exports.PropertyType.integer, value };
2579
2707
  }
2580
- if (Array.isArray(context)) {
2581
- return context
2582
- .map((e) => fn(e))
2583
- .filter((e) => !!e)
2584
- .flat();
2708
+ else if (typeof value === 'number') {
2709
+ return { type: exports.PropertyType.decimal, value };
2710
+ }
2711
+ else if (typeof value === 'boolean') {
2712
+ return { type: exports.PropertyType.boolean, value };
2713
+ }
2714
+ else if (typeof value === 'string') {
2715
+ return { type: exports.PropertyType.string, value };
2716
+ }
2717
+ else if (isQuantity(value)) {
2718
+ return { type: exports.PropertyType.Quantity, value };
2585
2719
  }
2586
2720
  else {
2587
- return fn(context);
2721
+ return { type: exports.PropertyType.BackboneElement, value };
2588
2722
  }
2589
2723
  }
2590
- /**
2591
- * Determines if the input is an empty array.
2592
- * @param obj Any value or array of values.
2593
- * @returns True if the input is an empty array.
2594
- */
2595
- function isEmptyArray(obj) {
2596
- return Array.isArray(obj) && obj.length === 0;
2597
- }
2598
- function isFalsy(obj) {
2599
- return !obj || isEmptyArray(obj);
2600
- }
2601
2724
  /**
2602
2725
  * Converts unknown object into a JavaScript boolean.
2603
2726
  * Note that this is different than the FHIRPath "toBoolean",
@@ -2606,10 +2729,7 @@
2606
2729
  * @returns The converted boolean value according to FHIRPath rules.
2607
2730
  */
2608
2731
  function toJsBoolean(obj) {
2609
- if (Array.isArray(obj)) {
2610
- return obj.length === 0 ? false : !!obj[0];
2611
- }
2612
- return !!obj;
2732
+ return obj.length === 0 ? false : !!obj[0].value;
2613
2733
  }
2614
2734
  /**
2615
2735
  * Removes duplicates in array using FHIRPath equality rules.
@@ -2621,7 +2741,7 @@
2621
2741
  for (const i of arr) {
2622
2742
  let found = false;
2623
2743
  for (const j of result) {
2624
- if (fhirPathEquals(i, j)) {
2744
+ if (toJsBoolean(fhirPathEquals(i, j))) {
2625
2745
  found = true;
2626
2746
  break;
2627
2747
  }
@@ -2632,6 +2752,29 @@
2632
2752
  }
2633
2753
  return result;
2634
2754
  }
2755
+ /**
2756
+ * Returns a negated FHIRPath boolean expression.
2757
+ * @param input The input array.
2758
+ * @returns The negated type value array.
2759
+ */
2760
+ function fhirPathNot(input) {
2761
+ return booleanToTypedValue(!toJsBoolean(input));
2762
+ }
2763
+ /**
2764
+ * Determines if two arrays are equal according to FHIRPath equality rules.
2765
+ * @param x The first array.
2766
+ * @param y The second array.
2767
+ * @returns FHIRPath true if the arrays are equal.
2768
+ */
2769
+ function fhirPathArrayEquals(x, y) {
2770
+ if (x.length === 0 || y.length === 0) {
2771
+ return [];
2772
+ }
2773
+ if (x.length !== y.length) {
2774
+ return booleanToTypedValue(false);
2775
+ }
2776
+ return booleanToTypedValue(x.every((val, index) => toJsBoolean(fhirPathEquals(val, y[index]))));
2777
+ }
2635
2778
  /**
2636
2779
  * Determines if two values are equal according to FHIRPath equality rules.
2637
2780
  * @param x The first value.
@@ -2639,69 +2782,89 @@
2639
2782
  * @returns True if equal.
2640
2783
  */
2641
2784
  function fhirPathEquals(x, y) {
2642
- if (isFalsy(x) && isFalsy(y)) {
2643
- return true;
2644
- }
2645
- if (isEmptyArray(x) || isEmptyArray(y)) {
2646
- return [];
2785
+ const xValue = x.value;
2786
+ const yValue = y.value;
2787
+ if (typeof xValue === 'number' && typeof yValue === 'number') {
2788
+ return booleanToTypedValue(Math.abs(xValue - yValue) < 1e-8);
2647
2789
  }
2648
- if (typeof x === 'number' && typeof y === 'number') {
2649
- return Math.abs(x - y) < 1e-8;
2790
+ if (isQuantity(xValue) && isQuantity(yValue)) {
2791
+ return booleanToTypedValue(isQuantityEquivalent(xValue, yValue));
2650
2792
  }
2651
- if (isQuantity(x) && isQuantity(y)) {
2652
- return isQuantityEquivalent(x, y);
2793
+ if (typeof xValue === 'object' && typeof yValue === 'object') {
2794
+ return booleanToTypedValue(deepEquals(x, y));
2653
2795
  }
2654
- if (Array.isArray(x) && Array.isArray(y)) {
2655
- return x.length === y.length && x.every((val, index) => fhirPathEquals(val, y[index]));
2796
+ return booleanToTypedValue(xValue === yValue);
2797
+ }
2798
+ /**
2799
+ * Determines if two arrays are equivalent according to FHIRPath equality rules.
2800
+ * @param x The first array.
2801
+ * @param y The second array.
2802
+ * @returns FHIRPath true if the arrays are equivalent.
2803
+ */
2804
+ function fhirPathArrayEquivalent(x, y) {
2805
+ if (x.length === 0 && y.length === 0) {
2806
+ return booleanToTypedValue(true);
2656
2807
  }
2657
- if (typeof x === 'object' && typeof y === 'object') {
2658
- return deepEquals(x, y);
2808
+ if (x.length !== y.length) {
2809
+ return booleanToTypedValue(false);
2659
2810
  }
2660
- return x === y;
2811
+ x.sort(fhirPathEquivalentCompare);
2812
+ y.sort(fhirPathEquivalentCompare);
2813
+ return booleanToTypedValue(x.every((val, index) => toJsBoolean(fhirPathEquivalent(val, y[index]))));
2661
2814
  }
2662
2815
  /**
2663
- * Determines if two values are equal according to FHIRPath equality rules.
2816
+ * Determines if two values are equivalent according to FHIRPath equality rules.
2664
2817
  * @param x The first value.
2665
2818
  * @param y The second value.
2666
- * @returns True if equal.
2819
+ * @returns True if equivalent.
2667
2820
  */
2668
2821
  function fhirPathEquivalent(x, y) {
2669
- if (isFalsy(x) && isFalsy(y)) {
2670
- return true;
2671
- }
2672
- if (isEmptyArray(x) || isEmptyArray(y)) {
2673
- // Note that this implies that if the collections have a different number of items to compare,
2674
- // or if one input is a value and the other is empty ({ }), the result will be false.
2675
- return false;
2676
- }
2677
- if (typeof x === 'number' && typeof y === 'number') {
2822
+ const xValue = x.value;
2823
+ const yValue = y.value;
2824
+ if (typeof xValue === 'number' && typeof yValue === 'number') {
2678
2825
  // Use more generous threshold than equality
2679
2826
  // Decimal: values must be equal, comparison is done on values rounded to the precision of the least precise operand.
2680
2827
  // Trailing zeroes after the decimal are ignored in determining precision.
2681
- return Math.abs(x - y) < 0.01;
2828
+ return booleanToTypedValue(Math.abs(xValue - yValue) < 0.01);
2682
2829
  }
2683
- if (isQuantity(x) && isQuantity(y)) {
2684
- return isQuantityEquivalent(x, y);
2830
+ if (isQuantity(xValue) && isQuantity(yValue)) {
2831
+ return booleanToTypedValue(isQuantityEquivalent(xValue, yValue));
2685
2832
  }
2686
- if (Array.isArray(x) && Array.isArray(y)) {
2687
- // If both operands are collections with multiple items:
2688
- // 1) Each item must be equivalent
2689
- // 2) Comparison is not order dependent
2690
- x.sort();
2691
- y.sort();
2692
- return x.length === y.length && x.every((val, index) => fhirPathEquals(val, y[index]));
2833
+ if (typeof xValue === 'object' && typeof yValue === 'object') {
2834
+ return booleanToTypedValue(deepEquals(xValue, yValue));
2693
2835
  }
2694
- if (typeof x === 'object' && typeof y === 'object') {
2695
- return deepEquals(x, y);
2696
- }
2697
- if (typeof x === 'string' && typeof y === 'string') {
2836
+ if (typeof xValue === 'string' && typeof yValue === 'string') {
2698
2837
  // String: the strings must be the same, ignoring case and locale, and normalizing whitespace
2699
2838
  // (see String Equivalence for more details).
2700
- return x.toLowerCase() === y.toLowerCase();
2839
+ return booleanToTypedValue(xValue.toLowerCase() === yValue.toLowerCase());
2840
+ }
2841
+ return booleanToTypedValue(xValue === yValue);
2842
+ }
2843
+ /**
2844
+ * Returns the sort order of two values for FHIRPath array equivalence.
2845
+ * @param x The first value.
2846
+ * @param y The second value.
2847
+ * @returns The sort order of the values.
2848
+ */
2849
+ function fhirPathEquivalentCompare(x, y) {
2850
+ const xValue = x.value;
2851
+ const yValue = y.value;
2852
+ if (typeof xValue === 'number' && typeof yValue === 'number') {
2853
+ return xValue - yValue;
2854
+ }
2855
+ if (typeof xValue === 'string' && typeof yValue === 'string') {
2856
+ return xValue.localeCompare(yValue);
2701
2857
  }
2702
- return x === y;
2858
+ return 0;
2703
2859
  }
2704
- function fhirPathIs(value, desiredType) {
2860
+ /**
2861
+ * Determines if the typed value is the desired type.
2862
+ * @param typedValue The typed value to check.
2863
+ * @param desiredType The desired type name.
2864
+ * @returns True if the typed value is of the desired type.
2865
+ */
2866
+ function fhirPathIs(typedValue, desiredType) {
2867
+ const { value } = typedValue;
2705
2868
  if (value === undefined || value === null) {
2706
2869
  return false;
2707
2870
  }
@@ -2781,6 +2944,7 @@
2781
2944
  return object !== null && typeof object === 'object';
2782
2945
  }
2783
2946
 
2947
+ var _SymbolAtom_instances, _SymbolAtom_evalValue;
2784
2948
  class FhirPathAtom {
2785
2949
  constructor(original, child) {
2786
2950
  this.original = original;
@@ -2788,15 +2952,11 @@
2788
2952
  }
2789
2953
  eval(context) {
2790
2954
  try {
2791
- const result = applyMaybeArray(context, (e) => this.child.eval(e));
2792
- if (Array.isArray(result)) {
2793
- return result.flat();
2794
- }
2795
- else if (result === undefined || result === null) {
2796
- return [];
2955
+ if (context.length > 0) {
2956
+ return context.map((e) => this.child.eval([e])).flat();
2797
2957
  }
2798
2958
  else {
2799
- return [result];
2959
+ return this.child.eval(context);
2800
2960
  }
2801
2961
  }
2802
2962
  catch (error) {
@@ -2809,34 +2969,53 @@
2809
2969
  this.value = value;
2810
2970
  }
2811
2971
  eval() {
2812
- return this.value;
2972
+ return [this.value];
2813
2973
  }
2814
2974
  }
2815
2975
  class SymbolAtom {
2816
2976
  constructor(name) {
2817
2977
  this.name = name;
2978
+ _SymbolAtom_instances.add(this);
2818
2979
  }
2819
2980
  eval(context) {
2820
2981
  if (this.name === '$this') {
2821
2982
  return context;
2822
2983
  }
2823
- return applyMaybeArray(context, (e) => {
2824
- if (e && typeof e === 'object') {
2825
- if ('resourceType' in e && e.resourceType === this.name) {
2826
- return e;
2827
- }
2828
- if (this.name in e) {
2829
- return e[this.name];
2830
- }
2831
- const propertyName = Object.keys(e).find((k) => k.startsWith(this.name));
2832
- if (propertyName) {
2833
- return e[propertyName];
2834
- }
2835
- }
2836
- return undefined;
2837
- });
2984
+ return context
2985
+ .map((e) => __classPrivateFieldGet(this, _SymbolAtom_instances, "m", _SymbolAtom_evalValue).call(this, e))
2986
+ .flat()
2987
+ .filter((e) => (e === null || e === void 0 ? void 0 : e.value) !== undefined);
2838
2988
  }
2839
2989
  }
2990
+ _SymbolAtom_instances = new WeakSet(), _SymbolAtom_evalValue = function _SymbolAtom_evalValue(typedValue) {
2991
+ const input = typedValue.value;
2992
+ if (!input || typeof input !== 'object') {
2993
+ return undefined;
2994
+ }
2995
+ if ('resourceType' in input && input.resourceType === this.name) {
2996
+ return typedValue;
2997
+ }
2998
+ let result = undefined;
2999
+ if (this.name in input) {
3000
+ result = input[this.name];
3001
+ }
3002
+ else {
3003
+ const propertyName = Object.keys(input).find((k) => k.startsWith(this.name));
3004
+ if (propertyName) {
3005
+ result = input[propertyName];
3006
+ }
3007
+ }
3008
+ if (result === undefined) {
3009
+ return undefined;
3010
+ }
3011
+ // TODO: Get the PropertyType from the choice of type
3012
+ if (Array.isArray(result)) {
3013
+ return result.map(toTypedValue);
3014
+ }
3015
+ else {
3016
+ return [toTypedValue(result)];
3017
+ }
3018
+ };
2840
3019
  class EmptySetAtom {
2841
3020
  eval() {
2842
3021
  return [];
@@ -2867,30 +3046,27 @@
2867
3046
  this.impl = impl;
2868
3047
  }
2869
3048
  eval(context) {
2870
- const leftValue = this.left.eval(context);
2871
- const rightValue = this.right.eval(context);
2872
- if (isQuantity(leftValue) && isQuantity(rightValue)) {
2873
- return Object.assign(Object.assign({}, leftValue), { value: this.impl(leftValue.value, rightValue.value) });
3049
+ const leftEvalResult = this.left.eval(context);
3050
+ if (leftEvalResult.length !== 1) {
3051
+ return [];
2874
3052
  }
2875
- else {
2876
- return this.impl(leftValue, rightValue);
3053
+ const rightEvalResult = this.right.eval(context);
3054
+ if (rightEvalResult.length !== 1) {
3055
+ return [];
2877
3056
  }
2878
- }
2879
- }
2880
- class ComparisonOperatorAtom {
2881
- constructor(left, right, impl) {
2882
- this.left = left;
2883
- this.right = right;
2884
- this.impl = impl;
2885
- }
2886
- eval(context) {
2887
- const leftValue = this.left.eval(context);
2888
- const rightValue = this.right.eval(context);
2889
- if (isQuantity(leftValue) && isQuantity(rightValue)) {
2890
- return this.impl(leftValue.value, rightValue.value);
3057
+ const leftValue = leftEvalResult[0].value;
3058
+ const rightValue = rightEvalResult[0].value;
3059
+ const leftNumber = isQuantity(leftValue) ? leftValue.value : leftValue;
3060
+ const rightNumber = isQuantity(rightValue) ? rightValue.value : rightValue;
3061
+ const result = this.impl(leftNumber, rightNumber);
3062
+ if (typeof result === 'boolean') {
3063
+ return booleanToTypedValue(result);
3064
+ }
3065
+ else if (isQuantity(leftValue)) {
3066
+ return [{ type: exports.PropertyType.Quantity, value: Object.assign(Object.assign({}, leftValue), { value: result }) }];
2891
3067
  }
2892
3068
  else {
2893
- return this.impl(leftValue, rightValue);
3069
+ return [toTypedValue(result)];
2894
3070
  }
2895
3071
  }
2896
3072
  }
@@ -2902,21 +3078,9 @@
2902
3078
  eval(context) {
2903
3079
  const leftValue = this.left.eval(context);
2904
3080
  const rightValue = this.right.eval(context);
2905
- const result = [];
2906
- function add(value) {
2907
- if (value) {
2908
- if (Array.isArray(value)) {
2909
- result.push(...value);
2910
- }
2911
- else {
2912
- result.push(value);
2913
- }
2914
- }
2915
- }
2916
- add(leftValue);
2917
- add(rightValue);
2918
- if (result.length > 0 && result.every((e) => typeof e === 'string')) {
2919
- return result.join('');
3081
+ const result = [...leftValue, ...rightValue];
3082
+ if (result.length > 0 && result.every((e) => typeof e.value === 'string')) {
3083
+ return [{ type: exports.PropertyType.string, value: result.map((e) => e.value).join('') }];
2920
3084
  }
2921
3085
  return result;
2922
3086
  }
@@ -2929,7 +3093,7 @@
2929
3093
  eval(context) {
2930
3094
  const leftValue = this.left.eval(context);
2931
3095
  const rightValue = this.right.eval(context);
2932
- return ensureArray(leftValue).includes(rightValue);
3096
+ return booleanToTypedValue(leftValue.some((e) => e.value === rightValue[0].value));
2933
3097
  }
2934
3098
  }
2935
3099
  class InAtom {
@@ -2940,7 +3104,7 @@
2940
3104
  eval(context) {
2941
3105
  const leftValue = this.left.eval(context);
2942
3106
  const rightValue = this.right.eval(context);
2943
- return ensureArray(rightValue).includes(leftValue);
3107
+ return booleanToTypedValue(rightValue.some((e) => e.value === leftValue[0].value));
2944
3108
  }
2945
3109
  }
2946
3110
  class DotAtom {
@@ -2960,20 +3124,7 @@
2960
3124
  eval(context) {
2961
3125
  const leftResult = this.left.eval(context);
2962
3126
  const rightResult = this.right.eval(context);
2963
- let resultArray;
2964
- if (leftResult !== undefined && rightResult !== undefined) {
2965
- resultArray = [leftResult, rightResult].flat();
2966
- }
2967
- else if (leftResult !== undefined) {
2968
- resultArray = ensureArray(leftResult);
2969
- }
2970
- else if (rightResult !== undefined) {
2971
- resultArray = ensureArray(rightResult);
2972
- }
2973
- else {
2974
- resultArray = [];
2975
- }
2976
- return removeDuplicates(resultArray);
3127
+ return removeDuplicates([...leftResult, ...rightResult]);
2977
3128
  }
2978
3129
  }
2979
3130
  class EqualsAtom {
@@ -2984,10 +3135,7 @@
2984
3135
  eval(context) {
2985
3136
  const leftValue = this.left.eval(context);
2986
3137
  const rightValue = this.right.eval(context);
2987
- if (Array.isArray(leftValue) && Array.isArray(rightValue)) {
2988
- return fhirPathEquals(leftValue.flat(), rightValue);
2989
- }
2990
- return applyMaybeArray(leftValue, (e) => fhirPathEquals(e, rightValue));
3138
+ return fhirPathArrayEquals(leftValue, rightValue);
2991
3139
  }
2992
3140
  }
2993
3141
  class NotEqualsAtom {
@@ -2998,14 +3146,7 @@
2998
3146
  eval(context) {
2999
3147
  const leftValue = this.left.eval(context);
3000
3148
  const rightValue = this.right.eval(context);
3001
- let result;
3002
- if (Array.isArray(rightValue)) {
3003
- result = fhirPathEquals(leftValue, rightValue);
3004
- }
3005
- else {
3006
- result = applyMaybeArray(leftValue, (e) => fhirPathEquals(e, rightValue));
3007
- }
3008
- return !toJsBoolean(result);
3149
+ return fhirPathNot(fhirPathArrayEquals(leftValue, rightValue));
3009
3150
  }
3010
3151
  }
3011
3152
  class EquivalentAtom {
@@ -3016,10 +3157,7 @@
3016
3157
  eval(context) {
3017
3158
  const leftValue = this.left.eval(context);
3018
3159
  const rightValue = this.right.eval(context);
3019
- if (Array.isArray(rightValue)) {
3020
- return fhirPathEquivalent(leftValue, rightValue);
3021
- }
3022
- return applyMaybeArray(leftValue, (e) => fhirPathEquivalent(e, rightValue));
3160
+ return fhirPathArrayEquivalent(leftValue, rightValue);
3023
3161
  }
3024
3162
  }
3025
3163
  class NotEquivalentAtom {
@@ -3030,14 +3168,7 @@
3030
3168
  eval(context) {
3031
3169
  const leftValue = this.left.eval(context);
3032
3170
  const rightValue = this.right.eval(context);
3033
- let result;
3034
- if (Array.isArray(rightValue)) {
3035
- result = fhirPathEquivalent(leftValue, rightValue);
3036
- }
3037
- else {
3038
- result = applyMaybeArray(leftValue, (e) => fhirPathEquivalent(e, rightValue));
3039
- }
3040
- return !toJsBoolean(result);
3171
+ return fhirPathNot(fhirPathArrayEquivalent(leftValue, rightValue));
3041
3172
  }
3042
3173
  }
3043
3174
  class IsAtom {
@@ -3046,8 +3177,12 @@
3046
3177
  this.right = right;
3047
3178
  }
3048
3179
  eval(context) {
3180
+ const leftValue = this.left.eval(context);
3181
+ if (leftValue.length !== 1) {
3182
+ return [];
3183
+ }
3049
3184
  const typeName = this.right.name;
3050
- return applyMaybeArray(this.left.eval(context), (e) => fhirPathIs(e, typeName));
3185
+ return booleanToTypedValue(fhirPathIs(leftValue[0], typeName));
3051
3186
  }
3052
3187
  }
3053
3188
  /**
@@ -3060,13 +3195,14 @@
3060
3195
  this.right = right;
3061
3196
  }
3062
3197
  eval(context) {
3198
+ var _a, _b, _c, _d;
3063
3199
  const leftValue = this.left.eval(context);
3064
3200
  const rightValue = this.right.eval(context);
3065
- if (leftValue === true && rightValue === true) {
3066
- return true;
3201
+ if (((_a = leftValue[0]) === null || _a === void 0 ? void 0 : _a.value) === true && ((_b = rightValue[0]) === null || _b === void 0 ? void 0 : _b.value) === true) {
3202
+ return booleanToTypedValue(true);
3067
3203
  }
3068
- if (leftValue === false || rightValue === false) {
3069
- return false;
3204
+ if (((_c = leftValue[0]) === null || _c === void 0 ? void 0 : _c.value) === false || ((_d = rightValue[0]) === null || _d === void 0 ? void 0 : _d.value) === false) {
3205
+ return booleanToTypedValue(false);
3070
3206
  }
3071
3207
  return [];
3072
3208
  }
@@ -3100,13 +3236,18 @@
3100
3236
  this.right = right;
3101
3237
  }
3102
3238
  eval(context) {
3103
- const leftValue = this.left.eval(context);
3104
- const rightValue = this.right.eval(context);
3239
+ const leftResult = this.left.eval(context);
3240
+ const rightResult = this.right.eval(context);
3241
+ if (leftResult.length === 0 && rightResult.length === 0) {
3242
+ return [];
3243
+ }
3244
+ const leftValue = leftResult.length === 0 ? null : leftResult[0].value;
3245
+ const rightValue = rightResult.length === 0 ? null : rightResult[0].value;
3105
3246
  if ((leftValue === true && rightValue !== true) || (leftValue !== true && rightValue === true)) {
3106
- return true;
3247
+ return booleanToTypedValue(true);
3107
3248
  }
3108
3249
  if ((leftValue === true && rightValue === true) || (leftValue === false && rightValue === false)) {
3109
- return false;
3250
+ return booleanToTypedValue(false);
3110
3251
  }
3111
3252
  return [];
3112
3253
  }
@@ -3118,7 +3259,7 @@
3118
3259
  this.impl = impl;
3119
3260
  }
3120
3261
  eval(context) {
3121
- return this.impl(ensureArray(context), ...this.args);
3262
+ return this.impl(context, ...this.args);
3122
3263
  }
3123
3264
  }
3124
3265
  class IndexerAtom {
@@ -3127,7 +3268,11 @@
3127
3268
  this.expr = expr;
3128
3269
  }
3129
3270
  eval(context) {
3130
- const index = this.expr.eval(context);
3271
+ const evalResult = this.expr.eval(context);
3272
+ if (evalResult.length !== 1) {
3273
+ return [];
3274
+ }
3275
+ const index = evalResult[0].value;
3131
3276
  if (typeof index !== 'number') {
3132
3277
  throw new Error(`Invalid indexer expression: should return integer}`);
3133
3278
  }
@@ -3135,7 +3280,7 @@
3135
3280
  if (!(index in leftResult)) {
3136
3281
  return [];
3137
3282
  }
3138
- return leftResult[index];
3283
+ return [leftResult[index]];
3139
3284
  }
3140
3285
  }
3141
3286
 
@@ -3161,1414 +3306,1453 @@
3161
3306
  }
3162
3307
  }
3163
3308
 
3164
- /*
3165
- * Collection of FHIRPath
3166
- * See: https://hl7.org/fhirpath/#functions
3167
- */
3168
3309
  /**
3169
3310
  * Temporary placholder for unimplemented methods.
3170
3311
  */
3171
3312
  const stub = () => [];
3172
- /*
3173
- * 5.1 Existence
3174
- * See: https://hl7.org/fhirpath/#existence
3175
- */
3176
- /**
3177
- * Returns true if the input collection is empty ({ }) and false otherwise.
3178
- *
3179
- * See: https://hl7.org/fhirpath/#empty-boolean
3180
- *
3181
- * @param input The input collection.
3182
- * @returns True if the input collection is empty ({ }) and false otherwise.
3183
- */
3184
- function empty(input) {
3185
- return [input.length === 0];
3186
- }
3187
- /**
3188
- * Returns true if the collection has unknown elements, and false otherwise.
3189
- * This is the opposite of empty(), and as such is a shorthand for empty().not().
3190
- * If the input collection is empty ({ }), the result is false.
3191
- *
3192
- * The function can also take an optional criteria to be applied to the collection
3193
- * prior to the determination of the exists. In this case, the function is shorthand
3194
- * for where(criteria).exists().
3195
- *
3196
- * See: https://hl7.org/fhirpath/#existscriteria-expression-boolean
3197
- *
3198
- * @param input
3199
- * @param criteria
3200
- * @returns True if the collection has unknown elements, and false otherwise.
3201
- */
3202
- function exists(input, criteria) {
3203
- if (criteria) {
3204
- return [input.filter((e) => toJsBoolean(criteria.eval(e))).length > 0];
3205
- }
3206
- else {
3207
- return [input.length > 0];
3208
- }
3209
- }
3210
- /**
3211
- * Returns true if for every element in the input collection, criteria evaluates to true.
3212
- * Otherwise, the result is false.
3213
- *
3214
- * If the input collection is empty ({ }), the result is true.
3215
- *
3216
- * See: https://hl7.org/fhirpath/#allcriteria-expression-boolean
3217
- *
3218
- * @param input The input collection.
3219
- * @param criteria The evaluation criteria.
3220
- * @returns True if for every element in the input collection, criteria evaluates to true.
3221
- */
3222
- function all(input, criteria) {
3223
- return [input.every((e) => toJsBoolean(criteria.eval(e)))];
3224
- }
3225
- /**
3226
- * Takes a collection of Boolean values and returns true if all the items are true.
3227
- * If unknown items are false, the result is false.
3228
- * If the input is empty ({ }), the result is true.
3229
- *
3230
- * See: https://hl7.org/fhirpath/#alltrue-boolean
3231
- *
3232
- * @param input The input collection.
3233
- * @param criteria The evaluation criteria.
3234
- * @returns True if all the items are true.
3235
- */
3236
- function allTrue(input) {
3237
- for (const value of input) {
3238
- if (!value) {
3239
- return [false];
3313
+ const functions = {
3314
+ /*
3315
+ * 5.1 Existence
3316
+ * See: https://hl7.org/fhirpath/#existence
3317
+ */
3318
+ /**
3319
+ * Returns true if the input collection is empty ({ }) and false otherwise.
3320
+ *
3321
+ * See: https://hl7.org/fhirpath/#empty-boolean
3322
+ *
3323
+ * @param input The input collection.
3324
+ * @returns True if the input collection is empty ({ }) and false otherwise.
3325
+ */
3326
+ empty: (input) => {
3327
+ return booleanToTypedValue(input.length === 0);
3328
+ },
3329
+ /**
3330
+ * Returns true if the collection has unknown elements, and false otherwise.
3331
+ * This is the opposite of empty(), and as such is a shorthand for empty().not().
3332
+ * If the input collection is empty ({ }), the result is false.
3333
+ *
3334
+ * The function can also take an optional criteria to be applied to the collection
3335
+ * prior to the determination of the exists. In this case, the function is shorthand
3336
+ * for where(criteria).exists().
3337
+ *
3338
+ * See: https://hl7.org/fhirpath/#existscriteria-expression-boolean
3339
+ *
3340
+ * @param input
3341
+ * @param criteria
3342
+ * @returns True if the collection has unknown elements, and false otherwise.
3343
+ */
3344
+ exists: (input, criteria) => {
3345
+ if (criteria) {
3346
+ return booleanToTypedValue(input.filter((e) => toJsBoolean(criteria.eval([e]))).length > 0);
3240
3347
  }
3241
- }
3242
- return [true];
3243
- }
3244
- /**
3245
- * Takes a collection of Boolean values and returns true if unknown of the items are true.
3246
- * If all the items are false, or if the input is empty ({ }), the result is false.
3247
- *
3248
- * See: https://hl7.org/fhirpath/#anytrue-boolean
3249
- *
3250
- * @param input The input collection.
3251
- * @param criteria The evaluation criteria.
3252
- * @returns True if unknown of the items are true.
3253
- */
3254
- function anyTrue(input) {
3255
- for (const value of input) {
3256
- if (value) {
3257
- return [true];
3348
+ else {
3349
+ return booleanToTypedValue(input.length > 0);
3258
3350
  }
3259
- }
3260
- return [false];
3261
- }
3262
- /**
3263
- * Takes a collection of Boolean values and returns true if all the items are false.
3264
- * If unknown items are true, the result is false.
3265
- * If the input is empty ({ }), the result is true.
3266
- *
3267
- * See: https://hl7.org/fhirpath/#allfalse-boolean
3268
- *
3269
- * @param input The input collection.
3270
- * @param criteria The evaluation criteria.
3271
- * @returns True if all the items are false.
3272
- */
3273
- function allFalse(input) {
3274
- for (const value of input) {
3275
- if (value) {
3276
- return [false];
3351
+ },
3352
+ /**
3353
+ * Returns true if for every element in the input collection, criteria evaluates to true.
3354
+ * Otherwise, the result is false.
3355
+ *
3356
+ * If the input collection is empty ({ }), the result is true.
3357
+ *
3358
+ * See: https://hl7.org/fhirpath/#allcriteria-expression-boolean
3359
+ *
3360
+ * @param input The input collection.
3361
+ * @param criteria The evaluation criteria.
3362
+ * @returns True if for every element in the input collection, criteria evaluates to true.
3363
+ */
3364
+ all: (input, criteria) => {
3365
+ return booleanToTypedValue(input.every((e) => toJsBoolean(criteria.eval([e]))));
3366
+ },
3367
+ /**
3368
+ * Takes a collection of Boolean values and returns true if all the items are true.
3369
+ * If unknown items are false, the result is false.
3370
+ * If the input is empty ({ }), the result is true.
3371
+ *
3372
+ * See: https://hl7.org/fhirpath/#alltrue-boolean
3373
+ *
3374
+ * @param input The input collection.
3375
+ * @param criteria The evaluation criteria.
3376
+ * @returns True if all the items are true.
3377
+ */
3378
+ allTrue: (input) => {
3379
+ for (const value of input) {
3380
+ if (!value.value) {
3381
+ return booleanToTypedValue(false);
3382
+ }
3277
3383
  }
3278
- }
3279
- return [true];
3280
- }
3281
- /**
3282
- * Takes a collection of Boolean values and returns true if unknown of the items are false.
3283
- * If all the items are true, or if the input is empty ({ }), the result is false.
3284
- *
3285
- * See: https://hl7.org/fhirpath/#anyfalse-boolean
3286
- *
3287
- * @param input The input collection.
3288
- * @param criteria The evaluation criteria.
3289
- * @returns True if for every element in the input collection, criteria evaluates to true.
3290
- */
3291
- function anyFalse(input) {
3292
- for (const value of input) {
3293
- if (!value) {
3294
- return [true];
3384
+ return booleanToTypedValue(true);
3385
+ },
3386
+ /**
3387
+ * Takes a collection of Boolean values and returns true if unknown of the items are true.
3388
+ * If all the items are false, or if the input is empty ({ }), the result is false.
3389
+ *
3390
+ * See: https://hl7.org/fhirpath/#anytrue-boolean
3391
+ *
3392
+ * @param input The input collection.
3393
+ * @param criteria The evaluation criteria.
3394
+ * @returns True if unknown of the items are true.
3395
+ */
3396
+ anyTrue: (input) => {
3397
+ for (const value of input) {
3398
+ if (value.value) {
3399
+ return booleanToTypedValue(true);
3400
+ }
3295
3401
  }
3296
- }
3297
- return [false];
3298
- }
3299
- /**
3300
- * Returns true if all items in the input collection are members of the collection passed
3301
- * as the other argument. Membership is determined using the = (Equals) (=) operation.
3302
- *
3303
- * Conceptually, this function is evaluated by testing each element in the input collection
3304
- * for membership in the other collection, with a default of true. This means that if the
3305
- * input collection is empty ({ }), the result is true, otherwise if the other collection
3306
- * is empty ({ }), the result is false.
3307
- *
3308
- * See: http://hl7.org/fhirpath/#subsetofother-collection-boolean
3309
- */
3310
- const subsetOf = stub;
3311
- /**
3312
- * Returns true if all items in the collection passed as the other argument are members of
3313
- * the input collection. Membership is determined using the = (Equals) (=) operation.
3314
- *
3315
- * Conceptually, this function is evaluated by testing each element in the other collection
3316
- * for membership in the input collection, with a default of true. This means that if the
3317
- * other collection is empty ({ }), the result is true, otherwise if the input collection
3318
- * is empty ({ }), the result is false.
3319
- *
3320
- * See: http://hl7.org/fhirpath/#supersetofother-collection-boolean
3321
- */
3322
- const supersetOf = stub;
3323
- /**
3324
- * Returns the integer count of the number of items in the input collection.
3325
- * Returns 0 when the input collection is empty.
3326
- *
3327
- * See: https://hl7.org/fhirpath/#count-integer
3328
- *
3329
- * @param input The input collection.
3330
- * @returns The integer count of the number of items in the input collection.
3331
- */
3332
- function count(input) {
3333
- return [input.length];
3334
- }
3335
- /**
3336
- * Returns a collection containing only the unique items in the input collection.
3337
- * To determine whether two items are the same, the = (Equals) (=) operator is used,
3338
- * as defined below.
3339
- *
3340
- * If the input collection is empty ({ }), the result is empty.
3341
- *
3342
- * Note that the order of elements in the input collection is not guaranteed to be
3343
- * preserved in the result.
3344
- *
3345
- * See: https://hl7.org/fhirpath/#distinct-collection
3346
- *
3347
- * @param input The input collection.
3348
- * @returns The integer count of the number of items in the input collection.
3349
- */
3350
- function distinct(input) {
3351
- return Array.from(new Set(input));
3352
- }
3353
- /**
3354
- * Returns true if all the items in the input collection are distinct.
3355
- * To determine whether two items are distinct, the = (Equals) (=) operator is used,
3356
- * as defined below.
3357
- *
3358
- * See: https://hl7.org/fhirpath/#isdistinct-boolean
3359
- *
3360
- * @param input The input collection.
3361
- * @returns The integer count of the number of items in the input collection.
3362
- */
3363
- function isDistinct(input) {
3364
- return [input.length === new Set(input).size];
3365
- }
3366
- /*
3367
- * 5.2 Filtering and projection
3368
- */
3369
- /**
3370
- * Returns a collection containing only those elements in the input collection
3371
- * for which the stated criteria expression evaluates to true.
3372
- * Elements for which the expression evaluates to false or empty ({ }) are not
3373
- * included in the result.
3374
- *
3375
- * If the input collection is empty ({ }), the result is empty.
3376
- *
3377
- * If the result of evaluating the condition is other than a single boolean value,
3378
- * the evaluation will end and signal an error to the calling environment,
3379
- * consistent with singleton evaluation of collections behavior.
3380
- *
3381
- * See: https://hl7.org/fhirpath/#wherecriteria-expression-collection
3382
- *
3383
- * @param input The input collection.
3384
- * @param condition The condition atom.
3385
- * @returns A collection containing only those elements in the input collection for which the stated criteria expression evaluates to true.
3386
- */
3387
- function where(input, criteria) {
3388
- return input.filter((e) => toJsBoolean(criteria.eval(e)));
3389
- }
3390
- /**
3391
- * Evaluates the projection expression for each item in the input collection.
3392
- * The result of each evaluation is added to the output collection. If the
3393
- * evaluation results in a collection with multiple items, all items are added
3394
- * to the output collection (collections resulting from evaluation of projection
3395
- * are flattened). This means that if the evaluation for an element results in
3396
- * the empty collection ({ }), no element is added to the result, and that if
3397
- * the input collection is empty ({ }), the result is empty as well.
3398
- *
3399
- * See: http://hl7.org/fhirpath/#selectprojection-expression-collection
3400
- */
3401
- function select(input, criteria) {
3402
- return ensureArray(input.map((e) => criteria.eval(e)).flat());
3403
- }
3404
- /**
3405
- * A version of select that will repeat the projection and add it to the output
3406
- * collection, as long as the projection yields new items (as determined by
3407
- * the = (Equals) (=) operator).
3408
- *
3409
- * See: http://hl7.org/fhirpath/#repeatprojection-expression-collection
3410
- */
3411
- const repeat = stub;
3412
- /**
3413
- * Returns a collection that contains all items in the input collection that
3414
- * are of the given type or a subclass thereof. If the input collection is
3415
- * empty ({ }), the result is empty. The type argument is an identifier that
3416
- * must resolve to the name of a type in a model
3417
- *
3418
- * See: http://hl7.org/fhirpath/#oftypetype-type-specifier-collection
3419
- */
3420
- const ofType = stub;
3421
- /*
3422
- * 5.3 Subsetting
3423
- */
3424
- /**
3425
- * Will return the single item in the input if there is just one item.
3426
- * If the input collection is empty ({ }), the result is empty.
3427
- * If there are multiple items, an error is signaled to the evaluation environment.
3428
- * This function is useful for ensuring that an error is returned if an assumption
3429
- * about cardinality is violated at run-time.
3430
- *
3431
- * See: https://hl7.org/fhirpath/#single-collection
3432
- *
3433
- * @param input The input collection.
3434
- * @returns The single item in the input if there is just one item.
3435
- */
3436
- function single(input) {
3437
- if (input.length > 1) {
3438
- throw new Error('Expected input length one for single()');
3439
- }
3440
- return input.length === 0 ? [] : input.slice(0, 1);
3441
- }
3442
- /**
3443
- * Returns a collection containing only the first item in the input collection.
3444
- * This function is equivalent to item[0], so it will return an empty collection if the input collection has no items.
3445
- *
3446
- * See: https://hl7.org/fhirpath/#first-collection
3447
- *
3448
- * @param input The input collection.
3449
- * @returns A collection containing only the first item in the input collection.
3450
- */
3451
- function first(input) {
3452
- return input.length === 0 ? [] : [input[0]];
3453
- }
3454
- /**
3455
- * Returns a collection containing only the last item in the input collection.
3456
- * Will return an empty collection if the input collection has no items.
3457
- *
3458
- * See: https://hl7.org/fhirpath/#last-collection
3459
- *
3460
- * @param input The input collection.
3461
- * @returns A collection containing only the last item in the input collection.
3462
- */
3463
- function last(input) {
3464
- return input.length === 0 ? [] : [input[input.length - 1]];
3465
- }
3466
- /**
3467
- * Returns a collection containing all but the first item in the input collection.
3468
- * Will return an empty collection if the input collection has no items, or only one item.
3469
- *
3470
- * See: https://hl7.org/fhirpath/#tail-collection
3471
- *
3472
- * @param input The input collection.
3473
- * @returns A collection containing all but the first item in the input collection.
3474
- */
3475
- function tail(input) {
3476
- return input.length === 0 ? [] : input.slice(1, input.length);
3477
- }
3478
- /**
3479
- * Returns a collection containing all but the first num items in the input collection.
3480
- * Will return an empty collection if there are no items remaining after the
3481
- * indicated number of items have been skipped, or if the input collection is empty.
3482
- * If num is less than or equal to zero, the input collection is simply returned.
3483
- *
3484
- * See: https://hl7.org/fhirpath/#skipnum-integer-collection
3485
- *
3486
- * @param input The input collection.
3487
- * @returns A collection containing all but the first item in the input collection.
3488
- */
3489
- function skip(input, num) {
3490
- const numValue = num.eval(0);
3491
- if (typeof numValue !== 'number') {
3492
- throw new Error('Expected a number for skip(num)');
3493
- }
3494
- if (numValue >= input.length) {
3495
- return [];
3496
- }
3497
- if (numValue <= 0) {
3498
- return input;
3499
- }
3500
- return input.slice(numValue, input.length);
3501
- }
3502
- /**
3503
- * Returns a collection containing the first num items in the input collection,
3504
- * or less if there are less than num items.
3505
- * If num is less than or equal to 0, or if the input collection is empty ({ }),
3506
- * take returns an empty collection.
3507
- *
3508
- * See: https://hl7.org/fhirpath/#takenum-integer-collection
3509
- *
3510
- * @param input The input collection.
3511
- * @returns A collection containing the first num items in the input collection.
3512
- */
3513
- function take(input, num) {
3514
- const numValue = num.eval(0);
3515
- if (typeof numValue !== 'number') {
3516
- throw new Error('Expected a number for take(num)');
3517
- }
3518
- if (numValue >= input.length) {
3519
- return input;
3520
- }
3521
- if (numValue <= 0) {
3522
- return [];
3523
- }
3524
- return input.slice(0, numValue);
3525
- }
3526
- /**
3527
- * Returns the set of elements that are in both collections.
3528
- * Duplicate items will be eliminated by this function.
3529
- * Order of items is not guaranteed to be preserved in the result of this function.
3530
- *
3531
- * See: http://hl7.org/fhirpath/#intersectother-collection-collection
3532
- */
3533
- function intersect(input, other) {
3534
- if (!other) {
3535
- return input;
3536
- }
3537
- const otherArray = ensureArray(other.eval(0));
3538
- return removeDuplicates(input.filter((e) => otherArray.includes(e)));
3539
- }
3540
- /**
3541
- * Returns the set of elements that are not in the other collection.
3542
- * Duplicate items will not be eliminated by this function, and order will be preserved.
3543
- *
3544
- * e.g. (1 | 2 | 3).exclude(2) returns (1 | 3).
3545
- *
3546
- * See: http://hl7.org/fhirpath/#excludeother-collection-collection
3547
- */
3548
- function exclude(input, other) {
3549
- if (!other) {
3550
- return input;
3551
- }
3552
- const otherArray = ensureArray(other.eval(0));
3553
- return input.filter((e) => !otherArray.includes(e));
3554
- }
3555
- /*
3556
- * 5.4. Combining
3557
- *
3558
- * See: https://hl7.org/fhirpath/#combining
3559
- */
3560
- /**
3561
- * Merge the two collections into a single collection,
3562
- * eliminating unknown duplicate values (using = (Equals) (=) to determine equality).
3563
- * There is no expectation of order in the resulting collection.
3564
- *
3565
- * In other words, this function returns the distinct list of elements from both inputs.
3566
- *
3567
- * See: http://hl7.org/fhirpath/#unionother-collection
3568
- */
3569
- function union(input, other) {
3570
- if (!other) {
3571
- return input;
3572
- }
3573
- return removeDuplicates([input, other.eval(0)].flat());
3574
- }
3575
- /**
3576
- * Merge the input and other collections into a single collection
3577
- * without eliminating duplicate values. Combining an empty collection
3578
- * with a non-empty collection will return the non-empty collection.
3579
- *
3580
- * There is no expectation of order in the resulting collection.
3581
- *
3582
- * See: http://hl7.org/fhirpath/#combineother-collection-collection
3583
- */
3584
- function combine(input, other) {
3585
- if (!other) {
3586
- return input;
3587
- }
3588
- return [input, other.eval(0)].flat();
3589
- }
3590
- /*
3591
- * 5.5. Conversion
3592
- *
3593
- * See: https://hl7.org/fhirpath/#conversion
3594
- */
3595
- /**
3596
- * The iif function in FHIRPath is an immediate if,
3597
- * also known as a conditional operator (such as C’s ? : operator).
3598
- *
3599
- * The criterion expression is expected to evaluate to a Boolean.
3600
- *
3601
- * If criterion is true, the function returns the value of the true-result argument.
3602
- *
3603
- * If criterion is false or an empty collection, the function returns otherwise-result,
3604
- * unless the optional otherwise-result is not given, in which case the function returns an empty collection.
3605
- *
3606
- * Note that short-circuit behavior is expected in this function. In other words,
3607
- * true-result should only be evaluated if the criterion evaluates to true,
3608
- * and otherwise-result should only be evaluated otherwise. For implementations,
3609
- * this means delaying evaluation of the arguments.
3610
- *
3611
- * @param input
3612
- * @param criterion
3613
- * @param trueResult
3614
- * @param otherwiseResult
3615
- * @returns
3616
- */
3617
- function iif(input, criterion, trueResult, otherwiseResult) {
3618
- const evalResult = ensureArray(criterion.eval(input));
3619
- if (evalResult.length > 1 || (evalResult.length === 1 && typeof evalResult[0] !== 'boolean')) {
3620
- throw new Error('Expected criterion to evaluate to a Boolean');
3621
- }
3622
- if (toJsBoolean(evalResult)) {
3623
- return ensureArray(trueResult.eval(input));
3624
- }
3625
- if (otherwiseResult) {
3626
- return ensureArray(otherwiseResult.eval(input));
3627
- }
3628
- return [];
3629
- }
3630
- /**
3631
- * Converts an input collection to a boolean.
3632
- *
3633
- * If the input collection contains a single item, this function will return a single boolean if:
3634
- * 1) the item is a Boolean
3635
- * 2) the item is an Integer and is equal to one of the possible integer representations of Boolean values
3636
- * 3) the item is a Decimal that is equal to one of the possible decimal representations of Boolean values
3637
- * 4) the item is a String that is equal to one of the possible string representations of Boolean values
3638
- *
3639
- * If the item is not one the above types, or the item is a String, Integer, or Decimal, but is not equal to one of the possible values convertible to a Boolean, the result is empty.
3640
- *
3641
- * See: https://hl7.org/fhirpath/#toboolean-boolean
3642
- *
3643
- * @param input
3644
- * @returns
3645
- */
3646
- function toBoolean(input) {
3647
- if (input.length === 0) {
3648
- return [];
3649
- }
3650
- const [value] = validateInput(input, 1);
3651
- if (typeof value === 'boolean') {
3652
- return [value];
3653
- }
3654
- if (typeof value === 'number') {
3655
- if (value === 0 || value === 1) {
3656
- return [!!value];
3402
+ return booleanToTypedValue(false);
3403
+ },
3404
+ /**
3405
+ * Takes a collection of Boolean values and returns true if all the items are false.
3406
+ * If unknown items are true, the result is false.
3407
+ * If the input is empty ({ }), the result is true.
3408
+ *
3409
+ * See: https://hl7.org/fhirpath/#allfalse-boolean
3410
+ *
3411
+ * @param input The input collection.
3412
+ * @param criteria The evaluation criteria.
3413
+ * @returns True if all the items are false.
3414
+ */
3415
+ allFalse: (input) => {
3416
+ for (const value of input) {
3417
+ if (value.value) {
3418
+ return booleanToTypedValue(false);
3419
+ }
3657
3420
  }
3658
- }
3659
- if (typeof value === 'string') {
3660
- const lowerStr = value.toLowerCase();
3661
- if (['true', 't', 'yes', 'y', '1', '1.0'].includes(lowerStr)) {
3662
- return [true];
3421
+ return booleanToTypedValue(true);
3422
+ },
3423
+ /**
3424
+ * Takes a collection of Boolean values and returns true if unknown of the items are false.
3425
+ * If all the items are true, or if the input is empty ({ }), the result is false.
3426
+ *
3427
+ * See: https://hl7.org/fhirpath/#anyfalse-boolean
3428
+ *
3429
+ * @param input The input collection.
3430
+ * @param criteria The evaluation criteria.
3431
+ * @returns True if for every element in the input collection, criteria evaluates to true.
3432
+ */
3433
+ anyFalse: (input) => {
3434
+ for (const value of input) {
3435
+ if (!value.value) {
3436
+ return booleanToTypedValue(true);
3437
+ }
3663
3438
  }
3664
- if (['false', 'f', 'no', 'n', '0', '0.0'].includes(lowerStr)) {
3665
- return [false];
3439
+ return booleanToTypedValue(false);
3440
+ },
3441
+ /**
3442
+ * Returns true if all items in the input collection are members of the collection passed
3443
+ * as the other argument. Membership is determined using the = (Equals) (=) operation.
3444
+ *
3445
+ * Conceptually, this function is evaluated by testing each element in the input collection
3446
+ * for membership in the other collection, with a default of true. This means that if the
3447
+ * input collection is empty ({ }), the result is true, otherwise if the other collection
3448
+ * is empty ({ }), the result is false.
3449
+ *
3450
+ * See: http://hl7.org/fhirpath/#subsetofother-collection-boolean
3451
+ */
3452
+ subsetOf: stub,
3453
+ /**
3454
+ * Returns true if all items in the collection passed as the other argument are members of
3455
+ * the input collection. Membership is determined using the = (Equals) (=) operation.
3456
+ *
3457
+ * Conceptually, this function is evaluated by testing each element in the other collection
3458
+ * for membership in the input collection, with a default of true. This means that if the
3459
+ * other collection is empty ({ }), the result is true, otherwise if the input collection
3460
+ * is empty ({ }), the result is false.
3461
+ *
3462
+ * See: http://hl7.org/fhirpath/#supersetofother-collection-boolean
3463
+ */
3464
+ supersetOf: stub,
3465
+ /**
3466
+ * Returns the integer count of the number of items in the input collection.
3467
+ * Returns 0 when the input collection is empty.
3468
+ *
3469
+ * See: https://hl7.org/fhirpath/#count-integer
3470
+ *
3471
+ * @param input The input collection.
3472
+ * @returns The integer count of the number of items in the input collection.
3473
+ */
3474
+ count: (input) => {
3475
+ return [{ type: exports.PropertyType.integer, value: input.length }];
3476
+ },
3477
+ /**
3478
+ * Returns a collection containing only the unique items in the input collection.
3479
+ * To determine whether two items are the same, the = (Equals) (=) operator is used,
3480
+ * as defined below.
3481
+ *
3482
+ * If the input collection is empty ({ }), the result is empty.
3483
+ *
3484
+ * Note that the order of elements in the input collection is not guaranteed to be
3485
+ * preserved in the result.
3486
+ *
3487
+ * See: https://hl7.org/fhirpath/#distinct-collection
3488
+ *
3489
+ * @param input The input collection.
3490
+ * @returns The integer count of the number of items in the input collection.
3491
+ */
3492
+ distinct: (input) => {
3493
+ const result = [];
3494
+ for (const value of input) {
3495
+ if (!result.some((e) => e.value === value.value)) {
3496
+ result.push(value);
3497
+ }
3498
+ }
3499
+ return result;
3500
+ },
3501
+ /**
3502
+ * Returns true if all the items in the input collection are distinct.
3503
+ * To determine whether two items are distinct, the = (Equals) (=) operator is used,
3504
+ * as defined below.
3505
+ *
3506
+ * See: https://hl7.org/fhirpath/#isdistinct-boolean
3507
+ *
3508
+ * @param input The input collection.
3509
+ * @returns The integer count of the number of items in the input collection.
3510
+ */
3511
+ isDistinct: (input) => {
3512
+ return booleanToTypedValue(input.length === functions.distinct(input).length);
3513
+ },
3514
+ /*
3515
+ * 5.2 Filtering and projection
3516
+ */
3517
+ /**
3518
+ * Returns a collection containing only those elements in the input collection
3519
+ * for which the stated criteria expression evaluates to true.
3520
+ * Elements for which the expression evaluates to false or empty ({ }) are not
3521
+ * included in the result.
3522
+ *
3523
+ * If the input collection is empty ({ }), the result is empty.
3524
+ *
3525
+ * If the result of evaluating the condition is other than a single boolean value,
3526
+ * the evaluation will end and signal an error to the calling environment,
3527
+ * consistent with singleton evaluation of collections behavior.
3528
+ *
3529
+ * See: https://hl7.org/fhirpath/#wherecriteria-expression-collection
3530
+ *
3531
+ * @param input The input collection.
3532
+ * @param condition The condition atom.
3533
+ * @returns A collection containing only those elements in the input collection for which the stated criteria expression evaluates to true.
3534
+ */
3535
+ where: (input, criteria) => {
3536
+ return input.filter((e) => toJsBoolean(criteria.eval([e])));
3537
+ },
3538
+ /**
3539
+ * Evaluates the projection expression for each item in the input collection.
3540
+ * The result of each evaluation is added to the output collection. If the
3541
+ * evaluation results in a collection with multiple items, all items are added
3542
+ * to the output collection (collections resulting from evaluation of projection
3543
+ * are flattened). This means that if the evaluation for an element results in
3544
+ * the empty collection ({ }), no element is added to the result, and that if
3545
+ * the input collection is empty ({ }), the result is empty as well.
3546
+ *
3547
+ * See: http://hl7.org/fhirpath/#selectprojection-expression-collection
3548
+ */
3549
+ select: (input, criteria) => {
3550
+ return input.map((e) => criteria.eval([e])).flat();
3551
+ },
3552
+ /**
3553
+ * A version of select that will repeat the projection and add it to the output
3554
+ * collection, as long as the projection yields new items (as determined by
3555
+ * the = (Equals) (=) operator).
3556
+ *
3557
+ * See: http://hl7.org/fhirpath/#repeatprojection-expression-collection
3558
+ */
3559
+ repeat: stub,
3560
+ /**
3561
+ * Returns a collection that contains all items in the input collection that
3562
+ * are of the given type or a subclass thereof. If the input collection is
3563
+ * empty ({ }), the result is empty. The type argument is an identifier that
3564
+ * must resolve to the name of a type in a model
3565
+ *
3566
+ * See: http://hl7.org/fhirpath/#oftypetype-type-specifier-collection
3567
+ */
3568
+ ofType: stub,
3569
+ /*
3570
+ * 5.3 Subsetting
3571
+ */
3572
+ /**
3573
+ * Will return the single item in the input if there is just one item.
3574
+ * If the input collection is empty ({ }), the result is empty.
3575
+ * If there are multiple items, an error is signaled to the evaluation environment.
3576
+ * This function is useful for ensuring that an error is returned if an assumption
3577
+ * about cardinality is violated at run-time.
3578
+ *
3579
+ * See: https://hl7.org/fhirpath/#single-collection
3580
+ *
3581
+ * @param input The input collection.
3582
+ * @returns The single item in the input if there is just one item.
3583
+ */
3584
+ single: (input) => {
3585
+ if (input.length > 1) {
3586
+ throw new Error('Expected input length one for single()');
3587
+ }
3588
+ return input.length === 0 ? [] : input.slice(0, 1);
3589
+ },
3590
+ /**
3591
+ * Returns a collection containing only the first item in the input collection.
3592
+ * This function is equivalent to item[0], so it will return an empty collection if the input collection has no items.
3593
+ *
3594
+ * See: https://hl7.org/fhirpath/#first-collection
3595
+ *
3596
+ * @param input The input collection.
3597
+ * @returns A collection containing only the first item in the input collection.
3598
+ */
3599
+ first: (input) => {
3600
+ return input.length === 0 ? [] : input.slice(0, 1);
3601
+ },
3602
+ /**
3603
+ * Returns a collection containing only the last item in the input collection.
3604
+ * Will return an empty collection if the input collection has no items.
3605
+ *
3606
+ * See: https://hl7.org/fhirpath/#last-collection
3607
+ *
3608
+ * @param input The input collection.
3609
+ * @returns A collection containing only the last item in the input collection.
3610
+ */
3611
+ last: (input) => {
3612
+ return input.length === 0 ? [] : input.slice(input.length - 1, input.length);
3613
+ },
3614
+ /**
3615
+ * Returns a collection containing all but the first item in the input collection.
3616
+ * Will return an empty collection if the input collection has no items, or only one item.
3617
+ *
3618
+ * See: https://hl7.org/fhirpath/#tail-collection
3619
+ *
3620
+ * @param input The input collection.
3621
+ * @returns A collection containing all but the first item in the input collection.
3622
+ */
3623
+ tail: (input) => {
3624
+ return input.length === 0 ? [] : input.slice(1, input.length);
3625
+ },
3626
+ /**
3627
+ * Returns a collection containing all but the first num items in the input collection.
3628
+ * Will return an empty collection if there are no items remaining after the
3629
+ * indicated number of items have been skipped, or if the input collection is empty.
3630
+ * If num is less than or equal to zero, the input collection is simply returned.
3631
+ *
3632
+ * See: https://hl7.org/fhirpath/#skipnum-integer-collection
3633
+ *
3634
+ * @param input The input collection.
3635
+ * @returns A collection containing all but the first item in the input collection.
3636
+ */
3637
+ skip: (input, num) => {
3638
+ var _a;
3639
+ const numValue = (_a = num.eval([])[0]) === null || _a === void 0 ? void 0 : _a.value;
3640
+ if (typeof numValue !== 'number') {
3641
+ throw new Error('Expected a number for skip(num)');
3642
+ }
3643
+ if (numValue >= input.length) {
3644
+ return [];
3645
+ }
3646
+ if (numValue <= 0) {
3647
+ return input;
3648
+ }
3649
+ return input.slice(numValue, input.length);
3650
+ },
3651
+ /**
3652
+ * Returns a collection containing the first num items in the input collection,
3653
+ * or less if there are less than num items.
3654
+ * If num is less than or equal to 0, or if the input collection is empty ({ }),
3655
+ * take returns an empty collection.
3656
+ *
3657
+ * See: https://hl7.org/fhirpath/#takenum-integer-collection
3658
+ *
3659
+ * @param input The input collection.
3660
+ * @returns A collection containing the first num items in the input collection.
3661
+ */
3662
+ take: (input, num) => {
3663
+ var _a;
3664
+ const numValue = (_a = num.eval([])[0]) === null || _a === void 0 ? void 0 : _a.value;
3665
+ if (typeof numValue !== 'number') {
3666
+ throw new Error('Expected a number for take(num)');
3667
+ }
3668
+ if (numValue >= input.length) {
3669
+ return input;
3670
+ }
3671
+ if (numValue <= 0) {
3672
+ return [];
3673
+ }
3674
+ return input.slice(0, numValue);
3675
+ },
3676
+ /**
3677
+ * Returns the set of elements that are in both collections.
3678
+ * Duplicate items will be eliminated by this function.
3679
+ * Order of items is not guaranteed to be preserved in the result of this function.
3680
+ *
3681
+ * See: http://hl7.org/fhirpath/#intersectother-collection-collection
3682
+ */
3683
+ intersect: (input, other) => {
3684
+ if (!other) {
3685
+ return input;
3686
+ }
3687
+ const otherArray = other.eval([]);
3688
+ const result = [];
3689
+ for (const value of input) {
3690
+ if (!result.some((e) => e.value === value.value) && otherArray.some((e) => e.value === value.value)) {
3691
+ result.push(value);
3692
+ }
3693
+ }
3694
+ return result;
3695
+ },
3696
+ /**
3697
+ * Returns the set of elements that are not in the other collection.
3698
+ * Duplicate items will not be eliminated by this function, and order will be preserved.
3699
+ *
3700
+ * e.g. (1 | 2 | 3).exclude(2) returns (1 | 3).
3701
+ *
3702
+ * See: http://hl7.org/fhirpath/#excludeother-collection-collection
3703
+ */
3704
+ exclude: (input, other) => {
3705
+ if (!other) {
3706
+ return input;
3707
+ }
3708
+ const otherArray = other.eval([]);
3709
+ const result = [];
3710
+ for (const value of input) {
3711
+ if (!otherArray.some((e) => e.value === value.value)) {
3712
+ result.push(value);
3713
+ }
3714
+ }
3715
+ return result;
3716
+ },
3717
+ /*
3718
+ * 5.4. Combining
3719
+ *
3720
+ * See: https://hl7.org/fhirpath/#combining
3721
+ */
3722
+ /**
3723
+ * Merge the two collections into a single collection,
3724
+ * eliminating unknown duplicate values (using = (Equals) (=) to determine equality).
3725
+ * There is no expectation of order in the resulting collection.
3726
+ *
3727
+ * In other words, this function returns the distinct list of elements from both inputs.
3728
+ *
3729
+ * See: http://hl7.org/fhirpath/#unionother-collection
3730
+ */
3731
+ union: (input, other) => {
3732
+ if (!other) {
3733
+ return input;
3734
+ }
3735
+ const otherArray = other.eval([]);
3736
+ return removeDuplicates([...input, ...otherArray]);
3737
+ },
3738
+ /**
3739
+ * Merge the input and other collections into a single collection
3740
+ * without eliminating duplicate values. Combining an empty collection
3741
+ * with a non-empty collection will return the non-empty collection.
3742
+ *
3743
+ * There is no expectation of order in the resulting collection.
3744
+ *
3745
+ * See: http://hl7.org/fhirpath/#combineother-collection-collection
3746
+ */
3747
+ combine: (input, other) => {
3748
+ if (!other) {
3749
+ return input;
3750
+ }
3751
+ const otherArray = other.eval([]);
3752
+ return [...input, ...otherArray];
3753
+ },
3754
+ /*
3755
+ * 5.5. Conversion
3756
+ *
3757
+ * See: https://hl7.org/fhirpath/#conversion
3758
+ */
3759
+ /**
3760
+ * The iif function in FHIRPath is an immediate if,
3761
+ * also known as a conditional operator (such as C’s ? : operator).
3762
+ *
3763
+ * The criterion expression is expected to evaluate to a Boolean.
3764
+ *
3765
+ * If criterion is true, the function returns the value of the true-result argument.
3766
+ *
3767
+ * If criterion is false or an empty collection, the function returns otherwise-result,
3768
+ * unless the optional otherwise-result is not given, in which case the function returns an empty collection.
3769
+ *
3770
+ * Note that short-circuit behavior is expected in this function. In other words,
3771
+ * true-result should only be evaluated if the criterion evaluates to true,
3772
+ * and otherwise-result should only be evaluated otherwise. For implementations,
3773
+ * this means delaying evaluation of the arguments.
3774
+ *
3775
+ * @param input
3776
+ * @param criterion
3777
+ * @param trueResult
3778
+ * @param otherwiseResult
3779
+ * @returns
3780
+ */
3781
+ iif: (input, criterion, trueResult, otherwiseResult) => {
3782
+ const evalResult = criterion.eval(input);
3783
+ if (evalResult.length > 1 || (evalResult.length === 1 && typeof evalResult[0].value !== 'boolean')) {
3784
+ throw new Error('Expected criterion to evaluate to a Boolean');
3785
+ }
3786
+ if (toJsBoolean(evalResult)) {
3787
+ return trueResult.eval(input);
3788
+ }
3789
+ if (otherwiseResult) {
3790
+ return otherwiseResult.eval(input);
3666
3791
  }
3667
- }
3668
- return [];
3669
- }
3670
- /**
3671
- * If the input collection contains a single item, this function will return true if:
3672
- * 1) the item is a Boolean
3673
- * 2) the item is an Integer that is equal to one of the possible integer representations of Boolean values
3674
- * 3) the item is a Decimal that is equal to one of the possible decimal representations of Boolean values
3675
- * 4) the item is a String that is equal to one of the possible string representations of Boolean values
3676
- *
3677
- * If the item is not one of the above types, or the item is a String, Integer, or Decimal, but is not equal to one of the possible values convertible to a Boolean, the result is false.
3678
- *
3679
- * Possible values for Integer, Decimal, and String are described in the toBoolean() function.
3680
- *
3681
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3682
- *
3683
- * If the input collection is empty, the result is empty.
3684
- *
3685
- * See: http://hl7.org/fhirpath/#convertstoboolean-boolean
3686
- *
3687
- * @param input
3688
- * @returns
3689
- */
3690
- function convertsToBoolean(input) {
3691
- if (input.length === 0) {
3692
- return [];
3693
- }
3694
- return [toBoolean(input).length === 1];
3695
- }
3696
- /**
3697
- * Returns the integer representation of the input.
3698
- *
3699
- * If the input collection contains a single item, this function will return a single integer if:
3700
- * 1) the item is an Integer
3701
- * 2) the item is a String and is convertible to an integer
3702
- * 3) the item is a Boolean, where true results in a 1 and false results in a 0.
3703
- *
3704
- * If the item is not one the above types, the result is empty.
3705
- *
3706
- * If the item is a String, but the string is not convertible to an integer (using the regex format (\\+|-)?\d+), the result is empty.
3707
- *
3708
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3709
- *
3710
- * If the input collection is empty, the result is empty.
3711
- *
3712
- * See: https://hl7.org/fhirpath/#tointeger-integer
3713
- *
3714
- * @param input The input collection.
3715
- * @returns The string representation of the input.
3716
- */
3717
- function toInteger(input) {
3718
- if (input.length === 0) {
3719
- return [];
3720
- }
3721
- const [value] = validateInput(input, 1);
3722
- if (typeof value === 'number') {
3723
- return [value];
3724
- }
3725
- if (typeof value === 'string' && value.match(/^[+-]?\d+$/)) {
3726
- return [parseInt(value, 10)];
3727
- }
3728
- if (typeof value === 'boolean') {
3729
- return [value ? 1 : 0];
3730
- }
3731
- return [];
3732
- }
3733
- /**
3734
- * Returns true if the input can be converted to string.
3735
- *
3736
- * If the input collection contains a single item, this function will return true if:
3737
- * 1) the item is an Integer
3738
- * 2) the item is a String and is convertible to an Integer
3739
- * 3) the item is a Boolean
3740
- * 4) If the item is not one of the above types, or the item is a String, but is not convertible to an Integer (using the regex format (\\+|-)?\d+), the result is false.
3741
- *
3742
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3743
- *
3744
- * If the input collection is empty, the result is empty.
3745
- *
3746
- * See: https://hl7.org/fhirpath/#convertstointeger-boolean
3747
- *
3748
- * @param input The input collection.
3749
- * @returns
3750
- */
3751
- function convertsToInteger(input) {
3752
- if (input.length === 0) {
3753
3792
  return [];
3754
- }
3755
- return [toInteger(input).length === 1];
3756
- }
3757
- /**
3758
- * If the input collection contains a single item, this function will return a single date if:
3759
- * 1) the item is a Date
3760
- * 2) the item is a DateTime
3761
- * 3) the item is a String and is convertible to a Date
3762
- *
3763
- * If the item is not one of the above types, the result is empty.
3764
- *
3765
- * If the item is a String, but the string is not convertible to a Date (using the format YYYY-MM-DD), the result is empty.
3766
- *
3767
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3768
- *
3769
- * If the input collection is empty, the result is empty.
3770
- *
3771
- * See: https://hl7.org/fhirpath/#todate-date
3772
- */
3773
- function toDate(input) {
3774
- if (input.length === 0) {
3793
+ },
3794
+ /**
3795
+ * Converts an input collection to a boolean.
3796
+ *
3797
+ * If the input collection contains a single item, this function will return a single boolean if:
3798
+ * 1) the item is a Boolean
3799
+ * 2) the item is an Integer and is equal to one of the possible integer representations of Boolean values
3800
+ * 3) the item is a Decimal that is equal to one of the possible decimal representations of Boolean values
3801
+ * 4) the item is a String that is equal to one of the possible string representations of Boolean values
3802
+ *
3803
+ * If the item is not one the above types, or the item is a String, Integer, or Decimal, but is not equal to one of the possible values convertible to a Boolean, the result is empty.
3804
+ *
3805
+ * See: https://hl7.org/fhirpath/#toboolean-boolean
3806
+ *
3807
+ * @param input
3808
+ * @returns
3809
+ */
3810
+ toBoolean: (input) => {
3811
+ if (input.length === 0) {
3812
+ return [];
3813
+ }
3814
+ const [{ value }] = validateInput(input, 1);
3815
+ if (typeof value === 'boolean') {
3816
+ return [{ type: exports.PropertyType.boolean, value }];
3817
+ }
3818
+ if (typeof value === 'number') {
3819
+ if (value === 0 || value === 1) {
3820
+ return booleanToTypedValue(!!value);
3821
+ }
3822
+ }
3823
+ if (typeof value === 'string') {
3824
+ const lowerStr = value.toLowerCase();
3825
+ if (['true', 't', 'yes', 'y', '1', '1.0'].includes(lowerStr)) {
3826
+ return booleanToTypedValue(true);
3827
+ }
3828
+ if (['false', 'f', 'no', 'n', '0', '0.0'].includes(lowerStr)) {
3829
+ return booleanToTypedValue(false);
3830
+ }
3831
+ }
3775
3832
  return [];
3776
- }
3777
- const [value] = validateInput(input, 1);
3778
- if (typeof value === 'string' && value.match(/^\d{4}(-\d{2}(-\d{2})?)?/)) {
3779
- return [parseDateString(value)];
3780
- }
3781
- return [];
3782
- }
3783
- /**
3784
- * If the input collection contains a single item, this function will return true if:
3785
- * 1) the item is a Date
3786
- * 2) the item is a DateTime
3787
- * 3) the item is a String and is convertible to a Date
3788
- *
3789
- * If the item is not one of the above types, or is not convertible to a Date (using the format YYYY-MM-DD), the result is false.
3790
- *
3791
- * If the item contains a partial date (e.g. '2012-01'), the result is a partial date.
3792
- *
3793
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3794
- *
3795
- * If the input collection is empty, the result is empty.
3796
- *
3797
- * See: https://hl7.org/fhirpath/#convertstodate-boolean
3798
- */
3799
- function convertsToDate(input) {
3800
- if (input.length === 0) {
3833
+ },
3834
+ /**
3835
+ * If the input collection contains a single item, this function will return true if:
3836
+ * 1) the item is a Boolean
3837
+ * 2) the item is an Integer that is equal to one of the possible integer representations of Boolean values
3838
+ * 3) the item is a Decimal that is equal to one of the possible decimal representations of Boolean values
3839
+ * 4) the item is a String that is equal to one of the possible string representations of Boolean values
3840
+ *
3841
+ * If the item is not one of the above types, or the item is a String, Integer, or Decimal, but is not equal to one of the possible values convertible to a Boolean, the result is false.
3842
+ *
3843
+ * Possible values for Integer, Decimal, and String are described in the toBoolean() function.
3844
+ *
3845
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3846
+ *
3847
+ * If the input collection is empty, the result is empty.
3848
+ *
3849
+ * See: http://hl7.org/fhirpath/#convertstoboolean-boolean
3850
+ *
3851
+ * @param input
3852
+ * @returns
3853
+ */
3854
+ convertsToBoolean: (input) => {
3855
+ if (input.length === 0) {
3856
+ return [];
3857
+ }
3858
+ return booleanToTypedValue(functions.toBoolean(input).length === 1);
3859
+ },
3860
+ /**
3861
+ * Returns the integer representation of the input.
3862
+ *
3863
+ * If the input collection contains a single item, this function will return a single integer if:
3864
+ * 1) the item is an Integer
3865
+ * 2) the item is a String and is convertible to an integer
3866
+ * 3) the item is a Boolean, where true results in a 1 and false results in a 0.
3867
+ *
3868
+ * If the item is not one the above types, the result is empty.
3869
+ *
3870
+ * If the item is a String, but the string is not convertible to an integer (using the regex format (\\+|-)?\d+), the result is empty.
3871
+ *
3872
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3873
+ *
3874
+ * If the input collection is empty, the result is empty.
3875
+ *
3876
+ * See: https://hl7.org/fhirpath/#tointeger-integer
3877
+ *
3878
+ * @param input The input collection.
3879
+ * @returns The string representation of the input.
3880
+ */
3881
+ toInteger: (input) => {
3882
+ if (input.length === 0) {
3883
+ return [];
3884
+ }
3885
+ const [{ value }] = validateInput(input, 1);
3886
+ if (typeof value === 'number') {
3887
+ return [{ type: exports.PropertyType.integer, value }];
3888
+ }
3889
+ if (typeof value === 'string' && value.match(/^[+-]?\d+$/)) {
3890
+ return [{ type: exports.PropertyType.integer, value: parseInt(value, 10) }];
3891
+ }
3892
+ if (typeof value === 'boolean') {
3893
+ return [{ type: exports.PropertyType.integer, value: value ? 1 : 0 }];
3894
+ }
3801
3895
  return [];
3802
- }
3803
- return [toDate(input).length === 1];
3804
- }
3805
- /**
3806
- * If the input collection contains a single item, this function will return a single datetime if:
3807
- * 1) the item is a DateTime
3808
- * 2) the item is a Date, in which case the result is a DateTime with the year, month, and day of the Date, and the time components empty (not set to zero)
3809
- * 3) the item is a String and is convertible to a DateTime
3810
- *
3811
- * If the item is not one of the above types, the result is empty.
3812
- *
3813
- * If the item is a String, but the string is not convertible to a DateTime (using the format YYYY-MM-DDThh:mm:ss.fff(+|-)hh:mm), the result is empty.
3814
- *
3815
- * If the item contains a partial datetime (e.g. '2012-01-01T10:00'), the result is a partial datetime.
3816
- *
3817
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3818
- *
3819
- * If the input collection is empty, the result is empty.
3820
-
3821
- * See: https://hl7.org/fhirpath/#todatetime-datetime
3822
- *
3823
- * @param input
3824
- * @returns
3825
- */
3826
- function toDateTime(input) {
3827
- if (input.length === 0) {
3896
+ },
3897
+ /**
3898
+ * Returns true if the input can be converted to string.
3899
+ *
3900
+ * If the input collection contains a single item, this function will return true if:
3901
+ * 1) the item is an Integer
3902
+ * 2) the item is a String and is convertible to an Integer
3903
+ * 3) the item is a Boolean
3904
+ * 4) If the item is not one of the above types, or the item is a String, but is not convertible to an Integer (using the regex format (\\+|-)?\d+), the result is false.
3905
+ *
3906
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3907
+ *
3908
+ * If the input collection is empty, the result is empty.
3909
+ *
3910
+ * See: https://hl7.org/fhirpath/#convertstointeger-boolean
3911
+ *
3912
+ * @param input The input collection.
3913
+ * @returns
3914
+ */
3915
+ convertsToInteger: (input) => {
3916
+ if (input.length === 0) {
3917
+ return [];
3918
+ }
3919
+ return booleanToTypedValue(functions.toInteger(input).length === 1);
3920
+ },
3921
+ /**
3922
+ * If the input collection contains a single item, this function will return a single date if:
3923
+ * 1) the item is a Date
3924
+ * 2) the item is a DateTime
3925
+ * 3) the item is a String and is convertible to a Date
3926
+ *
3927
+ * If the item is not one of the above types, the result is empty.
3928
+ *
3929
+ * If the item is a String, but the string is not convertible to a Date (using the format YYYY-MM-DD), the result is empty.
3930
+ *
3931
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3932
+ *
3933
+ * If the input collection is empty, the result is empty.
3934
+ *
3935
+ * See: https://hl7.org/fhirpath/#todate-date
3936
+ */
3937
+ toDate: (input) => {
3938
+ if (input.length === 0) {
3939
+ return [];
3940
+ }
3941
+ const [{ value }] = validateInput(input, 1);
3942
+ if (typeof value === 'string' && value.match(/^\d{4}(-\d{2}(-\d{2})?)?/)) {
3943
+ return [{ type: exports.PropertyType.date, value: parseDateString(value) }];
3944
+ }
3828
3945
  return [];
3829
- }
3830
- const [value] = validateInput(input, 1);
3831
- if (typeof value === 'string' && value.match(/^\d{4}(-\d{2}(-\d{2})?)?/)) {
3832
- return [parseDateString(value)];
3833
- }
3834
- return [];
3835
- }
3836
- /**
3837
- * If the input collection contains a single item, this function will return true if:
3838
- * 1) the item is a DateTime
3839
- * 2) the item is a Date
3840
- * 3) the item is a String and is convertible to a DateTime
3841
- *
3842
- * If the item is not one of the above types, or is not convertible to a DateTime (using the format YYYY-MM-DDThh:mm:ss.fff(+|-)hh:mm), the result is false.
3843
- *
3844
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3845
- *
3846
- * If the input collection is empty, the result is empty.
3847
- *
3848
- * See: https://hl7.org/fhirpath/#convertstodatetime-boolean
3849
- *
3850
- * @param input
3851
- * @returns
3852
- */
3853
- function convertsToDateTime(input) {
3854
- if (input.length === 0) {
3946
+ },
3947
+ /**
3948
+ * If the input collection contains a single item, this function will return true if:
3949
+ * 1) the item is a Date
3950
+ * 2) the item is a DateTime
3951
+ * 3) the item is a String and is convertible to a Date
3952
+ *
3953
+ * If the item is not one of the above types, or is not convertible to a Date (using the format YYYY-MM-DD), the result is false.
3954
+ *
3955
+ * If the item contains a partial date (e.g. '2012-01'), the result is a partial date.
3956
+ *
3957
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3958
+ *
3959
+ * If the input collection is empty, the result is empty.
3960
+ *
3961
+ * See: https://hl7.org/fhirpath/#convertstodate-boolean
3962
+ */
3963
+ convertsToDate: (input) => {
3964
+ if (input.length === 0) {
3965
+ return [];
3966
+ }
3967
+ return booleanToTypedValue(functions.toDate(input).length === 1);
3968
+ },
3969
+ /**
3970
+ * If the input collection contains a single item, this function will return a single datetime if:
3971
+ * 1) the item is a DateTime
3972
+ * 2) the item is a Date, in which case the result is a DateTime with the year, month, and day of the Date, and the time components empty (not set to zero)
3973
+ * 3) the item is a String and is convertible to a DateTime
3974
+ *
3975
+ * If the item is not one of the above types, the result is empty.
3976
+ *
3977
+ * If the item is a String, but the string is not convertible to a DateTime (using the format YYYY-MM-DDThh:mm:ss.fff(+|-)hh:mm), the result is empty.
3978
+ *
3979
+ * If the item contains a partial datetime (e.g. '2012-01-01T10:00'), the result is a partial datetime.
3980
+ *
3981
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3982
+ *
3983
+ * If the input collection is empty, the result is empty.
3984
+
3985
+ * See: https://hl7.org/fhirpath/#todatetime-datetime
3986
+ *
3987
+ * @param input
3988
+ * @returns
3989
+ */
3990
+ toDateTime: (input) => {
3991
+ if (input.length === 0) {
3992
+ return [];
3993
+ }
3994
+ const [{ value }] = validateInput(input, 1);
3995
+ if (typeof value === 'string' && value.match(/^\d{4}(-\d{2}(-\d{2})?)?/)) {
3996
+ return [{ type: exports.PropertyType.dateTime, value: parseDateString(value) }];
3997
+ }
3855
3998
  return [];
3856
- }
3857
- return [toDateTime(input).length === 1];
3858
- }
3859
- /**
3860
- * If the input collection contains a single item, this function will return a single decimal if:
3861
- * 1) the item is an Integer or Decimal
3862
- * 2) the item is a String and is convertible to a Decimal
3863
- * 3) the item is a Boolean, where true results in a 1.0 and false results in a 0.0.
3864
- * 4) If the item is not one of the above types, the result is empty.
3865
- *
3866
- * If the item is a String, but the string is not convertible to a Decimal (using the regex format (\\+|-)?\d+(\.\d+)?), the result is empty.
3867
- *
3868
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3869
- *
3870
- * If the input collection is empty, the result is empty.
3871
- *
3872
- * See: https://hl7.org/fhirpath/#decimal-conversion-functions
3873
- *
3874
- * @param input The input collection.
3875
- * @returns
3876
- */
3877
- function toDecimal(input) {
3878
- if (input.length === 0) {
3879
- return [];
3880
- }
3881
- const [value] = validateInput(input, 1);
3882
- if (typeof value === 'number') {
3883
- return [value];
3884
- }
3885
- if (typeof value === 'string' && value.match(/^-?\d{1,9}(\.\d{1,9})?$/)) {
3886
- return [parseFloat(value)];
3887
- }
3888
- if (typeof value === 'boolean') {
3889
- return [value ? 1 : 0];
3890
- }
3891
- return [];
3892
- }
3893
- /**
3894
- * If the input collection contains a single item, this function will true if:
3895
- * 1) the item is an Integer or Decimal
3896
- * 2) the item is a String and is convertible to a Decimal
3897
- * 3) the item is a Boolean
3898
- *
3899
- * If the item is not one of the above types, or is not convertible to a Decimal (using the regex format (\\+|-)?\d+(\.\d+)?), the result is false.
3900
- *
3901
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3902
- *
3903
- * If the input collection is empty, the result is empty.
3904
-
3905
- * See: https://hl7.org/fhirpath/#convertstodecimal-boolean
3906
- *
3907
- * @param input The input collection.
3908
- * @returns
3909
- */
3910
- function convertsToDecimal(input) {
3911
- if (input.length === 0) {
3912
- return [];
3913
- }
3914
- return [toDecimal(input).length === 1];
3915
- }
3916
- /**
3917
- * If the input collection contains a single item, this function will return a single quantity if:
3918
- * 1) the item is an Integer, or Decimal, where the resulting quantity will have the default unit ('1')
3919
- * 2) the item is a Quantity
3920
- * 3) the item is a String and is convertible to a Quantity
3921
- * 4) the item is a Boolean, where true results in the quantity 1.0 '1', and false results in the quantity 0.0 '1'
3922
- *
3923
- * If the item is not one of the above types, the result is empty.
3924
- *
3925
- * See: https://hl7.org/fhirpath/#quantity-conversion-functions
3926
- *
3927
- * @param input The input collection.
3928
- * @returns
3929
- */
3930
- function toQuantity(input) {
3931
- if (input.length === 0) {
3932
- return [];
3933
- }
3934
- const [value] = validateInput(input, 1);
3935
- if (isQuantity(value)) {
3936
- return [value];
3937
- }
3938
- if (typeof value === 'number') {
3939
- return [{ value, unit: '1' }];
3940
- }
3941
- if (typeof value === 'string' && value.match(/^-?\d{1,9}(\.\d{1,9})?/)) {
3942
- return [{ value: parseFloat(value), unit: '1' }];
3943
- }
3944
- if (typeof value === 'boolean') {
3945
- return [{ value: value ? 1 : 0, unit: '1' }];
3946
- }
3947
- return [];
3948
- }
3949
- /**
3950
- * If the input collection contains a single item, this function will return true if:
3951
- * 1) the item is an Integer, Decimal, or Quantity
3952
- * 2) the item is a String that is convertible to a Quantity
3953
- * 3) the item is a Boolean
3954
- *
3955
- * If the item is not one of the above types, or is not convertible to a Quantity using the following regex format:
3956
- *
3957
- * (?'value'(\+|-)?\d+(\.\d+)?)\s*('(?'unit'[^']+)'|(?'time'[a-zA-Z]+))?
3958
- *
3959
- * then the result is false.
3960
- *
3961
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3962
- *
3963
- * If the input collection is empty, the result is empty.
3964
- *
3965
- * If the unit argument is provided, it must be the string representation of a UCUM code (or a FHIRPath calendar duration keyword), and is used to determine whether the input quantity can be converted to the given unit, according to the unit conversion rules specified by UCUM. If the input quantity can be converted, the result is true, otherwise, the result is false.
3966
- *
3967
- * See: https://hl7.org/fhirpath/#convertstoquantityunit-string-boolean
3968
- *
3969
- * @param input The input collection.
3970
- * @returns
3971
- */
3972
- function convertsToQuantity(input) {
3973
- if (input.length === 0) {
3974
- return [];
3975
- }
3976
- return [toQuantity(input).length === 1];
3977
- }
3978
- /**
3979
- * Returns the string representation of the input.
3980
- *
3981
- * If the input collection contains a single item, this function will return a single String if:
3982
- *
3983
- * 1) the item in the input collection is a String
3984
- * 2) the item in the input collection is an Integer, Decimal, Date, Time, DateTime, or Quantity the output will contain its String representation
3985
- * 3) the item is a Boolean, where true results in 'true' and false in 'false'.
3986
- *
3987
- * If the item is not one of the above types, the result is false.
3988
- *
3989
- * See: https://hl7.org/fhirpath/#tostring-string
3990
- *
3991
- * @param input The input collection.
3992
- * @returns The string representation of the input.
3993
- */
3994
- function toString(input) {
3995
- if (input.length === 0) {
3996
- return [];
3997
- }
3998
- const [value] = validateInput(input, 1);
3999
- if (value === null || value === undefined) {
4000
- return [];
4001
- }
4002
- if (isQuantity(value)) {
4003
- return [`${value.value} '${value.unit}'`];
4004
- }
4005
- return [value.toString()];
4006
- }
4007
- /**
4008
- * Returns true if the input can be converted to string.
4009
- *
4010
- * If the input collection contains a single item, this function will return true if:
4011
- * 1) the item is a String
4012
- * 2) the item is an Integer, Decimal, Date, Time, or DateTime
4013
- * 3) the item is a Boolean
4014
- * 4) the item is a Quantity
4015
- *
4016
- * If the item is not one of the above types, the result is false.
4017
- *
4018
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4019
- *
4020
- * If the input collection is empty, the result is empty.
4021
- *
4022
- * See: https://hl7.org/fhirpath/#tostring-string
4023
- *
4024
- * @param input The input collection.
4025
- * @returns
4026
- */
4027
- function convertsToString(input) {
4028
- if (input.length === 0) {
4029
- return [];
4030
- }
4031
- return [toString(input).length === 1];
4032
- }
4033
- /**
4034
- * If the input collection contains a single item, this function will return a single time if:
4035
- * 1) the item is a Time
4036
- * 2) the item is a String and is convertible to a Time
4037
- *
4038
- * If the item is not one of the above types, the result is empty.
4039
- *
4040
- * If the item is a String, but the string is not convertible to a Time (using the format hh:mm:ss.fff(+|-)hh:mm), the result is empty.
4041
- *
4042
- * If the item contains a partial time (e.g. '10:00'), the result is a partial time.
4043
- *
4044
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4045
- *
4046
- * If the input collection is empty, the result is empty.
4047
- *
4048
- * See: https://hl7.org/fhirpath/#totime-time
4049
- *
4050
- * @param input
4051
- * @returns
4052
- */
4053
- function toTime(input) {
4054
- if (input.length === 0) {
3999
+ },
4000
+ /**
4001
+ * If the input collection contains a single item, this function will return true if:
4002
+ * 1) the item is a DateTime
4003
+ * 2) the item is a Date
4004
+ * 3) the item is a String and is convertible to a DateTime
4005
+ *
4006
+ * If the item is not one of the above types, or is not convertible to a DateTime (using the format YYYY-MM-DDThh:mm:ss.fff(+|-)hh:mm), the result is false.
4007
+ *
4008
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4009
+ *
4010
+ * If the input collection is empty, the result is empty.
4011
+ *
4012
+ * See: https://hl7.org/fhirpath/#convertstodatetime-boolean
4013
+ *
4014
+ * @param input
4015
+ * @returns
4016
+ */
4017
+ convertsToDateTime: (input) => {
4018
+ if (input.length === 0) {
4019
+ return [];
4020
+ }
4021
+ return booleanToTypedValue(functions.toDateTime(input).length === 1);
4022
+ },
4023
+ /**
4024
+ * If the input collection contains a single item, this function will return a single decimal if:
4025
+ * 1) the item is an Integer or Decimal
4026
+ * 2) the item is a String and is convertible to a Decimal
4027
+ * 3) the item is a Boolean, where true results in a 1.0 and false results in a 0.0.
4028
+ * 4) If the item is not one of the above types, the result is empty.
4029
+ *
4030
+ * If the item is a String, but the string is not convertible to a Decimal (using the regex format (\\+|-)?\d+(\.\d+)?), the result is empty.
4031
+ *
4032
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4033
+ *
4034
+ * If the input collection is empty, the result is empty.
4035
+ *
4036
+ * See: https://hl7.org/fhirpath/#decimal-conversion-functions
4037
+ *
4038
+ * @param input The input collection.
4039
+ * @returns
4040
+ */
4041
+ toDecimal: (input) => {
4042
+ if (input.length === 0) {
4043
+ return [];
4044
+ }
4045
+ const [{ value }] = validateInput(input, 1);
4046
+ if (typeof value === 'number') {
4047
+ return [{ type: exports.PropertyType.decimal, value }];
4048
+ }
4049
+ if (typeof value === 'string' && value.match(/^-?\d{1,9}(\.\d{1,9})?$/)) {
4050
+ return [{ type: exports.PropertyType.decimal, value: parseFloat(value) }];
4051
+ }
4052
+ if (typeof value === 'boolean') {
4053
+ return [{ type: exports.PropertyType.decimal, value: value ? 1 : 0 }];
4054
+ }
4055
4055
  return [];
4056
- }
4057
- const [value] = validateInput(input, 1);
4058
- if (typeof value === 'string') {
4059
- const match = value.match(/^T?(\d{2}(:\d{2}(:\d{2})?)?)/);
4060
- if (match) {
4061
- return [parseDateString('T' + match[1])];
4056
+ },
4057
+ /**
4058
+ * If the input collection contains a single item, this function will true if:
4059
+ * 1) the item is an Integer or Decimal
4060
+ * 2) the item is a String and is convertible to a Decimal
4061
+ * 3) the item is a Boolean
4062
+ *
4063
+ * If the item is not one of the above types, or is not convertible to a Decimal (using the regex format (\\+|-)?\d+(\.\d+)?), the result is false.
4064
+ *
4065
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4066
+ *
4067
+ * If the input collection is empty, the result is empty.
4068
+
4069
+ * See: https://hl7.org/fhirpath/#convertstodecimal-boolean
4070
+ *
4071
+ * @param input The input collection.
4072
+ * @returns
4073
+ */
4074
+ convertsToDecimal: (input) => {
4075
+ if (input.length === 0) {
4076
+ return [];
4077
+ }
4078
+ return booleanToTypedValue(functions.toDecimal(input).length === 1);
4079
+ },
4080
+ /**
4081
+ * If the input collection contains a single item, this function will return a single quantity if:
4082
+ * 1) the item is an Integer, or Decimal, where the resulting quantity will have the default unit ('1')
4083
+ * 2) the item is a Quantity
4084
+ * 3) the item is a String and is convertible to a Quantity
4085
+ * 4) the item is a Boolean, where true results in the quantity 1.0 '1', and false results in the quantity 0.0 '1'
4086
+ *
4087
+ * If the item is not one of the above types, the result is empty.
4088
+ *
4089
+ * See: https://hl7.org/fhirpath/#quantity-conversion-functions
4090
+ *
4091
+ * @param input The input collection.
4092
+ * @returns
4093
+ */
4094
+ toQuantity: (input) => {
4095
+ if (input.length === 0) {
4096
+ return [];
4097
+ }
4098
+ const [{ value }] = validateInput(input, 1);
4099
+ if (isQuantity(value)) {
4100
+ return [{ type: exports.PropertyType.Quantity, value }];
4101
+ }
4102
+ if (typeof value === 'number') {
4103
+ return [{ type: exports.PropertyType.Quantity, value: { value, unit: '1' } }];
4104
+ }
4105
+ if (typeof value === 'string' && value.match(/^-?\d{1,9}(\.\d{1,9})?/)) {
4106
+ return [{ type: exports.PropertyType.Quantity, value: { value: parseFloat(value), unit: '1' } }];
4107
+ }
4108
+ if (typeof value === 'boolean') {
4109
+ return [{ type: exports.PropertyType.Quantity, value: { value: value ? 1 : 0, unit: '1' } }];
4062
4110
  }
4063
- }
4064
- return [];
4065
- }
4066
- /**
4067
- * If the input collection contains a single item, this function will return true if:
4068
- * 1) the item is a Time
4069
- * 2) the item is a String and is convertible to a Time
4070
- *
4071
- * If the item is not one of the above types, or is not convertible to a Time (using the format hh:mm:ss.fff(+|-)hh:mm), the result is false.
4072
- *
4073
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4074
- *
4075
- * If the input collection is empty, the result is empty.
4076
- *
4077
- * See: https://hl7.org/fhirpath/#convertstotime-boolean
4078
- *
4079
- * @param input
4080
- * @returns
4081
- */
4082
- function convertsToTime(input) {
4083
- if (input.length === 0) {
4084
4111
  return [];
4085
- }
4086
- return [toTime(input).length === 1];
4087
- }
4088
- /*
4089
- * 5.6. String Manipulation.
4090
- *
4091
- * See: https://hl7.org/fhirpath/#string-manipulation
4092
- */
4093
- /**
4094
- * Returns the 0-based index of the first position substring is found in the input string, or -1 if it is not found.
4095
- *
4096
- * If substring is an empty string (''), the function returns 0.
4097
- *
4098
- * If the input or substring is empty ({ }), the result is empty ({ }).
4099
- *
4100
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4101
- *
4102
- * See: https://hl7.org/fhirpath/#indexofsubstring-string-integer
4103
- *
4104
- * @param input The input collection.
4105
- * @returns The index of the substring.
4106
- */
4107
- function indexOf(input, substringAtom) {
4108
- return applyStringFunc((str, substring) => str.indexOf(substring), input, substringAtom);
4109
- }
4110
- /**
4111
- * Returns the part of the string starting at position start (zero-based). If length is given, will return at most length number of characters from the input string.
4112
- *
4113
- * If start lies outside the length of the string, the function returns empty ({ }). If there are less remaining characters in the string than indicated by length, the function returns just the remaining characters.
4114
- *
4115
- * If the input or start is empty, the result is empty.
4116
- *
4117
- * If an empty length is provided, the behavior is the same as if length had not been provided.
4118
- *
4119
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4120
- *
4121
- * @param input The input collection.
4122
- * @returns The index of the substring.
4123
- */
4124
- function substring(input, startAtom, lengthAtom) {
4125
- return applyStringFunc((str, start, length) => {
4126
- const startIndex = start;
4127
- const endIndex = length ? startIndex + length : str.length;
4128
- return startIndex < 0 || startIndex >= str.length ? undefined : str.substring(startIndex, endIndex);
4129
- }, input, startAtom, lengthAtom);
4130
- }
4131
- /**
4132
- *
4133
- * @param input The input collection.
4134
- * @returns The index of the substring.
4135
- */
4136
- function startsWith(input, prefixAtom) {
4137
- return applyStringFunc((str, prefix) => str.startsWith(prefix), input, prefixAtom);
4138
- }
4139
- /**
4140
- *
4141
- * @param input The input collection.
4142
- * @returns The index of the substring.
4143
- */
4144
- function endsWith(input, suffixAtom) {
4145
- return applyStringFunc((str, suffix) => str.endsWith(suffix), input, suffixAtom);
4146
- }
4147
- /**
4148
- *
4149
- * @param input The input collection.
4150
- * @returns The index of the substring.
4151
- */
4152
- function contains(input, substringAtom) {
4153
- return applyStringFunc((str, substring) => str.includes(substring), input, substringAtom);
4154
- }
4155
- /**
4156
- *
4157
- * @param input The input collection.
4158
- * @returns The index of the substring.
4159
- */
4160
- function upper(input) {
4161
- return applyStringFunc((str) => str.toUpperCase(), input);
4162
- }
4163
- /**
4164
- *
4165
- * @param input The input collection.
4166
- * @returns The index of the substring.
4167
- */
4168
- function lower(input) {
4169
- return applyStringFunc((str) => str.toLowerCase(), input);
4170
- }
4171
- /**
4172
- *
4173
- * @param input The input collection.
4174
- * @returns The index of the substring.
4175
- */
4176
- function replace(input, patternAtom, substitionAtom) {
4177
- return applyStringFunc((str, pattern, substition) => str.replaceAll(pattern, substition), input, patternAtom, substitionAtom);
4178
- }
4179
- /**
4180
- *
4181
- * @param input The input collection.
4182
- * @returns The index of the substring.
4183
- */
4184
- function matches(input, regexAtom) {
4185
- return applyStringFunc((str, regex) => !!str.match(regex), input, regexAtom);
4186
- }
4187
- /**
4188
- *
4189
- * @param input The input collection.
4190
- * @returns The index of the substring.
4191
- */
4192
- function replaceMatches(input, regexAtom, substitionAtom) {
4193
- return applyStringFunc((str, pattern, substition) => str.replaceAll(pattern, substition), input, regexAtom, substitionAtom);
4194
- }
4195
- /**
4196
- *
4197
- * @param input The input collection.
4198
- * @returns The index of the substring.
4199
- */
4200
- function length(input) {
4201
- return applyStringFunc((str) => str.length, input);
4202
- }
4203
- /**
4204
- * Returns the list of characters in the input string. If the input collection is empty ({ }), the result is empty.
4205
- *
4206
- * See: https://hl7.org/fhirpath/#tochars-collection
4207
- *
4208
- * @param input The input collection.
4209
- */
4210
- function toChars(input) {
4211
- return applyStringFunc((str) => (str ? str.split('') : undefined), input);
4212
- }
4213
- /*
4214
- * 5.7. Math
4215
- */
4216
- /**
4217
- * Returns the absolute value of the input. When taking the absolute value of a quantity, the unit is unchanged.
4218
- *
4219
- * If the input collection is empty, the result is empty.
4220
- *
4221
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4222
- *
4223
- * See: https://hl7.org/fhirpath/#abs-integer-decimal-quantity
4224
- *
4225
- * @param input The input collection.
4226
- * @returns A collection containing the result.
4227
- */
4228
- function abs(input) {
4229
- return applyMathFunc(Math.abs, input);
4230
- }
4231
- /**
4232
- * Returns the first integer greater than or equal to the input.
4233
- *
4234
- * If the input collection is empty, the result is empty.
4235
- *
4236
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4237
- *
4238
- * See: https://hl7.org/fhirpath/#ceiling-integer
4239
- *
4240
- * @param input The input collection.
4241
- * @returns A collection containing the result.
4242
- */
4243
- function ceiling(input) {
4244
- return applyMathFunc(Math.ceil, input);
4245
- }
4246
- /**
4247
- * Returns e raised to the power of the input.
4248
- *
4249
- * If the input collection contains an Integer, it will be implicitly converted to a Decimal and the result will be a Decimal.
4250
- *
4251
- * If the input collection is empty, the result is empty.
4252
- *
4253
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4254
- *
4255
- * See: https://hl7.org/fhirpath/#exp-decimal
4256
- *
4257
- * @param input The input collection.
4258
- * @returns A collection containing the result.
4259
- */
4260
- function exp(input) {
4261
- return applyMathFunc(Math.exp, input);
4262
- }
4263
- /**
4264
- * Returns the first integer less than or equal to the input.
4265
- *
4266
- * If the input collection is empty, the result is empty.
4267
- *
4268
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4269
- *
4270
- * See: https://hl7.org/fhirpath/#floor-integer
4271
- *
4272
- * @param input The input collection.
4273
- * @returns A collection containing the result.
4274
- */
4275
- function floor(input) {
4276
- return applyMathFunc(Math.floor, input);
4277
- }
4278
- /**
4279
- * Returns the natural logarithm of the input (i.e. the logarithm base e).
4280
- *
4281
- * When used with an Integer, it will be implicitly converted to a Decimal.
4282
- *
4283
- * If the input collection is empty, the result is empty.
4284
- *
4285
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4286
- *
4287
- * See: https://hl7.org/fhirpath/#ln-decimal
4288
- *
4289
- * @param input The input collection.
4290
- * @returns A collection containing the result.
4291
- */
4292
- function ln(input) {
4293
- return applyMathFunc(Math.log, input);
4294
- }
4295
- /**
4296
- * Returns the logarithm base base of the input number.
4297
- *
4298
- * When used with Integers, the arguments will be implicitly converted to Decimal.
4299
- *
4300
- * If base is empty, the result is empty.
4301
- *
4302
- * If the input collection is empty, the result is empty.
4303
- *
4304
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4305
- *
4306
- * See: https://hl7.org/fhirpath/#logbase-decimal-decimal
4307
- *
4308
- * @param input The input collection.
4309
- * @returns A collection containing the result.
4310
- */
4311
- function log(input, baseAtom) {
4312
- return applyMathFunc((value, base) => Math.log(value) / Math.log(base), input, baseAtom);
4313
- }
4314
- /**
4315
- * Raises a number to the exponent power. If this function is used with Integers, the result is an Integer. If the function is used with Decimals, the result is a Decimal. If the function is used with a mixture of Integer and Decimal, the Integer is implicitly converted to a Decimal and the result is a Decimal.
4316
- *
4317
- * If the power cannot be represented (such as the -1 raised to the 0.5), the result is empty.
4318
- *
4319
- * If the input is empty, or exponent is empty, the result is empty.
4320
- *
4321
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4322
- *
4323
- * See: https://hl7.org/fhirpath/#powerexponent-integer-decimal-integer-decimal
4324
- *
4325
- * @param input The input collection.
4326
- * @returns A collection containing the result.
4327
- */
4328
- function power(input, expAtom) {
4329
- return applyMathFunc(Math.pow, input, expAtom);
4330
- }
4331
- /**
4332
- * Rounds the decimal to the nearest whole number using a traditional round (i.e. 0.5 or higher will round to 1). If specified, the precision argument determines the decimal place at which the rounding will occur. If not specified, the rounding will default to 0 decimal places.
4333
- *
4334
- * If specified, the number of digits of precision must be >= 0 or the evaluation will end and signal an error to the calling environment.
4335
- *
4336
- * If the input collection contains a single item of type Integer, it will be implicitly converted to a Decimal.
4337
- *
4338
- * If the input collection is empty, the result is empty.
4339
- *
4340
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4341
- *
4342
- * See: https://hl7.org/fhirpath/#roundprecision-integer-decimal
4343
- *
4344
- * @param input The input collection.
4345
- * @returns A collection containing the result.
4346
- */
4347
- function round(input) {
4348
- return applyMathFunc(Math.round, input);
4349
- }
4350
- /**
4351
- * Returns the square root of the input number as a Decimal.
4352
- *
4353
- * If the square root cannot be represented (such as the square root of -1), the result is empty.
4354
- *
4355
- * If the input collection is empty, the result is empty.
4356
- *
4357
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4358
- *
4359
- * Note that this function is equivalent to raising a number of the power of 0.5 using the power() function.
4360
- *
4361
- * See: https://hl7.org/fhirpath/#sqrt-decimal
4362
- *
4363
- * @param input The input collection.
4364
- * @returns A collection containing the result.
4365
- */
4366
- function sqrt(input) {
4367
- return applyMathFunc(Math.sqrt, input);
4368
- }
4369
- /**
4370
- * Returns the integer portion of the input.
4371
- *
4372
- * If the input collection is empty, the result is empty.
4373
- *
4374
- * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4375
- *
4376
- * See: https://hl7.org/fhirpath/#truncate-integer
4377
- *
4378
- * @param input The input collection.
4379
- * @returns A collection containing the result.
4380
- */
4381
- function truncate(input) {
4382
- return applyMathFunc((x) => x | 0, input);
4383
- }
4384
- /*
4385
- * 5.8. Tree navigation
4386
- */
4387
- const children = stub;
4388
- const descendants = stub;
4389
- /*
4390
- * 5.9. Utility functions
4391
- */
4392
- /**
4393
- * Adds a String representation of the input collection to the diagnostic log,
4394
- * using the name argument as the name in the log. This log should be made available
4395
- * to the user in some appropriate fashion. Does not change the input, so returns
4396
- * the input collection as output.
4397
- *
4398
- * If the projection argument is used, the trace would log the result of evaluating
4399
- * the project expression on the input, but still return the input to the trace
4400
- * function unchanged.
4401
- *
4402
- * See: https://hl7.org/fhirpath/#tracename-string-projection-expression-collection
4403
- *
4404
- * @param input The input collection.
4405
- * @param nameAtom The log name.
4406
- */
4407
- function trace(input, nameAtom) {
4408
- console.log('trace', input, nameAtom);
4409
- return input;
4410
- }
4411
- /**
4412
- * Returns the current date and time, including timezone offset.
4413
- *
4414
- * See: https://hl7.org/fhirpath/#now-datetime
4415
- */
4416
- function now() {
4417
- return [new Date().toISOString()];
4418
- }
4419
- /**
4420
- * Returns the current time.
4421
- *
4422
- * See: https://hl7.org/fhirpath/#timeofday-time
4423
- */
4424
- function timeOfDay() {
4425
- return [new Date().toISOString().substring(11)];
4426
- }
4427
- /**
4428
- * Returns the current date.
4429
- *
4430
- * See: https://hl7.org/fhirpath/#today-date
4431
- */
4432
- function today() {
4433
- return [new Date().toISOString().substring(0, 10)];
4434
- }
4435
- /**
4436
- * Calculates the difference between two dates or date/times.
4437
- *
4438
- * This is not part of the official FHIRPath spec.
4439
- *
4440
- * IBM FHIR issue: https://github.com/IBM/FHIR/issues/1014
4441
- * IBM FHIR PR: https://github.com/IBM/FHIR/pull/1023
4442
- */
4443
- function between(context, startAtom, endAtom, unitsAtom) {
4444
- const startDate = toDateTime(ensureArray(startAtom.eval(context)));
4445
- if (startDate.length === 0) {
4446
- throw new Error('Invalid start date');
4447
- }
4448
- const endDate = toDateTime(ensureArray(endAtom.eval(context)));
4449
- if (endDate.length === 0) {
4450
- throw new Error('Invalid end date');
4451
- }
4452
- const unit = unitsAtom.eval(context);
4453
- if (unit !== 'years' && unit !== 'months' && unit !== 'days') {
4454
- throw new Error('Invalid units');
4455
- }
4456
- const age = calculateAge(startDate[0], endDate[0]);
4457
- return [{ value: age[unit], unit }];
4458
- }
4459
- /*
4460
- * 6.3 Types
4461
- */
4462
- /**
4463
- * The is() function is supported for backwards compatibility with previous
4464
- * implementations of FHIRPath. Just as with the is keyword, the type argument
4465
- * is an identifier that must resolve to the name of a type in a model.
4466
- *
4467
- * For implementations with compile-time typing, this requires special-case
4468
- * handling when processing the argument to treat it as a type specifier rather
4469
- * than an identifier expression:
4470
- *
4471
- * @param input
4472
- * @param typeAtom
4473
- * @returns
4474
- */
4475
- function is(input, typeAtom) {
4476
- const typeName = typeAtom.name;
4477
- return input.map((value) => fhirPathIs(value, typeName));
4478
- }
4479
- /*
4480
- * 6.5 Boolean logic
4481
- */
4482
- /**
4483
- * 6.5.3. not() : Boolean
4484
- *
4485
- * Returns true if the input collection evaluates to false, and false if it evaluates to true. Otherwise, the result is empty ({ }):
4486
- *
4487
- * @param input
4488
- * @returns
4489
- */
4490
- function not(input) {
4491
- return toBoolean(input).map((value) => !value);
4492
- }
4493
- /*
4494
- * Additional functions
4495
- * See: https://hl7.org/fhir/fhirpath.html#functions
4496
- */
4497
- /**
4498
- * For each item in the collection, if it is a string that is a uri (or canonical or url), locate the target of the reference, and add it to the resulting collection. If the item does not resolve to a resource, the item is ignored and nothing is added to the output collection.
4499
- * The items in the collection may also represent a Reference, in which case the Reference.reference is resolved.
4500
- * @param input The input collection.
4501
- * @returns
4502
- */
4503
- function resolve(input) {
4504
- return input
4505
- .map((e) => {
4506
- let refStr;
4507
- if (typeof e === 'string') {
4508
- refStr = e;
4509
- }
4510
- else if (typeof e === 'object') {
4511
- const ref = e;
4512
- if (ref.resource) {
4513
- return ref.resource;
4112
+ },
4113
+ /**
4114
+ * If the input collection contains a single item, this function will return true if:
4115
+ * 1) the item is an Integer, Decimal, or Quantity
4116
+ * 2) the item is a String that is convertible to a Quantity
4117
+ * 3) the item is a Boolean
4118
+ *
4119
+ * If the item is not one of the above types, or is not convertible to a Quantity using the following regex format:
4120
+ *
4121
+ * (?'value'(\+|-)?\d+(\.\d+)?)\s*('(?'unit'[^']+)'|(?'time'[a-zA-Z]+))?
4122
+ *
4123
+ * then the result is false.
4124
+ *
4125
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4126
+ *
4127
+ * If the input collection is empty, the result is empty.
4128
+ *
4129
+ * If the unit argument is provided, it must be the string representation of a UCUM code (or a FHIRPath calendar duration keyword), and is used to determine whether the input quantity can be converted to the given unit, according to the unit conversion rules specified by UCUM. If the input quantity can be converted, the result is true, otherwise, the result is false.
4130
+ *
4131
+ * See: https://hl7.org/fhirpath/#convertstoquantityunit-string-boolean
4132
+ *
4133
+ * @param input The input collection.
4134
+ * @returns
4135
+ */
4136
+ convertsToQuantity: (input) => {
4137
+ if (input.length === 0) {
4138
+ return [];
4139
+ }
4140
+ return booleanToTypedValue(functions.toQuantity(input).length === 1);
4141
+ },
4142
+ /**
4143
+ * Returns the string representation of the input.
4144
+ *
4145
+ * If the input collection contains a single item, this function will return a single String if:
4146
+ *
4147
+ * 1) the item in the input collection is a String
4148
+ * 2) the item in the input collection is an Integer, Decimal, Date, Time, DateTime, or Quantity the output will contain its String representation
4149
+ * 3) the item is a Boolean, where true results in 'true' and false in 'false'.
4150
+ *
4151
+ * If the item is not one of the above types, the result is false.
4152
+ *
4153
+ * See: https://hl7.org/fhirpath/#tostring-string
4154
+ *
4155
+ * @param input The input collection.
4156
+ * @returns The string representation of the input.
4157
+ */
4158
+ toString: (input) => {
4159
+ if (input.length === 0) {
4160
+ return [];
4161
+ }
4162
+ const [{ value }] = validateInput(input, 1);
4163
+ if (value === null || value === undefined) {
4164
+ return [];
4165
+ }
4166
+ if (isQuantity(value)) {
4167
+ return [{ type: exports.PropertyType.string, value: `${value.value} '${value.unit}'` }];
4168
+ }
4169
+ return [{ type: exports.PropertyType.string, value: value.toString() }];
4170
+ },
4171
+ /**
4172
+ * Returns true if the input can be converted to string.
4173
+ *
4174
+ * If the input collection contains a single item, this function will return true if:
4175
+ * 1) the item is a String
4176
+ * 2) the item is an Integer, Decimal, Date, Time, or DateTime
4177
+ * 3) the item is a Boolean
4178
+ * 4) the item is a Quantity
4179
+ *
4180
+ * If the item is not one of the above types, the result is false.
4181
+ *
4182
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4183
+ *
4184
+ * If the input collection is empty, the result is empty.
4185
+ *
4186
+ * See: https://hl7.org/fhirpath/#tostring-string
4187
+ *
4188
+ * @param input The input collection.
4189
+ * @returns
4190
+ */
4191
+ convertsToString: (input) => {
4192
+ if (input.length === 0) {
4193
+ return [];
4194
+ }
4195
+ return booleanToTypedValue(functions.toString(input).length === 1);
4196
+ },
4197
+ /**
4198
+ * If the input collection contains a single item, this function will return a single time if:
4199
+ * 1) the item is a Time
4200
+ * 2) the item is a String and is convertible to a Time
4201
+ *
4202
+ * If the item is not one of the above types, the result is empty.
4203
+ *
4204
+ * If the item is a String, but the string is not convertible to a Time (using the format hh:mm:ss.fff(+|-)hh:mm), the result is empty.
4205
+ *
4206
+ * If the item contains a partial time (e.g. '10:00'), the result is a partial time.
4207
+ *
4208
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4209
+ *
4210
+ * If the input collection is empty, the result is empty.
4211
+ *
4212
+ * See: https://hl7.org/fhirpath/#totime-time
4213
+ *
4214
+ * @param input
4215
+ * @returns
4216
+ */
4217
+ toTime: (input) => {
4218
+ if (input.length === 0) {
4219
+ return [];
4220
+ }
4221
+ const [{ value }] = validateInput(input, 1);
4222
+ if (typeof value === 'string') {
4223
+ const match = value.match(/^T?(\d{2}(:\d{2}(:\d{2})?)?)/);
4224
+ if (match) {
4225
+ return [{ type: exports.PropertyType.time, value: parseDateString('T' + match[1]) }];
4514
4226
  }
4515
- refStr = ref.reference;
4516
4227
  }
4517
- if (!refStr) {
4518
- return undefined;
4228
+ return [];
4229
+ },
4230
+ /**
4231
+ * If the input collection contains a single item, this function will return true if:
4232
+ * 1) the item is a Time
4233
+ * 2) the item is a String and is convertible to a Time
4234
+ *
4235
+ * If the item is not one of the above types, or is not convertible to a Time (using the format hh:mm:ss.fff(+|-)hh:mm), the result is false.
4236
+ *
4237
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4238
+ *
4239
+ * If the input collection is empty, the result is empty.
4240
+ *
4241
+ * See: https://hl7.org/fhirpath/#convertstotime-boolean
4242
+ *
4243
+ * @param input
4244
+ * @returns
4245
+ */
4246
+ convertsToTime: (input) => {
4247
+ if (input.length === 0) {
4248
+ return [];
4519
4249
  }
4520
- const [resourceType, id] = refStr.split('/');
4521
- return { resourceType, id };
4522
- })
4523
- .filter((e) => !!e);
4524
- }
4525
- /**
4526
- * The as operator can be used to treat a value as a specific type.
4527
- * @param context The context value.
4528
- * @returns The value as the specific type.
4529
- */
4530
- function as(context) {
4531
- return context;
4532
- }
4533
- /*
4534
- * 12. Formal Specifications
4535
- */
4536
- /**
4537
- * Returns the type of the input.
4538
- *
4539
- * 12.2. Model Information
4540
- *
4541
- * The model information returned by the reflection function type() is specified as an
4542
- * XML Schema document (xsd) and included in this specification at the following link:
4543
- * https://hl7.org/fhirpath/modelinfo.xsd
4544
- *
4545
- * See: https://hl7.org/fhirpath/#model-information
4546
- *
4547
- * @param input The input collection.
4548
- * @returns
4549
- */
4550
- function type(input) {
4551
- return input.map((value) => {
4552
- if (typeof value === 'boolean') {
4553
- return { namespace: 'System', name: 'Boolean' };
4250
+ return booleanToTypedValue(functions.toTime(input).length === 1);
4251
+ },
4252
+ /*
4253
+ * 5.6. String Manipulation.
4254
+ *
4255
+ * See: https://hl7.org/fhirpath/#string-manipulation
4256
+ */
4257
+ /**
4258
+ * Returns the 0-based index of the first position substring is found in the input string, or -1 if it is not found.
4259
+ *
4260
+ * If substring is an empty string (''), the function returns 0.
4261
+ *
4262
+ * If the input or substring is empty ({ }), the result is empty ({ }).
4263
+ *
4264
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4265
+ *
4266
+ * See: https://hl7.org/fhirpath/#indexofsubstring-string-integer
4267
+ *
4268
+ * @param input The input collection.
4269
+ * @returns The index of the substring.
4270
+ */
4271
+ indexOf: (input, substringAtom) => {
4272
+ return applyStringFunc((str, substring) => str.indexOf(substring), input, substringAtom);
4273
+ },
4274
+ /**
4275
+ * Returns the part of the string starting at position start (zero-based). If length is given, will return at most length number of characters from the input string.
4276
+ *
4277
+ * If start lies outside the length of the string, the function returns empty ({ }). If there are less remaining characters in the string than indicated by length, the function returns just the remaining characters.
4278
+ *
4279
+ * If the input or start is empty, the result is empty.
4280
+ *
4281
+ * If an empty length is provided, the behavior is the same as if length had not been provided.
4282
+ *
4283
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4284
+ *
4285
+ * @param input The input collection.
4286
+ * @returns The index of the substring.
4287
+ */
4288
+ substring: (input, startAtom, lengthAtom) => {
4289
+ return applyStringFunc((str, start, length) => {
4290
+ const startIndex = start;
4291
+ const endIndex = length ? startIndex + length : str.length;
4292
+ return startIndex < 0 || startIndex >= str.length ? undefined : str.substring(startIndex, endIndex);
4293
+ }, input, startAtom, lengthAtom);
4294
+ },
4295
+ /**
4296
+ *
4297
+ * @param input The input collection.
4298
+ * @returns The index of the substring.
4299
+ */
4300
+ startsWith: (input, prefixAtom) => {
4301
+ return applyStringFunc((str, prefix) => str.startsWith(prefix), input, prefixAtom);
4302
+ },
4303
+ /**
4304
+ *
4305
+ * @param input The input collection.
4306
+ * @returns The index of the substring.
4307
+ */
4308
+ endsWith: (input, suffixAtom) => {
4309
+ return applyStringFunc((str, suffix) => str.endsWith(suffix), input, suffixAtom);
4310
+ },
4311
+ /**
4312
+ *
4313
+ * @param input The input collection.
4314
+ * @returns The index of the substring.
4315
+ */
4316
+ contains: (input, substringAtom) => {
4317
+ return applyStringFunc((str, substring) => str.includes(substring), input, substringAtom);
4318
+ },
4319
+ /**
4320
+ *
4321
+ * @param input The input collection.
4322
+ * @returns The index of the substring.
4323
+ */
4324
+ upper: (input) => {
4325
+ return applyStringFunc((str) => str.toUpperCase(), input);
4326
+ },
4327
+ /**
4328
+ *
4329
+ * @param input The input collection.
4330
+ * @returns The index of the substring.
4331
+ */
4332
+ lower: (input) => {
4333
+ return applyStringFunc((str) => str.toLowerCase(), input);
4334
+ },
4335
+ /**
4336
+ *
4337
+ * @param input The input collection.
4338
+ * @returns The index of the substring.
4339
+ */
4340
+ replace: (input, patternAtom, substitionAtom) => {
4341
+ return applyStringFunc((str, pattern, substition) => str.replaceAll(pattern, substition), input, patternAtom, substitionAtom);
4342
+ },
4343
+ /**
4344
+ *
4345
+ * @param input The input collection.
4346
+ * @returns The index of the substring.
4347
+ */
4348
+ matches: (input, regexAtom) => {
4349
+ return applyStringFunc((str, regex) => !!str.match(regex), input, regexAtom);
4350
+ },
4351
+ /**
4352
+ *
4353
+ * @param input The input collection.
4354
+ * @returns The index of the substring.
4355
+ */
4356
+ replaceMatches: (input, regexAtom, substitionAtom) => {
4357
+ return applyStringFunc((str, pattern, substition) => str.replaceAll(pattern, substition), input, regexAtom, substitionAtom);
4358
+ },
4359
+ /**
4360
+ *
4361
+ * @param input The input collection.
4362
+ * @returns The index of the substring.
4363
+ */
4364
+ length: (input) => {
4365
+ return applyStringFunc((str) => str.length, input);
4366
+ },
4367
+ /**
4368
+ * Returns the list of characters in the input string. If the input collection is empty ({ }), the result is empty.
4369
+ *
4370
+ * See: https://hl7.org/fhirpath/#tochars-collection
4371
+ *
4372
+ * @param input The input collection.
4373
+ */
4374
+ toChars: (input) => {
4375
+ return applyStringFunc((str) => (str ? str.split('') : undefined), input);
4376
+ },
4377
+ /*
4378
+ * 5.7. Math
4379
+ */
4380
+ /**
4381
+ * Returns the absolute value of the input. When taking the absolute value of a quantity, the unit is unchanged.
4382
+ *
4383
+ * If the input collection is empty, the result is empty.
4384
+ *
4385
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4386
+ *
4387
+ * See: https://hl7.org/fhirpath/#abs-integer-decimal-quantity
4388
+ *
4389
+ * @param input The input collection.
4390
+ * @returns A collection containing the result.
4391
+ */
4392
+ abs: (input) => {
4393
+ return applyMathFunc(Math.abs, input);
4394
+ },
4395
+ /**
4396
+ * Returns the first integer greater than or equal to the input.
4397
+ *
4398
+ * If the input collection is empty, the result is empty.
4399
+ *
4400
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4401
+ *
4402
+ * See: https://hl7.org/fhirpath/#ceiling-integer
4403
+ *
4404
+ * @param input The input collection.
4405
+ * @returns A collection containing the result.
4406
+ */
4407
+ ceiling: (input) => {
4408
+ return applyMathFunc(Math.ceil, input);
4409
+ },
4410
+ /**
4411
+ * Returns e raised to the power of the input.
4412
+ *
4413
+ * If the input collection contains an Integer, it will be implicitly converted to a Decimal and the result will be a Decimal.
4414
+ *
4415
+ * If the input collection is empty, the result is empty.
4416
+ *
4417
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4418
+ *
4419
+ * See: https://hl7.org/fhirpath/#exp-decimal
4420
+ *
4421
+ * @param input The input collection.
4422
+ * @returns A collection containing the result.
4423
+ */
4424
+ exp: (input) => {
4425
+ return applyMathFunc(Math.exp, input);
4426
+ },
4427
+ /**
4428
+ * Returns the first integer less than or equal to the input.
4429
+ *
4430
+ * If the input collection is empty, the result is empty.
4431
+ *
4432
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4433
+ *
4434
+ * See: https://hl7.org/fhirpath/#floor-integer
4435
+ *
4436
+ * @param input The input collection.
4437
+ * @returns A collection containing the result.
4438
+ */
4439
+ floor: (input) => {
4440
+ return applyMathFunc(Math.floor, input);
4441
+ },
4442
+ /**
4443
+ * Returns the natural logarithm of the input (i.e. the logarithm base e).
4444
+ *
4445
+ * When used with an Integer, it will be implicitly converted to a Decimal.
4446
+ *
4447
+ * If the input collection is empty, the result is empty.
4448
+ *
4449
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4450
+ *
4451
+ * See: https://hl7.org/fhirpath/#ln-decimal
4452
+ *
4453
+ * @param input The input collection.
4454
+ * @returns A collection containing the result.
4455
+ */
4456
+ ln: (input) => {
4457
+ return applyMathFunc(Math.log, input);
4458
+ },
4459
+ /**
4460
+ * Returns the logarithm base base of the input number.
4461
+ *
4462
+ * When used with Integers, the arguments will be implicitly converted to Decimal.
4463
+ *
4464
+ * If base is empty, the result is empty.
4465
+ *
4466
+ * If the input collection is empty, the result is empty.
4467
+ *
4468
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4469
+ *
4470
+ * See: https://hl7.org/fhirpath/#logbase-decimal-decimal
4471
+ *
4472
+ * @param input The input collection.
4473
+ * @returns A collection containing the result.
4474
+ */
4475
+ log: (input, baseAtom) => {
4476
+ return applyMathFunc((value, base) => Math.log(value) / Math.log(base), input, baseAtom);
4477
+ },
4478
+ /**
4479
+ * Raises a number to the exponent power. If this function is used with Integers, the result is an Integer. If the function is used with Decimals, the result is a Decimal. If the function is used with a mixture of Integer and Decimal, the Integer is implicitly converted to a Decimal and the result is a Decimal.
4480
+ *
4481
+ * If the power cannot be represented (such as the -1 raised to the 0.5), the result is empty.
4482
+ *
4483
+ * If the input is empty, or exponent is empty, the result is empty.
4484
+ *
4485
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4486
+ *
4487
+ * See: https://hl7.org/fhirpath/#powerexponent-integer-decimal-integer-decimal
4488
+ *
4489
+ * @param input The input collection.
4490
+ * @returns A collection containing the result.
4491
+ */
4492
+ power: (input, expAtom) => {
4493
+ return applyMathFunc(Math.pow, input, expAtom);
4494
+ },
4495
+ /**
4496
+ * Rounds the decimal to the nearest whole number using a traditional round (i.e. 0.5 or higher will round to 1). If specified, the precision argument determines the decimal place at which the rounding will occur. If not specified, the rounding will default to 0 decimal places.
4497
+ *
4498
+ * If specified, the number of digits of precision must be >= 0 or the evaluation will end and signal an error to the calling environment.
4499
+ *
4500
+ * If the input collection contains a single item of type Integer, it will be implicitly converted to a Decimal.
4501
+ *
4502
+ * If the input collection is empty, the result is empty.
4503
+ *
4504
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4505
+ *
4506
+ * See: https://hl7.org/fhirpath/#roundprecision-integer-decimal
4507
+ *
4508
+ * @param input The input collection.
4509
+ * @returns A collection containing the result.
4510
+ */
4511
+ round: (input) => {
4512
+ return applyMathFunc(Math.round, input);
4513
+ },
4514
+ /**
4515
+ * Returns the square root of the input number as a Decimal.
4516
+ *
4517
+ * If the square root cannot be represented (such as the square root of -1), the result is empty.
4518
+ *
4519
+ * If the input collection is empty, the result is empty.
4520
+ *
4521
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4522
+ *
4523
+ * Note that this function is equivalent to raising a number of the power of 0.5 using the power() function.
4524
+ *
4525
+ * See: https://hl7.org/fhirpath/#sqrt-decimal
4526
+ *
4527
+ * @param input The input collection.
4528
+ * @returns A collection containing the result.
4529
+ */
4530
+ sqrt: (input) => {
4531
+ return applyMathFunc(Math.sqrt, input);
4532
+ },
4533
+ /**
4534
+ * Returns the integer portion of the input.
4535
+ *
4536
+ * If the input collection is empty, the result is empty.
4537
+ *
4538
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4539
+ *
4540
+ * See: https://hl7.org/fhirpath/#truncate-integer
4541
+ *
4542
+ * @param input The input collection.
4543
+ * @returns A collection containing the result.
4544
+ */
4545
+ truncate: (input) => {
4546
+ return applyMathFunc((x) => x | 0, input);
4547
+ },
4548
+ /*
4549
+ * 5.8. Tree navigation
4550
+ */
4551
+ children: stub,
4552
+ descendants: stub,
4553
+ /*
4554
+ * 5.9. Utility functions
4555
+ */
4556
+ /**
4557
+ * Adds a String representation of the input collection to the diagnostic log,
4558
+ * using the name argument as the name in the log. This log should be made available
4559
+ * to the user in some appropriate fashion. Does not change the input, so returns
4560
+ * the input collection as output.
4561
+ *
4562
+ * If the projection argument is used, the trace would log the result of evaluating
4563
+ * the project expression on the input, but still return the input to the trace
4564
+ * function unchanged.
4565
+ *
4566
+ * See: https://hl7.org/fhirpath/#tracename-string-projection-expression-collection
4567
+ *
4568
+ * @param input The input collection.
4569
+ * @param nameAtom The log name.
4570
+ */
4571
+ trace: (input, nameAtom) => {
4572
+ console.log('trace', input, nameAtom);
4573
+ return input;
4574
+ },
4575
+ /**
4576
+ * Returns the current date and time, including timezone offset.
4577
+ *
4578
+ * See: https://hl7.org/fhirpath/#now-datetime
4579
+ */
4580
+ now: () => {
4581
+ return [{ type: exports.PropertyType.dateTime, value: new Date().toISOString() }];
4582
+ },
4583
+ /**
4584
+ * Returns the current time.
4585
+ *
4586
+ * See: https://hl7.org/fhirpath/#timeofday-time
4587
+ */
4588
+ timeOfDay: () => {
4589
+ return [{ type: exports.PropertyType.time, value: new Date().toISOString().substring(11) }];
4590
+ },
4591
+ /**
4592
+ * Returns the current date.
4593
+ *
4594
+ * See: https://hl7.org/fhirpath/#today-date
4595
+ */
4596
+ today: () => {
4597
+ return [{ type: exports.PropertyType.date, value: new Date().toISOString().substring(0, 10) }];
4598
+ },
4599
+ /**
4600
+ * Calculates the difference between two dates or date/times.
4601
+ *
4602
+ * This is not part of the official FHIRPath spec.
4603
+ *
4604
+ * IBM FHIR issue: https://github.com/IBM/FHIR/issues/1014
4605
+ * IBM FHIR PR: https://github.com/IBM/FHIR/pull/1023
4606
+ */
4607
+ between: (context, startAtom, endAtom, unitsAtom) => {
4608
+ const startDate = functions.toDateTime(startAtom.eval(context));
4609
+ if (startDate.length === 0) {
4610
+ throw new Error('Invalid start date');
4554
4611
  }
4555
- if (typeof value === 'number') {
4556
- return { namespace: 'System', name: 'Integer' };
4612
+ const endDate = functions.toDateTime(endAtom.eval(context));
4613
+ if (endDate.length === 0) {
4614
+ throw new Error('Invalid end date');
4557
4615
  }
4558
- if (value && typeof value === 'object' && 'resourceType' in value) {
4559
- return { namespace: 'FHIR', name: value.resourceType };
4616
+ const unit = unitsAtom.eval(context)[0].value;
4617
+ if (unit !== 'years' && unit !== 'months' && unit !== 'days') {
4618
+ throw new Error('Invalid units');
4560
4619
  }
4561
- return null;
4562
- });
4563
- }
4564
- function conformsTo(input, systemAtom) {
4565
- const system = systemAtom.eval(undefined);
4566
- if (!system.startsWith('http://hl7.org/fhir/StructureDefinition/')) {
4567
- throw new Error('Expected a StructureDefinition URL');
4568
- }
4569
- const expectedResourceType = system.replace('http://hl7.org/fhir/StructureDefinition/', '');
4570
- return input.map((resource) => (resource === null || resource === void 0 ? void 0 : resource.resourceType) === expectedResourceType);
4571
- }
4620
+ const age = calculateAge(startDate[0].value, endDate[0].value);
4621
+ return [{ type: exports.PropertyType.Quantity, value: { value: age[unit], unit } }];
4622
+ },
4623
+ /*
4624
+ * 6.3 Types
4625
+ */
4626
+ /**
4627
+ * The is() function is supported for backwards compatibility with previous
4628
+ * implementations of FHIRPath. Just as with the is keyword, the type argument
4629
+ * is an identifier that must resolve to the name of a type in a model.
4630
+ *
4631
+ * For implementations with compile-time typing, this requires special-case
4632
+ * handling when processing the argument to treat it as a type specifier rather
4633
+ * than an identifier expression:
4634
+ *
4635
+ * @param input
4636
+ * @param typeAtom
4637
+ * @returns
4638
+ */
4639
+ is: (input, typeAtom) => {
4640
+ let typeName = '';
4641
+ if (typeAtom instanceof SymbolAtom) {
4642
+ typeName = typeAtom.name;
4643
+ }
4644
+ else if (typeAtom instanceof DotAtom) {
4645
+ typeName = typeAtom.left.name + '.' + typeAtom.right.name;
4646
+ }
4647
+ if (!typeName) {
4648
+ return [];
4649
+ }
4650
+ return input.map((value) => ({ type: exports.PropertyType.boolean, value: fhirPathIs(value, typeName) }));
4651
+ },
4652
+ /*
4653
+ * 6.5 Boolean logic
4654
+ */
4655
+ /**
4656
+ * 6.5.3. not() : Boolean
4657
+ *
4658
+ * Returns true if the input collection evaluates to false, and false if it evaluates to true. Otherwise, the result is empty ({ }):
4659
+ *
4660
+ * @param input
4661
+ * @returns
4662
+ */
4663
+ not: (input) => {
4664
+ return functions.toBoolean(input).map((value) => ({ type: exports.PropertyType.boolean, value: !value.value }));
4665
+ },
4666
+ /*
4667
+ * Additional functions
4668
+ * See: https://hl7.org/fhir/fhirpath.html#functions
4669
+ */
4670
+ /**
4671
+ * For each item in the collection, if it is a string that is a uri (or canonical or url), locate the target of the reference, and add it to the resulting collection. If the item does not resolve to a resource, the item is ignored and nothing is added to the output collection.
4672
+ * The items in the collection may also represent a Reference, in which case the Reference.reference is resolved.
4673
+ * @param input The input collection.
4674
+ * @returns
4675
+ */
4676
+ resolve: (input) => {
4677
+ return input
4678
+ .map((e) => {
4679
+ const value = e.value;
4680
+ let refStr;
4681
+ if (typeof value === 'string') {
4682
+ refStr = value;
4683
+ }
4684
+ else if (typeof value === 'object') {
4685
+ const ref = value;
4686
+ if (ref.resource) {
4687
+ return { type: exports.PropertyType.BackboneElement, value: ref.resource };
4688
+ }
4689
+ refStr = ref.reference;
4690
+ }
4691
+ if (!refStr) {
4692
+ return { type: exports.PropertyType.BackboneElement, value: null };
4693
+ }
4694
+ const [resourceType, id] = refStr.split('/');
4695
+ return { type: exports.PropertyType.BackboneElement, value: { resourceType, id } };
4696
+ })
4697
+ .filter((e) => !!e.value);
4698
+ },
4699
+ /**
4700
+ * The as operator can be used to treat a value as a specific type.
4701
+ * @param context The context value.
4702
+ * @returns The value as the specific type.
4703
+ */
4704
+ as: (context) => {
4705
+ return context;
4706
+ },
4707
+ /*
4708
+ * 12. Formal Specifications
4709
+ */
4710
+ /**
4711
+ * Returns the type of the input.
4712
+ *
4713
+ * 12.2. Model Information
4714
+ *
4715
+ * The model information returned by the reflection function type() is specified as an
4716
+ * XML Schema document (xsd) and included in this specification at the following link:
4717
+ * https://hl7.org/fhirpath/modelinfo.xsd
4718
+ *
4719
+ * See: https://hl7.org/fhirpath/#model-information
4720
+ *
4721
+ * @param input The input collection.
4722
+ * @returns
4723
+ */
4724
+ type: (input) => {
4725
+ return input.map(({ value }) => {
4726
+ if (typeof value === 'boolean') {
4727
+ return { type: exports.PropertyType.BackboneElement, value: { namespace: 'System', name: 'Boolean' } };
4728
+ }
4729
+ if (typeof value === 'number') {
4730
+ return { type: exports.PropertyType.BackboneElement, value: { namespace: 'System', name: 'Integer' } };
4731
+ }
4732
+ if (value && typeof value === 'object' && 'resourceType' in value) {
4733
+ return {
4734
+ type: exports.PropertyType.BackboneElement,
4735
+ value: { namespace: 'FHIR', name: value.resourceType },
4736
+ };
4737
+ }
4738
+ return { type: exports.PropertyType.BackboneElement, value: null };
4739
+ });
4740
+ },
4741
+ conformsTo: (input, systemAtom) => {
4742
+ const system = systemAtom.eval([])[0].value;
4743
+ if (!system.startsWith('http://hl7.org/fhir/StructureDefinition/')) {
4744
+ throw new Error('Expected a StructureDefinition URL');
4745
+ }
4746
+ const expectedResourceType = system.replace('http://hl7.org/fhir/StructureDefinition/', '');
4747
+ return input.map((value) => {
4748
+ var _a;
4749
+ return ({
4750
+ type: exports.PropertyType.boolean,
4751
+ value: ((_a = value.value) === null || _a === void 0 ? void 0 : _a.resourceType) === expectedResourceType,
4752
+ });
4753
+ });
4754
+ },
4755
+ };
4572
4756
  /*
4573
4757
  * Helper utilities
4574
4758
  */
@@ -4576,115 +4760,46 @@
4576
4760
  if (input.length === 0) {
4577
4761
  return [];
4578
4762
  }
4579
- const [value] = validateInput(input, 1);
4763
+ const [{ value }] = validateInput(input, 1);
4580
4764
  if (typeof value !== 'string') {
4581
4765
  throw new Error('String function cannot be called with non-string');
4582
4766
  }
4583
- const result = func(value, ...argsAtoms.map((atom) => atom && atom.eval(value)));
4584
- return result === undefined ? [] : [result];
4767
+ const result = func(value, ...argsAtoms.map((atom) => { var _a, _b; return atom && ((_b = (_a = atom.eval(input)) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.value); }));
4768
+ if (result === undefined) {
4769
+ return [];
4770
+ }
4771
+ if (Array.isArray(result)) {
4772
+ return result.map(toTypedValue);
4773
+ }
4774
+ return [toTypedValue(result)];
4585
4775
  }
4586
4776
  function applyMathFunc(func, input, ...argsAtoms) {
4587
4777
  if (input.length === 0) {
4588
4778
  return [];
4589
4779
  }
4590
- const [value] = validateInput(input, 1);
4780
+ const [{ value }] = validateInput(input, 1);
4591
4781
  const quantity = isQuantity(value);
4592
4782
  const numberInput = quantity ? value.value : value;
4593
4783
  if (typeof numberInput !== 'number') {
4594
4784
  throw new Error('Math function cannot be called with non-number');
4595
4785
  }
4596
- const result = func(numberInput, ...argsAtoms.map((atom) => atom.eval(undefined)));
4597
- return quantity ? [Object.assign(Object.assign({}, value), { value: result })] : [result];
4786
+ const result = func(numberInput, ...argsAtoms.map((atom) => { var _a, _b; return (_b = (_a = atom.eval([])) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.value; }));
4787
+ const type = quantity ? exports.PropertyType.Quantity : input[0].type;
4788
+ const returnValue = quantity ? Object.assign(Object.assign({}, value), { value: result }) : result;
4789
+ return [{ type, value: returnValue }];
4598
4790
  }
4599
4791
  function validateInput(input, count) {
4600
4792
  if (input.length !== count) {
4601
4793
  throw new Error(`Expected ${count} arguments`);
4602
4794
  }
4795
+ for (const element of input) {
4796
+ if (element === null || element === undefined) {
4797
+ throw new Error('Expected non-null argument');
4798
+ }
4799
+ }
4603
4800
  return input;
4604
4801
  }
4605
4802
 
4606
- var functions = /*#__PURE__*/Object.freeze({
4607
- __proto__: null,
4608
- empty: empty,
4609
- exists: exists,
4610
- all: all,
4611
- allTrue: allTrue,
4612
- anyTrue: anyTrue,
4613
- allFalse: allFalse,
4614
- anyFalse: anyFalse,
4615
- subsetOf: subsetOf,
4616
- supersetOf: supersetOf,
4617
- count: count,
4618
- distinct: distinct,
4619
- isDistinct: isDistinct,
4620
- where: where,
4621
- select: select,
4622
- repeat: repeat,
4623
- ofType: ofType,
4624
- single: single,
4625
- first: first,
4626
- last: last,
4627
- tail: tail,
4628
- skip: skip,
4629
- take: take,
4630
- intersect: intersect,
4631
- exclude: exclude,
4632
- union: union,
4633
- combine: combine,
4634
- iif: iif,
4635
- toBoolean: toBoolean,
4636
- convertsToBoolean: convertsToBoolean,
4637
- toInteger: toInteger,
4638
- convertsToInteger: convertsToInteger,
4639
- toDate: toDate,
4640
- convertsToDate: convertsToDate,
4641
- toDateTime: toDateTime,
4642
- convertsToDateTime: convertsToDateTime,
4643
- toDecimal: toDecimal,
4644
- convertsToDecimal: convertsToDecimal,
4645
- toQuantity: toQuantity,
4646
- convertsToQuantity: convertsToQuantity,
4647
- toString: toString,
4648
- convertsToString: convertsToString,
4649
- toTime: toTime,
4650
- convertsToTime: convertsToTime,
4651
- indexOf: indexOf,
4652
- substring: substring,
4653
- startsWith: startsWith,
4654
- endsWith: endsWith,
4655
- contains: contains,
4656
- upper: upper,
4657
- lower: lower,
4658
- replace: replace,
4659
- matches: matches,
4660
- replaceMatches: replaceMatches,
4661
- length: length,
4662
- toChars: toChars,
4663
- abs: abs,
4664
- ceiling: ceiling,
4665
- exp: exp,
4666
- floor: floor,
4667
- ln: ln,
4668
- log: log,
4669
- power: power,
4670
- round: round,
4671
- sqrt: sqrt,
4672
- truncate: truncate,
4673
- children: children,
4674
- descendants: descendants,
4675
- trace: trace,
4676
- now: now,
4677
- timeOfDay: timeOfDay,
4678
- today: today,
4679
- between: between,
4680
- is: is,
4681
- not: not,
4682
- resolve: resolve,
4683
- as: as,
4684
- type: type,
4685
- conformsTo: conformsTo
4686
- });
4687
-
4688
4803
  var _Tokenizer_instances, _Tokenizer_str, _Tokenizer_pos, _Tokenizer_peekToken, _Tokenizer_consumeToken, _Tokenizer_consumeWhitespace, _Tokenizer_consumeMultiLineComment, _Tokenizer_consumeSingleLineComment, _Tokenizer_consumeString, _Tokenizer_consumeBacktickSymbol, _Tokenizer_consumeDateTime, _Tokenizer_consumeNumber, _Tokenizer_consumeSymbol, _Tokenizer_consumeOperator, _Tokenizer_consumeWhile, _Tokenizer_curr, _Tokenizer_prev, _Tokenizer_peek;
4689
4804
  function tokenize(str) {
4690
4805
  return new Tokenizer(str).tokenize();
@@ -5003,24 +5118,24 @@
5003
5118
  }
5004
5119
  const parserBuilder = new ParserBuilder()
5005
5120
  .registerPrefix('String', {
5006
- parse: (_, token) => new LiteralAtom(token.value),
5121
+ parse: (_, token) => new LiteralAtom({ type: exports.PropertyType.string, value: token.value }),
5007
5122
  })
5008
5123
  .registerPrefix('DateTime', {
5009
- parse: (_, token) => new LiteralAtom(parseDateString(token.value)),
5124
+ parse: (_, token) => new LiteralAtom({ type: exports.PropertyType.dateTime, value: parseDateString(token.value) }),
5010
5125
  })
5011
5126
  .registerPrefix('Quantity', {
5012
- parse: (_, token) => new LiteralAtom(parseQuantity(token.value)),
5127
+ parse: (_, token) => new LiteralAtom({ type: exports.PropertyType.Quantity, value: parseQuantity(token.value) }),
5013
5128
  })
5014
5129
  .registerPrefix('Number', {
5015
- parse: (_, token) => new LiteralAtom(parseFloat(token.value)),
5130
+ parse: (_, token) => new LiteralAtom({ type: exports.PropertyType.decimal, value: parseFloat(token.value) }),
5016
5131
  })
5017
5132
  .registerPrefix('Symbol', {
5018
5133
  parse: (_, token) => {
5019
5134
  if (token.value === 'false') {
5020
- return new LiteralAtom(false);
5135
+ return new LiteralAtom({ type: exports.PropertyType.boolean, value: false });
5021
5136
  }
5022
5137
  if (token.value === 'true') {
5023
- return new LiteralAtom(true);
5138
+ return new LiteralAtom({ type: exports.PropertyType.boolean, value: true });
5024
5139
  }
5025
5140
  return new SymbolAtom(token.value);
5026
5141
  },
@@ -5041,10 +5156,10 @@
5041
5156
  .infixLeft('!=', 9 /* Precedence.Equals */, (left, _, right) => new NotEqualsAtom(left, right))
5042
5157
  .infixLeft('~', 9 /* Precedence.Equivalent */, (left, _, right) => new EquivalentAtom(left, right))
5043
5158
  .infixLeft('!~', 9 /* Precedence.NotEquivalent */, (left, _, right) => new NotEquivalentAtom(left, right))
5044
- .infixLeft('<', 8 /* Precedence.LessThan */, (left, _, right) => new ComparisonOperatorAtom(left, right, (x, y) => x < y))
5045
- .infixLeft('<=', 8 /* Precedence.LessThanOrEquals */, (left, _, right) => new ComparisonOperatorAtom(left, right, (x, y) => x <= y))
5046
- .infixLeft('>', 8 /* Precedence.GreaterThan */, (left, _, right) => new ComparisonOperatorAtom(left, right, (x, y) => x > y))
5047
- .infixLeft('>=', 8 /* Precedence.GreaterThanOrEquals */, (left, _, right) => new ComparisonOperatorAtom(left, right, (x, y) => x >= y))
5159
+ .infixLeft('<', 8 /* Precedence.LessThan */, (left, _, right) => new ArithemticOperatorAtom(left, right, (x, y) => x < y))
5160
+ .infixLeft('<=', 8 /* Precedence.LessThanOrEquals */, (left, _, right) => new ArithemticOperatorAtom(left, right, (x, y) => x <= y))
5161
+ .infixLeft('>', 8 /* Precedence.GreaterThan */, (left, _, right) => new ArithemticOperatorAtom(left, right, (x, y) => x > y))
5162
+ .infixLeft('>=', 8 /* Precedence.GreaterThanOrEquals */, (left, _, right) => new ArithemticOperatorAtom(left, right, (x, y) => x >= y))
5048
5163
  .infixLeft('&', 5 /* Precedence.Ampersand */, (left, _, right) => new ConcatAtom(left, right))
5049
5164
  .infixLeft('Symbol', 6 /* Precedence.Is */, (left, symbol, right) => {
5050
5165
  switch (symbol.value) {
@@ -5093,7 +5208,21 @@
5093
5208
  * @returns The result of the FHIRPath expression against the resource or object.
5094
5209
  */
5095
5210
  function evalFhirPath(input, context) {
5096
- return parseFhirPath(input).eval(context);
5211
+ // eval requires a TypedValue array
5212
+ // As a convenience, we can accept array or non-array, and TypedValue or unknown value
5213
+ if (!Array.isArray(context)) {
5214
+ context = [context];
5215
+ }
5216
+ const array = Array.isArray(context) ? context : [context];
5217
+ for (let i = 0; i < array.length; i++) {
5218
+ const el = array[i];
5219
+ if (!(typeof el === 'object' && 'type' in el && 'value' in el)) {
5220
+ array[i] = { type: exports.PropertyType.BackboneElement, value: el };
5221
+ }
5222
+ }
5223
+ return parseFhirPath(input)
5224
+ .eval(array)
5225
+ .map((e) => e.value);
5097
5226
  }
5098
5227
 
5099
5228
  const SEGMENT_SEPARATOR = '\r';
@@ -5286,7 +5415,12 @@
5286
5415
  let type = exports.SearchParameterType.TEXT;
5287
5416
  switch (searchParam.type) {
5288
5417
  case 'date':
5289
- type = exports.SearchParameterType.DATE;
5418
+ if (propertyType === exports.PropertyType.dateTime || propertyType === exports.PropertyType.instant) {
5419
+ type = exports.SearchParameterType.DATETIME;
5420
+ }
5421
+ else {
5422
+ type = exports.SearchParameterType.DATE;
5423
+ }
5290
5424
  break;
5291
5425
  case 'number':
5292
5426
  type = exports.SearchParameterType.NUMBER;