@medplum/core 0.9.7 → 0.9.8

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