@medplum/core 0.9.7 → 0.9.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/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,106 @@ 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 window !== 'undefined' && typeof pdfMake !== 'undefined') {
824
+ return generatePdfClientSide(docDefinition, tableLayouts, fonts);
825
+ }
826
+ if (typeof process !== 'undefined' && typeof require !== 'undefined') {
827
+ return generatePdfServerSide(docDefinition, tableLayouts, fonts);
828
+ }
829
+ throw new Error('Unable to determine PDF environment');
830
+ });
831
+ }
832
+ function generatePdfServerSide(docDefinition, tableLayouts, fonts) {
833
+ return __awaiter(this, void 0, void 0, function* () {
834
+ if (!fonts) {
835
+ fonts = {
836
+ Helvetica: {
837
+ normal: 'Helvetica',
838
+ bold: 'Helvetica-Bold',
839
+ italics: 'Helvetica-Oblique',
840
+ bolditalics: 'Helvetica-BoldOblique',
841
+ },
842
+ };
843
+ }
844
+ return new Promise((resolve, reject) => {
845
+ try {
846
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
847
+ const PdfPrinter = require('pdfmake');
848
+ const printer = new PdfPrinter(fonts);
849
+ const pdfDoc = printer.createPdfKitDocument(docDefinition, { tableLayouts });
850
+ const chunks = [];
851
+ pdfDoc.on('data', (chunk) => chunks.push(chunk));
852
+ pdfDoc.on('end', () => resolve(concat(chunks)));
853
+ pdfDoc.on('error', reject);
854
+ pdfDoc.end();
855
+ }
856
+ catch (err) {
857
+ reject(err);
858
+ }
859
+ });
860
+ });
861
+ }
862
+ function generatePdfClientSide(docDefinition, tableLayouts, fonts) {
863
+ return __awaiter(this, void 0, void 0, function* () {
864
+ if (!fonts) {
865
+ fonts = {
866
+ Helvetica: {
867
+ normal: 'https://static.medplum.com/fonts/Helvetica.ttf',
868
+ bold: 'https://static.medplum.com/fonts/Helvetica-bold.ttf',
869
+ italics: 'https://static.medplum.com/fonts/Helvetica-italic.ttf',
870
+ bolditalics: 'https://static.medplum.com/fonts/Helvetica-bold-italic.ttf',
871
+ },
872
+ Roboto: {
873
+ normal: 'https://static.medplum.com/fonts/Roboto-Regular.ttf',
874
+ bold: 'https://static.medplum.com/fonts/Roboto-Medium.ttf',
875
+ italics: 'https://static.medplum.com/fonts/Roboto-Italic.ttf',
876
+ bolditalics: 'https://static.medplum.com/fonts/Roboto-MediumItalic.ttf',
877
+ },
878
+ Avenir: {
879
+ normal: 'https://static.medplum.com/fonts/avenir.ttf',
880
+ },
881
+ };
882
+ }
883
+ return new Promise((resolve) => {
884
+ pdfMake.createPdf(docDefinition, tableLayouts, fonts).getBlob(resolve);
885
+ });
886
+ });
887
+ }
888
+ /**
889
+ * Concatenates an array of Uint8Arrays into a single Uint8Array.
890
+ * @param arrays An array of arrays of bytes.
891
+ * @returns A single array of bytes.
892
+ */
893
+ function concat(arrays) {
894
+ let len = 0;
895
+ for (const array of arrays) {
896
+ len += array.length;
897
+ }
898
+ const result = new Uint8Array(len);
899
+ let index = 0;
900
+ for (const array of arrays) {
901
+ result.set(array, index);
902
+ index += array.length;
903
+ }
904
+ return result;
905
+ }
906
+
783
907
  var _ReadablePromise_suspender, _ReadablePromise_status, _ReadablePromise_response, _ReadablePromise_error, _a;
784
908
  /**
785
909
  * The ReadablePromise class wraps a request promise suitable for React Suspense.
@@ -1466,6 +1590,26 @@ class MedplumClient extends EventTarget {
1466
1590
  __classPrivateFieldSet(this, _MedplumClient_config, undefined, "f");
1467
1591
  this.dispatchEvent({ type: 'change' });
1468
1592
  }
1593
+ /**
1594
+ * Invalidates any cached values or cached requests for the given URL.
1595
+ * @param url The URL to invalidate.
1596
+ */
1597
+ invalidateUrl(url) {
1598
+ url = url.toString();
1599
+ __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").delete(url);
1600
+ }
1601
+ /**
1602
+ * Invalidates all cached search results or cached requests for the given resourceType.
1603
+ * @param resourceType The resource type to invalidate.
1604
+ */
1605
+ invalidateSearches(resourceType) {
1606
+ const url = 'fhir/R4/' + resourceType;
1607
+ for (const key of __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").keys()) {
1608
+ if (key.endsWith(url) || key.includes(url + '?')) {
1609
+ __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").delete(key);
1610
+ }
1611
+ }
1612
+ }
1469
1613
  /**
1470
1614
  * Makes an HTTP GET request to the specified URL.
1471
1615
  *
@@ -1510,7 +1654,7 @@ class MedplumClient extends EventTarget {
1510
1654
  if (contentType) {
1511
1655
  __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_setRequestContentType).call(this, options, contentType);
1512
1656
  }
1513
- __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").delete(url);
1657
+ this.invalidateUrl(url);
1514
1658
  return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_request).call(this, 'POST', url, options);
1515
1659
  }
1516
1660
  /**
@@ -1534,7 +1678,7 @@ class MedplumClient extends EventTarget {
1534
1678
  if (contentType) {
1535
1679
  __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_setRequestContentType).call(this, options, contentType);
1536
1680
  }
1537
- __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").delete(url);
1681
+ this.invalidateUrl(url);
1538
1682
  return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_request).call(this, 'PUT', url, options);
1539
1683
  }
1540
1684
  /**
@@ -1553,7 +1697,7 @@ class MedplumClient extends EventTarget {
1553
1697
  url = url.toString();
1554
1698
  __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_setRequestBody).call(this, options, operations);
1555
1699
  __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_setRequestContentType).call(this, options, PATCH_CONTENT_TYPE);
1556
- __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").delete(url);
1700
+ this.invalidateUrl(url);
1557
1701
  return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_request).call(this, 'PATCH', url, options);
1558
1702
  }
1559
1703
  /**
@@ -1569,7 +1713,7 @@ class MedplumClient extends EventTarget {
1569
1713
  */
1570
1714
  delete(url, options = {}) {
1571
1715
  url = url.toString();
1572
- __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").delete(url);
1716
+ this.invalidateUrl(url);
1573
1717
  return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_request).call(this, 'DELETE', url, options);
1574
1718
  }
1575
1719
  /**
@@ -1647,6 +1791,19 @@ class MedplumClient extends EventTarget {
1647
1791
  fhirUrl(...path) {
1648
1792
  return new URL(__classPrivateFieldGet(this, _MedplumClient_baseUrl, "f") + 'fhir/R4/' + path.join('/'));
1649
1793
  }
1794
+ /**
1795
+ * Builds a FHIR search URL from a search query or structured query object.
1796
+ * @param query The FHIR search query or structured query object.
1797
+ * @returns The well-formed FHIR URL.
1798
+ */
1799
+ fhirSearchUrl(query) {
1800
+ if (typeof query === 'string') {
1801
+ return this.fhirUrl(query);
1802
+ }
1803
+ const url = this.fhirUrl(query.resourceType);
1804
+ url.search = formatSearchQuery(query);
1805
+ return url;
1806
+ }
1650
1807
  /**
1651
1808
  * Sends a FHIR search request.
1652
1809
  *
@@ -1702,7 +1859,7 @@ class MedplumClient extends EventTarget {
1702
1859
  * @returns Promise to the search result bundle.
1703
1860
  */
1704
1861
  search(query, options = {}) {
1705
- return this.get(typeof query === 'string' ? 'fhir/R4/' + query : this.fhirUrl(query.resourceType) + formatSearchQuery(query), options);
1862
+ return this.get(this.fhirSearchUrl(query), options);
1706
1863
  }
1707
1864
  /**
1708
1865
  * Sends a FHIR search request for a single resource.
@@ -1726,7 +1883,16 @@ class MedplumClient extends EventTarget {
1726
1883
  searchOne(query, options = {}) {
1727
1884
  const search = typeof query === 'string' ? parseSearchDefinition(query) : query;
1728
1885
  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; }));
1886
+ const cacheKey = this.fhirSearchUrl(query).toString() + '-searchOne';
1887
+ if (!(options === null || options === void 0 ? void 0 : options.cache)) {
1888
+ const cached = __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").get(cacheKey);
1889
+ if (cached) {
1890
+ return cached;
1891
+ }
1892
+ }
1893
+ 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; }));
1894
+ __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").set(cacheKey, promise);
1895
+ return promise;
1730
1896
  }
1731
1897
  /**
1732
1898
  * Sends a FHIR search request for an array of resources.
@@ -1748,7 +1914,16 @@ class MedplumClient extends EventTarget {
1748
1914
  * @returns Promise to the search result bundle.
1749
1915
  */
1750
1916
  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 : []; }));
1917
+ const cacheKey = this.fhirSearchUrl(query).toString() + '-searchResources';
1918
+ if (!(options === null || options === void 0 ? void 0 : options.cache)) {
1919
+ const cached = __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").get(cacheKey);
1920
+ if (cached) {
1921
+ return cached;
1922
+ }
1923
+ }
1924
+ 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 : []; }));
1925
+ __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").set(cacheKey, promise);
1926
+ return promise;
1752
1927
  }
1753
1928
  /**
1754
1929
  * Searches a ValueSet resource using the "expand" operation.
@@ -1803,27 +1978,6 @@ class MedplumClient extends EventTarget {
1803
1978
  readResource(resourceType, id) {
1804
1979
  return this.get(this.fhirUrl(resourceType, id));
1805
1980
  }
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
1981
  /**
1828
1982
  * Reads a resource by `Reference`.
1829
1983
  *
@@ -1850,34 +2004,6 @@ class MedplumClient extends EventTarget {
1850
2004
  const [resourceType, id] = refString.split('/');
1851
2005
  return this.readResource(resourceType, id);
1852
2006
  }
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
2007
  /**
1882
2008
  * Returns a cached schema for a resource type.
1883
2009
  * If the schema is not cached, returns undefined.
@@ -2008,6 +2134,7 @@ class MedplumClient extends EventTarget {
2008
2134
  if (!resource.resourceType) {
2009
2135
  throw new Error('Missing resourceType');
2010
2136
  }
2137
+ this.invalidateSearches(resource.resourceType);
2011
2138
  return this.post(this.fhirUrl(resource.resourceType), resource);
2012
2139
  }
2013
2140
  /**
@@ -2102,15 +2229,14 @@ class MedplumClient extends EventTarget {
2102
2229
  *
2103
2230
  * See the pdfmake document definition for full details: https://pdfmake.github.io/docs/0.1/document-definition-object/
2104
2231
  *
2105
- * @param docDefinition The FHIR resource to create.
2232
+ * @param docDefinition The PDF document definition.
2106
2233
  * @returns The result of the create operation.
2107
2234
  */
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');
2235
+ createPdf(docDefinition, filename, tableLayouts, fonts) {
2236
+ return __awaiter(this, void 0, void 0, function* () {
2237
+ const blob = yield generatePdf(docDefinition, tableLayouts, fonts);
2238
+ return this.createBinary(blob, filename, 'application/pdf');
2239
+ });
2114
2240
  }
2115
2241
  /**
2116
2242
  * Creates a FHIR `Communication` resource with the provided data content.
@@ -2177,6 +2303,7 @@ class MedplumClient extends EventTarget {
2177
2303
  if (!resource.id) {
2178
2304
  throw new Error('Missing id');
2179
2305
  }
2306
+ this.invalidateSearches(resource.resourceType);
2180
2307
  return this.put(this.fhirUrl(resource.resourceType, resource.id), resource);
2181
2308
  }
2182
2309
  /**
@@ -2203,6 +2330,7 @@ class MedplumClient extends EventTarget {
2203
2330
  * @returns The result of the patch operations.
2204
2331
  */
2205
2332
  patchResource(resourceType, id, operations) {
2333
+ this.invalidateSearches(resourceType);
2206
2334
  return this.patch(this.fhirUrl(resourceType, id), operations);
2207
2335
  }
2208
2336
  /**
@@ -2221,6 +2349,7 @@ class MedplumClient extends EventTarget {
2221
2349
  * @returns The result of the delete operation.
2222
2350
  */
2223
2351
  deleteResource(resourceType, id) {
2352
+ this.invalidateSearches(resourceType);
2224
2353
  return this.delete(this.fhirUrl(resourceType, id));
2225
2354
  }
2226
2355
  /**
@@ -2436,7 +2565,10 @@ _MedplumClient_fetch = new WeakMap(), _MedplumClient_storage = new WeakMap(), _M
2436
2565
  const headers = options.headers;
2437
2566
  headers['Content-Type'] = contentType;
2438
2567
  }, _MedplumClient_setRequestBody = function _MedplumClient_setRequestBody(options, data) {
2439
- if (typeof data === 'string' || (typeof File !== 'undefined' && data instanceof File)) {
2568
+ if (typeof data === 'string' ||
2569
+ (typeof Blob !== 'undefined' && data instanceof Blob) ||
2570
+ (typeof File !== 'undefined' && data instanceof File) ||
2571
+ (typeof Uint8Array !== 'undefined' && data instanceof Uint8Array)) {
2440
2572
  options.body = data;
2441
2573
  }
2442
2574
  else if (data) {
@@ -2551,47 +2683,38 @@ function getBaseUrl() {
2551
2683
  }
2552
2684
 
2553
2685
  /**
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.
2686
+ * Returns a single element array with a typed boolean value.
2687
+ * @param value The primitive boolean value.
2688
+ * @returns Single element array with a typed boolean value.
2557
2689
  */
2558
- function ensureArray(input) {
2559
- if (input === null || input === undefined) {
2560
- return [];
2561
- }
2562
- return Array.isArray(input) ? input : [input];
2690
+ function booleanToTypedValue(value) {
2691
+ return [{ type: PropertyType.boolean, value }];
2563
2692
  }
2564
2693
  /**
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.
2694
+ * Returns a "best guess" TypedValue for a given value.
2695
+ * @param value The unknown value to check.
2696
+ * @returns A "best guess" TypedValue for the given value.
2569
2697
  */
2570
- function applyMaybeArray(context, fn) {
2571
- if (context === undefined) {
2572
- return undefined;
2698
+ function toTypedValue(value) {
2699
+ if (Number.isSafeInteger(value)) {
2700
+ return { type: PropertyType.integer, value };
2573
2701
  }
2574
- if (Array.isArray(context)) {
2575
- return context
2576
- .map((e) => fn(e))
2577
- .filter((e) => !!e)
2578
- .flat();
2702
+ else if (typeof value === 'number') {
2703
+ return { type: PropertyType.decimal, value };
2704
+ }
2705
+ else if (typeof value === 'boolean') {
2706
+ return { type: PropertyType.boolean, value };
2707
+ }
2708
+ else if (typeof value === 'string') {
2709
+ return { type: PropertyType.string, value };
2710
+ }
2711
+ else if (isQuantity(value)) {
2712
+ return { type: PropertyType.Quantity, value };
2579
2713
  }
2580
2714
  else {
2581
- return fn(context);
2715
+ return { type: PropertyType.BackboneElement, value };
2582
2716
  }
2583
2717
  }
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
2718
  /**
2596
2719
  * Converts unknown object into a JavaScript boolean.
2597
2720
  * Note that this is different than the FHIRPath "toBoolean",
@@ -2600,10 +2723,7 @@ function isFalsy(obj) {
2600
2723
  * @returns The converted boolean value according to FHIRPath rules.
2601
2724
  */
2602
2725
  function toJsBoolean(obj) {
2603
- if (Array.isArray(obj)) {
2604
- return obj.length === 0 ? false : !!obj[0];
2605
- }
2606
- return !!obj;
2726
+ return obj.length === 0 ? false : !!obj[0].value;
2607
2727
  }
2608
2728
  /**
2609
2729
  * Removes duplicates in array using FHIRPath equality rules.
@@ -2615,7 +2735,7 @@ function removeDuplicates(arr) {
2615
2735
  for (const i of arr) {
2616
2736
  let found = false;
2617
2737
  for (const j of result) {
2618
- if (fhirPathEquals(i, j)) {
2738
+ if (toJsBoolean(fhirPathEquals(i, j))) {
2619
2739
  found = true;
2620
2740
  break;
2621
2741
  }
@@ -2626,6 +2746,29 @@ function removeDuplicates(arr) {
2626
2746
  }
2627
2747
  return result;
2628
2748
  }
2749
+ /**
2750
+ * Returns a negated FHIRPath boolean expression.
2751
+ * @param input The input array.
2752
+ * @returns The negated type value array.
2753
+ */
2754
+ function fhirPathNot(input) {
2755
+ return booleanToTypedValue(!toJsBoolean(input));
2756
+ }
2757
+ /**
2758
+ * Determines if two arrays are equal according to FHIRPath equality rules.
2759
+ * @param x The first array.
2760
+ * @param y The second array.
2761
+ * @returns FHIRPath true if the arrays are equal.
2762
+ */
2763
+ function fhirPathArrayEquals(x, y) {
2764
+ if (x.length === 0 || y.length === 0) {
2765
+ return [];
2766
+ }
2767
+ if (x.length !== y.length) {
2768
+ return booleanToTypedValue(false);
2769
+ }
2770
+ return booleanToTypedValue(x.every((val, index) => toJsBoolean(fhirPathEquals(val, y[index]))));
2771
+ }
2629
2772
  /**
2630
2773
  * Determines if two values are equal according to FHIRPath equality rules.
2631
2774
  * @param x The first value.
@@ -2633,69 +2776,89 @@ function removeDuplicates(arr) {
2633
2776
  * @returns True if equal.
2634
2777
  */
2635
2778
  function fhirPathEquals(x, y) {
2636
- if (isFalsy(x) && isFalsy(y)) {
2637
- return true;
2638
- }
2639
- if (isEmptyArray(x) || isEmptyArray(y)) {
2640
- return [];
2779
+ const xValue = x.value;
2780
+ const yValue = y.value;
2781
+ if (typeof xValue === 'number' && typeof yValue === 'number') {
2782
+ return booleanToTypedValue(Math.abs(xValue - yValue) < 1e-8);
2641
2783
  }
2642
- if (typeof x === 'number' && typeof y === 'number') {
2643
- return Math.abs(x - y) < 1e-8;
2784
+ if (isQuantity(xValue) && isQuantity(yValue)) {
2785
+ return booleanToTypedValue(isQuantityEquivalent(xValue, yValue));
2644
2786
  }
2645
- if (isQuantity(x) && isQuantity(y)) {
2646
- return isQuantityEquivalent(x, y);
2787
+ if (typeof xValue === 'object' && typeof yValue === 'object') {
2788
+ return booleanToTypedValue(deepEquals(x, y));
2647
2789
  }
2648
- if (Array.isArray(x) && Array.isArray(y)) {
2649
- return x.length === y.length && x.every((val, index) => fhirPathEquals(val, y[index]));
2790
+ return booleanToTypedValue(xValue === yValue);
2791
+ }
2792
+ /**
2793
+ * Determines if two arrays are equivalent according to FHIRPath equality rules.
2794
+ * @param x The first array.
2795
+ * @param y The second array.
2796
+ * @returns FHIRPath true if the arrays are equivalent.
2797
+ */
2798
+ function fhirPathArrayEquivalent(x, y) {
2799
+ if (x.length === 0 && y.length === 0) {
2800
+ return booleanToTypedValue(true);
2650
2801
  }
2651
- if (typeof x === 'object' && typeof y === 'object') {
2652
- return deepEquals(x, y);
2802
+ if (x.length !== y.length) {
2803
+ return booleanToTypedValue(false);
2653
2804
  }
2654
- return x === y;
2805
+ x.sort(fhirPathEquivalentCompare);
2806
+ y.sort(fhirPathEquivalentCompare);
2807
+ return booleanToTypedValue(x.every((val, index) => toJsBoolean(fhirPathEquivalent(val, y[index]))));
2655
2808
  }
2656
2809
  /**
2657
- * Determines if two values are equal according to FHIRPath equality rules.
2810
+ * Determines if two values are equivalent according to FHIRPath equality rules.
2658
2811
  * @param x The first value.
2659
2812
  * @param y The second value.
2660
- * @returns True if equal.
2813
+ * @returns True if equivalent.
2661
2814
  */
2662
2815
  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') {
2816
+ const xValue = x.value;
2817
+ const yValue = y.value;
2818
+ if (typeof xValue === 'number' && typeof yValue === 'number') {
2672
2819
  // Use more generous threshold than equality
2673
2820
  // Decimal: values must be equal, comparison is done on values rounded to the precision of the least precise operand.
2674
2821
  // Trailing zeroes after the decimal are ignored in determining precision.
2675
- return Math.abs(x - y) < 0.01;
2822
+ return booleanToTypedValue(Math.abs(xValue - yValue) < 0.01);
2676
2823
  }
2677
- if (isQuantity(x) && isQuantity(y)) {
2678
- return isQuantityEquivalent(x, y);
2824
+ if (isQuantity(xValue) && isQuantity(yValue)) {
2825
+ return booleanToTypedValue(isQuantityEquivalent(xValue, yValue));
2679
2826
  }
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]));
2827
+ if (typeof xValue === 'object' && typeof yValue === 'object') {
2828
+ return booleanToTypedValue(deepEquals(xValue, yValue));
2687
2829
  }
2688
- if (typeof x === 'object' && typeof y === 'object') {
2689
- return deepEquals(x, y);
2690
- }
2691
- if (typeof x === 'string' && typeof y === 'string') {
2830
+ if (typeof xValue === 'string' && typeof yValue === 'string') {
2692
2831
  // String: the strings must be the same, ignoring case and locale, and normalizing whitespace
2693
2832
  // (see String Equivalence for more details).
2694
- return x.toLowerCase() === y.toLowerCase();
2833
+ return booleanToTypedValue(xValue.toLowerCase() === yValue.toLowerCase());
2834
+ }
2835
+ return booleanToTypedValue(xValue === yValue);
2836
+ }
2837
+ /**
2838
+ * Returns the sort order of two values for FHIRPath array equivalence.
2839
+ * @param x The first value.
2840
+ * @param y The second value.
2841
+ * @returns The sort order of the values.
2842
+ */
2843
+ function fhirPathEquivalentCompare(x, y) {
2844
+ const xValue = x.value;
2845
+ const yValue = y.value;
2846
+ if (typeof xValue === 'number' && typeof yValue === 'number') {
2847
+ return xValue - yValue;
2848
+ }
2849
+ if (typeof xValue === 'string' && typeof yValue === 'string') {
2850
+ return xValue.localeCompare(yValue);
2695
2851
  }
2696
- return x === y;
2852
+ return 0;
2697
2853
  }
2698
- function fhirPathIs(value, desiredType) {
2854
+ /**
2855
+ * Determines if the typed value is the desired type.
2856
+ * @param typedValue The typed value to check.
2857
+ * @param desiredType The desired type name.
2858
+ * @returns True if the typed value is of the desired type.
2859
+ */
2860
+ function fhirPathIs(typedValue, desiredType) {
2861
+ const { value } = typedValue;
2699
2862
  if (value === undefined || value === null) {
2700
2863
  return false;
2701
2864
  }
@@ -2775,6 +2938,7 @@ function isObject(object) {
2775
2938
  return object !== null && typeof object === 'object';
2776
2939
  }
2777
2940
 
2941
+ var _SymbolAtom_instances, _SymbolAtom_evalValue;
2778
2942
  class FhirPathAtom {
2779
2943
  constructor(original, child) {
2780
2944
  this.original = original;
@@ -2782,15 +2946,11 @@ class FhirPathAtom {
2782
2946
  }
2783
2947
  eval(context) {
2784
2948
  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 [];
2949
+ if (context.length > 0) {
2950
+ return context.map((e) => this.child.eval([e])).flat();
2791
2951
  }
2792
2952
  else {
2793
- return [result];
2953
+ return this.child.eval(context);
2794
2954
  }
2795
2955
  }
2796
2956
  catch (error) {
@@ -2803,34 +2963,53 @@ class LiteralAtom {
2803
2963
  this.value = value;
2804
2964
  }
2805
2965
  eval() {
2806
- return this.value;
2966
+ return [this.value];
2807
2967
  }
2808
2968
  }
2809
2969
  class SymbolAtom {
2810
2970
  constructor(name) {
2811
2971
  this.name = name;
2972
+ _SymbolAtom_instances.add(this);
2812
2973
  }
2813
2974
  eval(context) {
2814
2975
  if (this.name === '$this') {
2815
2976
  return context;
2816
2977
  }
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
- });
2978
+ return context
2979
+ .map((e) => __classPrivateFieldGet(this, _SymbolAtom_instances, "m", _SymbolAtom_evalValue).call(this, e))
2980
+ .flat()
2981
+ .filter((e) => (e === null || e === void 0 ? void 0 : e.value) !== undefined);
2832
2982
  }
2833
2983
  }
2984
+ _SymbolAtom_instances = new WeakSet(), _SymbolAtom_evalValue = function _SymbolAtom_evalValue(typedValue) {
2985
+ const input = typedValue.value;
2986
+ if (!input || typeof input !== 'object') {
2987
+ return undefined;
2988
+ }
2989
+ if ('resourceType' in input && input.resourceType === this.name) {
2990
+ return typedValue;
2991
+ }
2992
+ let result = undefined;
2993
+ if (this.name in input) {
2994
+ result = input[this.name];
2995
+ }
2996
+ else {
2997
+ const propertyName = Object.keys(input).find((k) => k.startsWith(this.name));
2998
+ if (propertyName) {
2999
+ result = input[propertyName];
3000
+ }
3001
+ }
3002
+ if (result === undefined) {
3003
+ return undefined;
3004
+ }
3005
+ // TODO: Get the PropertyType from the choice of type
3006
+ if (Array.isArray(result)) {
3007
+ return result.map(toTypedValue);
3008
+ }
3009
+ else {
3010
+ return [toTypedValue(result)];
3011
+ }
3012
+ };
2834
3013
  class EmptySetAtom {
2835
3014
  eval() {
2836
3015
  return [];
@@ -2861,30 +3040,27 @@ class ArithemticOperatorAtom {
2861
3040
  this.impl = impl;
2862
3041
  }
2863
3042
  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) });
3043
+ const leftEvalResult = this.left.eval(context);
3044
+ if (leftEvalResult.length !== 1) {
3045
+ return [];
2868
3046
  }
2869
- else {
2870
- return this.impl(leftValue, rightValue);
3047
+ const rightEvalResult = this.right.eval(context);
3048
+ if (rightEvalResult.length !== 1) {
3049
+ return [];
2871
3050
  }
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);
3051
+ const leftValue = leftEvalResult[0].value;
3052
+ const rightValue = rightEvalResult[0].value;
3053
+ const leftNumber = isQuantity(leftValue) ? leftValue.value : leftValue;
3054
+ const rightNumber = isQuantity(rightValue) ? rightValue.value : rightValue;
3055
+ const result = this.impl(leftNumber, rightNumber);
3056
+ if (typeof result === 'boolean') {
3057
+ return booleanToTypedValue(result);
3058
+ }
3059
+ else if (isQuantity(leftValue)) {
3060
+ return [{ type: PropertyType.Quantity, value: Object.assign(Object.assign({}, leftValue), { value: result }) }];
2885
3061
  }
2886
3062
  else {
2887
- return this.impl(leftValue, rightValue);
3063
+ return [toTypedValue(result)];
2888
3064
  }
2889
3065
  }
2890
3066
  }
@@ -2896,21 +3072,9 @@ class ConcatAtom {
2896
3072
  eval(context) {
2897
3073
  const leftValue = this.left.eval(context);
2898
3074
  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('');
3075
+ const result = [...leftValue, ...rightValue];
3076
+ if (result.length > 0 && result.every((e) => typeof e.value === 'string')) {
3077
+ return [{ type: PropertyType.string, value: result.map((e) => e.value).join('') }];
2914
3078
  }
2915
3079
  return result;
2916
3080
  }
@@ -2923,7 +3087,7 @@ class ContainsAtom {
2923
3087
  eval(context) {
2924
3088
  const leftValue = this.left.eval(context);
2925
3089
  const rightValue = this.right.eval(context);
2926
- return ensureArray(leftValue).includes(rightValue);
3090
+ return booleanToTypedValue(leftValue.some((e) => e.value === rightValue[0].value));
2927
3091
  }
2928
3092
  }
2929
3093
  class InAtom {
@@ -2934,7 +3098,7 @@ class InAtom {
2934
3098
  eval(context) {
2935
3099
  const leftValue = this.left.eval(context);
2936
3100
  const rightValue = this.right.eval(context);
2937
- return ensureArray(rightValue).includes(leftValue);
3101
+ return booleanToTypedValue(rightValue.some((e) => e.value === leftValue[0].value));
2938
3102
  }
2939
3103
  }
2940
3104
  class DotAtom {
@@ -2954,20 +3118,7 @@ class UnionAtom {
2954
3118
  eval(context) {
2955
3119
  const leftResult = this.left.eval(context);
2956
3120
  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);
3121
+ return removeDuplicates([...leftResult, ...rightResult]);
2971
3122
  }
2972
3123
  }
2973
3124
  class EqualsAtom {
@@ -2978,10 +3129,7 @@ class EqualsAtom {
2978
3129
  eval(context) {
2979
3130
  const leftValue = this.left.eval(context);
2980
3131
  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));
3132
+ return fhirPathArrayEquals(leftValue, rightValue);
2985
3133
  }
2986
3134
  }
2987
3135
  class NotEqualsAtom {
@@ -2992,14 +3140,7 @@ class NotEqualsAtom {
2992
3140
  eval(context) {
2993
3141
  const leftValue = this.left.eval(context);
2994
3142
  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);
3143
+ return fhirPathNot(fhirPathArrayEquals(leftValue, rightValue));
3003
3144
  }
3004
3145
  }
3005
3146
  class EquivalentAtom {
@@ -3010,10 +3151,7 @@ class EquivalentAtom {
3010
3151
  eval(context) {
3011
3152
  const leftValue = this.left.eval(context);
3012
3153
  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));
3154
+ return fhirPathArrayEquivalent(leftValue, rightValue);
3017
3155
  }
3018
3156
  }
3019
3157
  class NotEquivalentAtom {
@@ -3024,14 +3162,7 @@ class NotEquivalentAtom {
3024
3162
  eval(context) {
3025
3163
  const leftValue = this.left.eval(context);
3026
3164
  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);
3165
+ return fhirPathNot(fhirPathArrayEquivalent(leftValue, rightValue));
3035
3166
  }
3036
3167
  }
3037
3168
  class IsAtom {
@@ -3040,8 +3171,12 @@ class IsAtom {
3040
3171
  this.right = right;
3041
3172
  }
3042
3173
  eval(context) {
3174
+ const leftValue = this.left.eval(context);
3175
+ if (leftValue.length !== 1) {
3176
+ return [];
3177
+ }
3043
3178
  const typeName = this.right.name;
3044
- return applyMaybeArray(this.left.eval(context), (e) => fhirPathIs(e, typeName));
3179
+ return booleanToTypedValue(fhirPathIs(leftValue[0], typeName));
3045
3180
  }
3046
3181
  }
3047
3182
  /**
@@ -3054,13 +3189,14 @@ class AndAtom {
3054
3189
  this.right = right;
3055
3190
  }
3056
3191
  eval(context) {
3192
+ var _a, _b, _c, _d;
3057
3193
  const leftValue = this.left.eval(context);
3058
3194
  const rightValue = this.right.eval(context);
3059
- if (leftValue === true && rightValue === true) {
3060
- return true;
3195
+ 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) {
3196
+ return booleanToTypedValue(true);
3061
3197
  }
3062
- if (leftValue === false || rightValue === false) {
3063
- return false;
3198
+ 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) {
3199
+ return booleanToTypedValue(false);
3064
3200
  }
3065
3201
  return [];
3066
3202
  }
@@ -3094,13 +3230,18 @@ class XorAtom {
3094
3230
  this.right = right;
3095
3231
  }
3096
3232
  eval(context) {
3097
- const leftValue = this.left.eval(context);
3098
- const rightValue = this.right.eval(context);
3233
+ const leftResult = this.left.eval(context);
3234
+ const rightResult = this.right.eval(context);
3235
+ if (leftResult.length === 0 && rightResult.length === 0) {
3236
+ return [];
3237
+ }
3238
+ const leftValue = leftResult.length === 0 ? null : leftResult[0].value;
3239
+ const rightValue = rightResult.length === 0 ? null : rightResult[0].value;
3099
3240
  if ((leftValue === true && rightValue !== true) || (leftValue !== true && rightValue === true)) {
3100
- return true;
3241
+ return booleanToTypedValue(true);
3101
3242
  }
3102
3243
  if ((leftValue === true && rightValue === true) || (leftValue === false && rightValue === false)) {
3103
- return false;
3244
+ return booleanToTypedValue(false);
3104
3245
  }
3105
3246
  return [];
3106
3247
  }
@@ -3112,7 +3253,7 @@ class FunctionAtom {
3112
3253
  this.impl = impl;
3113
3254
  }
3114
3255
  eval(context) {
3115
- return this.impl(ensureArray(context), ...this.args);
3256
+ return this.impl(context, ...this.args);
3116
3257
  }
3117
3258
  }
3118
3259
  class IndexerAtom {
@@ -3121,7 +3262,11 @@ class IndexerAtom {
3121
3262
  this.expr = expr;
3122
3263
  }
3123
3264
  eval(context) {
3124
- const index = this.expr.eval(context);
3265
+ const evalResult = this.expr.eval(context);
3266
+ if (evalResult.length !== 1) {
3267
+ return [];
3268
+ }
3269
+ const index = evalResult[0].value;
3125
3270
  if (typeof index !== 'number') {
3126
3271
  throw new Error(`Invalid indexer expression: should return integer}`);
3127
3272
  }
@@ -3129,7 +3274,7 @@ class IndexerAtom {
3129
3274
  if (!(index in leftResult)) {
3130
3275
  return [];
3131
3276
  }
3132
- return leftResult[index];
3277
+ return [leftResult[index]];
3133
3278
  }
3134
3279
  }
3135
3280
 
@@ -3155,1414 +3300,1453 @@ function parseDateString(str) {
3155
3300
  }
3156
3301
  }
3157
3302
 
3158
- /*
3159
- * Collection of FHIRPath
3160
- * See: https://hl7.org/fhirpath/#functions
3161
- */
3162
3303
  /**
3163
3304
  * Temporary placholder for unimplemented methods.
3164
3305
  */
3165
3306
  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];
3307
+ const functions = {
3308
+ /*
3309
+ * 5.1 Existence
3310
+ * See: https://hl7.org/fhirpath/#existence
3311
+ */
3312
+ /**
3313
+ * Returns true if the input collection is empty ({ }) and false otherwise.
3314
+ *
3315
+ * See: https://hl7.org/fhirpath/#empty-boolean
3316
+ *
3317
+ * @param input The input collection.
3318
+ * @returns True if the input collection is empty ({ }) and false otherwise.
3319
+ */
3320
+ empty: (input) => {
3321
+ return booleanToTypedValue(input.length === 0);
3322
+ },
3323
+ /**
3324
+ * Returns true if the collection has unknown elements, and false otherwise.
3325
+ * This is the opposite of empty(), and as such is a shorthand for empty().not().
3326
+ * If the input collection is empty ({ }), the result is false.
3327
+ *
3328
+ * The function can also take an optional criteria to be applied to the collection
3329
+ * prior to the determination of the exists. In this case, the function is shorthand
3330
+ * for where(criteria).exists().
3331
+ *
3332
+ * See: https://hl7.org/fhirpath/#existscriteria-expression-boolean
3333
+ *
3334
+ * @param input
3335
+ * @param criteria
3336
+ * @returns True if the collection has unknown elements, and false otherwise.
3337
+ */
3338
+ exists: (input, criteria) => {
3339
+ if (criteria) {
3340
+ return booleanToTypedValue(input.filter((e) => toJsBoolean(criteria.eval([e]))).length > 0);
3234
3341
  }
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];
3342
+ else {
3343
+ return booleanToTypedValue(input.length > 0);
3252
3344
  }
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];
3345
+ },
3346
+ /**
3347
+ * Returns true if for every element in the input collection, criteria evaluates to true.
3348
+ * Otherwise, the result is false.
3349
+ *
3350
+ * If the input collection is empty ({ }), the result is true.
3351
+ *
3352
+ * See: https://hl7.org/fhirpath/#allcriteria-expression-boolean
3353
+ *
3354
+ * @param input The input collection.
3355
+ * @param criteria The evaluation criteria.
3356
+ * @returns True if for every element in the input collection, criteria evaluates to true.
3357
+ */
3358
+ all: (input, criteria) => {
3359
+ return booleanToTypedValue(input.every((e) => toJsBoolean(criteria.eval([e]))));
3360
+ },
3361
+ /**
3362
+ * Takes a collection of Boolean values and returns true if all the items are true.
3363
+ * If unknown items are false, the result is false.
3364
+ * If the input is empty ({ }), the result is true.
3365
+ *
3366
+ * See: https://hl7.org/fhirpath/#alltrue-boolean
3367
+ *
3368
+ * @param input The input collection.
3369
+ * @param criteria The evaluation criteria.
3370
+ * @returns True if all the items are true.
3371
+ */
3372
+ allTrue: (input) => {
3373
+ for (const value of input) {
3374
+ if (!value.value) {
3375
+ return booleanToTypedValue(false);
3376
+ }
3271
3377
  }
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];
3378
+ return booleanToTypedValue(true);
3379
+ },
3380
+ /**
3381
+ * Takes a collection of Boolean values and returns true if unknown of the items are true.
3382
+ * If all the items are false, or if the input is empty ({ }), the result is false.
3383
+ *
3384
+ * See: https://hl7.org/fhirpath/#anytrue-boolean
3385
+ *
3386
+ * @param input The input collection.
3387
+ * @param criteria The evaluation criteria.
3388
+ * @returns True if unknown of the items are true.
3389
+ */
3390
+ anyTrue: (input) => {
3391
+ for (const value of input) {
3392
+ if (value.value) {
3393
+ return booleanToTypedValue(true);
3394
+ }
3289
3395
  }
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];
3396
+ return booleanToTypedValue(false);
3397
+ },
3398
+ /**
3399
+ * Takes a collection of Boolean values and returns true if all the items are false.
3400
+ * If unknown items are true, the result is false.
3401
+ * If the input is empty ({ }), the result is true.
3402
+ *
3403
+ * See: https://hl7.org/fhirpath/#allfalse-boolean
3404
+ *
3405
+ * @param input The input collection.
3406
+ * @param criteria The evaluation criteria.
3407
+ * @returns True if all the items are false.
3408
+ */
3409
+ allFalse: (input) => {
3410
+ for (const value of input) {
3411
+ if (value.value) {
3412
+ return booleanToTypedValue(false);
3413
+ }
3651
3414
  }
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];
3415
+ return booleanToTypedValue(true);
3416
+ },
3417
+ /**
3418
+ * Takes a collection of Boolean values and returns true if unknown of the items are false.
3419
+ * If all the items are true, or if the input is empty ({ }), the result is false.
3420
+ *
3421
+ * See: https://hl7.org/fhirpath/#anyfalse-boolean
3422
+ *
3423
+ * @param input The input collection.
3424
+ * @param criteria The evaluation criteria.
3425
+ * @returns True if for every element in the input collection, criteria evaluates to true.
3426
+ */
3427
+ anyFalse: (input) => {
3428
+ for (const value of input) {
3429
+ if (!value.value) {
3430
+ return booleanToTypedValue(true);
3431
+ }
3657
3432
  }
3658
- if (['false', 'f', 'no', 'n', '0', '0.0'].includes(lowerStr)) {
3659
- return [false];
3433
+ return booleanToTypedValue(false);
3434
+ },
3435
+ /**
3436
+ * Returns true if all items in the input collection are members of the collection passed
3437
+ * as the other argument. Membership is determined using the = (Equals) (=) operation.
3438
+ *
3439
+ * Conceptually, this function is evaluated by testing each element in the input collection
3440
+ * for membership in the other collection, with a default of true. This means that if the
3441
+ * input collection is empty ({ }), the result is true, otherwise if the other collection
3442
+ * is empty ({ }), the result is false.
3443
+ *
3444
+ * See: http://hl7.org/fhirpath/#subsetofother-collection-boolean
3445
+ */
3446
+ subsetOf: stub,
3447
+ /**
3448
+ * Returns true if all items in the collection passed as the other argument are members of
3449
+ * the input collection. Membership is determined using the = (Equals) (=) operation.
3450
+ *
3451
+ * Conceptually, this function is evaluated by testing each element in the other collection
3452
+ * for membership in the input collection, with a default of true. This means that if the
3453
+ * other collection is empty ({ }), the result is true, otherwise if the input collection
3454
+ * is empty ({ }), the result is false.
3455
+ *
3456
+ * See: http://hl7.org/fhirpath/#supersetofother-collection-boolean
3457
+ */
3458
+ supersetOf: stub,
3459
+ /**
3460
+ * Returns the integer count of the number of items in the input collection.
3461
+ * Returns 0 when the input collection is empty.
3462
+ *
3463
+ * See: https://hl7.org/fhirpath/#count-integer
3464
+ *
3465
+ * @param input The input collection.
3466
+ * @returns The integer count of the number of items in the input collection.
3467
+ */
3468
+ count: (input) => {
3469
+ return [{ type: PropertyType.integer, value: input.length }];
3470
+ },
3471
+ /**
3472
+ * Returns a collection containing only the unique items in the input collection.
3473
+ * To determine whether two items are the same, the = (Equals) (=) operator is used,
3474
+ * as defined below.
3475
+ *
3476
+ * If the input collection is empty ({ }), the result is empty.
3477
+ *
3478
+ * Note that the order of elements in the input collection is not guaranteed to be
3479
+ * preserved in the result.
3480
+ *
3481
+ * See: https://hl7.org/fhirpath/#distinct-collection
3482
+ *
3483
+ * @param input The input collection.
3484
+ * @returns The integer count of the number of items in the input collection.
3485
+ */
3486
+ distinct: (input) => {
3487
+ const result = [];
3488
+ for (const value of input) {
3489
+ if (!result.some((e) => e.value === value.value)) {
3490
+ result.push(value);
3491
+ }
3492
+ }
3493
+ return result;
3494
+ },
3495
+ /**
3496
+ * Returns true if all the items in the input collection are distinct.
3497
+ * To determine whether two items are distinct, the = (Equals) (=) operator is used,
3498
+ * as defined below.
3499
+ *
3500
+ * See: https://hl7.org/fhirpath/#isdistinct-boolean
3501
+ *
3502
+ * @param input The input collection.
3503
+ * @returns The integer count of the number of items in the input collection.
3504
+ */
3505
+ isDistinct: (input) => {
3506
+ return booleanToTypedValue(input.length === functions.distinct(input).length);
3507
+ },
3508
+ /*
3509
+ * 5.2 Filtering and projection
3510
+ */
3511
+ /**
3512
+ * Returns a collection containing only those elements in the input collection
3513
+ * for which the stated criteria expression evaluates to true.
3514
+ * Elements for which the expression evaluates to false or empty ({ }) are not
3515
+ * included in the result.
3516
+ *
3517
+ * If the input collection is empty ({ }), the result is empty.
3518
+ *
3519
+ * If the result of evaluating the condition is other than a single boolean value,
3520
+ * the evaluation will end and signal an error to the calling environment,
3521
+ * consistent with singleton evaluation of collections behavior.
3522
+ *
3523
+ * See: https://hl7.org/fhirpath/#wherecriteria-expression-collection
3524
+ *
3525
+ * @param input The input collection.
3526
+ * @param condition The condition atom.
3527
+ * @returns A collection containing only those elements in the input collection for which the stated criteria expression evaluates to true.
3528
+ */
3529
+ where: (input, criteria) => {
3530
+ return input.filter((e) => toJsBoolean(criteria.eval([e])));
3531
+ },
3532
+ /**
3533
+ * Evaluates the projection expression for each item in the input collection.
3534
+ * The result of each evaluation is added to the output collection. If the
3535
+ * evaluation results in a collection with multiple items, all items are added
3536
+ * to the output collection (collections resulting from evaluation of projection
3537
+ * are flattened). This means that if the evaluation for an element results in
3538
+ * the empty collection ({ }), no element is added to the result, and that if
3539
+ * the input collection is empty ({ }), the result is empty as well.
3540
+ *
3541
+ * See: http://hl7.org/fhirpath/#selectprojection-expression-collection
3542
+ */
3543
+ select: (input, criteria) => {
3544
+ return input.map((e) => criteria.eval([e])).flat();
3545
+ },
3546
+ /**
3547
+ * A version of select that will repeat the projection and add it to the output
3548
+ * collection, as long as the projection yields new items (as determined by
3549
+ * the = (Equals) (=) operator).
3550
+ *
3551
+ * See: http://hl7.org/fhirpath/#repeatprojection-expression-collection
3552
+ */
3553
+ repeat: stub,
3554
+ /**
3555
+ * Returns a collection that contains all items in the input collection that
3556
+ * are of the given type or a subclass thereof. If the input collection is
3557
+ * empty ({ }), the result is empty. The type argument is an identifier that
3558
+ * must resolve to the name of a type in a model
3559
+ *
3560
+ * See: http://hl7.org/fhirpath/#oftypetype-type-specifier-collection
3561
+ */
3562
+ ofType: stub,
3563
+ /*
3564
+ * 5.3 Subsetting
3565
+ */
3566
+ /**
3567
+ * Will return the single item in the input if there is just one item.
3568
+ * If the input collection is empty ({ }), the result is empty.
3569
+ * If there are multiple items, an error is signaled to the evaluation environment.
3570
+ * This function is useful for ensuring that an error is returned if an assumption
3571
+ * about cardinality is violated at run-time.
3572
+ *
3573
+ * See: https://hl7.org/fhirpath/#single-collection
3574
+ *
3575
+ * @param input The input collection.
3576
+ * @returns The single item in the input if there is just one item.
3577
+ */
3578
+ single: (input) => {
3579
+ if (input.length > 1) {
3580
+ throw new Error('Expected input length one for single()');
3581
+ }
3582
+ return input.length === 0 ? [] : input.slice(0, 1);
3583
+ },
3584
+ /**
3585
+ * Returns a collection containing only the first item in the input collection.
3586
+ * This function is equivalent to item[0], so it will return an empty collection if the input collection has no items.
3587
+ *
3588
+ * See: https://hl7.org/fhirpath/#first-collection
3589
+ *
3590
+ * @param input The input collection.
3591
+ * @returns A collection containing only the first item in the input collection.
3592
+ */
3593
+ first: (input) => {
3594
+ return input.length === 0 ? [] : input.slice(0, 1);
3595
+ },
3596
+ /**
3597
+ * Returns a collection containing only the last item in the input collection.
3598
+ * Will return an empty collection if the input collection has no items.
3599
+ *
3600
+ * See: https://hl7.org/fhirpath/#last-collection
3601
+ *
3602
+ * @param input The input collection.
3603
+ * @returns A collection containing only the last item in the input collection.
3604
+ */
3605
+ last: (input) => {
3606
+ return input.length === 0 ? [] : input.slice(input.length - 1, input.length);
3607
+ },
3608
+ /**
3609
+ * Returns a collection containing all but the first item in the input collection.
3610
+ * Will return an empty collection if the input collection has no items, or only one item.
3611
+ *
3612
+ * See: https://hl7.org/fhirpath/#tail-collection
3613
+ *
3614
+ * @param input The input collection.
3615
+ * @returns A collection containing all but the first item in the input collection.
3616
+ */
3617
+ tail: (input) => {
3618
+ return input.length === 0 ? [] : input.slice(1, input.length);
3619
+ },
3620
+ /**
3621
+ * Returns a collection containing all but the first num items in the input collection.
3622
+ * Will return an empty collection if there are no items remaining after the
3623
+ * indicated number of items have been skipped, or if the input collection is empty.
3624
+ * If num is less than or equal to zero, the input collection is simply returned.
3625
+ *
3626
+ * See: https://hl7.org/fhirpath/#skipnum-integer-collection
3627
+ *
3628
+ * @param input The input collection.
3629
+ * @returns A collection containing all but the first item in the input collection.
3630
+ */
3631
+ skip: (input, num) => {
3632
+ var _a;
3633
+ const numValue = (_a = num.eval([])[0]) === null || _a === void 0 ? void 0 : _a.value;
3634
+ if (typeof numValue !== 'number') {
3635
+ throw new Error('Expected a number for skip(num)');
3636
+ }
3637
+ if (numValue >= input.length) {
3638
+ return [];
3639
+ }
3640
+ if (numValue <= 0) {
3641
+ return input;
3642
+ }
3643
+ return input.slice(numValue, input.length);
3644
+ },
3645
+ /**
3646
+ * Returns a collection containing the first num items in the input collection,
3647
+ * or less if there are less than num items.
3648
+ * If num is less than or equal to 0, or if the input collection is empty ({ }),
3649
+ * take returns an empty collection.
3650
+ *
3651
+ * See: https://hl7.org/fhirpath/#takenum-integer-collection
3652
+ *
3653
+ * @param input The input collection.
3654
+ * @returns A collection containing the first num items in the input collection.
3655
+ */
3656
+ take: (input, num) => {
3657
+ var _a;
3658
+ const numValue = (_a = num.eval([])[0]) === null || _a === void 0 ? void 0 : _a.value;
3659
+ if (typeof numValue !== 'number') {
3660
+ throw new Error('Expected a number for take(num)');
3661
+ }
3662
+ if (numValue >= input.length) {
3663
+ return input;
3664
+ }
3665
+ if (numValue <= 0) {
3666
+ return [];
3667
+ }
3668
+ return input.slice(0, numValue);
3669
+ },
3670
+ /**
3671
+ * Returns the set of elements that are in both collections.
3672
+ * Duplicate items will be eliminated by this function.
3673
+ * Order of items is not guaranteed to be preserved in the result of this function.
3674
+ *
3675
+ * See: http://hl7.org/fhirpath/#intersectother-collection-collection
3676
+ */
3677
+ intersect: (input, other) => {
3678
+ if (!other) {
3679
+ return input;
3680
+ }
3681
+ const otherArray = other.eval([]);
3682
+ const result = [];
3683
+ for (const value of input) {
3684
+ if (!result.some((e) => e.value === value.value) && otherArray.some((e) => e.value === value.value)) {
3685
+ result.push(value);
3686
+ }
3687
+ }
3688
+ return result;
3689
+ },
3690
+ /**
3691
+ * Returns the set of elements that are not in the other collection.
3692
+ * Duplicate items will not be eliminated by this function, and order will be preserved.
3693
+ *
3694
+ * e.g. (1 | 2 | 3).exclude(2) returns (1 | 3).
3695
+ *
3696
+ * See: http://hl7.org/fhirpath/#excludeother-collection-collection
3697
+ */
3698
+ exclude: (input, other) => {
3699
+ if (!other) {
3700
+ return input;
3701
+ }
3702
+ const otherArray = other.eval([]);
3703
+ const result = [];
3704
+ for (const value of input) {
3705
+ if (!otherArray.some((e) => e.value === value.value)) {
3706
+ result.push(value);
3707
+ }
3708
+ }
3709
+ return result;
3710
+ },
3711
+ /*
3712
+ * 5.4. Combining
3713
+ *
3714
+ * See: https://hl7.org/fhirpath/#combining
3715
+ */
3716
+ /**
3717
+ * Merge the two collections into a single collection,
3718
+ * eliminating unknown duplicate values (using = (Equals) (=) to determine equality).
3719
+ * There is no expectation of order in the resulting collection.
3720
+ *
3721
+ * In other words, this function returns the distinct list of elements from both inputs.
3722
+ *
3723
+ * See: http://hl7.org/fhirpath/#unionother-collection
3724
+ */
3725
+ union: (input, other) => {
3726
+ if (!other) {
3727
+ return input;
3728
+ }
3729
+ const otherArray = other.eval([]);
3730
+ return removeDuplicates([...input, ...otherArray]);
3731
+ },
3732
+ /**
3733
+ * Merge the input and other collections into a single collection
3734
+ * without eliminating duplicate values. Combining an empty collection
3735
+ * with a non-empty collection will return the non-empty collection.
3736
+ *
3737
+ * There is no expectation of order in the resulting collection.
3738
+ *
3739
+ * See: http://hl7.org/fhirpath/#combineother-collection-collection
3740
+ */
3741
+ combine: (input, other) => {
3742
+ if (!other) {
3743
+ return input;
3744
+ }
3745
+ const otherArray = other.eval([]);
3746
+ return [...input, ...otherArray];
3747
+ },
3748
+ /*
3749
+ * 5.5. Conversion
3750
+ *
3751
+ * See: https://hl7.org/fhirpath/#conversion
3752
+ */
3753
+ /**
3754
+ * The iif function in FHIRPath is an immediate if,
3755
+ * also known as a conditional operator (such as C’s ? : operator).
3756
+ *
3757
+ * The criterion expression is expected to evaluate to a Boolean.
3758
+ *
3759
+ * If criterion is true, the function returns the value of the true-result argument.
3760
+ *
3761
+ * If criterion is false or an empty collection, the function returns otherwise-result,
3762
+ * unless the optional otherwise-result is not given, in which case the function returns an empty collection.
3763
+ *
3764
+ * Note that short-circuit behavior is expected in this function. In other words,
3765
+ * true-result should only be evaluated if the criterion evaluates to true,
3766
+ * and otherwise-result should only be evaluated otherwise. For implementations,
3767
+ * this means delaying evaluation of the arguments.
3768
+ *
3769
+ * @param input
3770
+ * @param criterion
3771
+ * @param trueResult
3772
+ * @param otherwiseResult
3773
+ * @returns
3774
+ */
3775
+ iif: (input, criterion, trueResult, otherwiseResult) => {
3776
+ const evalResult = criterion.eval(input);
3777
+ if (evalResult.length > 1 || (evalResult.length === 1 && typeof evalResult[0].value !== 'boolean')) {
3778
+ throw new Error('Expected criterion to evaluate to a Boolean');
3779
+ }
3780
+ if (toJsBoolean(evalResult)) {
3781
+ return trueResult.eval(input);
3782
+ }
3783
+ if (otherwiseResult) {
3784
+ return otherwiseResult.eval(input);
3660
3785
  }
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
3786
  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) {
3787
+ },
3788
+ /**
3789
+ * Converts an input collection to a boolean.
3790
+ *
3791
+ * If the input collection contains a single item, this function will return a single boolean if:
3792
+ * 1) the item is a Boolean
3793
+ * 2) the item is an Integer and is equal to one of the possible integer representations of Boolean values
3794
+ * 3) the item is a Decimal that is equal to one of the possible decimal representations of Boolean values
3795
+ * 4) the item is a String that is equal to one of the possible string representations of Boolean values
3796
+ *
3797
+ * 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.
3798
+ *
3799
+ * See: https://hl7.org/fhirpath/#toboolean-boolean
3800
+ *
3801
+ * @param input
3802
+ * @returns
3803
+ */
3804
+ toBoolean: (input) => {
3805
+ if (input.length === 0) {
3806
+ return [];
3807
+ }
3808
+ const [{ value }] = validateInput(input, 1);
3809
+ if (typeof value === 'boolean') {
3810
+ return [{ type: PropertyType.boolean, value }];
3811
+ }
3812
+ if (typeof value === 'number') {
3813
+ if (value === 0 || value === 1) {
3814
+ return booleanToTypedValue(!!value);
3815
+ }
3816
+ }
3817
+ if (typeof value === 'string') {
3818
+ const lowerStr = value.toLowerCase();
3819
+ if (['true', 't', 'yes', 'y', '1', '1.0'].includes(lowerStr)) {
3820
+ return booleanToTypedValue(true);
3821
+ }
3822
+ if (['false', 'f', 'no', 'n', '0', '0.0'].includes(lowerStr)) {
3823
+ return booleanToTypedValue(false);
3824
+ }
3825
+ }
3769
3826
  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) {
3827
+ },
3828
+ /**
3829
+ * If the input collection contains a single item, this function will return true if:
3830
+ * 1) the item is a Boolean
3831
+ * 2) the item is an Integer that is equal to one of the possible integer representations of Boolean values
3832
+ * 3) the item is a Decimal that is equal to one of the possible decimal representations of Boolean values
3833
+ * 4) the item is a String that is equal to one of the possible string representations of Boolean values
3834
+ *
3835
+ * 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.
3836
+ *
3837
+ * Possible values for Integer, Decimal, and String are described in the toBoolean() function.
3838
+ *
3839
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3840
+ *
3841
+ * If the input collection is empty, the result is empty.
3842
+ *
3843
+ * See: http://hl7.org/fhirpath/#convertstoboolean-boolean
3844
+ *
3845
+ * @param input
3846
+ * @returns
3847
+ */
3848
+ convertsToBoolean: (input) => {
3849
+ if (input.length === 0) {
3850
+ return [];
3851
+ }
3852
+ return booleanToTypedValue(functions.toBoolean(input).length === 1);
3853
+ },
3854
+ /**
3855
+ * Returns the integer representation of the input.
3856
+ *
3857
+ * If the input collection contains a single item, this function will return a single integer if:
3858
+ * 1) the item is an Integer
3859
+ * 2) the item is a String and is convertible to an integer
3860
+ * 3) the item is a Boolean, where true results in a 1 and false results in a 0.
3861
+ *
3862
+ * If the item is not one the above types, the result is empty.
3863
+ *
3864
+ * If the item is a String, but the string is not convertible to an integer (using the regex format (\\+|-)?\d+), the result is empty.
3865
+ *
3866
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3867
+ *
3868
+ * If the input collection is empty, the result is empty.
3869
+ *
3870
+ * See: https://hl7.org/fhirpath/#tointeger-integer
3871
+ *
3872
+ * @param input The input collection.
3873
+ * @returns The string representation of the input.
3874
+ */
3875
+ toInteger: (input) => {
3876
+ if (input.length === 0) {
3877
+ return [];
3878
+ }
3879
+ const [{ value }] = validateInput(input, 1);
3880
+ if (typeof value === 'number') {
3881
+ return [{ type: PropertyType.integer, value }];
3882
+ }
3883
+ if (typeof value === 'string' && value.match(/^[+-]?\d+$/)) {
3884
+ return [{ type: PropertyType.integer, value: parseInt(value, 10) }];
3885
+ }
3886
+ if (typeof value === 'boolean') {
3887
+ return [{ type: PropertyType.integer, value: value ? 1 : 0 }];
3888
+ }
3795
3889
  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) {
3890
+ },
3891
+ /**
3892
+ * Returns true if the input can be converted to string.
3893
+ *
3894
+ * If the input collection contains a single item, this function will return true if:
3895
+ * 1) the item is an Integer
3896
+ * 2) the item is a String and is convertible to an Integer
3897
+ * 3) the item is a Boolean
3898
+ * 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.
3899
+ *
3900
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3901
+ *
3902
+ * If the input collection is empty, the result is empty.
3903
+ *
3904
+ * See: https://hl7.org/fhirpath/#convertstointeger-boolean
3905
+ *
3906
+ * @param input The input collection.
3907
+ * @returns
3908
+ */
3909
+ convertsToInteger: (input) => {
3910
+ if (input.length === 0) {
3911
+ return [];
3912
+ }
3913
+ return booleanToTypedValue(functions.toInteger(input).length === 1);
3914
+ },
3915
+ /**
3916
+ * If the input collection contains a single item, this function will return a single date if:
3917
+ * 1) the item is a Date
3918
+ * 2) the item is a DateTime
3919
+ * 3) the item is a String and is convertible to a Date
3920
+ *
3921
+ * If the item is not one of the above types, the result is empty.
3922
+ *
3923
+ * 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.
3924
+ *
3925
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3926
+ *
3927
+ * If the input collection is empty, the result is empty.
3928
+ *
3929
+ * See: https://hl7.org/fhirpath/#todate-date
3930
+ */
3931
+ toDate: (input) => {
3932
+ if (input.length === 0) {
3933
+ return [];
3934
+ }
3935
+ const [{ value }] = validateInput(input, 1);
3936
+ if (typeof value === 'string' && value.match(/^\d{4}(-\d{2}(-\d{2})?)?/)) {
3937
+ return [{ type: PropertyType.date, value: parseDateString(value) }];
3938
+ }
3822
3939
  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) {
3940
+ },
3941
+ /**
3942
+ * If the input collection contains a single item, this function will return true if:
3943
+ * 1) the item is a Date
3944
+ * 2) the item is a DateTime
3945
+ * 3) the item is a String and is convertible to a Date
3946
+ *
3947
+ * 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.
3948
+ *
3949
+ * If the item contains a partial date (e.g. '2012-01'), the result is a partial date.
3950
+ *
3951
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3952
+ *
3953
+ * If the input collection is empty, the result is empty.
3954
+ *
3955
+ * See: https://hl7.org/fhirpath/#convertstodate-boolean
3956
+ */
3957
+ convertsToDate: (input) => {
3958
+ if (input.length === 0) {
3959
+ return [];
3960
+ }
3961
+ return booleanToTypedValue(functions.toDate(input).length === 1);
3962
+ },
3963
+ /**
3964
+ * If the input collection contains a single item, this function will return a single datetime if:
3965
+ * 1) the item is a DateTime
3966
+ * 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)
3967
+ * 3) the item is a String and is convertible to a DateTime
3968
+ *
3969
+ * If the item is not one of the above types, the result is empty.
3970
+ *
3971
+ * 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.
3972
+ *
3973
+ * If the item contains a partial datetime (e.g. '2012-01-01T10:00'), the result is a partial datetime.
3974
+ *
3975
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3976
+ *
3977
+ * If the input collection is empty, the result is empty.
3978
+
3979
+ * See: https://hl7.org/fhirpath/#todatetime-datetime
3980
+ *
3981
+ * @param input
3982
+ * @returns
3983
+ */
3984
+ toDateTime: (input) => {
3985
+ if (input.length === 0) {
3986
+ return [];
3987
+ }
3988
+ const [{ value }] = validateInput(input, 1);
3989
+ if (typeof value === 'string' && value.match(/^\d{4}(-\d{2}(-\d{2})?)?/)) {
3990
+ return [{ type: PropertyType.dateTime, value: parseDateString(value) }];
3991
+ }
3849
3992
  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) {
3873
- 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) {
4023
- 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) {
3993
+ },
3994
+ /**
3995
+ * If the input collection contains a single item, this function will return true if:
3996
+ * 1) the item is a DateTime
3997
+ * 2) the item is a Date
3998
+ * 3) the item is a String and is convertible to a DateTime
3999
+ *
4000
+ * 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.
4001
+ *
4002
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4003
+ *
4004
+ * If the input collection is empty, the result is empty.
4005
+ *
4006
+ * See: https://hl7.org/fhirpath/#convertstodatetime-boolean
4007
+ *
4008
+ * @param input
4009
+ * @returns
4010
+ */
4011
+ convertsToDateTime: (input) => {
4012
+ if (input.length === 0) {
4013
+ return [];
4014
+ }
4015
+ return booleanToTypedValue(functions.toDateTime(input).length === 1);
4016
+ },
4017
+ /**
4018
+ * If the input collection contains a single item, this function will return a single decimal if:
4019
+ * 1) the item is an Integer or Decimal
4020
+ * 2) the item is a String and is convertible to a Decimal
4021
+ * 3) the item is a Boolean, where true results in a 1.0 and false results in a 0.0.
4022
+ * 4) If the item is not one of the above types, the result is empty.
4023
+ *
4024
+ * 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.
4025
+ *
4026
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4027
+ *
4028
+ * If the input collection is empty, the result is empty.
4029
+ *
4030
+ * See: https://hl7.org/fhirpath/#decimal-conversion-functions
4031
+ *
4032
+ * @param input The input collection.
4033
+ * @returns
4034
+ */
4035
+ toDecimal: (input) => {
4036
+ if (input.length === 0) {
4037
+ return [];
4038
+ }
4039
+ const [{ value }] = validateInput(input, 1);
4040
+ if (typeof value === 'number') {
4041
+ return [{ type: PropertyType.decimal, value }];
4042
+ }
4043
+ if (typeof value === 'string' && value.match(/^-?\d{1,9}(\.\d{1,9})?$/)) {
4044
+ return [{ type: PropertyType.decimal, value: parseFloat(value) }];
4045
+ }
4046
+ if (typeof value === 'boolean') {
4047
+ return [{ type: PropertyType.decimal, value: value ? 1 : 0 }];
4048
+ }
4049
4049
  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])];
4050
+ },
4051
+ /**
4052
+ * If the input collection contains a single item, this function will true if:
4053
+ * 1) the item is an Integer or Decimal
4054
+ * 2) the item is a String and is convertible to a Decimal
4055
+ * 3) the item is a Boolean
4056
+ *
4057
+ * 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.
4058
+ *
4059
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4060
+ *
4061
+ * If the input collection is empty, the result is empty.
4062
+
4063
+ * See: https://hl7.org/fhirpath/#convertstodecimal-boolean
4064
+ *
4065
+ * @param input The input collection.
4066
+ * @returns
4067
+ */
4068
+ convertsToDecimal: (input) => {
4069
+ if (input.length === 0) {
4070
+ return [];
4071
+ }
4072
+ return booleanToTypedValue(functions.toDecimal(input).length === 1);
4073
+ },
4074
+ /**
4075
+ * If the input collection contains a single item, this function will return a single quantity if:
4076
+ * 1) the item is an Integer, or Decimal, where the resulting quantity will have the default unit ('1')
4077
+ * 2) the item is a Quantity
4078
+ * 3) the item is a String and is convertible to a Quantity
4079
+ * 4) the item is a Boolean, where true results in the quantity 1.0 '1', and false results in the quantity 0.0 '1'
4080
+ *
4081
+ * If the item is not one of the above types, the result is empty.
4082
+ *
4083
+ * See: https://hl7.org/fhirpath/#quantity-conversion-functions
4084
+ *
4085
+ * @param input The input collection.
4086
+ * @returns
4087
+ */
4088
+ toQuantity: (input) => {
4089
+ if (input.length === 0) {
4090
+ return [];
4091
+ }
4092
+ const [{ value }] = validateInput(input, 1);
4093
+ if (isQuantity(value)) {
4094
+ return [{ type: PropertyType.Quantity, value }];
4095
+ }
4096
+ if (typeof value === 'number') {
4097
+ return [{ type: PropertyType.Quantity, value: { value, unit: '1' } }];
4098
+ }
4099
+ if (typeof value === 'string' && value.match(/^-?\d{1,9}(\.\d{1,9})?/)) {
4100
+ return [{ type: PropertyType.Quantity, value: { value: parseFloat(value), unit: '1' } }];
4101
+ }
4102
+ if (typeof value === 'boolean') {
4103
+ return [{ type: PropertyType.Quantity, value: { value: value ? 1 : 0, unit: '1' } }];
4056
4104
  }
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
4105
  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;
4106
+ },
4107
+ /**
4108
+ * If the input collection contains a single item, this function will return true if:
4109
+ * 1) the item is an Integer, Decimal, or Quantity
4110
+ * 2) the item is a String that is convertible to a Quantity
4111
+ * 3) the item is a Boolean
4112
+ *
4113
+ * If the item is not one of the above types, or is not convertible to a Quantity using the following regex format:
4114
+ *
4115
+ * (?'value'(\+|-)?\d+(\.\d+)?)\s*('(?'unit'[^']+)'|(?'time'[a-zA-Z]+))?
4116
+ *
4117
+ * then the result is false.
4118
+ *
4119
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4120
+ *
4121
+ * If the input collection is empty, the result is empty.
4122
+ *
4123
+ * 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.
4124
+ *
4125
+ * See: https://hl7.org/fhirpath/#convertstoquantityunit-string-boolean
4126
+ *
4127
+ * @param input The input collection.
4128
+ * @returns
4129
+ */
4130
+ convertsToQuantity: (input) => {
4131
+ if (input.length === 0) {
4132
+ return [];
4133
+ }
4134
+ return booleanToTypedValue(functions.toQuantity(input).length === 1);
4135
+ },
4136
+ /**
4137
+ * Returns the string representation of the input.
4138
+ *
4139
+ * If the input collection contains a single item, this function will return a single String if:
4140
+ *
4141
+ * 1) the item in the input collection is a String
4142
+ * 2) the item in the input collection is an Integer, Decimal, Date, Time, DateTime, or Quantity the output will contain its String representation
4143
+ * 3) the item is a Boolean, where true results in 'true' and false in 'false'.
4144
+ *
4145
+ * If the item is not one of the above types, the result is false.
4146
+ *
4147
+ * See: https://hl7.org/fhirpath/#tostring-string
4148
+ *
4149
+ * @param input The input collection.
4150
+ * @returns The string representation of the input.
4151
+ */
4152
+ toString: (input) => {
4153
+ if (input.length === 0) {
4154
+ return [];
4155
+ }
4156
+ const [{ value }] = validateInput(input, 1);
4157
+ if (value === null || value === undefined) {
4158
+ return [];
4159
+ }
4160
+ if (isQuantity(value)) {
4161
+ return [{ type: PropertyType.string, value: `${value.value} '${value.unit}'` }];
4162
+ }
4163
+ return [{ type: PropertyType.string, value: value.toString() }];
4164
+ },
4165
+ /**
4166
+ * Returns true if the input can be converted to string.
4167
+ *
4168
+ * If the input collection contains a single item, this function will return true if:
4169
+ * 1) the item is a String
4170
+ * 2) the item is an Integer, Decimal, Date, Time, or DateTime
4171
+ * 3) the item is a Boolean
4172
+ * 4) the item is a Quantity
4173
+ *
4174
+ * If the item is not one of the above types, the result is false.
4175
+ *
4176
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4177
+ *
4178
+ * If the input collection is empty, the result is empty.
4179
+ *
4180
+ * See: https://hl7.org/fhirpath/#tostring-string
4181
+ *
4182
+ * @param input The input collection.
4183
+ * @returns
4184
+ */
4185
+ convertsToString: (input) => {
4186
+ if (input.length === 0) {
4187
+ return [];
4188
+ }
4189
+ return booleanToTypedValue(functions.toString(input).length === 1);
4190
+ },
4191
+ /**
4192
+ * If the input collection contains a single item, this function will return a single time if:
4193
+ * 1) the item is a Time
4194
+ * 2) the item is a String and is convertible to a Time
4195
+ *
4196
+ * If the item is not one of the above types, the result is empty.
4197
+ *
4198
+ * 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.
4199
+ *
4200
+ * If the item contains a partial time (e.g. '10:00'), the result is a partial time.
4201
+ *
4202
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4203
+ *
4204
+ * If the input collection is empty, the result is empty.
4205
+ *
4206
+ * See: https://hl7.org/fhirpath/#totime-time
4207
+ *
4208
+ * @param input
4209
+ * @returns
4210
+ */
4211
+ toTime: (input) => {
4212
+ if (input.length === 0) {
4213
+ return [];
4214
+ }
4215
+ const [{ value }] = validateInput(input, 1);
4216
+ if (typeof value === 'string') {
4217
+ const match = value.match(/^T?(\d{2}(:\d{2}(:\d{2})?)?)/);
4218
+ if (match) {
4219
+ return [{ type: PropertyType.time, value: parseDateString('T' + match[1]) }];
4508
4220
  }
4509
- refStr = ref.reference;
4510
4221
  }
4511
- if (!refStr) {
4512
- return undefined;
4222
+ return [];
4223
+ },
4224
+ /**
4225
+ * If the input collection contains a single item, this function will return true if:
4226
+ * 1) the item is a Time
4227
+ * 2) the item is a String and is convertible to a Time
4228
+ *
4229
+ * 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.
4230
+ *
4231
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4232
+ *
4233
+ * If the input collection is empty, the result is empty.
4234
+ *
4235
+ * See: https://hl7.org/fhirpath/#convertstotime-boolean
4236
+ *
4237
+ * @param input
4238
+ * @returns
4239
+ */
4240
+ convertsToTime: (input) => {
4241
+ if (input.length === 0) {
4242
+ return [];
4513
4243
  }
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' };
4244
+ return booleanToTypedValue(functions.toTime(input).length === 1);
4245
+ },
4246
+ /*
4247
+ * 5.6. String Manipulation.
4248
+ *
4249
+ * See: https://hl7.org/fhirpath/#string-manipulation
4250
+ */
4251
+ /**
4252
+ * Returns the 0-based index of the first position substring is found in the input string, or -1 if it is not found.
4253
+ *
4254
+ * If substring is an empty string (''), the function returns 0.
4255
+ *
4256
+ * If the input or substring is empty ({ }), the result is empty ({ }).
4257
+ *
4258
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4259
+ *
4260
+ * See: https://hl7.org/fhirpath/#indexofsubstring-string-integer
4261
+ *
4262
+ * @param input The input collection.
4263
+ * @returns The index of the substring.
4264
+ */
4265
+ indexOf: (input, substringAtom) => {
4266
+ return applyStringFunc((str, substring) => str.indexOf(substring), input, substringAtom);
4267
+ },
4268
+ /**
4269
+ * 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.
4270
+ *
4271
+ * 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.
4272
+ *
4273
+ * If the input or start is empty, the result is empty.
4274
+ *
4275
+ * If an empty length is provided, the behavior is the same as if length had not been provided.
4276
+ *
4277
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4278
+ *
4279
+ * @param input The input collection.
4280
+ * @returns The index of the substring.
4281
+ */
4282
+ substring: (input, startAtom, lengthAtom) => {
4283
+ return applyStringFunc((str, start, length) => {
4284
+ const startIndex = start;
4285
+ const endIndex = length ? startIndex + length : str.length;
4286
+ return startIndex < 0 || startIndex >= str.length ? undefined : str.substring(startIndex, endIndex);
4287
+ }, input, startAtom, lengthAtom);
4288
+ },
4289
+ /**
4290
+ *
4291
+ * @param input The input collection.
4292
+ * @returns The index of the substring.
4293
+ */
4294
+ startsWith: (input, prefixAtom) => {
4295
+ return applyStringFunc((str, prefix) => str.startsWith(prefix), input, prefixAtom);
4296
+ },
4297
+ /**
4298
+ *
4299
+ * @param input The input collection.
4300
+ * @returns The index of the substring.
4301
+ */
4302
+ endsWith: (input, suffixAtom) => {
4303
+ return applyStringFunc((str, suffix) => str.endsWith(suffix), input, suffixAtom);
4304
+ },
4305
+ /**
4306
+ *
4307
+ * @param input The input collection.
4308
+ * @returns The index of the substring.
4309
+ */
4310
+ contains: (input, substringAtom) => {
4311
+ return applyStringFunc((str, substring) => str.includes(substring), input, substringAtom);
4312
+ },
4313
+ /**
4314
+ *
4315
+ * @param input The input collection.
4316
+ * @returns The index of the substring.
4317
+ */
4318
+ upper: (input) => {
4319
+ return applyStringFunc((str) => str.toUpperCase(), input);
4320
+ },
4321
+ /**
4322
+ *
4323
+ * @param input The input collection.
4324
+ * @returns The index of the substring.
4325
+ */
4326
+ lower: (input) => {
4327
+ return applyStringFunc((str) => str.toLowerCase(), input);
4328
+ },
4329
+ /**
4330
+ *
4331
+ * @param input The input collection.
4332
+ * @returns The index of the substring.
4333
+ */
4334
+ replace: (input, patternAtom, substitionAtom) => {
4335
+ return applyStringFunc((str, pattern, substition) => str.replaceAll(pattern, substition), input, patternAtom, substitionAtom);
4336
+ },
4337
+ /**
4338
+ *
4339
+ * @param input The input collection.
4340
+ * @returns The index of the substring.
4341
+ */
4342
+ matches: (input, regexAtom) => {
4343
+ return applyStringFunc((str, regex) => !!str.match(regex), input, regexAtom);
4344
+ },
4345
+ /**
4346
+ *
4347
+ * @param input The input collection.
4348
+ * @returns The index of the substring.
4349
+ */
4350
+ replaceMatches: (input, regexAtom, substitionAtom) => {
4351
+ return applyStringFunc((str, pattern, substition) => str.replaceAll(pattern, substition), input, regexAtom, substitionAtom);
4352
+ },
4353
+ /**
4354
+ *
4355
+ * @param input The input collection.
4356
+ * @returns The index of the substring.
4357
+ */
4358
+ length: (input) => {
4359
+ return applyStringFunc((str) => str.length, input);
4360
+ },
4361
+ /**
4362
+ * Returns the list of characters in the input string. If the input collection is empty ({ }), the result is empty.
4363
+ *
4364
+ * See: https://hl7.org/fhirpath/#tochars-collection
4365
+ *
4366
+ * @param input The input collection.
4367
+ */
4368
+ toChars: (input) => {
4369
+ return applyStringFunc((str) => (str ? str.split('') : undefined), input);
4370
+ },
4371
+ /*
4372
+ * 5.7. Math
4373
+ */
4374
+ /**
4375
+ * Returns the absolute value of the input. When taking the absolute value of a quantity, the unit is unchanged.
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/#abs-integer-decimal-quantity
4382
+ *
4383
+ * @param input The input collection.
4384
+ * @returns A collection containing the result.
4385
+ */
4386
+ abs: (input) => {
4387
+ return applyMathFunc(Math.abs, input);
4388
+ },
4389
+ /**
4390
+ * Returns the first integer greater than or equal to the input.
4391
+ *
4392
+ * If the input collection is empty, the result is empty.
4393
+ *
4394
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4395
+ *
4396
+ * See: https://hl7.org/fhirpath/#ceiling-integer
4397
+ *
4398
+ * @param input The input collection.
4399
+ * @returns A collection containing the result.
4400
+ */
4401
+ ceiling: (input) => {
4402
+ return applyMathFunc(Math.ceil, input);
4403
+ },
4404
+ /**
4405
+ * Returns e raised to the power of the input.
4406
+ *
4407
+ * If the input collection contains an Integer, it will be implicitly converted to a Decimal and the result will be a Decimal.
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/#exp-decimal
4414
+ *
4415
+ * @param input The input collection.
4416
+ * @returns A collection containing the result.
4417
+ */
4418
+ exp: (input) => {
4419
+ return applyMathFunc(Math.exp, input);
4420
+ },
4421
+ /**
4422
+ * Returns the first integer less than or equal to the input.
4423
+ *
4424
+ * If the input collection is empty, the result is empty.
4425
+ *
4426
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4427
+ *
4428
+ * See: https://hl7.org/fhirpath/#floor-integer
4429
+ *
4430
+ * @param input The input collection.
4431
+ * @returns A collection containing the result.
4432
+ */
4433
+ floor: (input) => {
4434
+ return applyMathFunc(Math.floor, input);
4435
+ },
4436
+ /**
4437
+ * Returns the natural logarithm of the input (i.e. the logarithm base e).
4438
+ *
4439
+ * When used with an Integer, it will be implicitly converted to a Decimal.
4440
+ *
4441
+ * If the input collection is empty, the result is empty.
4442
+ *
4443
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4444
+ *
4445
+ * See: https://hl7.org/fhirpath/#ln-decimal
4446
+ *
4447
+ * @param input The input collection.
4448
+ * @returns A collection containing the result.
4449
+ */
4450
+ ln: (input) => {
4451
+ return applyMathFunc(Math.log, input);
4452
+ },
4453
+ /**
4454
+ * Returns the logarithm base base of the input number.
4455
+ *
4456
+ * When used with Integers, the arguments will be implicitly converted to Decimal.
4457
+ *
4458
+ * If base is empty, the result is empty.
4459
+ *
4460
+ * If the input collection is empty, the result is empty.
4461
+ *
4462
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4463
+ *
4464
+ * See: https://hl7.org/fhirpath/#logbase-decimal-decimal
4465
+ *
4466
+ * @param input The input collection.
4467
+ * @returns A collection containing the result.
4468
+ */
4469
+ log: (input, baseAtom) => {
4470
+ return applyMathFunc((value, base) => Math.log(value) / Math.log(base), input, baseAtom);
4471
+ },
4472
+ /**
4473
+ * 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.
4474
+ *
4475
+ * If the power cannot be represented (such as the -1 raised to the 0.5), the result is empty.
4476
+ *
4477
+ * If the input is empty, or exponent is empty, the result is empty.
4478
+ *
4479
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4480
+ *
4481
+ * See: https://hl7.org/fhirpath/#powerexponent-integer-decimal-integer-decimal
4482
+ *
4483
+ * @param input The input collection.
4484
+ * @returns A collection containing the result.
4485
+ */
4486
+ power: (input, expAtom) => {
4487
+ return applyMathFunc(Math.pow, input, expAtom);
4488
+ },
4489
+ /**
4490
+ * 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.
4491
+ *
4492
+ * If specified, the number of digits of precision must be >= 0 or the evaluation will end and signal an error to the calling environment.
4493
+ *
4494
+ * If the input collection contains a single item of type Integer, it will be implicitly converted to a Decimal.
4495
+ *
4496
+ * If the input collection is empty, the result is empty.
4497
+ *
4498
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4499
+ *
4500
+ * See: https://hl7.org/fhirpath/#roundprecision-integer-decimal
4501
+ *
4502
+ * @param input The input collection.
4503
+ * @returns A collection containing the result.
4504
+ */
4505
+ round: (input) => {
4506
+ return applyMathFunc(Math.round, input);
4507
+ },
4508
+ /**
4509
+ * Returns the square root of the input number as a Decimal.
4510
+ *
4511
+ * If the square root cannot be represented (such as the square root of -1), the result is empty.
4512
+ *
4513
+ * If the input collection is empty, the result is empty.
4514
+ *
4515
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4516
+ *
4517
+ * Note that this function is equivalent to raising a number of the power of 0.5 using the power() function.
4518
+ *
4519
+ * See: https://hl7.org/fhirpath/#sqrt-decimal
4520
+ *
4521
+ * @param input The input collection.
4522
+ * @returns A collection containing the result.
4523
+ */
4524
+ sqrt: (input) => {
4525
+ return applyMathFunc(Math.sqrt, input);
4526
+ },
4527
+ /**
4528
+ * Returns the integer portion of the input.
4529
+ *
4530
+ * If the input collection is empty, the result is empty.
4531
+ *
4532
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4533
+ *
4534
+ * See: https://hl7.org/fhirpath/#truncate-integer
4535
+ *
4536
+ * @param input The input collection.
4537
+ * @returns A collection containing the result.
4538
+ */
4539
+ truncate: (input) => {
4540
+ return applyMathFunc((x) => x | 0, input);
4541
+ },
4542
+ /*
4543
+ * 5.8. Tree navigation
4544
+ */
4545
+ children: stub,
4546
+ descendants: stub,
4547
+ /*
4548
+ * 5.9. Utility functions
4549
+ */
4550
+ /**
4551
+ * Adds a String representation of the input collection to the diagnostic log,
4552
+ * using the name argument as the name in the log. This log should be made available
4553
+ * to the user in some appropriate fashion. Does not change the input, so returns
4554
+ * the input collection as output.
4555
+ *
4556
+ * If the projection argument is used, the trace would log the result of evaluating
4557
+ * the project expression on the input, but still return the input to the trace
4558
+ * function unchanged.
4559
+ *
4560
+ * See: https://hl7.org/fhirpath/#tracename-string-projection-expression-collection
4561
+ *
4562
+ * @param input The input collection.
4563
+ * @param nameAtom The log name.
4564
+ */
4565
+ trace: (input, nameAtom) => {
4566
+ console.log('trace', input, nameAtom);
4567
+ return input;
4568
+ },
4569
+ /**
4570
+ * Returns the current date and time, including timezone offset.
4571
+ *
4572
+ * See: https://hl7.org/fhirpath/#now-datetime
4573
+ */
4574
+ now: () => {
4575
+ return [{ type: PropertyType.dateTime, value: new Date().toISOString() }];
4576
+ },
4577
+ /**
4578
+ * Returns the current time.
4579
+ *
4580
+ * See: https://hl7.org/fhirpath/#timeofday-time
4581
+ */
4582
+ timeOfDay: () => {
4583
+ return [{ type: PropertyType.time, value: new Date().toISOString().substring(11) }];
4584
+ },
4585
+ /**
4586
+ * Returns the current date.
4587
+ *
4588
+ * See: https://hl7.org/fhirpath/#today-date
4589
+ */
4590
+ today: () => {
4591
+ return [{ type: PropertyType.date, value: new Date().toISOString().substring(0, 10) }];
4592
+ },
4593
+ /**
4594
+ * Calculates the difference between two dates or date/times.
4595
+ *
4596
+ * This is not part of the official FHIRPath spec.
4597
+ *
4598
+ * IBM FHIR issue: https://github.com/IBM/FHIR/issues/1014
4599
+ * IBM FHIR PR: https://github.com/IBM/FHIR/pull/1023
4600
+ */
4601
+ between: (context, startAtom, endAtom, unitsAtom) => {
4602
+ const startDate = functions.toDateTime(startAtom.eval(context));
4603
+ if (startDate.length === 0) {
4604
+ throw new Error('Invalid start date');
4548
4605
  }
4549
- if (typeof value === 'number') {
4550
- return { namespace: 'System', name: 'Integer' };
4606
+ const endDate = functions.toDateTime(endAtom.eval(context));
4607
+ if (endDate.length === 0) {
4608
+ throw new Error('Invalid end date');
4551
4609
  }
4552
- if (value && typeof value === 'object' && 'resourceType' in value) {
4553
- return { namespace: 'FHIR', name: value.resourceType };
4610
+ const unit = unitsAtom.eval(context)[0].value;
4611
+ if (unit !== 'years' && unit !== 'months' && unit !== 'days') {
4612
+ throw new Error('Invalid units');
4554
4613
  }
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
- }
4614
+ const age = calculateAge(startDate[0].value, endDate[0].value);
4615
+ return [{ type: PropertyType.Quantity, value: { value: age[unit], unit } }];
4616
+ },
4617
+ /*
4618
+ * 6.3 Types
4619
+ */
4620
+ /**
4621
+ * The is() function is supported for backwards compatibility with previous
4622
+ * implementations of FHIRPath. Just as with the is keyword, the type argument
4623
+ * is an identifier that must resolve to the name of a type in a model.
4624
+ *
4625
+ * For implementations with compile-time typing, this requires special-case
4626
+ * handling when processing the argument to treat it as a type specifier rather
4627
+ * than an identifier expression:
4628
+ *
4629
+ * @param input
4630
+ * @param typeAtom
4631
+ * @returns
4632
+ */
4633
+ is: (input, typeAtom) => {
4634
+ let typeName = '';
4635
+ if (typeAtom instanceof SymbolAtom) {
4636
+ typeName = typeAtom.name;
4637
+ }
4638
+ else if (typeAtom instanceof DotAtom) {
4639
+ typeName = typeAtom.left.name + '.' + typeAtom.right.name;
4640
+ }
4641
+ if (!typeName) {
4642
+ return [];
4643
+ }
4644
+ return input.map((value) => ({ type: PropertyType.boolean, value: fhirPathIs(value, typeName) }));
4645
+ },
4646
+ /*
4647
+ * 6.5 Boolean logic
4648
+ */
4649
+ /**
4650
+ * 6.5.3. not() : Boolean
4651
+ *
4652
+ * Returns true if the input collection evaluates to false, and false if it evaluates to true. Otherwise, the result is empty ({ }):
4653
+ *
4654
+ * @param input
4655
+ * @returns
4656
+ */
4657
+ not: (input) => {
4658
+ return functions.toBoolean(input).map((value) => ({ type: PropertyType.boolean, value: !value.value }));
4659
+ },
4660
+ /*
4661
+ * Additional functions
4662
+ * See: https://hl7.org/fhir/fhirpath.html#functions
4663
+ */
4664
+ /**
4665
+ * 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.
4666
+ * The items in the collection may also represent a Reference, in which case the Reference.reference is resolved.
4667
+ * @param input The input collection.
4668
+ * @returns
4669
+ */
4670
+ resolve: (input) => {
4671
+ return input
4672
+ .map((e) => {
4673
+ const value = e.value;
4674
+ let refStr;
4675
+ if (typeof value === 'string') {
4676
+ refStr = value;
4677
+ }
4678
+ else if (typeof value === 'object') {
4679
+ const ref = value;
4680
+ if (ref.resource) {
4681
+ return { type: PropertyType.BackboneElement, value: ref.resource };
4682
+ }
4683
+ refStr = ref.reference;
4684
+ }
4685
+ if (!refStr) {
4686
+ return { type: PropertyType.BackboneElement, value: null };
4687
+ }
4688
+ const [resourceType, id] = refStr.split('/');
4689
+ return { type: PropertyType.BackboneElement, value: { resourceType, id } };
4690
+ })
4691
+ .filter((e) => !!e.value);
4692
+ },
4693
+ /**
4694
+ * The as operator can be used to treat a value as a specific type.
4695
+ * @param context The context value.
4696
+ * @returns The value as the specific type.
4697
+ */
4698
+ as: (context) => {
4699
+ return context;
4700
+ },
4701
+ /*
4702
+ * 12. Formal Specifications
4703
+ */
4704
+ /**
4705
+ * Returns the type of the input.
4706
+ *
4707
+ * 12.2. Model Information
4708
+ *
4709
+ * The model information returned by the reflection function type() is specified as an
4710
+ * XML Schema document (xsd) and included in this specification at the following link:
4711
+ * https://hl7.org/fhirpath/modelinfo.xsd
4712
+ *
4713
+ * See: https://hl7.org/fhirpath/#model-information
4714
+ *
4715
+ * @param input The input collection.
4716
+ * @returns
4717
+ */
4718
+ type: (input) => {
4719
+ return input.map(({ value }) => {
4720
+ if (typeof value === 'boolean') {
4721
+ return { type: PropertyType.BackboneElement, value: { namespace: 'System', name: 'Boolean' } };
4722
+ }
4723
+ if (typeof value === 'number') {
4724
+ return { type: PropertyType.BackboneElement, value: { namespace: 'System', name: 'Integer' } };
4725
+ }
4726
+ if (value && typeof value === 'object' && 'resourceType' in value) {
4727
+ return {
4728
+ type: PropertyType.BackboneElement,
4729
+ value: { namespace: 'FHIR', name: value.resourceType },
4730
+ };
4731
+ }
4732
+ return { type: PropertyType.BackboneElement, value: null };
4733
+ });
4734
+ },
4735
+ conformsTo: (input, systemAtom) => {
4736
+ const system = systemAtom.eval([])[0].value;
4737
+ if (!system.startsWith('http://hl7.org/fhir/StructureDefinition/')) {
4738
+ throw new Error('Expected a StructureDefinition URL');
4739
+ }
4740
+ const expectedResourceType = system.replace('http://hl7.org/fhir/StructureDefinition/', '');
4741
+ return input.map((value) => {
4742
+ var _a;
4743
+ return ({
4744
+ type: PropertyType.boolean,
4745
+ value: ((_a = value.value) === null || _a === void 0 ? void 0 : _a.resourceType) === expectedResourceType,
4746
+ });
4747
+ });
4748
+ },
4749
+ };
4566
4750
  /*
4567
4751
  * Helper utilities
4568
4752
  */
@@ -4570,115 +4754,46 @@ function applyStringFunc(func, input, ...argsAtoms) {
4570
4754
  if (input.length === 0) {
4571
4755
  return [];
4572
4756
  }
4573
- const [value] = validateInput(input, 1);
4757
+ const [{ value }] = validateInput(input, 1);
4574
4758
  if (typeof value !== 'string') {
4575
4759
  throw new Error('String function cannot be called with non-string');
4576
4760
  }
4577
- const result = func(value, ...argsAtoms.map((atom) => atom && atom.eval(value)));
4578
- return result === undefined ? [] : [result];
4761
+ 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); }));
4762
+ if (result === undefined) {
4763
+ return [];
4764
+ }
4765
+ if (Array.isArray(result)) {
4766
+ return result.map(toTypedValue);
4767
+ }
4768
+ return [toTypedValue(result)];
4579
4769
  }
4580
4770
  function applyMathFunc(func, input, ...argsAtoms) {
4581
4771
  if (input.length === 0) {
4582
4772
  return [];
4583
4773
  }
4584
- const [value] = validateInput(input, 1);
4774
+ const [{ value }] = validateInput(input, 1);
4585
4775
  const quantity = isQuantity(value);
4586
4776
  const numberInput = quantity ? value.value : value;
4587
4777
  if (typeof numberInput !== 'number') {
4588
4778
  throw new Error('Math function cannot be called with non-number');
4589
4779
  }
4590
- const result = func(numberInput, ...argsAtoms.map((atom) => atom.eval(undefined)));
4591
- return quantity ? [Object.assign(Object.assign({}, value), { value: result })] : [result];
4780
+ 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; }));
4781
+ const type = quantity ? PropertyType.Quantity : input[0].type;
4782
+ const returnValue = quantity ? Object.assign(Object.assign({}, value), { value: result }) : result;
4783
+ return [{ type, value: returnValue }];
4592
4784
  }
4593
4785
  function validateInput(input, count) {
4594
4786
  if (input.length !== count) {
4595
4787
  throw new Error(`Expected ${count} arguments`);
4596
4788
  }
4789
+ for (const element of input) {
4790
+ if (element === null || element === undefined) {
4791
+ throw new Error('Expected non-null argument');
4792
+ }
4793
+ }
4597
4794
  return input;
4598
4795
  }
4599
4796
 
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
4797
  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
4798
  function tokenize(str) {
4684
4799
  return new Tokenizer(str).tokenize();
@@ -4997,24 +5112,24 @@ function parseQuantity(str) {
4997
5112
  }
4998
5113
  const parserBuilder = new ParserBuilder()
4999
5114
  .registerPrefix('String', {
5000
- parse: (_, token) => new LiteralAtom(token.value),
5115
+ parse: (_, token) => new LiteralAtom({ type: PropertyType.string, value: token.value }),
5001
5116
  })
5002
5117
  .registerPrefix('DateTime', {
5003
- parse: (_, token) => new LiteralAtom(parseDateString(token.value)),
5118
+ parse: (_, token) => new LiteralAtom({ type: PropertyType.dateTime, value: parseDateString(token.value) }),
5004
5119
  })
5005
5120
  .registerPrefix('Quantity', {
5006
- parse: (_, token) => new LiteralAtom(parseQuantity(token.value)),
5121
+ parse: (_, token) => new LiteralAtom({ type: PropertyType.Quantity, value: parseQuantity(token.value) }),
5007
5122
  })
5008
5123
  .registerPrefix('Number', {
5009
- parse: (_, token) => new LiteralAtom(parseFloat(token.value)),
5124
+ parse: (_, token) => new LiteralAtom({ type: PropertyType.decimal, value: parseFloat(token.value) }),
5010
5125
  })
5011
5126
  .registerPrefix('Symbol', {
5012
5127
  parse: (_, token) => {
5013
5128
  if (token.value === 'false') {
5014
- return new LiteralAtom(false);
5129
+ return new LiteralAtom({ type: PropertyType.boolean, value: false });
5015
5130
  }
5016
5131
  if (token.value === 'true') {
5017
- return new LiteralAtom(true);
5132
+ return new LiteralAtom({ type: PropertyType.boolean, value: true });
5018
5133
  }
5019
5134
  return new SymbolAtom(token.value);
5020
5135
  },
@@ -5035,10 +5150,10 @@ const parserBuilder = new ParserBuilder()
5035
5150
  .infixLeft('!=', 9 /* Precedence.Equals */, (left, _, right) => new NotEqualsAtom(left, right))
5036
5151
  .infixLeft('~', 9 /* Precedence.Equivalent */, (left, _, right) => new EquivalentAtom(left, right))
5037
5152
  .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))
5153
+ .infixLeft('<', 8 /* Precedence.LessThan */, (left, _, right) => new ArithemticOperatorAtom(left, right, (x, y) => x < y))
5154
+ .infixLeft('<=', 8 /* Precedence.LessThanOrEquals */, (left, _, right) => new ArithemticOperatorAtom(left, right, (x, y) => x <= y))
5155
+ .infixLeft('>', 8 /* Precedence.GreaterThan */, (left, _, right) => new ArithemticOperatorAtom(left, right, (x, y) => x > y))
5156
+ .infixLeft('>=', 8 /* Precedence.GreaterThanOrEquals */, (left, _, right) => new ArithemticOperatorAtom(left, right, (x, y) => x >= y))
5042
5157
  .infixLeft('&', 5 /* Precedence.Ampersand */, (left, _, right) => new ConcatAtom(left, right))
5043
5158
  .infixLeft('Symbol', 6 /* Precedence.Is */, (left, symbol, right) => {
5044
5159
  switch (symbol.value) {
@@ -5087,7 +5202,21 @@ function parseFhirPath(input) {
5087
5202
  * @returns The result of the FHIRPath expression against the resource or object.
5088
5203
  */
5089
5204
  function evalFhirPath(input, context) {
5090
- return parseFhirPath(input).eval(context);
5205
+ // eval requires a TypedValue array
5206
+ // As a convenience, we can accept array or non-array, and TypedValue or unknown value
5207
+ if (!Array.isArray(context)) {
5208
+ context = [context];
5209
+ }
5210
+ const array = Array.isArray(context) ? context : [context];
5211
+ for (let i = 0; i < array.length; i++) {
5212
+ const el = array[i];
5213
+ if (!(typeof el === 'object' && 'type' in el && 'value' in el)) {
5214
+ array[i] = { type: PropertyType.BackboneElement, value: el };
5215
+ }
5216
+ }
5217
+ return parseFhirPath(input)
5218
+ .eval(array)
5219
+ .map((e) => e.value);
5091
5220
  }
5092
5221
 
5093
5222
  const SEGMENT_SEPARATOR = '\r';
@@ -5280,7 +5409,12 @@ function getSearchParameterType(searchParam, propertyType) {
5280
5409
  let type = SearchParameterType.TEXT;
5281
5410
  switch (searchParam.type) {
5282
5411
  case 'date':
5283
- type = SearchParameterType.DATE;
5412
+ if (propertyType === PropertyType.dateTime || propertyType === PropertyType.instant) {
5413
+ type = SearchParameterType.DATETIME;
5414
+ }
5415
+ else {
5416
+ type = SearchParameterType.DATE;
5417
+ }
5284
5418
  break;
5285
5419
  case 'number':
5286
5420
  type = SearchParameterType.NUMBER;