@medplum/core 0.9.6 → 0.9.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/index.js CHANGED
@@ -343,6 +343,30 @@
343
343
  }
344
344
  }
345
345
  }
346
+ /**
347
+ * Returns the resource identifier for the given system.
348
+ *
349
+ * If multiple identifiers exist with the same system, the first one is returned.
350
+ *
351
+ * If the system is not found, then returns undefined.
352
+ *
353
+ * @param resource The resource to check.
354
+ * @param system The identifier system.
355
+ * @returns The identifier value if found; otherwise undefined.
356
+ */
357
+ function getIdentifier(resource, system) {
358
+ const identifiers = resource.identifier;
359
+ if (!identifiers) {
360
+ return undefined;
361
+ }
362
+ const array = Array.isArray(identifiers) ? identifiers : [identifiers];
363
+ for (const identifier of array) {
364
+ if (identifier.system === system) {
365
+ return identifier.value;
366
+ }
367
+ }
368
+ return undefined;
369
+ }
346
370
  /**
347
371
  * Returns an extension value by extension URLs.
348
372
  * @param resource The base resource.
@@ -408,7 +432,7 @@
408
432
  * @param object2 The second object.
409
433
  * @returns True if the objects are equal.
410
434
  */
411
- function deepEquals(object1, object2, path) {
435
+ function deepEquals$1(object1, object2, path) {
412
436
  if (object1 === object2) {
413
437
  return true;
414
438
  }
@@ -424,10 +448,10 @@
424
448
  if (Array.isArray(object1) || Array.isArray(object2)) {
425
449
  return false;
426
450
  }
427
- if (isObject(object1) && isObject(object2)) {
451
+ if (isObject$1(object1) && isObject$1(object2)) {
428
452
  return deepEqualsObject(object1, object2, path);
429
453
  }
430
- if (isObject(object1) || isObject(object2)) {
454
+ if (isObject$1(object1) || isObject$1(object2)) {
431
455
  return false;
432
456
  }
433
457
  return false;
@@ -437,7 +461,7 @@
437
461
  return false;
438
462
  }
439
463
  for (let i = 0; i < array1.length; i++) {
440
- if (!deepEquals(array1[i], array2[i])) {
464
+ if (!deepEquals$1(array1[i], array2[i])) {
441
465
  return false;
442
466
  }
443
467
  }
@@ -455,7 +479,7 @@
455
479
  for (const key of keySet) {
456
480
  const val1 = object1[key];
457
481
  const val2 = object2[key];
458
- if (!deepEquals(val1, val2, key)) {
482
+ if (!deepEquals$1(val1, val2, key)) {
459
483
  return false;
460
484
  }
461
485
  }
@@ -474,7 +498,7 @@
474
498
  * @param object The candidate object.
475
499
  * @returns True if the input is a non-null non-undefined object.
476
500
  */
477
- function isObject(obj) {
501
+ function isObject$1(obj) {
478
502
  return obj !== null && typeof obj === 'object';
479
503
  }
480
504
  /**
@@ -1018,7 +1042,7 @@
1018
1042
  params.push('_count=' + definition.count);
1019
1043
  }
1020
1044
  if (definition.total !== undefined) {
1021
- params.push('_total=' + encodeURIComponent(definition.total));
1045
+ params.push('_total=' + definition.total);
1022
1046
  }
1023
1047
  if (params.length === 0) {
1024
1048
  return '';
@@ -1379,7 +1403,6 @@
1379
1403
  * const bundle = await medplum.search('Patient?name=Alice');
1380
1404
  * console.log(bundle.total);
1381
1405
  * ```
1382
- *
1383
1406
  */
1384
1407
  class MedplumClient extends EventTarget {
1385
1408
  constructor(options) {
@@ -1428,6 +1451,15 @@
1428
1451
  }
1429
1452
  __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_setupStorageListener).call(this);
1430
1453
  }
1454
+ /**
1455
+ * Returns the current base URL for all API requests.
1456
+ * By default, this is set to `https://api.medplum.com/`.
1457
+ * This can be overridden by setting the `baseUrl` option when creating the client.
1458
+ * @returns The current base URL for all API requests.
1459
+ */
1460
+ getBaseUrl() {
1461
+ return __classPrivateFieldGet(this, _MedplumClient_baseUrl, "f");
1462
+ }
1431
1463
  /**
1432
1464
  * Clears all auth state including local storage and session storage.
1433
1465
  */
@@ -1452,6 +1484,7 @@
1452
1484
  * @returns Promise to the response content.
1453
1485
  */
1454
1486
  get(url, options = {}) {
1487
+ url = url.toString();
1455
1488
  if (!(options === null || options === void 0 ? void 0 : options.cache)) {
1456
1489
  const cached = __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").get(url);
1457
1490
  if (cached) {
@@ -1476,6 +1509,7 @@
1476
1509
  * @returns Promise to the response content.
1477
1510
  */
1478
1511
  post(url, body, contentType, options = {}) {
1512
+ url = url.toString();
1479
1513
  if (body) {
1480
1514
  __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_setRequestBody).call(this, options, body);
1481
1515
  }
@@ -1499,6 +1533,7 @@
1499
1533
  * @returns Promise to the response content.
1500
1534
  */
1501
1535
  put(url, body, contentType, options = {}) {
1536
+ url = url.toString();
1502
1537
  if (body) {
1503
1538
  __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_setRequestBody).call(this, options, body);
1504
1539
  }
@@ -1521,6 +1556,7 @@
1521
1556
  * @returns Promise to the response content.
1522
1557
  */
1523
1558
  patch(url, operations, options = {}) {
1559
+ url = url.toString();
1524
1560
  __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_setRequestBody).call(this, options, operations);
1525
1561
  __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_setRequestContentType).call(this, options, PATCH_CONTENT_TYPE);
1526
1562
  __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").delete(url);
@@ -1538,6 +1574,7 @@
1538
1574
  * @returns Promise to the response content.
1539
1575
  */
1540
1576
  delete(url, options = {}) {
1577
+ url = url.toString();
1541
1578
  __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").delete(url);
1542
1579
  return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_request).call(this, 'DELETE', url, options);
1543
1580
  }
@@ -1614,9 +1651,7 @@
1614
1651
  * @returns The well-formed FHIR URL.
1615
1652
  */
1616
1653
  fhirUrl(...path) {
1617
- const builder = [__classPrivateFieldGet(this, _MedplumClient_baseUrl, "f"), 'fhir/R4'];
1618
- path.forEach((p) => builder.push('/', encodeURIComponent(p)));
1619
- return builder.join('');
1654
+ return new URL(__classPrivateFieldGet(this, _MedplumClient_baseUrl, "f") + 'fhir/R4/' + path.join('/'));
1620
1655
  }
1621
1656
  /**
1622
1657
  * Sends a FHIR search request.
@@ -1695,13 +1730,9 @@
1695
1730
  * @returns Promise to the search result bundle.
1696
1731
  */
1697
1732
  searchOne(query, options = {}) {
1698
- var _a, _b;
1699
- return __awaiter(this, void 0, void 0, function* () {
1700
- const search = typeof query === 'string' ? parseSearchDefinition(query) : query;
1701
- search.count = 1;
1702
- const bundle = yield this.search(search, options);
1703
- return (_b = (_a = bundle.entry) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.resource;
1704
- });
1733
+ const search = typeof query === 'string' ? parseSearchDefinition(query) : query;
1734
+ search.count = 1;
1735
+ return new ReadablePromise(this.search(search, options).then((bundle) => { var _a, _b; return (_b = (_a = bundle.entry) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.resource; }));
1705
1736
  }
1706
1737
  /**
1707
1738
  * Sends a FHIR search request for an array of resources.
@@ -1723,11 +1754,7 @@
1723
1754
  * @returns Promise to the search result bundle.
1724
1755
  */
1725
1756
  searchResources(query, options = {}) {
1726
- var _a, _b;
1727
- return __awaiter(this, void 0, void 0, function* () {
1728
- const bundle = yield this.search(query, options);
1729
- return (_b = (_a = bundle.entry) === null || _a === void 0 ? void 0 : _a.map((entry) => entry.resource)) !== null && _b !== void 0 ? _b : [];
1730
- });
1757
+ return new ReadablePromise(this.search(query, options).then((bundle) => { var _a, _b; return (_b = (_a = bundle.entry) === null || _a === void 0 ? void 0 : _a.map((entry) => entry.resource)) !== null && _b !== void 0 ? _b : []; }));
1731
1758
  }
1732
1759
  /**
1733
1760
  * Searches a ValueSet resource using the "expand" operation.
@@ -1737,9 +1764,10 @@
1737
1764
  * @returns Promise to expanded ValueSet.
1738
1765
  */
1739
1766
  searchValueSet(system, filter, options = {}) {
1740
- return this.get(this.fhirUrl('ValueSet', '$expand') +
1741
- `?url=${encodeURIComponent(system)}` +
1742
- `&filter=${encodeURIComponent(filter)}`, options);
1767
+ const url = this.fhirUrl('ValueSet', '$expand');
1768
+ url.searchParams.set('url', system);
1769
+ url.searchParams.set('filter', filter);
1770
+ return this.get(url.toString(), options);
1743
1771
  }
1744
1772
  /**
1745
1773
  * Returns a cached resource if it is available.
@@ -1748,7 +1776,7 @@
1748
1776
  * @returns The resource if it is available in the cache; undefined otherwise.
1749
1777
  */
1750
1778
  getCached(resourceType, id) {
1751
- const cached = __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").get(this.fhirUrl(resourceType, id));
1779
+ const cached = __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").get(this.fhirUrl(resourceType, id).toString());
1752
1780
  return cached && !cached.isPending() ? cached.read() : undefined;
1753
1781
  }
1754
1782
  /**
@@ -1823,7 +1851,7 @@
1823
1851
  readReference(reference) {
1824
1852
  const refString = reference === null || reference === void 0 ? void 0 : reference.reference;
1825
1853
  if (!refString) {
1826
- return new ReadablePromise(Promise.reject('Missing reference'));
1854
+ return new ReadablePromise(Promise.reject(new Error('Missing reference')));
1827
1855
  }
1828
1856
  const [resourceType, id] = refString.split('/');
1829
1857
  return this.readResource(resourceType, id);
@@ -1851,7 +1879,7 @@
1851
1879
  readCachedReference(reference) {
1852
1880
  const refString = reference === null || reference === void 0 ? void 0 : reference.reference;
1853
1881
  if (!refString) {
1854
- return new ReadablePromise(Promise.reject('Missing reference'));
1882
+ return new ReadablePromise(Promise.reject(new Error('Missing reference')));
1855
1883
  }
1856
1884
  const [resourceType, id] = refString.split('/');
1857
1885
  return this.readCached(resourceType, id);
@@ -1878,7 +1906,7 @@
1878
1906
  return Promise.resolve(__classPrivateFieldGet(this, _MedplumClient_schema, "f"));
1879
1907
  }
1880
1908
  const query = `{
1881
- StructureDefinitionList(name: "${encodeURIComponent(resourceType)}") {
1909
+ StructureDefinitionList(name: "${resourceType}") {
1882
1910
  name,
1883
1911
  description,
1884
1912
  snapshot {
@@ -1898,7 +1926,7 @@
1898
1926
  }
1899
1927
  }
1900
1928
  }
1901
- SearchParameterList(base: "${encodeURIComponent(resourceType)}", _count: 100) {
1929
+ SearchParameterList(base: "${resourceType}", _count: 100) {
1902
1930
  base,
1903
1931
  code,
1904
1932
  type,
@@ -1932,7 +1960,7 @@
1932
1960
  *
1933
1961
  * @param resourceType The FHIR resource type.
1934
1962
  * @param id The resource ID.
1935
- * @returns The resource if available; undefined otherwise.
1963
+ * @returns Promise to the resource history.
1936
1964
  */
1937
1965
  readHistory(resourceType, id) {
1938
1966
  return this.get(this.fhirUrl(resourceType, id, '_history'));
@@ -1984,7 +2012,7 @@
1984
2012
  */
1985
2013
  createResource(resource) {
1986
2014
  if (!resource.resourceType) {
1987
- return Promise.reject('Missing resourceType');
2015
+ throw new Error('Missing resourceType');
1988
2016
  }
1989
2017
  return this.post(this.fhirUrl(resource.resourceType), resource);
1990
2018
  }
@@ -2056,9 +2084,9 @@
2056
2084
  * @returns The result of the create operation.
2057
2085
  */
2058
2086
  createBinary(data, filename, contentType) {
2059
- let url = this.fhirUrl('Binary');
2087
+ const url = this.fhirUrl('Binary');
2060
2088
  if (filename) {
2061
- url += '?_filename=' + encodeURIComponent(filename);
2089
+ url.searchParams.set('_filename', filename);
2062
2090
  }
2063
2091
  return this.post(url, data, contentType);
2064
2092
  }
@@ -2084,12 +2112,46 @@
2084
2112
  * @returns The result of the create operation.
2085
2113
  */
2086
2114
  createPdf(docDefinition, filename) {
2087
- let url = this.fhirUrl('Binary') + '/$pdf';
2115
+ const url = this.fhirUrl('Binary', '$pdf');
2088
2116
  if (filename) {
2089
- url += '?_filename=' + encodeURIComponent(filename);
2117
+ url.searchParams.set('_filename', filename);
2090
2118
  }
2091
2119
  return this.post(url, docDefinition, 'application/json');
2092
2120
  }
2121
+ /**
2122
+ * Creates a FHIR `Communication` resource with the provided data content.
2123
+ *
2124
+ * This is a convenience method to handle commmon cases where a `Communication` resource is created with a `payload`.
2125
+ *
2126
+ * @param resource The FHIR resource to comment on.
2127
+ * @param text The text of the comment.
2128
+ * @returns The result of the create operation.
2129
+ */
2130
+ createComment(resource, text) {
2131
+ const profile = this.getProfile();
2132
+ let encounter = undefined;
2133
+ let subject = undefined;
2134
+ if (resource.resourceType === 'Encounter') {
2135
+ encounter = createReference(resource);
2136
+ subject = resource.subject;
2137
+ }
2138
+ if (resource.resourceType === 'ServiceRequest') {
2139
+ encounter = resource.encounter;
2140
+ subject = resource.subject;
2141
+ }
2142
+ if (resource.resourceType === 'Patient') {
2143
+ subject = createReference(resource);
2144
+ }
2145
+ return this.createResource({
2146
+ resourceType: 'Communication',
2147
+ basedOn: [createReference(resource)],
2148
+ encounter,
2149
+ subject,
2150
+ sender: profile ? createReference(profile) : undefined,
2151
+ sent: new Date().toISOString(),
2152
+ payload: [{ contentString: text }],
2153
+ });
2154
+ }
2093
2155
  /**
2094
2156
  * Updates a FHIR resource.
2095
2157
  *
@@ -2116,10 +2178,10 @@
2116
2178
  */
2117
2179
  updateResource(resource) {
2118
2180
  if (!resource.resourceType) {
2119
- return Promise.reject('Missing resourceType');
2181
+ throw new Error('Missing resourceType');
2120
2182
  }
2121
2183
  if (!resource.id) {
2122
- return Promise.reject('Missing id');
2184
+ throw new Error('Missing id');
2123
2185
  }
2124
2186
  return this.put(this.fhirUrl(resource.resourceType, resource.id), resource);
2125
2187
  }
@@ -2205,7 +2267,7 @@
2205
2267
  * @returns Promise to the operation outcome.
2206
2268
  */
2207
2269
  sendEmail(email) {
2208
- return this.post('email/v1/send', email);
2270
+ return this.post('email/v1/send', email, 'application/json');
2209
2271
  }
2210
2272
  graphql(query, options) {
2211
2273
  return this.post(this.fhirUrl('$graphql'), { query }, JSON_CONTENT_TYPE, options);
@@ -2226,6 +2288,9 @@
2226
2288
  yield __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_refreshProfile).call(this);
2227
2289
  });
2228
2290
  }
2291
+ getAccessToken() {
2292
+ return __classPrivateFieldGet(this, _MedplumClient_accessToken, "f");
2293
+ }
2229
2294
  setAccessToken(accessToken) {
2230
2295
  __classPrivateFieldSet(this, _MedplumClient_accessToken, accessToken, "f");
2231
2296
  __classPrivateFieldSet(this, _MedplumClient_refreshToken, undefined, "f");
@@ -2264,7 +2329,7 @@
2264
2329
  yield __classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f");
2265
2330
  }
2266
2331
  __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_addFetchOptionsDefaults).call(this, options);
2267
- const response = yield __classPrivateFieldGet(this, _MedplumClient_fetch, "f").call(this, url, options);
2332
+ const response = yield __classPrivateFieldGet(this, _MedplumClient_fetch, "f").call(this, url.toString(), options);
2268
2333
  return response.blob();
2269
2334
  });
2270
2335
  }
@@ -2277,29 +2342,35 @@
2277
2342
  const pkceState = __classPrivateFieldGet(this, _MedplumClient_storage, "f").getString('pkceState');
2278
2343
  if (!pkceState) {
2279
2344
  this.clear();
2280
- return Promise.reject('Invalid PCKE state');
2345
+ throw new Error('Invalid PCKE state');
2281
2346
  }
2282
2347
  const codeVerifier = __classPrivateFieldGet(this, _MedplumClient_storage, "f").getString('codeVerifier');
2283
2348
  if (!codeVerifier) {
2284
2349
  this.clear();
2285
- return Promise.reject('Invalid PCKE code verifier');
2286
- }
2287
- return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_fetchTokens).call(this, 'grant_type=authorization_code' +
2288
- (__classPrivateFieldGet(this, _MedplumClient_clientId, "f") ? '&client_id=' + encodeURIComponent(__classPrivateFieldGet(this, _MedplumClient_clientId, "f")) : '') +
2289
- '&code_verifier=' +
2290
- encodeURIComponent(codeVerifier) +
2291
- '&redirect_uri=' +
2292
- encodeURIComponent(getBaseUrl()) +
2293
- '&code=' +
2294
- encodeURIComponent(code));
2295
- }
2296
- clientCredentials(clientId, clientSecret) {
2350
+ throw new Error('Invalid PCKE code verifier');
2351
+ }
2352
+ const formBody = new URLSearchParams();
2353
+ formBody.set('grant_type', 'authorization_code');
2354
+ formBody.set('client_id', __classPrivateFieldGet(this, _MedplumClient_clientId, "f"));
2355
+ formBody.set('code_verifier', codeVerifier);
2356
+ formBody.set('code', code);
2357
+ formBody.set('redirect_uri', getBaseUrl());
2358
+ return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_fetchTokens).call(this, formBody);
2359
+ }
2360
+ /**
2361
+ * Starts a new OAuth2 client credentials flow.
2362
+ * See: https://datatracker.ietf.org/doc/html/rfc6749#section-4.4
2363
+ * @param clientId The client ID.
2364
+ * @param clientSecret The client secret.
2365
+ * @returns Promise that resolves to the client profile.
2366
+ */
2367
+ startClientLogin(clientId, clientSecret) {
2297
2368
  return __awaiter(this, void 0, void 0, function* () {
2298
- return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_fetchTokens).call(this, 'grant_type=client_credentials' +
2299
- '&client_id=' +
2300
- encodeURIComponent(clientId) +
2301
- '&client_secret=' +
2302
- encodeURIComponent(clientSecret));
2369
+ const formBody = new URLSearchParams();
2370
+ formBody.set('grant_type', 'client_credentials');
2371
+ formBody.set('client_id', clientId);
2372
+ formBody.set('client_secret', clientSecret);
2373
+ return __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_fetchTokens).call(this, formBody);
2303
2374
  });
2304
2375
  }
2305
2376
  }
@@ -2375,7 +2446,7 @@
2375
2446
  options.body = data;
2376
2447
  }
2377
2448
  else if (data) {
2378
- options.body = stringify(data);
2449
+ options.body = JSON.stringify(data);
2379
2450
  }
2380
2451
  }, _MedplumClient_handleUnauthenticated = function _MedplumClient_handleUnauthenticated(method, url, options) {
2381
2452
  return __awaiter(this, void 0, void 0, function* () {
@@ -2400,25 +2471,16 @@
2400
2471
  __classPrivateFieldGet(this, _MedplumClient_storage, "f").setString('codeChallenge', codeChallenge);
2401
2472
  });
2402
2473
  }, _MedplumClient_requestAuthorization = function _MedplumClient_requestAuthorization() {
2403
- return __awaiter(this, void 0, void 0, function* () {
2404
- if (!__classPrivateFieldGet(this, _MedplumClient_authorizeUrl, "f")) {
2405
- return Promise.reject('Missing authorize URL');
2406
- }
2407
- __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_startPkce).call(this);
2408
- window.location.assign(__classPrivateFieldGet(this, _MedplumClient_authorizeUrl, "f") +
2409
- '?response_type=code' +
2410
- '&state=' +
2411
- encodeURIComponent(__classPrivateFieldGet(this, _MedplumClient_storage, "f").getString('pkceState')) +
2412
- '&client_id=' +
2413
- encodeURIComponent(__classPrivateFieldGet(this, _MedplumClient_clientId, "f")) +
2414
- '&redirect_uri=' +
2415
- encodeURIComponent(getBaseUrl()) +
2416
- '&scope=' +
2417
- encodeURIComponent(DEFAULT_SCOPE) +
2418
- '&code_challenge_method=S256' +
2419
- '&code_challenge=' +
2420
- encodeURIComponent(__classPrivateFieldGet(this, _MedplumClient_storage, "f").getString('codeChallenge')));
2421
- });
2474
+ __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_startPkce).call(this);
2475
+ const url = new URL(__classPrivateFieldGet(this, _MedplumClient_authorizeUrl, "f"));
2476
+ url.searchParams.set('response_type', 'code');
2477
+ url.searchParams.set('state', __classPrivateFieldGet(this, _MedplumClient_storage, "f").getString('pkceState'));
2478
+ url.searchParams.set('client_id', __classPrivateFieldGet(this, _MedplumClient_clientId, "f"));
2479
+ url.searchParams.set('redirect_uri', getBaseUrl());
2480
+ url.searchParams.set('scope', DEFAULT_SCOPE);
2481
+ url.searchParams.set('code_challenge_method', 'S256');
2482
+ url.searchParams.set('code_challenge', __classPrivateFieldGet(this, _MedplumClient_storage, "f").getString('codeChallenge'));
2483
+ window.location.assign(url.toString());
2422
2484
  }, _MedplumClient_refresh = function _MedplumClient_refresh() {
2423
2485
  return __awaiter(this, void 0, void 0, function* () {
2424
2486
  if (__classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f")) {
@@ -2426,20 +2488,17 @@
2426
2488
  }
2427
2489
  if (!__classPrivateFieldGet(this, _MedplumClient_refreshToken, "f")) {
2428
2490
  this.clear();
2429
- return Promise.reject('Invalid refresh token');
2491
+ throw new Error('Invalid refresh token');
2430
2492
  }
2431
- __classPrivateFieldSet(this, _MedplumClient_refreshPromise, __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_fetchTokens).call(this, 'grant_type=refresh_token' +
2432
- '&client_id=' +
2433
- encodeURIComponent(__classPrivateFieldGet(this, _MedplumClient_clientId, "f")) +
2434
- '&refresh_token=' +
2435
- encodeURIComponent(__classPrivateFieldGet(this, _MedplumClient_refreshToken, "f"))), "f");
2493
+ const formBody = new URLSearchParams();
2494
+ formBody.set('grant_type', 'refresh_token');
2495
+ formBody.set('client_id', __classPrivateFieldGet(this, _MedplumClient_clientId, "f"));
2496
+ formBody.set('refresh_token', __classPrivateFieldGet(this, _MedplumClient_refreshToken, "f"));
2497
+ __classPrivateFieldSet(this, _MedplumClient_refreshPromise, __classPrivateFieldGet(this, _MedplumClient_instances, "m", _MedplumClient_fetchTokens).call(this, formBody), "f");
2436
2498
  yield __classPrivateFieldGet(this, _MedplumClient_refreshPromise, "f");
2437
2499
  });
2438
2500
  }, _MedplumClient_fetchTokens = function _MedplumClient_fetchTokens(formBody) {
2439
2501
  return __awaiter(this, void 0, void 0, function* () {
2440
- if (!__classPrivateFieldGet(this, _MedplumClient_tokenUrl, "f")) {
2441
- return Promise.reject('Missing token URL');
2442
- }
2443
2502
  return __classPrivateFieldGet(this, _MedplumClient_fetch, "f").call(this, __classPrivateFieldGet(this, _MedplumClient_tokenUrl, "f"), {
2444
2503
  method: 'POST',
2445
2504
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
@@ -2447,7 +2506,7 @@
2447
2506
  })
2448
2507
  .then((response) => {
2449
2508
  if (!response.ok) {
2450
- return Promise.reject('Failed to fetch tokens');
2509
+ throw new Error('Failed to fetch tokens');
2451
2510
  }
2452
2511
  return response.json();
2453
2512
  })
@@ -2461,12 +2520,12 @@
2461
2520
  const tokenPayload = parseJWTPayload(token);
2462
2521
  if (Date.now() >= tokenPayload.exp * 1000) {
2463
2522
  this.clear();
2464
- return Promise.reject('Token expired');
2523
+ throw new Error('Token expired');
2465
2524
  }
2466
2525
  // Verify app_client_id
2467
2526
  if (__classPrivateFieldGet(this, _MedplumClient_clientId, "f") && tokenPayload.client_id !== __classPrivateFieldGet(this, _MedplumClient_clientId, "f")) {
2468
2527
  this.clear();
2469
- return Promise.reject('Token was not issued for this audience');
2528
+ throw new Error('Token was not issued for this audience');
2470
2529
  }
2471
2530
  yield this.setActiveLogin({
2472
2531
  accessToken: token,
@@ -2497,6 +2556,2546 @@
2497
2556
  return window.location.protocol + '//' + window.location.host + '/';
2498
2557
  }
2499
2558
 
2559
+ /**
2560
+ * Ensures that the value is wrapped in an array.
2561
+ * @param input The input as a an array or a value.
2562
+ * @returns The input as an array.
2563
+ */
2564
+ function ensureArray(input) {
2565
+ if (input === null || input === undefined) {
2566
+ return [];
2567
+ }
2568
+ return Array.isArray(input) ? input : [input];
2569
+ }
2570
+ /**
2571
+ * Applies a function to single value or an array of values.
2572
+ * @param context The context which will be passed to the function.
2573
+ * @param fn The function to apply.
2574
+ * @returns The result of the function.
2575
+ */
2576
+ function applyMaybeArray(context, fn) {
2577
+ if (context === undefined) {
2578
+ return undefined;
2579
+ }
2580
+ if (Array.isArray(context)) {
2581
+ return context
2582
+ .map((e) => fn(e))
2583
+ .filter((e) => !!e)
2584
+ .flat();
2585
+ }
2586
+ else {
2587
+ return fn(context);
2588
+ }
2589
+ }
2590
+ /**
2591
+ * Determines if the input is an empty array.
2592
+ * @param obj Any value or array of values.
2593
+ * @returns True if the input is an empty array.
2594
+ */
2595
+ function isEmptyArray(obj) {
2596
+ return Array.isArray(obj) && obj.length === 0;
2597
+ }
2598
+ function isFalsy(obj) {
2599
+ return !obj || isEmptyArray(obj);
2600
+ }
2601
+ /**
2602
+ * Converts unknown object into a JavaScript boolean.
2603
+ * Note that this is different than the FHIRPath "toBoolean",
2604
+ * which has particular semantics around arrays, empty arrays, and type conversions.
2605
+ * @param obj Any value or array of values.
2606
+ * @returns The converted boolean value according to FHIRPath rules.
2607
+ */
2608
+ function toJsBoolean(obj) {
2609
+ if (Array.isArray(obj)) {
2610
+ return obj.length === 0 ? false : !!obj[0];
2611
+ }
2612
+ return !!obj;
2613
+ }
2614
+ /**
2615
+ * Removes duplicates in array using FHIRPath equality rules.
2616
+ * @param arr The input array.
2617
+ * @returns The result array with duplicates removed.
2618
+ */
2619
+ function removeDuplicates(arr) {
2620
+ const result = [];
2621
+ for (const i of arr) {
2622
+ let found = false;
2623
+ for (const j of result) {
2624
+ if (fhirPathEquals(i, j)) {
2625
+ found = true;
2626
+ break;
2627
+ }
2628
+ }
2629
+ if (!found) {
2630
+ result.push(i);
2631
+ }
2632
+ }
2633
+ return result;
2634
+ }
2635
+ /**
2636
+ * Determines if two values are equal according to FHIRPath equality rules.
2637
+ * @param x The first value.
2638
+ * @param y The second value.
2639
+ * @returns True if equal.
2640
+ */
2641
+ function fhirPathEquals(x, y) {
2642
+ if (isFalsy(x) && isFalsy(y)) {
2643
+ return true;
2644
+ }
2645
+ if (isEmptyArray(x) || isEmptyArray(y)) {
2646
+ return [];
2647
+ }
2648
+ if (typeof x === 'number' && typeof y === 'number') {
2649
+ return Math.abs(x - y) < 1e-8;
2650
+ }
2651
+ if (isQuantity(x) && isQuantity(y)) {
2652
+ return isQuantityEquivalent(x, y);
2653
+ }
2654
+ if (Array.isArray(x) && Array.isArray(y)) {
2655
+ return x.length === y.length && x.every((val, index) => fhirPathEquals(val, y[index]));
2656
+ }
2657
+ if (typeof x === 'object' && typeof y === 'object') {
2658
+ return deepEquals(x, y);
2659
+ }
2660
+ return x === y;
2661
+ }
2662
+ /**
2663
+ * Determines if two values are equal according to FHIRPath equality rules.
2664
+ * @param x The first value.
2665
+ * @param y The second value.
2666
+ * @returns True if equal.
2667
+ */
2668
+ function fhirPathEquivalent(x, y) {
2669
+ if (isFalsy(x) && isFalsy(y)) {
2670
+ return true;
2671
+ }
2672
+ if (isEmptyArray(x) || isEmptyArray(y)) {
2673
+ // Note that this implies that if the collections have a different number of items to compare,
2674
+ // or if one input is a value and the other is empty ({ }), the result will be false.
2675
+ return false;
2676
+ }
2677
+ if (typeof x === 'number' && typeof y === 'number') {
2678
+ // Use more generous threshold than equality
2679
+ // Decimal: values must be equal, comparison is done on values rounded to the precision of the least precise operand.
2680
+ // Trailing zeroes after the decimal are ignored in determining precision.
2681
+ return Math.abs(x - y) < 0.01;
2682
+ }
2683
+ if (isQuantity(x) && isQuantity(y)) {
2684
+ return isQuantityEquivalent(x, y);
2685
+ }
2686
+ if (Array.isArray(x) && Array.isArray(y)) {
2687
+ // If both operands are collections with multiple items:
2688
+ // 1) Each item must be equivalent
2689
+ // 2) Comparison is not order dependent
2690
+ x.sort();
2691
+ y.sort();
2692
+ return x.length === y.length && x.every((val, index) => fhirPathEquals(val, y[index]));
2693
+ }
2694
+ if (typeof x === 'object' && typeof y === 'object') {
2695
+ return deepEquals(x, y);
2696
+ }
2697
+ if (typeof x === 'string' && typeof y === 'string') {
2698
+ // String: the strings must be the same, ignoring case and locale, and normalizing whitespace
2699
+ // (see String Equivalence for more details).
2700
+ return x.toLowerCase() === y.toLowerCase();
2701
+ }
2702
+ return x === y;
2703
+ }
2704
+ function fhirPathIs(value, desiredType) {
2705
+ if (value === undefined || value === null) {
2706
+ return false;
2707
+ }
2708
+ switch (desiredType) {
2709
+ case 'Boolean':
2710
+ return typeof value === 'boolean';
2711
+ case 'Decimal':
2712
+ case 'Integer':
2713
+ return typeof value === 'number';
2714
+ case 'Date':
2715
+ return typeof value === 'string' && !!value.match(/^\d{4}(-\d{2}(-\d{2})?)?/);
2716
+ case 'DateTime':
2717
+ return typeof value === 'string' && !!value.match(/^\d{4}(-\d{2}(-\d{2})?)?T/);
2718
+ case 'Time':
2719
+ return typeof value === 'string' && !!value.match(/^T\d/);
2720
+ case 'Period':
2721
+ return isPeriod(value);
2722
+ case 'Quantity':
2723
+ return isQuantity(value);
2724
+ default:
2725
+ return typeof value === 'object' && (value === null || value === void 0 ? void 0 : value.resourceType) === desiredType;
2726
+ }
2727
+ }
2728
+ /**
2729
+ * Determines if the input is a Period object.
2730
+ * This is heuristic based, as we do not have strong typing at runtime.
2731
+ * @param input The input value.
2732
+ * @returns True if the input is a period.
2733
+ */
2734
+ function isPeriod(input) {
2735
+ return !!(input && typeof input === 'object' && 'start' in input);
2736
+ }
2737
+ /**
2738
+ * Determines if the input is a Quantity object.
2739
+ * This is heuristic based, as we do not have strong typing at runtime.
2740
+ * @param input The input value.
2741
+ * @returns True if the input is a quantity.
2742
+ */
2743
+ function isQuantity(input) {
2744
+ return !!(input && typeof input === 'object' && 'value' in input && typeof input.value === 'number');
2745
+ }
2746
+ function isQuantityEquivalent(x, y) {
2747
+ return (Math.abs(x.value - y.value) < 0.01 &&
2748
+ (x.unit === y.unit || x.code === y.code || x.unit === y.code || x.code === y.unit));
2749
+ }
2750
+ /**
2751
+ * Resource equality.
2752
+ * Ignores meta.versionId and meta.lastUpdated.
2753
+ * See: https://dmitripavlutin.com/how-to-compare-objects-in-javascript/#4-deep-equality
2754
+ * @param object1 The first object.
2755
+ * @param object2 The second object.
2756
+ * @returns True if the objects are equal.
2757
+ */
2758
+ function deepEquals(object1, object2) {
2759
+ const keys1 = Object.keys(object1);
2760
+ const keys2 = Object.keys(object2);
2761
+ if (keys1.length !== keys2.length) {
2762
+ return false;
2763
+ }
2764
+ for (const key of keys1) {
2765
+ const val1 = object1[key];
2766
+ const val2 = object2[key];
2767
+ if (isObject(val1) && isObject(val2)) {
2768
+ if (!deepEquals(val1, val2)) {
2769
+ return false;
2770
+ }
2771
+ }
2772
+ else {
2773
+ if (val1 !== val2) {
2774
+ return false;
2775
+ }
2776
+ }
2777
+ }
2778
+ return true;
2779
+ }
2780
+ function isObject(object) {
2781
+ return object !== null && typeof object === 'object';
2782
+ }
2783
+
2784
+ class FhirPathAtom {
2785
+ constructor(original, child) {
2786
+ this.original = original;
2787
+ this.child = child;
2788
+ }
2789
+ eval(context) {
2790
+ try {
2791
+ const result = applyMaybeArray(context, (e) => this.child.eval(e));
2792
+ if (Array.isArray(result)) {
2793
+ return result.flat();
2794
+ }
2795
+ else if (result === undefined || result === null) {
2796
+ return [];
2797
+ }
2798
+ else {
2799
+ return [result];
2800
+ }
2801
+ }
2802
+ catch (error) {
2803
+ throw new Error(`FhirPathError on "${this.original}": ${error}`);
2804
+ }
2805
+ }
2806
+ }
2807
+ class LiteralAtom {
2808
+ constructor(value) {
2809
+ this.value = value;
2810
+ }
2811
+ eval() {
2812
+ return this.value;
2813
+ }
2814
+ }
2815
+ class SymbolAtom {
2816
+ constructor(name) {
2817
+ this.name = name;
2818
+ }
2819
+ eval(context) {
2820
+ if (this.name === '$this') {
2821
+ return context;
2822
+ }
2823
+ return applyMaybeArray(context, (e) => {
2824
+ if (e && typeof e === 'object') {
2825
+ if ('resourceType' in e && e.resourceType === this.name) {
2826
+ return e;
2827
+ }
2828
+ if (this.name in e) {
2829
+ return e[this.name];
2830
+ }
2831
+ const propertyName = Object.keys(e).find((k) => k.startsWith(this.name));
2832
+ if (propertyName) {
2833
+ return e[propertyName];
2834
+ }
2835
+ }
2836
+ return undefined;
2837
+ });
2838
+ }
2839
+ }
2840
+ class EmptySetAtom {
2841
+ eval() {
2842
+ return [];
2843
+ }
2844
+ }
2845
+ class UnaryOperatorAtom {
2846
+ constructor(child, impl) {
2847
+ this.child = child;
2848
+ this.impl = impl;
2849
+ }
2850
+ eval(context) {
2851
+ return this.impl(this.child.eval(context));
2852
+ }
2853
+ }
2854
+ class AsAtom {
2855
+ constructor(left, right) {
2856
+ this.left = left;
2857
+ this.right = right;
2858
+ }
2859
+ eval(context) {
2860
+ return this.left.eval(context);
2861
+ }
2862
+ }
2863
+ class ArithemticOperatorAtom {
2864
+ constructor(left, right, impl) {
2865
+ this.left = left;
2866
+ this.right = right;
2867
+ this.impl = impl;
2868
+ }
2869
+ eval(context) {
2870
+ const leftValue = this.left.eval(context);
2871
+ const rightValue = this.right.eval(context);
2872
+ if (isQuantity(leftValue) && isQuantity(rightValue)) {
2873
+ return Object.assign(Object.assign({}, leftValue), { value: this.impl(leftValue.value, rightValue.value) });
2874
+ }
2875
+ else {
2876
+ return this.impl(leftValue, rightValue);
2877
+ }
2878
+ }
2879
+ }
2880
+ class ComparisonOperatorAtom {
2881
+ constructor(left, right, impl) {
2882
+ this.left = left;
2883
+ this.right = right;
2884
+ this.impl = impl;
2885
+ }
2886
+ eval(context) {
2887
+ const leftValue = this.left.eval(context);
2888
+ const rightValue = this.right.eval(context);
2889
+ if (isQuantity(leftValue) && isQuantity(rightValue)) {
2890
+ return this.impl(leftValue.value, rightValue.value);
2891
+ }
2892
+ else {
2893
+ return this.impl(leftValue, rightValue);
2894
+ }
2895
+ }
2896
+ }
2897
+ class ConcatAtom {
2898
+ constructor(left, right) {
2899
+ this.left = left;
2900
+ this.right = right;
2901
+ }
2902
+ eval(context) {
2903
+ const leftValue = this.left.eval(context);
2904
+ const rightValue = this.right.eval(context);
2905
+ const result = [];
2906
+ function add(value) {
2907
+ if (value) {
2908
+ if (Array.isArray(value)) {
2909
+ result.push(...value);
2910
+ }
2911
+ else {
2912
+ result.push(value);
2913
+ }
2914
+ }
2915
+ }
2916
+ add(leftValue);
2917
+ add(rightValue);
2918
+ if (result.length > 0 && result.every((e) => typeof e === 'string')) {
2919
+ return result.join('');
2920
+ }
2921
+ return result;
2922
+ }
2923
+ }
2924
+ class ContainsAtom {
2925
+ constructor(left, right) {
2926
+ this.left = left;
2927
+ this.right = right;
2928
+ }
2929
+ eval(context) {
2930
+ const leftValue = this.left.eval(context);
2931
+ const rightValue = this.right.eval(context);
2932
+ return ensureArray(leftValue).includes(rightValue);
2933
+ }
2934
+ }
2935
+ class InAtom {
2936
+ constructor(left, right) {
2937
+ this.left = left;
2938
+ this.right = right;
2939
+ }
2940
+ eval(context) {
2941
+ const leftValue = this.left.eval(context);
2942
+ const rightValue = this.right.eval(context);
2943
+ return ensureArray(rightValue).includes(leftValue);
2944
+ }
2945
+ }
2946
+ class DotAtom {
2947
+ constructor(left, right) {
2948
+ this.left = left;
2949
+ this.right = right;
2950
+ }
2951
+ eval(context) {
2952
+ return this.right.eval(this.left.eval(context));
2953
+ }
2954
+ }
2955
+ class UnionAtom {
2956
+ constructor(left, right) {
2957
+ this.left = left;
2958
+ this.right = right;
2959
+ }
2960
+ eval(context) {
2961
+ const leftResult = this.left.eval(context);
2962
+ const rightResult = this.right.eval(context);
2963
+ let resultArray;
2964
+ if (leftResult !== undefined && rightResult !== undefined) {
2965
+ resultArray = [leftResult, rightResult].flat();
2966
+ }
2967
+ else if (leftResult !== undefined) {
2968
+ resultArray = ensureArray(leftResult);
2969
+ }
2970
+ else if (rightResult !== undefined) {
2971
+ resultArray = ensureArray(rightResult);
2972
+ }
2973
+ else {
2974
+ resultArray = [];
2975
+ }
2976
+ return removeDuplicates(resultArray);
2977
+ }
2978
+ }
2979
+ class EqualsAtom {
2980
+ constructor(left, right) {
2981
+ this.left = left;
2982
+ this.right = right;
2983
+ }
2984
+ eval(context) {
2985
+ const leftValue = this.left.eval(context);
2986
+ const rightValue = this.right.eval(context);
2987
+ if (Array.isArray(leftValue) && Array.isArray(rightValue)) {
2988
+ return fhirPathEquals(leftValue.flat(), rightValue);
2989
+ }
2990
+ return applyMaybeArray(leftValue, (e) => fhirPathEquals(e, rightValue));
2991
+ }
2992
+ }
2993
+ class NotEqualsAtom {
2994
+ constructor(left, right) {
2995
+ this.left = left;
2996
+ this.right = right;
2997
+ }
2998
+ eval(context) {
2999
+ const leftValue = this.left.eval(context);
3000
+ const rightValue = this.right.eval(context);
3001
+ let result;
3002
+ if (Array.isArray(rightValue)) {
3003
+ result = fhirPathEquals(leftValue, rightValue);
3004
+ }
3005
+ else {
3006
+ result = applyMaybeArray(leftValue, (e) => fhirPathEquals(e, rightValue));
3007
+ }
3008
+ return !toJsBoolean(result);
3009
+ }
3010
+ }
3011
+ class EquivalentAtom {
3012
+ constructor(left, right) {
3013
+ this.left = left;
3014
+ this.right = right;
3015
+ }
3016
+ eval(context) {
3017
+ const leftValue = this.left.eval(context);
3018
+ const rightValue = this.right.eval(context);
3019
+ if (Array.isArray(rightValue)) {
3020
+ return fhirPathEquivalent(leftValue, rightValue);
3021
+ }
3022
+ return applyMaybeArray(leftValue, (e) => fhirPathEquivalent(e, rightValue));
3023
+ }
3024
+ }
3025
+ class NotEquivalentAtom {
3026
+ constructor(left, right) {
3027
+ this.left = left;
3028
+ this.right = right;
3029
+ }
3030
+ eval(context) {
3031
+ const leftValue = this.left.eval(context);
3032
+ const rightValue = this.right.eval(context);
3033
+ let result;
3034
+ if (Array.isArray(rightValue)) {
3035
+ result = fhirPathEquivalent(leftValue, rightValue);
3036
+ }
3037
+ else {
3038
+ result = applyMaybeArray(leftValue, (e) => fhirPathEquivalent(e, rightValue));
3039
+ }
3040
+ return !toJsBoolean(result);
3041
+ }
3042
+ }
3043
+ class IsAtom {
3044
+ constructor(left, right) {
3045
+ this.left = left;
3046
+ this.right = right;
3047
+ }
3048
+ eval(context) {
3049
+ const typeName = this.right.name;
3050
+ return applyMaybeArray(this.left.eval(context), (e) => fhirPathIs(e, typeName));
3051
+ }
3052
+ }
3053
+ /**
3054
+ * 6.5.1. and
3055
+ * Returns true if both operands evaluate to true, false if either operand evaluates to false, and the empty collection ({ }) otherwise.
3056
+ */
3057
+ class AndAtom {
3058
+ constructor(left, right) {
3059
+ this.left = left;
3060
+ this.right = right;
3061
+ }
3062
+ eval(context) {
3063
+ const leftValue = this.left.eval(context);
3064
+ const rightValue = this.right.eval(context);
3065
+ if (leftValue === true && rightValue === true) {
3066
+ return true;
3067
+ }
3068
+ if (leftValue === false || rightValue === false) {
3069
+ return false;
3070
+ }
3071
+ return [];
3072
+ }
3073
+ }
3074
+ class OrAtom {
3075
+ constructor(left, right) {
3076
+ this.left = left;
3077
+ this.right = right;
3078
+ }
3079
+ eval(context) {
3080
+ const leftValue = this.left.eval(context);
3081
+ if (toJsBoolean(leftValue)) {
3082
+ return leftValue;
3083
+ }
3084
+ const rightValue = this.right.eval(context);
3085
+ if (toJsBoolean(rightValue)) {
3086
+ return rightValue;
3087
+ }
3088
+ return [];
3089
+ }
3090
+ }
3091
+ /**
3092
+ * 6.5.4. xor
3093
+ * Returns true if exactly one of the operands evaluates to true,
3094
+ * false if either both operands evaluate to true or both operands evaluate to false,
3095
+ * and the empty collection ({ }) otherwise:
3096
+ */
3097
+ class XorAtom {
3098
+ constructor(left, right) {
3099
+ this.left = left;
3100
+ this.right = right;
3101
+ }
3102
+ eval(context) {
3103
+ const leftValue = this.left.eval(context);
3104
+ const rightValue = this.right.eval(context);
3105
+ if ((leftValue === true && rightValue !== true) || (leftValue !== true && rightValue === true)) {
3106
+ return true;
3107
+ }
3108
+ if ((leftValue === true && rightValue === true) || (leftValue === false && rightValue === false)) {
3109
+ return false;
3110
+ }
3111
+ return [];
3112
+ }
3113
+ }
3114
+ class FunctionAtom {
3115
+ constructor(name, args, impl) {
3116
+ this.name = name;
3117
+ this.args = args;
3118
+ this.impl = impl;
3119
+ }
3120
+ eval(context) {
3121
+ return this.impl(ensureArray(context), ...this.args);
3122
+ }
3123
+ }
3124
+ class IndexerAtom {
3125
+ constructor(left, expr) {
3126
+ this.left = left;
3127
+ this.expr = expr;
3128
+ }
3129
+ eval(context) {
3130
+ const index = this.expr.eval(context);
3131
+ if (typeof index !== 'number') {
3132
+ throw new Error(`Invalid indexer expression: should return integer}`);
3133
+ }
3134
+ const leftResult = this.left.eval(context);
3135
+ if (!(index in leftResult)) {
3136
+ return [];
3137
+ }
3138
+ return leftResult[index];
3139
+ }
3140
+ }
3141
+
3142
+ function parseDateString(str) {
3143
+ if (str.startsWith('T')) {
3144
+ // If a time string,
3145
+ // then normalize to full length.
3146
+ return str + 'T00:00:00.000Z'.substring(str.length);
3147
+ }
3148
+ if (str.length <= 10) {
3149
+ // If a local date (i.e., "2021-01-01"),
3150
+ // then return as-is.
3151
+ return str;
3152
+ }
3153
+ try {
3154
+ // Try to normalize to UTC
3155
+ return new Date(str).toISOString();
3156
+ }
3157
+ catch (e) {
3158
+ // Fallback to original input
3159
+ // This happens on unsupported time formats such as "2021-01-01T12"
3160
+ return str;
3161
+ }
3162
+ }
3163
+
3164
+ /*
3165
+ * Collection of FHIRPath
3166
+ * See: https://hl7.org/fhirpath/#functions
3167
+ */
3168
+ /**
3169
+ * Temporary placholder for unimplemented methods.
3170
+ */
3171
+ const stub = () => [];
3172
+ /*
3173
+ * 5.1 Existence
3174
+ * See: https://hl7.org/fhirpath/#existence
3175
+ */
3176
+ /**
3177
+ * Returns true if the input collection is empty ({ }) and false otherwise.
3178
+ *
3179
+ * See: https://hl7.org/fhirpath/#empty-boolean
3180
+ *
3181
+ * @param input The input collection.
3182
+ * @returns True if the input collection is empty ({ }) and false otherwise.
3183
+ */
3184
+ function empty(input) {
3185
+ return [input.length === 0];
3186
+ }
3187
+ /**
3188
+ * Returns true if the collection has unknown elements, and false otherwise.
3189
+ * This is the opposite of empty(), and as such is a shorthand for empty().not().
3190
+ * If the input collection is empty ({ }), the result is false.
3191
+ *
3192
+ * The function can also take an optional criteria to be applied to the collection
3193
+ * prior to the determination of the exists. In this case, the function is shorthand
3194
+ * for where(criteria).exists().
3195
+ *
3196
+ * See: https://hl7.org/fhirpath/#existscriteria-expression-boolean
3197
+ *
3198
+ * @param input
3199
+ * @param criteria
3200
+ * @returns True if the collection has unknown elements, and false otherwise.
3201
+ */
3202
+ function exists(input, criteria) {
3203
+ if (criteria) {
3204
+ return [input.filter((e) => toJsBoolean(criteria.eval(e))).length > 0];
3205
+ }
3206
+ else {
3207
+ return [input.length > 0];
3208
+ }
3209
+ }
3210
+ /**
3211
+ * Returns true if for every element in the input collection, criteria evaluates to true.
3212
+ * Otherwise, the result is false.
3213
+ *
3214
+ * If the input collection is empty ({ }), the result is true.
3215
+ *
3216
+ * See: https://hl7.org/fhirpath/#allcriteria-expression-boolean
3217
+ *
3218
+ * @param input The input collection.
3219
+ * @param criteria The evaluation criteria.
3220
+ * @returns True if for every element in the input collection, criteria evaluates to true.
3221
+ */
3222
+ function all(input, criteria) {
3223
+ return [input.every((e) => toJsBoolean(criteria.eval(e)))];
3224
+ }
3225
+ /**
3226
+ * Takes a collection of Boolean values and returns true if all the items are true.
3227
+ * If unknown items are false, the result is false.
3228
+ * If the input is empty ({ }), the result is true.
3229
+ *
3230
+ * See: https://hl7.org/fhirpath/#alltrue-boolean
3231
+ *
3232
+ * @param input The input collection.
3233
+ * @param criteria The evaluation criteria.
3234
+ * @returns True if all the items are true.
3235
+ */
3236
+ function allTrue(input) {
3237
+ for (const value of input) {
3238
+ if (!value) {
3239
+ return [false];
3240
+ }
3241
+ }
3242
+ return [true];
3243
+ }
3244
+ /**
3245
+ * Takes a collection of Boolean values and returns true if unknown of the items are true.
3246
+ * If all the items are false, or if the input is empty ({ }), the result is false.
3247
+ *
3248
+ * See: https://hl7.org/fhirpath/#anytrue-boolean
3249
+ *
3250
+ * @param input The input collection.
3251
+ * @param criteria The evaluation criteria.
3252
+ * @returns True if unknown of the items are true.
3253
+ */
3254
+ function anyTrue(input) {
3255
+ for (const value of input) {
3256
+ if (value) {
3257
+ return [true];
3258
+ }
3259
+ }
3260
+ return [false];
3261
+ }
3262
+ /**
3263
+ * Takes a collection of Boolean values and returns true if all the items are false.
3264
+ * If unknown items are true, the result is false.
3265
+ * If the input is empty ({ }), the result is true.
3266
+ *
3267
+ * See: https://hl7.org/fhirpath/#allfalse-boolean
3268
+ *
3269
+ * @param input The input collection.
3270
+ * @param criteria The evaluation criteria.
3271
+ * @returns True if all the items are false.
3272
+ */
3273
+ function allFalse(input) {
3274
+ for (const value of input) {
3275
+ if (value) {
3276
+ return [false];
3277
+ }
3278
+ }
3279
+ return [true];
3280
+ }
3281
+ /**
3282
+ * Takes a collection of Boolean values and returns true if unknown of the items are false.
3283
+ * If all the items are true, or if the input is empty ({ }), the result is false.
3284
+ *
3285
+ * See: https://hl7.org/fhirpath/#anyfalse-boolean
3286
+ *
3287
+ * @param input The input collection.
3288
+ * @param criteria The evaluation criteria.
3289
+ * @returns True if for every element in the input collection, criteria evaluates to true.
3290
+ */
3291
+ function anyFalse(input) {
3292
+ for (const value of input) {
3293
+ if (!value) {
3294
+ return [true];
3295
+ }
3296
+ }
3297
+ return [false];
3298
+ }
3299
+ /**
3300
+ * Returns true if all items in the input collection are members of the collection passed
3301
+ * as the other argument. Membership is determined using the = (Equals) (=) operation.
3302
+ *
3303
+ * Conceptually, this function is evaluated by testing each element in the input collection
3304
+ * for membership in the other collection, with a default of true. This means that if the
3305
+ * input collection is empty ({ }), the result is true, otherwise if the other collection
3306
+ * is empty ({ }), the result is false.
3307
+ *
3308
+ * See: http://hl7.org/fhirpath/#subsetofother-collection-boolean
3309
+ */
3310
+ const subsetOf = stub;
3311
+ /**
3312
+ * Returns true if all items in the collection passed as the other argument are members of
3313
+ * the input collection. Membership is determined using the = (Equals) (=) operation.
3314
+ *
3315
+ * Conceptually, this function is evaluated by testing each element in the other collection
3316
+ * for membership in the input collection, with a default of true. This means that if the
3317
+ * other collection is empty ({ }), the result is true, otherwise if the input collection
3318
+ * is empty ({ }), the result is false.
3319
+ *
3320
+ * See: http://hl7.org/fhirpath/#supersetofother-collection-boolean
3321
+ */
3322
+ const supersetOf = stub;
3323
+ /**
3324
+ * Returns the integer count of the number of items in the input collection.
3325
+ * Returns 0 when the input collection is empty.
3326
+ *
3327
+ * See: https://hl7.org/fhirpath/#count-integer
3328
+ *
3329
+ * @param input The input collection.
3330
+ * @returns The integer count of the number of items in the input collection.
3331
+ */
3332
+ function count(input) {
3333
+ return [input.length];
3334
+ }
3335
+ /**
3336
+ * Returns a collection containing only the unique items in the input collection.
3337
+ * To determine whether two items are the same, the = (Equals) (=) operator is used,
3338
+ * as defined below.
3339
+ *
3340
+ * If the input collection is empty ({ }), the result is empty.
3341
+ *
3342
+ * Note that the order of elements in the input collection is not guaranteed to be
3343
+ * preserved in the result.
3344
+ *
3345
+ * See: https://hl7.org/fhirpath/#distinct-collection
3346
+ *
3347
+ * @param input The input collection.
3348
+ * @returns The integer count of the number of items in the input collection.
3349
+ */
3350
+ function distinct(input) {
3351
+ return Array.from(new Set(input));
3352
+ }
3353
+ /**
3354
+ * Returns true if all the items in the input collection are distinct.
3355
+ * To determine whether two items are distinct, the = (Equals) (=) operator is used,
3356
+ * as defined below.
3357
+ *
3358
+ * See: https://hl7.org/fhirpath/#isdistinct-boolean
3359
+ *
3360
+ * @param input The input collection.
3361
+ * @returns The integer count of the number of items in the input collection.
3362
+ */
3363
+ function isDistinct(input) {
3364
+ return [input.length === new Set(input).size];
3365
+ }
3366
+ /*
3367
+ * 5.2 Filtering and projection
3368
+ */
3369
+ /**
3370
+ * Returns a collection containing only those elements in the input collection
3371
+ * for which the stated criteria expression evaluates to true.
3372
+ * Elements for which the expression evaluates to false or empty ({ }) are not
3373
+ * included in the result.
3374
+ *
3375
+ * If the input collection is empty ({ }), the result is empty.
3376
+ *
3377
+ * If the result of evaluating the condition is other than a single boolean value,
3378
+ * the evaluation will end and signal an error to the calling environment,
3379
+ * consistent with singleton evaluation of collections behavior.
3380
+ *
3381
+ * See: https://hl7.org/fhirpath/#wherecriteria-expression-collection
3382
+ *
3383
+ * @param input The input collection.
3384
+ * @param condition The condition atom.
3385
+ * @returns A collection containing only those elements in the input collection for which the stated criteria expression evaluates to true.
3386
+ */
3387
+ function where(input, criteria) {
3388
+ return input.filter((e) => toJsBoolean(criteria.eval(e)));
3389
+ }
3390
+ /**
3391
+ * Evaluates the projection expression for each item in the input collection.
3392
+ * The result of each evaluation is added to the output collection. If the
3393
+ * evaluation results in a collection with multiple items, all items are added
3394
+ * to the output collection (collections resulting from evaluation of projection
3395
+ * are flattened). This means that if the evaluation for an element results in
3396
+ * the empty collection ({ }), no element is added to the result, and that if
3397
+ * the input collection is empty ({ }), the result is empty as well.
3398
+ *
3399
+ * See: http://hl7.org/fhirpath/#selectprojection-expression-collection
3400
+ */
3401
+ function select(input, criteria) {
3402
+ return ensureArray(input.map((e) => criteria.eval(e)).flat());
3403
+ }
3404
+ /**
3405
+ * A version of select that will repeat the projection and add it to the output
3406
+ * collection, as long as the projection yields new items (as determined by
3407
+ * the = (Equals) (=) operator).
3408
+ *
3409
+ * See: http://hl7.org/fhirpath/#repeatprojection-expression-collection
3410
+ */
3411
+ const repeat = stub;
3412
+ /**
3413
+ * Returns a collection that contains all items in the input collection that
3414
+ * are of the given type or a subclass thereof. If the input collection is
3415
+ * empty ({ }), the result is empty. The type argument is an identifier that
3416
+ * must resolve to the name of a type in a model
3417
+ *
3418
+ * See: http://hl7.org/fhirpath/#oftypetype-type-specifier-collection
3419
+ */
3420
+ const ofType = stub;
3421
+ /*
3422
+ * 5.3 Subsetting
3423
+ */
3424
+ /**
3425
+ * Will return the single item in the input if there is just one item.
3426
+ * If the input collection is empty ({ }), the result is empty.
3427
+ * If there are multiple items, an error is signaled to the evaluation environment.
3428
+ * This function is useful for ensuring that an error is returned if an assumption
3429
+ * about cardinality is violated at run-time.
3430
+ *
3431
+ * See: https://hl7.org/fhirpath/#single-collection
3432
+ *
3433
+ * @param input The input collection.
3434
+ * @returns The single item in the input if there is just one item.
3435
+ */
3436
+ function single(input) {
3437
+ if (input.length > 1) {
3438
+ throw new Error('Expected input length one for single()');
3439
+ }
3440
+ return input.length === 0 ? [] : input.slice(0, 1);
3441
+ }
3442
+ /**
3443
+ * Returns a collection containing only the first item in the input collection.
3444
+ * This function is equivalent to item[0], so it will return an empty collection if the input collection has no items.
3445
+ *
3446
+ * See: https://hl7.org/fhirpath/#first-collection
3447
+ *
3448
+ * @param input The input collection.
3449
+ * @returns A collection containing only the first item in the input collection.
3450
+ */
3451
+ function first(input) {
3452
+ return input.length === 0 ? [] : [input[0]];
3453
+ }
3454
+ /**
3455
+ * Returns a collection containing only the last item in the input collection.
3456
+ * Will return an empty collection if the input collection has no items.
3457
+ *
3458
+ * See: https://hl7.org/fhirpath/#last-collection
3459
+ *
3460
+ * @param input The input collection.
3461
+ * @returns A collection containing only the last item in the input collection.
3462
+ */
3463
+ function last(input) {
3464
+ return input.length === 0 ? [] : [input[input.length - 1]];
3465
+ }
3466
+ /**
3467
+ * Returns a collection containing all but the first item in the input collection.
3468
+ * Will return an empty collection if the input collection has no items, or only one item.
3469
+ *
3470
+ * See: https://hl7.org/fhirpath/#tail-collection
3471
+ *
3472
+ * @param input The input collection.
3473
+ * @returns A collection containing all but the first item in the input collection.
3474
+ */
3475
+ function tail(input) {
3476
+ return input.length === 0 ? [] : input.slice(1, input.length);
3477
+ }
3478
+ /**
3479
+ * Returns a collection containing all but the first num items in the input collection.
3480
+ * Will return an empty collection if there are no items remaining after the
3481
+ * indicated number of items have been skipped, or if the input collection is empty.
3482
+ * If num is less than or equal to zero, the input collection is simply returned.
3483
+ *
3484
+ * See: https://hl7.org/fhirpath/#skipnum-integer-collection
3485
+ *
3486
+ * @param input The input collection.
3487
+ * @returns A collection containing all but the first item in the input collection.
3488
+ */
3489
+ function skip(input, num) {
3490
+ const numValue = num.eval(0);
3491
+ if (typeof numValue !== 'number') {
3492
+ throw new Error('Expected a number for skip(num)');
3493
+ }
3494
+ if (numValue >= input.length) {
3495
+ return [];
3496
+ }
3497
+ if (numValue <= 0) {
3498
+ return input;
3499
+ }
3500
+ return input.slice(numValue, input.length);
3501
+ }
3502
+ /**
3503
+ * Returns a collection containing the first num items in the input collection,
3504
+ * or less if there are less than num items.
3505
+ * If num is less than or equal to 0, or if the input collection is empty ({ }),
3506
+ * take returns an empty collection.
3507
+ *
3508
+ * See: https://hl7.org/fhirpath/#takenum-integer-collection
3509
+ *
3510
+ * @param input The input collection.
3511
+ * @returns A collection containing the first num items in the input collection.
3512
+ */
3513
+ function take(input, num) {
3514
+ const numValue = num.eval(0);
3515
+ if (typeof numValue !== 'number') {
3516
+ throw new Error('Expected a number for take(num)');
3517
+ }
3518
+ if (numValue >= input.length) {
3519
+ return input;
3520
+ }
3521
+ if (numValue <= 0) {
3522
+ return [];
3523
+ }
3524
+ return input.slice(0, numValue);
3525
+ }
3526
+ /**
3527
+ * Returns the set of elements that are in both collections.
3528
+ * Duplicate items will be eliminated by this function.
3529
+ * Order of items is not guaranteed to be preserved in the result of this function.
3530
+ *
3531
+ * See: http://hl7.org/fhirpath/#intersectother-collection-collection
3532
+ */
3533
+ function intersect(input, other) {
3534
+ if (!other) {
3535
+ return input;
3536
+ }
3537
+ const otherArray = ensureArray(other.eval(0));
3538
+ return removeDuplicates(input.filter((e) => otherArray.includes(e)));
3539
+ }
3540
+ /**
3541
+ * Returns the set of elements that are not in the other collection.
3542
+ * Duplicate items will not be eliminated by this function, and order will be preserved.
3543
+ *
3544
+ * e.g. (1 | 2 | 3).exclude(2) returns (1 | 3).
3545
+ *
3546
+ * See: http://hl7.org/fhirpath/#excludeother-collection-collection
3547
+ */
3548
+ function exclude(input, other) {
3549
+ if (!other) {
3550
+ return input;
3551
+ }
3552
+ const otherArray = ensureArray(other.eval(0));
3553
+ return input.filter((e) => !otherArray.includes(e));
3554
+ }
3555
+ /*
3556
+ * 5.4. Combining
3557
+ *
3558
+ * See: https://hl7.org/fhirpath/#combining
3559
+ */
3560
+ /**
3561
+ * Merge the two collections into a single collection,
3562
+ * eliminating unknown duplicate values (using = (Equals) (=) to determine equality).
3563
+ * There is no expectation of order in the resulting collection.
3564
+ *
3565
+ * In other words, this function returns the distinct list of elements from both inputs.
3566
+ *
3567
+ * See: http://hl7.org/fhirpath/#unionother-collection
3568
+ */
3569
+ function union(input, other) {
3570
+ if (!other) {
3571
+ return input;
3572
+ }
3573
+ return removeDuplicates([input, other.eval(0)].flat());
3574
+ }
3575
+ /**
3576
+ * Merge the input and other collections into a single collection
3577
+ * without eliminating duplicate values. Combining an empty collection
3578
+ * with a non-empty collection will return the non-empty collection.
3579
+ *
3580
+ * There is no expectation of order in the resulting collection.
3581
+ *
3582
+ * See: http://hl7.org/fhirpath/#combineother-collection-collection
3583
+ */
3584
+ function combine(input, other) {
3585
+ if (!other) {
3586
+ return input;
3587
+ }
3588
+ return [input, other.eval(0)].flat();
3589
+ }
3590
+ /*
3591
+ * 5.5. Conversion
3592
+ *
3593
+ * See: https://hl7.org/fhirpath/#conversion
3594
+ */
3595
+ /**
3596
+ * The iif function in FHIRPath is an immediate if,
3597
+ * also known as a conditional operator (such as C’s ? : operator).
3598
+ *
3599
+ * The criterion expression is expected to evaluate to a Boolean.
3600
+ *
3601
+ * If criterion is true, the function returns the value of the true-result argument.
3602
+ *
3603
+ * If criterion is false or an empty collection, the function returns otherwise-result,
3604
+ * unless the optional otherwise-result is not given, in which case the function returns an empty collection.
3605
+ *
3606
+ * Note that short-circuit behavior is expected in this function. In other words,
3607
+ * true-result should only be evaluated if the criterion evaluates to true,
3608
+ * and otherwise-result should only be evaluated otherwise. For implementations,
3609
+ * this means delaying evaluation of the arguments.
3610
+ *
3611
+ * @param input
3612
+ * @param criterion
3613
+ * @param trueResult
3614
+ * @param otherwiseResult
3615
+ * @returns
3616
+ */
3617
+ function iif(input, criterion, trueResult, otherwiseResult) {
3618
+ const evalResult = ensureArray(criterion.eval(input));
3619
+ if (evalResult.length > 1 || (evalResult.length === 1 && typeof evalResult[0] !== 'boolean')) {
3620
+ throw new Error('Expected criterion to evaluate to a Boolean');
3621
+ }
3622
+ if (toJsBoolean(evalResult)) {
3623
+ return ensureArray(trueResult.eval(input));
3624
+ }
3625
+ if (otherwiseResult) {
3626
+ return ensureArray(otherwiseResult.eval(input));
3627
+ }
3628
+ return [];
3629
+ }
3630
+ /**
3631
+ * Converts an input collection to a boolean.
3632
+ *
3633
+ * If the input collection contains a single item, this function will return a single boolean if:
3634
+ * 1) the item is a Boolean
3635
+ * 2) the item is an Integer and is equal to one of the possible integer representations of Boolean values
3636
+ * 3) the item is a Decimal that is equal to one of the possible decimal representations of Boolean values
3637
+ * 4) the item is a String that is equal to one of the possible string representations of Boolean values
3638
+ *
3639
+ * If the item is not one the above types, or the item is a String, Integer, or Decimal, but is not equal to one of the possible values convertible to a Boolean, the result is empty.
3640
+ *
3641
+ * See: https://hl7.org/fhirpath/#toboolean-boolean
3642
+ *
3643
+ * @param input
3644
+ * @returns
3645
+ */
3646
+ function toBoolean(input) {
3647
+ if (input.length === 0) {
3648
+ return [];
3649
+ }
3650
+ const [value] = validateInput(input, 1);
3651
+ if (typeof value === 'boolean') {
3652
+ return [value];
3653
+ }
3654
+ if (typeof value === 'number') {
3655
+ if (value === 0 || value === 1) {
3656
+ return [!!value];
3657
+ }
3658
+ }
3659
+ if (typeof value === 'string') {
3660
+ const lowerStr = value.toLowerCase();
3661
+ if (['true', 't', 'yes', 'y', '1', '1.0'].includes(lowerStr)) {
3662
+ return [true];
3663
+ }
3664
+ if (['false', 'f', 'no', 'n', '0', '0.0'].includes(lowerStr)) {
3665
+ return [false];
3666
+ }
3667
+ }
3668
+ return [];
3669
+ }
3670
+ /**
3671
+ * If the input collection contains a single item, this function will return true if:
3672
+ * 1) the item is a Boolean
3673
+ * 2) the item is an Integer that is equal to one of the possible integer representations of Boolean values
3674
+ * 3) the item is a Decimal that is equal to one of the possible decimal representations of Boolean values
3675
+ * 4) the item is a String that is equal to one of the possible string representations of Boolean values
3676
+ *
3677
+ * If the item is not one of the above types, or the item is a String, Integer, or Decimal, but is not equal to one of the possible values convertible to a Boolean, the result is false.
3678
+ *
3679
+ * Possible values for Integer, Decimal, and String are described in the toBoolean() function.
3680
+ *
3681
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3682
+ *
3683
+ * If the input collection is empty, the result is empty.
3684
+ *
3685
+ * See: http://hl7.org/fhirpath/#convertstoboolean-boolean
3686
+ *
3687
+ * @param input
3688
+ * @returns
3689
+ */
3690
+ function convertsToBoolean(input) {
3691
+ if (input.length === 0) {
3692
+ return [];
3693
+ }
3694
+ return [toBoolean(input).length === 1];
3695
+ }
3696
+ /**
3697
+ * Returns the integer representation of the input.
3698
+ *
3699
+ * If the input collection contains a single item, this function will return a single integer if:
3700
+ * 1) the item is an Integer
3701
+ * 2) the item is a String and is convertible to an integer
3702
+ * 3) the item is a Boolean, where true results in a 1 and false results in a 0.
3703
+ *
3704
+ * If the item is not one the above types, the result is empty.
3705
+ *
3706
+ * If the item is a String, but the string is not convertible to an integer (using the regex format (\\+|-)?\d+), the result is empty.
3707
+ *
3708
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3709
+ *
3710
+ * If the input collection is empty, the result is empty.
3711
+ *
3712
+ * See: https://hl7.org/fhirpath/#tointeger-integer
3713
+ *
3714
+ * @param input The input collection.
3715
+ * @returns The string representation of the input.
3716
+ */
3717
+ function toInteger(input) {
3718
+ if (input.length === 0) {
3719
+ return [];
3720
+ }
3721
+ const [value] = validateInput(input, 1);
3722
+ if (typeof value === 'number') {
3723
+ return [value];
3724
+ }
3725
+ if (typeof value === 'string' && value.match(/^[+-]?\d+$/)) {
3726
+ return [parseInt(value, 10)];
3727
+ }
3728
+ if (typeof value === 'boolean') {
3729
+ return [value ? 1 : 0];
3730
+ }
3731
+ return [];
3732
+ }
3733
+ /**
3734
+ * Returns true if the input can be converted to string.
3735
+ *
3736
+ * If the input collection contains a single item, this function will return true if:
3737
+ * 1) the item is an Integer
3738
+ * 2) the item is a String and is convertible to an Integer
3739
+ * 3) the item is a Boolean
3740
+ * 4) If the item is not one of the above types, or the item is a String, but is not convertible to an Integer (using the regex format (\\+|-)?\d+), the result is false.
3741
+ *
3742
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3743
+ *
3744
+ * If the input collection is empty, the result is empty.
3745
+ *
3746
+ * See: https://hl7.org/fhirpath/#convertstointeger-boolean
3747
+ *
3748
+ * @param input The input collection.
3749
+ * @returns
3750
+ */
3751
+ function convertsToInteger(input) {
3752
+ if (input.length === 0) {
3753
+ return [];
3754
+ }
3755
+ return [toInteger(input).length === 1];
3756
+ }
3757
+ /**
3758
+ * If the input collection contains a single item, this function will return a single date if:
3759
+ * 1) the item is a Date
3760
+ * 2) the item is a DateTime
3761
+ * 3) the item is a String and is convertible to a Date
3762
+ *
3763
+ * If the item is not one of the above types, the result is empty.
3764
+ *
3765
+ * If the item is a String, but the string is not convertible to a Date (using the format YYYY-MM-DD), the result is empty.
3766
+ *
3767
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3768
+ *
3769
+ * If the input collection is empty, the result is empty.
3770
+ *
3771
+ * See: https://hl7.org/fhirpath/#todate-date
3772
+ */
3773
+ function toDate(input) {
3774
+ if (input.length === 0) {
3775
+ return [];
3776
+ }
3777
+ const [value] = validateInput(input, 1);
3778
+ if (typeof value === 'string' && value.match(/^\d{4}(-\d{2}(-\d{2})?)?/)) {
3779
+ return [parseDateString(value)];
3780
+ }
3781
+ return [];
3782
+ }
3783
+ /**
3784
+ * If the input collection contains a single item, this function will return true if:
3785
+ * 1) the item is a Date
3786
+ * 2) the item is a DateTime
3787
+ * 3) the item is a String and is convertible to a Date
3788
+ *
3789
+ * If the item is not one of the above types, or is not convertible to a Date (using the format YYYY-MM-DD), the result is false.
3790
+ *
3791
+ * If the item contains a partial date (e.g. '2012-01'), the result is a partial date.
3792
+ *
3793
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3794
+ *
3795
+ * If the input collection is empty, the result is empty.
3796
+ *
3797
+ * See: https://hl7.org/fhirpath/#convertstodate-boolean
3798
+ */
3799
+ function convertsToDate(input) {
3800
+ if (input.length === 0) {
3801
+ return [];
3802
+ }
3803
+ return [toDate(input).length === 1];
3804
+ }
3805
+ /**
3806
+ * If the input collection contains a single item, this function will return a single datetime if:
3807
+ * 1) the item is a DateTime
3808
+ * 2) the item is a Date, in which case the result is a DateTime with the year, month, and day of the Date, and the time components empty (not set to zero)
3809
+ * 3) the item is a String and is convertible to a DateTime
3810
+ *
3811
+ * If the item is not one of the above types, the result is empty.
3812
+ *
3813
+ * If the item is a String, but the string is not convertible to a DateTime (using the format YYYY-MM-DDThh:mm:ss.fff(+|-)hh:mm), the result is empty.
3814
+ *
3815
+ * If the item contains a partial datetime (e.g. '2012-01-01T10:00'), the result is a partial datetime.
3816
+ *
3817
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3818
+ *
3819
+ * If the input collection is empty, the result is empty.
3820
+
3821
+ * See: https://hl7.org/fhirpath/#todatetime-datetime
3822
+ *
3823
+ * @param input
3824
+ * @returns
3825
+ */
3826
+ function toDateTime(input) {
3827
+ if (input.length === 0) {
3828
+ return [];
3829
+ }
3830
+ const [value] = validateInput(input, 1);
3831
+ if (typeof value === 'string' && value.match(/^\d{4}(-\d{2}(-\d{2})?)?/)) {
3832
+ return [parseDateString(value)];
3833
+ }
3834
+ return [];
3835
+ }
3836
+ /**
3837
+ * If the input collection contains a single item, this function will return true if:
3838
+ * 1) the item is a DateTime
3839
+ * 2) the item is a Date
3840
+ * 3) the item is a String and is convertible to a DateTime
3841
+ *
3842
+ * If the item is not one of the above types, or is not convertible to a DateTime (using the format YYYY-MM-DDThh:mm:ss.fff(+|-)hh:mm), the result is false.
3843
+ *
3844
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3845
+ *
3846
+ * If the input collection is empty, the result is empty.
3847
+ *
3848
+ * See: https://hl7.org/fhirpath/#convertstodatetime-boolean
3849
+ *
3850
+ * @param input
3851
+ * @returns
3852
+ */
3853
+ function convertsToDateTime(input) {
3854
+ if (input.length === 0) {
3855
+ return [];
3856
+ }
3857
+ return [toDateTime(input).length === 1];
3858
+ }
3859
+ /**
3860
+ * If the input collection contains a single item, this function will return a single decimal if:
3861
+ * 1) the item is an Integer or Decimal
3862
+ * 2) the item is a String and is convertible to a Decimal
3863
+ * 3) the item is a Boolean, where true results in a 1.0 and false results in a 0.0.
3864
+ * 4) If the item is not one of the above types, the result is empty.
3865
+ *
3866
+ * If the item is a String, but the string is not convertible to a Decimal (using the regex format (\\+|-)?\d+(\.\d+)?), the result is empty.
3867
+ *
3868
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3869
+ *
3870
+ * If the input collection is empty, the result is empty.
3871
+ *
3872
+ * See: https://hl7.org/fhirpath/#decimal-conversion-functions
3873
+ *
3874
+ * @param input The input collection.
3875
+ * @returns
3876
+ */
3877
+ function toDecimal(input) {
3878
+ if (input.length === 0) {
3879
+ return [];
3880
+ }
3881
+ const [value] = validateInput(input, 1);
3882
+ if (typeof value === 'number') {
3883
+ return [value];
3884
+ }
3885
+ if (typeof value === 'string' && value.match(/^-?\d{1,9}(\.\d{1,9})?$/)) {
3886
+ return [parseFloat(value)];
3887
+ }
3888
+ if (typeof value === 'boolean') {
3889
+ return [value ? 1 : 0];
3890
+ }
3891
+ return [];
3892
+ }
3893
+ /**
3894
+ * If the input collection contains a single item, this function will true if:
3895
+ * 1) the item is an Integer or Decimal
3896
+ * 2) the item is a String and is convertible to a Decimal
3897
+ * 3) the item is a Boolean
3898
+ *
3899
+ * If the item is not one of the above types, or is not convertible to a Decimal (using the regex format (\\+|-)?\d+(\.\d+)?), the result is false.
3900
+ *
3901
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3902
+ *
3903
+ * If the input collection is empty, the result is empty.
3904
+
3905
+ * See: https://hl7.org/fhirpath/#convertstodecimal-boolean
3906
+ *
3907
+ * @param input The input collection.
3908
+ * @returns
3909
+ */
3910
+ function convertsToDecimal(input) {
3911
+ if (input.length === 0) {
3912
+ return [];
3913
+ }
3914
+ return [toDecimal(input).length === 1];
3915
+ }
3916
+ /**
3917
+ * If the input collection contains a single item, this function will return a single quantity if:
3918
+ * 1) the item is an Integer, or Decimal, where the resulting quantity will have the default unit ('1')
3919
+ * 2) the item is a Quantity
3920
+ * 3) the item is a String and is convertible to a Quantity
3921
+ * 4) the item is a Boolean, where true results in the quantity 1.0 '1', and false results in the quantity 0.0 '1'
3922
+ *
3923
+ * If the item is not one of the above types, the result is empty.
3924
+ *
3925
+ * See: https://hl7.org/fhirpath/#quantity-conversion-functions
3926
+ *
3927
+ * @param input The input collection.
3928
+ * @returns
3929
+ */
3930
+ function toQuantity(input) {
3931
+ if (input.length === 0) {
3932
+ return [];
3933
+ }
3934
+ const [value] = validateInput(input, 1);
3935
+ if (isQuantity(value)) {
3936
+ return [value];
3937
+ }
3938
+ if (typeof value === 'number') {
3939
+ return [{ value, unit: '1' }];
3940
+ }
3941
+ if (typeof value === 'string' && value.match(/^-?\d{1,9}(\.\d{1,9})?/)) {
3942
+ return [{ value: parseFloat(value), unit: '1' }];
3943
+ }
3944
+ if (typeof value === 'boolean') {
3945
+ return [{ value: value ? 1 : 0, unit: '1' }];
3946
+ }
3947
+ return [];
3948
+ }
3949
+ /**
3950
+ * If the input collection contains a single item, this function will return true if:
3951
+ * 1) the item is an Integer, Decimal, or Quantity
3952
+ * 2) the item is a String that is convertible to a Quantity
3953
+ * 3) the item is a Boolean
3954
+ *
3955
+ * If the item is not one of the above types, or is not convertible to a Quantity using the following regex format:
3956
+ *
3957
+ * (?'value'(\+|-)?\d+(\.\d+)?)\s*('(?'unit'[^']+)'|(?'time'[a-zA-Z]+))?
3958
+ *
3959
+ * then the result is false.
3960
+ *
3961
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
3962
+ *
3963
+ * If the input collection is empty, the result is empty.
3964
+ *
3965
+ * If the unit argument is provided, it must be the string representation of a UCUM code (or a FHIRPath calendar duration keyword), and is used to determine whether the input quantity can be converted to the given unit, according to the unit conversion rules specified by UCUM. If the input quantity can be converted, the result is true, otherwise, the result is false.
3966
+ *
3967
+ * See: https://hl7.org/fhirpath/#convertstoquantityunit-string-boolean
3968
+ *
3969
+ * @param input The input collection.
3970
+ * @returns
3971
+ */
3972
+ function convertsToQuantity(input) {
3973
+ if (input.length === 0) {
3974
+ return [];
3975
+ }
3976
+ return [toQuantity(input).length === 1];
3977
+ }
3978
+ /**
3979
+ * Returns the string representation of the input.
3980
+ *
3981
+ * If the input collection contains a single item, this function will return a single String if:
3982
+ *
3983
+ * 1) the item in the input collection is a String
3984
+ * 2) the item in the input collection is an Integer, Decimal, Date, Time, DateTime, or Quantity the output will contain its String representation
3985
+ * 3) the item is a Boolean, where true results in 'true' and false in 'false'.
3986
+ *
3987
+ * If the item is not one of the above types, the result is false.
3988
+ *
3989
+ * See: https://hl7.org/fhirpath/#tostring-string
3990
+ *
3991
+ * @param input The input collection.
3992
+ * @returns The string representation of the input.
3993
+ */
3994
+ function toString(input) {
3995
+ if (input.length === 0) {
3996
+ return [];
3997
+ }
3998
+ const [value] = validateInput(input, 1);
3999
+ if (value === null || value === undefined) {
4000
+ return [];
4001
+ }
4002
+ if (isQuantity(value)) {
4003
+ return [`${value.value} '${value.unit}'`];
4004
+ }
4005
+ return [value.toString()];
4006
+ }
4007
+ /**
4008
+ * Returns true if the input can be converted to string.
4009
+ *
4010
+ * If the input collection contains a single item, this function will return true if:
4011
+ * 1) the item is a String
4012
+ * 2) the item is an Integer, Decimal, Date, Time, or DateTime
4013
+ * 3) the item is a Boolean
4014
+ * 4) the item is a Quantity
4015
+ *
4016
+ * If the item is not one of the above types, the result is false.
4017
+ *
4018
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4019
+ *
4020
+ * If the input collection is empty, the result is empty.
4021
+ *
4022
+ * See: https://hl7.org/fhirpath/#tostring-string
4023
+ *
4024
+ * @param input The input collection.
4025
+ * @returns
4026
+ */
4027
+ function convertsToString(input) {
4028
+ if (input.length === 0) {
4029
+ return [];
4030
+ }
4031
+ return [toString(input).length === 1];
4032
+ }
4033
+ /**
4034
+ * If the input collection contains a single item, this function will return a single time if:
4035
+ * 1) the item is a Time
4036
+ * 2) the item is a String and is convertible to a Time
4037
+ *
4038
+ * If the item is not one of the above types, the result is empty.
4039
+ *
4040
+ * If the item is a String, but the string is not convertible to a Time (using the format hh:mm:ss.fff(+|-)hh:mm), the result is empty.
4041
+ *
4042
+ * If the item contains a partial time (e.g. '10:00'), the result is a partial time.
4043
+ *
4044
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4045
+ *
4046
+ * If the input collection is empty, the result is empty.
4047
+ *
4048
+ * See: https://hl7.org/fhirpath/#totime-time
4049
+ *
4050
+ * @param input
4051
+ * @returns
4052
+ */
4053
+ function toTime(input) {
4054
+ if (input.length === 0) {
4055
+ return [];
4056
+ }
4057
+ const [value] = validateInput(input, 1);
4058
+ if (typeof value === 'string') {
4059
+ const match = value.match(/^T?(\d{2}(:\d{2}(:\d{2})?)?)/);
4060
+ if (match) {
4061
+ return [parseDateString('T' + match[1])];
4062
+ }
4063
+ }
4064
+ return [];
4065
+ }
4066
+ /**
4067
+ * If the input collection contains a single item, this function will return true if:
4068
+ * 1) the item is a Time
4069
+ * 2) the item is a String and is convertible to a Time
4070
+ *
4071
+ * If the item is not one of the above types, or is not convertible to a Time (using the format hh:mm:ss.fff(+|-)hh:mm), the result is false.
4072
+ *
4073
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4074
+ *
4075
+ * If the input collection is empty, the result is empty.
4076
+ *
4077
+ * See: https://hl7.org/fhirpath/#convertstotime-boolean
4078
+ *
4079
+ * @param input
4080
+ * @returns
4081
+ */
4082
+ function convertsToTime(input) {
4083
+ if (input.length === 0) {
4084
+ return [];
4085
+ }
4086
+ return [toTime(input).length === 1];
4087
+ }
4088
+ /*
4089
+ * 5.6. String Manipulation.
4090
+ *
4091
+ * See: https://hl7.org/fhirpath/#string-manipulation
4092
+ */
4093
+ /**
4094
+ * Returns the 0-based index of the first position substring is found in the input string, or -1 if it is not found.
4095
+ *
4096
+ * If substring is an empty string (''), the function returns 0.
4097
+ *
4098
+ * If the input or substring is empty ({ }), the result is empty ({ }).
4099
+ *
4100
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4101
+ *
4102
+ * See: https://hl7.org/fhirpath/#indexofsubstring-string-integer
4103
+ *
4104
+ * @param input The input collection.
4105
+ * @returns The index of the substring.
4106
+ */
4107
+ function indexOf(input, substringAtom) {
4108
+ return applyStringFunc((str, substring) => str.indexOf(substring), input, substringAtom);
4109
+ }
4110
+ /**
4111
+ * Returns the part of the string starting at position start (zero-based). If length is given, will return at most length number of characters from the input string.
4112
+ *
4113
+ * If start lies outside the length of the string, the function returns empty ({ }). If there are less remaining characters in the string than indicated by length, the function returns just the remaining characters.
4114
+ *
4115
+ * If the input or start is empty, the result is empty.
4116
+ *
4117
+ * If an empty length is provided, the behavior is the same as if length had not been provided.
4118
+ *
4119
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4120
+ *
4121
+ * @param input The input collection.
4122
+ * @returns The index of the substring.
4123
+ */
4124
+ function substring(input, startAtom, lengthAtom) {
4125
+ return applyStringFunc((str, start, length) => {
4126
+ const startIndex = start;
4127
+ const endIndex = length ? startIndex + length : str.length;
4128
+ return startIndex < 0 || startIndex >= str.length ? undefined : str.substring(startIndex, endIndex);
4129
+ }, input, startAtom, lengthAtom);
4130
+ }
4131
+ /**
4132
+ *
4133
+ * @param input The input collection.
4134
+ * @returns The index of the substring.
4135
+ */
4136
+ function startsWith(input, prefixAtom) {
4137
+ return applyStringFunc((str, prefix) => str.startsWith(prefix), input, prefixAtom);
4138
+ }
4139
+ /**
4140
+ *
4141
+ * @param input The input collection.
4142
+ * @returns The index of the substring.
4143
+ */
4144
+ function endsWith(input, suffixAtom) {
4145
+ return applyStringFunc((str, suffix) => str.endsWith(suffix), input, suffixAtom);
4146
+ }
4147
+ /**
4148
+ *
4149
+ * @param input The input collection.
4150
+ * @returns The index of the substring.
4151
+ */
4152
+ function contains(input, substringAtom) {
4153
+ return applyStringFunc((str, substring) => str.includes(substring), input, substringAtom);
4154
+ }
4155
+ /**
4156
+ *
4157
+ * @param input The input collection.
4158
+ * @returns The index of the substring.
4159
+ */
4160
+ function upper(input) {
4161
+ return applyStringFunc((str) => str.toUpperCase(), input);
4162
+ }
4163
+ /**
4164
+ *
4165
+ * @param input The input collection.
4166
+ * @returns The index of the substring.
4167
+ */
4168
+ function lower(input) {
4169
+ return applyStringFunc((str) => str.toLowerCase(), input);
4170
+ }
4171
+ /**
4172
+ *
4173
+ * @param input The input collection.
4174
+ * @returns The index of the substring.
4175
+ */
4176
+ function replace(input, patternAtom, substitionAtom) {
4177
+ return applyStringFunc((str, pattern, substition) => str.replaceAll(pattern, substition), input, patternAtom, substitionAtom);
4178
+ }
4179
+ /**
4180
+ *
4181
+ * @param input The input collection.
4182
+ * @returns The index of the substring.
4183
+ */
4184
+ function matches(input, regexAtom) {
4185
+ return applyStringFunc((str, regex) => !!str.match(regex), input, regexAtom);
4186
+ }
4187
+ /**
4188
+ *
4189
+ * @param input The input collection.
4190
+ * @returns The index of the substring.
4191
+ */
4192
+ function replaceMatches(input, regexAtom, substitionAtom) {
4193
+ return applyStringFunc((str, pattern, substition) => str.replaceAll(pattern, substition), input, regexAtom, substitionAtom);
4194
+ }
4195
+ /**
4196
+ *
4197
+ * @param input The input collection.
4198
+ * @returns The index of the substring.
4199
+ */
4200
+ function length(input) {
4201
+ return applyStringFunc((str) => str.length, input);
4202
+ }
4203
+ /**
4204
+ * Returns the list of characters in the input string. If the input collection is empty ({ }), the result is empty.
4205
+ *
4206
+ * See: https://hl7.org/fhirpath/#tochars-collection
4207
+ *
4208
+ * @param input The input collection.
4209
+ */
4210
+ function toChars(input) {
4211
+ return applyStringFunc((str) => (str ? str.split('') : undefined), input);
4212
+ }
4213
+ /*
4214
+ * 5.7. Math
4215
+ */
4216
+ /**
4217
+ * Returns the absolute value of the input. When taking the absolute value of a quantity, the unit is unchanged.
4218
+ *
4219
+ * If the input collection is empty, the result is empty.
4220
+ *
4221
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4222
+ *
4223
+ * See: https://hl7.org/fhirpath/#abs-integer-decimal-quantity
4224
+ *
4225
+ * @param input The input collection.
4226
+ * @returns A collection containing the result.
4227
+ */
4228
+ function abs(input) {
4229
+ return applyMathFunc(Math.abs, input);
4230
+ }
4231
+ /**
4232
+ * Returns the first integer greater than or equal to the input.
4233
+ *
4234
+ * If the input collection is empty, the result is empty.
4235
+ *
4236
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4237
+ *
4238
+ * See: https://hl7.org/fhirpath/#ceiling-integer
4239
+ *
4240
+ * @param input The input collection.
4241
+ * @returns A collection containing the result.
4242
+ */
4243
+ function ceiling(input) {
4244
+ return applyMathFunc(Math.ceil, input);
4245
+ }
4246
+ /**
4247
+ * Returns e raised to the power of the input.
4248
+ *
4249
+ * If the input collection contains an Integer, it will be implicitly converted to a Decimal and the result will be a Decimal.
4250
+ *
4251
+ * If the input collection is empty, the result is empty.
4252
+ *
4253
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4254
+ *
4255
+ * See: https://hl7.org/fhirpath/#exp-decimal
4256
+ *
4257
+ * @param input The input collection.
4258
+ * @returns A collection containing the result.
4259
+ */
4260
+ function exp(input) {
4261
+ return applyMathFunc(Math.exp, input);
4262
+ }
4263
+ /**
4264
+ * Returns the first integer less than or equal to the input.
4265
+ *
4266
+ * If the input collection is empty, the result is empty.
4267
+ *
4268
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4269
+ *
4270
+ * See: https://hl7.org/fhirpath/#floor-integer
4271
+ *
4272
+ * @param input The input collection.
4273
+ * @returns A collection containing the result.
4274
+ */
4275
+ function floor(input) {
4276
+ return applyMathFunc(Math.floor, input);
4277
+ }
4278
+ /**
4279
+ * Returns the natural logarithm of the input (i.e. the logarithm base e).
4280
+ *
4281
+ * When used with an Integer, it will be implicitly converted to a Decimal.
4282
+ *
4283
+ * If the input collection is empty, the result is empty.
4284
+ *
4285
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4286
+ *
4287
+ * See: https://hl7.org/fhirpath/#ln-decimal
4288
+ *
4289
+ * @param input The input collection.
4290
+ * @returns A collection containing the result.
4291
+ */
4292
+ function ln(input) {
4293
+ return applyMathFunc(Math.log, input);
4294
+ }
4295
+ /**
4296
+ * Returns the logarithm base base of the input number.
4297
+ *
4298
+ * When used with Integers, the arguments will be implicitly converted to Decimal.
4299
+ *
4300
+ * If base is empty, the result is empty.
4301
+ *
4302
+ * If the input collection is empty, the result is empty.
4303
+ *
4304
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4305
+ *
4306
+ * See: https://hl7.org/fhirpath/#logbase-decimal-decimal
4307
+ *
4308
+ * @param input The input collection.
4309
+ * @returns A collection containing the result.
4310
+ */
4311
+ function log(input, baseAtom) {
4312
+ return applyMathFunc((value, base) => Math.log(value) / Math.log(base), input, baseAtom);
4313
+ }
4314
+ /**
4315
+ * Raises a number to the exponent power. If this function is used with Integers, the result is an Integer. If the function is used with Decimals, the result is a Decimal. If the function is used with a mixture of Integer and Decimal, the Integer is implicitly converted to a Decimal and the result is a Decimal.
4316
+ *
4317
+ * If the power cannot be represented (such as the -1 raised to the 0.5), the result is empty.
4318
+ *
4319
+ * If the input is empty, or exponent is empty, the result is empty.
4320
+ *
4321
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4322
+ *
4323
+ * See: https://hl7.org/fhirpath/#powerexponent-integer-decimal-integer-decimal
4324
+ *
4325
+ * @param input The input collection.
4326
+ * @returns A collection containing the result.
4327
+ */
4328
+ function power(input, expAtom) {
4329
+ return applyMathFunc(Math.pow, input, expAtom);
4330
+ }
4331
+ /**
4332
+ * Rounds the decimal to the nearest whole number using a traditional round (i.e. 0.5 or higher will round to 1). If specified, the precision argument determines the decimal place at which the rounding will occur. If not specified, the rounding will default to 0 decimal places.
4333
+ *
4334
+ * If specified, the number of digits of precision must be >= 0 or the evaluation will end and signal an error to the calling environment.
4335
+ *
4336
+ * If the input collection contains a single item of type Integer, it will be implicitly converted to a Decimal.
4337
+ *
4338
+ * If the input collection is empty, the result is empty.
4339
+ *
4340
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4341
+ *
4342
+ * See: https://hl7.org/fhirpath/#roundprecision-integer-decimal
4343
+ *
4344
+ * @param input The input collection.
4345
+ * @returns A collection containing the result.
4346
+ */
4347
+ function round(input) {
4348
+ return applyMathFunc(Math.round, input);
4349
+ }
4350
+ /**
4351
+ * Returns the square root of the input number as a Decimal.
4352
+ *
4353
+ * If the square root cannot be represented (such as the square root of -1), the result is empty.
4354
+ *
4355
+ * If the input collection is empty, the result is empty.
4356
+ *
4357
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4358
+ *
4359
+ * Note that this function is equivalent to raising a number of the power of 0.5 using the power() function.
4360
+ *
4361
+ * See: https://hl7.org/fhirpath/#sqrt-decimal
4362
+ *
4363
+ * @param input The input collection.
4364
+ * @returns A collection containing the result.
4365
+ */
4366
+ function sqrt(input) {
4367
+ return applyMathFunc(Math.sqrt, input);
4368
+ }
4369
+ /**
4370
+ * Returns the integer portion of the input.
4371
+ *
4372
+ * If the input collection is empty, the result is empty.
4373
+ *
4374
+ * If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment.
4375
+ *
4376
+ * See: https://hl7.org/fhirpath/#truncate-integer
4377
+ *
4378
+ * @param input The input collection.
4379
+ * @returns A collection containing the result.
4380
+ */
4381
+ function truncate(input) {
4382
+ return applyMathFunc((x) => x | 0, input);
4383
+ }
4384
+ /*
4385
+ * 5.8. Tree navigation
4386
+ */
4387
+ const children = stub;
4388
+ const descendants = stub;
4389
+ /*
4390
+ * 5.9. Utility functions
4391
+ */
4392
+ /**
4393
+ * Adds a String representation of the input collection to the diagnostic log,
4394
+ * using the name argument as the name in the log. This log should be made available
4395
+ * to the user in some appropriate fashion. Does not change the input, so returns
4396
+ * the input collection as output.
4397
+ *
4398
+ * If the projection argument is used, the trace would log the result of evaluating
4399
+ * the project expression on the input, but still return the input to the trace
4400
+ * function unchanged.
4401
+ *
4402
+ * See: https://hl7.org/fhirpath/#tracename-string-projection-expression-collection
4403
+ *
4404
+ * @param input The input collection.
4405
+ * @param nameAtom The log name.
4406
+ */
4407
+ function trace(input, nameAtom) {
4408
+ console.log('trace', input, nameAtom);
4409
+ return input;
4410
+ }
4411
+ /**
4412
+ * Returns the current date and time, including timezone offset.
4413
+ *
4414
+ * See: https://hl7.org/fhirpath/#now-datetime
4415
+ */
4416
+ function now() {
4417
+ return [new Date().toISOString()];
4418
+ }
4419
+ /**
4420
+ * Returns the current time.
4421
+ *
4422
+ * See: https://hl7.org/fhirpath/#timeofday-time
4423
+ */
4424
+ function timeOfDay() {
4425
+ return [new Date().toISOString().substring(11)];
4426
+ }
4427
+ /**
4428
+ * Returns the current date.
4429
+ *
4430
+ * See: https://hl7.org/fhirpath/#today-date
4431
+ */
4432
+ function today() {
4433
+ return [new Date().toISOString().substring(0, 10)];
4434
+ }
4435
+ /**
4436
+ * Calculates the difference between two dates or date/times.
4437
+ *
4438
+ * This is not part of the official FHIRPath spec.
4439
+ *
4440
+ * IBM FHIR issue: https://github.com/IBM/FHIR/issues/1014
4441
+ * IBM FHIR PR: https://github.com/IBM/FHIR/pull/1023
4442
+ */
4443
+ function between(context, startAtom, endAtom, unitsAtom) {
4444
+ const startDate = toDateTime(ensureArray(startAtom.eval(context)));
4445
+ if (startDate.length === 0) {
4446
+ throw new Error('Invalid start date');
4447
+ }
4448
+ const endDate = toDateTime(ensureArray(endAtom.eval(context)));
4449
+ if (endDate.length === 0) {
4450
+ throw new Error('Invalid end date');
4451
+ }
4452
+ const unit = unitsAtom.eval(context);
4453
+ if (unit !== 'years' && unit !== 'months' && unit !== 'days') {
4454
+ throw new Error('Invalid units');
4455
+ }
4456
+ const age = calculateAge(startDate[0], endDate[0]);
4457
+ return [{ value: age[unit], unit }];
4458
+ }
4459
+ /*
4460
+ * 6.3 Types
4461
+ */
4462
+ /**
4463
+ * The is() function is supported for backwards compatibility with previous
4464
+ * implementations of FHIRPath. Just as with the is keyword, the type argument
4465
+ * is an identifier that must resolve to the name of a type in a model.
4466
+ *
4467
+ * For implementations with compile-time typing, this requires special-case
4468
+ * handling when processing the argument to treat it as a type specifier rather
4469
+ * than an identifier expression:
4470
+ *
4471
+ * @param input
4472
+ * @param typeAtom
4473
+ * @returns
4474
+ */
4475
+ function is(input, typeAtom) {
4476
+ const typeName = typeAtom.name;
4477
+ return input.map((value) => fhirPathIs(value, typeName));
4478
+ }
4479
+ /*
4480
+ * 6.5 Boolean logic
4481
+ */
4482
+ /**
4483
+ * 6.5.3. not() : Boolean
4484
+ *
4485
+ * Returns true if the input collection evaluates to false, and false if it evaluates to true. Otherwise, the result is empty ({ }):
4486
+ *
4487
+ * @param input
4488
+ * @returns
4489
+ */
4490
+ function not(input) {
4491
+ return toBoolean(input).map((value) => !value);
4492
+ }
4493
+ /*
4494
+ * Additional functions
4495
+ * See: https://hl7.org/fhir/fhirpath.html#functions
4496
+ */
4497
+ /**
4498
+ * For each item in the collection, if it is a string that is a uri (or canonical or url), locate the target of the reference, and add it to the resulting collection. If the item does not resolve to a resource, the item is ignored and nothing is added to the output collection.
4499
+ * The items in the collection may also represent a Reference, in which case the Reference.reference is resolved.
4500
+ * @param input The input collection.
4501
+ * @returns
4502
+ */
4503
+ function resolve(input) {
4504
+ return input
4505
+ .map((e) => {
4506
+ let refStr;
4507
+ if (typeof e === 'string') {
4508
+ refStr = e;
4509
+ }
4510
+ else if (typeof e === 'object') {
4511
+ const ref = e;
4512
+ if (ref.resource) {
4513
+ return ref.resource;
4514
+ }
4515
+ refStr = ref.reference;
4516
+ }
4517
+ if (!refStr) {
4518
+ return undefined;
4519
+ }
4520
+ const [resourceType, id] = refStr.split('/');
4521
+ return { resourceType, id };
4522
+ })
4523
+ .filter((e) => !!e);
4524
+ }
4525
+ /**
4526
+ * The as operator can be used to treat a value as a specific type.
4527
+ * @param context The context value.
4528
+ * @returns The value as the specific type.
4529
+ */
4530
+ function as(context) {
4531
+ return context;
4532
+ }
4533
+ /*
4534
+ * 12. Formal Specifications
4535
+ */
4536
+ /**
4537
+ * Returns the type of the input.
4538
+ *
4539
+ * 12.2. Model Information
4540
+ *
4541
+ * The model information returned by the reflection function type() is specified as an
4542
+ * XML Schema document (xsd) and included in this specification at the following link:
4543
+ * https://hl7.org/fhirpath/modelinfo.xsd
4544
+ *
4545
+ * See: https://hl7.org/fhirpath/#model-information
4546
+ *
4547
+ * @param input The input collection.
4548
+ * @returns
4549
+ */
4550
+ function type(input) {
4551
+ return input.map((value) => {
4552
+ if (typeof value === 'boolean') {
4553
+ return { namespace: 'System', name: 'Boolean' };
4554
+ }
4555
+ if (typeof value === 'number') {
4556
+ return { namespace: 'System', name: 'Integer' };
4557
+ }
4558
+ if (value && typeof value === 'object' && 'resourceType' in value) {
4559
+ return { namespace: 'FHIR', name: value.resourceType };
4560
+ }
4561
+ return null;
4562
+ });
4563
+ }
4564
+ function conformsTo(input, systemAtom) {
4565
+ const system = systemAtom.eval(undefined);
4566
+ if (!system.startsWith('http://hl7.org/fhir/StructureDefinition/')) {
4567
+ throw new Error('Expected a StructureDefinition URL');
4568
+ }
4569
+ const expectedResourceType = system.replace('http://hl7.org/fhir/StructureDefinition/', '');
4570
+ return input.map((resource) => (resource === null || resource === void 0 ? void 0 : resource.resourceType) === expectedResourceType);
4571
+ }
4572
+ /*
4573
+ * Helper utilities
4574
+ */
4575
+ function applyStringFunc(func, input, ...argsAtoms) {
4576
+ if (input.length === 0) {
4577
+ return [];
4578
+ }
4579
+ const [value] = validateInput(input, 1);
4580
+ if (typeof value !== 'string') {
4581
+ throw new Error('String function cannot be called with non-string');
4582
+ }
4583
+ const result = func(value, ...argsAtoms.map((atom) => atom && atom.eval(value)));
4584
+ return result === undefined ? [] : [result];
4585
+ }
4586
+ function applyMathFunc(func, input, ...argsAtoms) {
4587
+ if (input.length === 0) {
4588
+ return [];
4589
+ }
4590
+ const [value] = validateInput(input, 1);
4591
+ const quantity = isQuantity(value);
4592
+ const numberInput = quantity ? value.value : value;
4593
+ if (typeof numberInput !== 'number') {
4594
+ throw new Error('Math function cannot be called with non-number');
4595
+ }
4596
+ const result = func(numberInput, ...argsAtoms.map((atom) => atom.eval(undefined)));
4597
+ return quantity ? [Object.assign(Object.assign({}, value), { value: result })] : [result];
4598
+ }
4599
+ function validateInput(input, count) {
4600
+ if (input.length !== count) {
4601
+ throw new Error(`Expected ${count} arguments`);
4602
+ }
4603
+ return input;
4604
+ }
4605
+
4606
+ var functions = /*#__PURE__*/Object.freeze({
4607
+ __proto__: null,
4608
+ empty: empty,
4609
+ exists: exists,
4610
+ all: all,
4611
+ allTrue: allTrue,
4612
+ anyTrue: anyTrue,
4613
+ allFalse: allFalse,
4614
+ anyFalse: anyFalse,
4615
+ subsetOf: subsetOf,
4616
+ supersetOf: supersetOf,
4617
+ count: count,
4618
+ distinct: distinct,
4619
+ isDistinct: isDistinct,
4620
+ where: where,
4621
+ select: select,
4622
+ repeat: repeat,
4623
+ ofType: ofType,
4624
+ single: single,
4625
+ first: first,
4626
+ last: last,
4627
+ tail: tail,
4628
+ skip: skip,
4629
+ take: take,
4630
+ intersect: intersect,
4631
+ exclude: exclude,
4632
+ union: union,
4633
+ combine: combine,
4634
+ iif: iif,
4635
+ toBoolean: toBoolean,
4636
+ convertsToBoolean: convertsToBoolean,
4637
+ toInteger: toInteger,
4638
+ convertsToInteger: convertsToInteger,
4639
+ toDate: toDate,
4640
+ convertsToDate: convertsToDate,
4641
+ toDateTime: toDateTime,
4642
+ convertsToDateTime: convertsToDateTime,
4643
+ toDecimal: toDecimal,
4644
+ convertsToDecimal: convertsToDecimal,
4645
+ toQuantity: toQuantity,
4646
+ convertsToQuantity: convertsToQuantity,
4647
+ toString: toString,
4648
+ convertsToString: convertsToString,
4649
+ toTime: toTime,
4650
+ convertsToTime: convertsToTime,
4651
+ indexOf: indexOf,
4652
+ substring: substring,
4653
+ startsWith: startsWith,
4654
+ endsWith: endsWith,
4655
+ contains: contains,
4656
+ upper: upper,
4657
+ lower: lower,
4658
+ replace: replace,
4659
+ matches: matches,
4660
+ replaceMatches: replaceMatches,
4661
+ length: length,
4662
+ toChars: toChars,
4663
+ abs: abs,
4664
+ ceiling: ceiling,
4665
+ exp: exp,
4666
+ floor: floor,
4667
+ ln: ln,
4668
+ log: log,
4669
+ power: power,
4670
+ round: round,
4671
+ sqrt: sqrt,
4672
+ truncate: truncate,
4673
+ children: children,
4674
+ descendants: descendants,
4675
+ trace: trace,
4676
+ now: now,
4677
+ timeOfDay: timeOfDay,
4678
+ today: today,
4679
+ between: between,
4680
+ is: is,
4681
+ not: not,
4682
+ resolve: resolve,
4683
+ as: as,
4684
+ type: type,
4685
+ conformsTo: conformsTo
4686
+ });
4687
+
4688
+ var _Tokenizer_instances, _Tokenizer_str, _Tokenizer_pos, _Tokenizer_peekToken, _Tokenizer_consumeToken, _Tokenizer_consumeWhitespace, _Tokenizer_consumeMultiLineComment, _Tokenizer_consumeSingleLineComment, _Tokenizer_consumeString, _Tokenizer_consumeBacktickSymbol, _Tokenizer_consumeDateTime, _Tokenizer_consumeNumber, _Tokenizer_consumeSymbol, _Tokenizer_consumeOperator, _Tokenizer_consumeWhile, _Tokenizer_curr, _Tokenizer_prev, _Tokenizer_peek;
4689
+ function tokenize(str) {
4690
+ return new Tokenizer(str).tokenize();
4691
+ }
4692
+ const STANDARD_UNITS = [
4693
+ 'year',
4694
+ 'years',
4695
+ 'month',
4696
+ 'months',
4697
+ 'week',
4698
+ 'weeks',
4699
+ 'day',
4700
+ 'days',
4701
+ 'hour',
4702
+ 'hours',
4703
+ 'minute',
4704
+ 'minutes',
4705
+ 'second',
4706
+ 'seconds',
4707
+ 'millisecond',
4708
+ 'milliseconds',
4709
+ ];
4710
+ const TWO_CHAR_OPERATORS = ['!=', '!~', '<=', '>=', '{}'];
4711
+ class Tokenizer {
4712
+ constructor(str) {
4713
+ _Tokenizer_instances.add(this);
4714
+ _Tokenizer_str.set(this, void 0);
4715
+ _Tokenizer_pos.set(this, void 0);
4716
+ __classPrivateFieldSet(this, _Tokenizer_str, str, "f");
4717
+ __classPrivateFieldSet(this, _Tokenizer_pos, 0, "f");
4718
+ }
4719
+ tokenize() {
4720
+ const result = [];
4721
+ while (__classPrivateFieldGet(this, _Tokenizer_pos, "f") < __classPrivateFieldGet(this, _Tokenizer_str, "f").length) {
4722
+ const token = __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeToken).call(this);
4723
+ if (token) {
4724
+ result.push(token);
4725
+ }
4726
+ }
4727
+ return result;
4728
+ }
4729
+ }
4730
+ _Tokenizer_str = new WeakMap(), _Tokenizer_pos = new WeakMap(), _Tokenizer_instances = new WeakSet(), _Tokenizer_peekToken = function _Tokenizer_peekToken() {
4731
+ const start = __classPrivateFieldGet(this, _Tokenizer_pos, "f");
4732
+ const token = __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeToken).call(this);
4733
+ __classPrivateFieldSet(this, _Tokenizer_pos, start, "f");
4734
+ return token;
4735
+ }, _Tokenizer_consumeToken = function _Tokenizer_consumeToken() {
4736
+ __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeWhitespace).call(this);
4737
+ const c = __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_curr).call(this);
4738
+ if (!c) {
4739
+ return undefined;
4740
+ }
4741
+ const next = __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_peek).call(this);
4742
+ if (c === '/' && next === '*') {
4743
+ return __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeMultiLineComment).call(this);
4744
+ }
4745
+ if (c === '/' && next === '/') {
4746
+ return __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeSingleLineComment).call(this);
4747
+ }
4748
+ if (c === "'") {
4749
+ return __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeString).call(this);
4750
+ }
4751
+ if (c === '`') {
4752
+ return __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeBacktickSymbol).call(this);
4753
+ }
4754
+ if (c === '@') {
4755
+ return __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeDateTime).call(this);
4756
+ }
4757
+ if (c.match(/\d/)) {
4758
+ return __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeNumber).call(this);
4759
+ }
4760
+ if (c.match(/\w/)) {
4761
+ return __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeSymbol).call(this);
4762
+ }
4763
+ if (c === '$' && next.match(/\w/)) {
4764
+ return __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeSymbol).call(this);
4765
+ }
4766
+ return __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeOperator).call(this);
4767
+ }, _Tokenizer_consumeWhitespace = function _Tokenizer_consumeWhitespace() {
4768
+ return buildToken('Whitespace', __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeWhile).call(this, () => __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_curr).call(this).match(/\s/)));
4769
+ }, _Tokenizer_consumeMultiLineComment = function _Tokenizer_consumeMultiLineComment() {
4770
+ const start = __classPrivateFieldGet(this, _Tokenizer_pos, "f");
4771
+ __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeWhile).call(this, () => __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_curr).call(this) !== '*' || __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_peek).call(this) !== '/');
4772
+ __classPrivateFieldSet(this, _Tokenizer_pos, __classPrivateFieldGet(this, _Tokenizer_pos, "f") + 2, "f");
4773
+ return buildToken('Comment', __classPrivateFieldGet(this, _Tokenizer_str, "f").substring(start, __classPrivateFieldGet(this, _Tokenizer_pos, "f")));
4774
+ }, _Tokenizer_consumeSingleLineComment = function _Tokenizer_consumeSingleLineComment() {
4775
+ return buildToken('Comment', __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeWhile).call(this, () => __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_curr).call(this) !== '\n'));
4776
+ }, _Tokenizer_consumeString = function _Tokenizer_consumeString() {
4777
+ var _a, _b;
4778
+ __classPrivateFieldSet(this, _Tokenizer_pos, (_a = __classPrivateFieldGet(this, _Tokenizer_pos, "f"), _a++, _a), "f");
4779
+ const result = buildToken('String', __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeWhile).call(this, () => __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_prev).call(this) === '\\' || __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_curr).call(this) !== "'"));
4780
+ __classPrivateFieldSet(this, _Tokenizer_pos, (_b = __classPrivateFieldGet(this, _Tokenizer_pos, "f"), _b++, _b), "f");
4781
+ return result;
4782
+ }, _Tokenizer_consumeBacktickSymbol = function _Tokenizer_consumeBacktickSymbol() {
4783
+ var _a, _b;
4784
+ __classPrivateFieldSet(this, _Tokenizer_pos, (_a = __classPrivateFieldGet(this, _Tokenizer_pos, "f"), _a++, _a), "f");
4785
+ const result = buildToken('Symbol', __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeWhile).call(this, () => __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_curr).call(this) !== '`'));
4786
+ __classPrivateFieldSet(this, _Tokenizer_pos, (_b = __classPrivateFieldGet(this, _Tokenizer_pos, "f"), _b++, _b), "f");
4787
+ return result;
4788
+ }, _Tokenizer_consumeDateTime = function _Tokenizer_consumeDateTime() {
4789
+ var _a, _b, _c, _d, _e;
4790
+ const start = __classPrivateFieldGet(this, _Tokenizer_pos, "f");
4791
+ __classPrivateFieldSet(this, _Tokenizer_pos, (_a = __classPrivateFieldGet(this, _Tokenizer_pos, "f"), _a++, _a), "f");
4792
+ __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeWhile).call(this, () => __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_curr).call(this).match(/[\d-]/));
4793
+ if (__classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_curr).call(this) === 'T') {
4794
+ __classPrivateFieldSet(this, _Tokenizer_pos, (_b = __classPrivateFieldGet(this, _Tokenizer_pos, "f"), _b++, _b), "f");
4795
+ __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeWhile).call(this, () => __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_curr).call(this).match(/[\d:]/));
4796
+ if (__classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_curr).call(this) === '.' && __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_peek).call(this).match(/\d/)) {
4797
+ __classPrivateFieldSet(this, _Tokenizer_pos, (_c = __classPrivateFieldGet(this, _Tokenizer_pos, "f"), _c++, _c), "f");
4798
+ __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeWhile).call(this, () => __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_curr).call(this).match(/[\d]/));
4799
+ }
4800
+ if (__classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_curr).call(this) === 'Z') {
4801
+ __classPrivateFieldSet(this, _Tokenizer_pos, (_d = __classPrivateFieldGet(this, _Tokenizer_pos, "f"), _d++, _d), "f");
4802
+ }
4803
+ else if (__classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_curr).call(this) === '+' || __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_curr).call(this) === '-') {
4804
+ __classPrivateFieldSet(this, _Tokenizer_pos, (_e = __classPrivateFieldGet(this, _Tokenizer_pos, "f"), _e++, _e), "f");
4805
+ __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeWhile).call(this, () => __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_curr).call(this).match(/[\d:]/));
4806
+ }
4807
+ }
4808
+ return buildToken('DateTime', __classPrivateFieldGet(this, _Tokenizer_str, "f").substring(start + 1, __classPrivateFieldGet(this, _Tokenizer_pos, "f")));
4809
+ }, _Tokenizer_consumeNumber = function _Tokenizer_consumeNumber() {
4810
+ var _a;
4811
+ const start = __classPrivateFieldGet(this, _Tokenizer_pos, "f");
4812
+ let id = 'Number';
4813
+ __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeWhile).call(this, () => __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_curr).call(this).match(/\d/));
4814
+ if (__classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_curr).call(this) === '.' && __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_peek).call(this).match(/\d/)) {
4815
+ __classPrivateFieldSet(this, _Tokenizer_pos, (_a = __classPrivateFieldGet(this, _Tokenizer_pos, "f"), _a++, _a), "f");
4816
+ __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeWhile).call(this, () => __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_curr).call(this).match(/\d/));
4817
+ }
4818
+ if (__classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_curr).call(this) === ' ') {
4819
+ if (isUnitToken(__classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_peekToken).call(this))) {
4820
+ id = 'Quantity';
4821
+ __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeToken).call(this);
4822
+ }
4823
+ }
4824
+ return buildToken(id, __classPrivateFieldGet(this, _Tokenizer_str, "f").substring(start, __classPrivateFieldGet(this, _Tokenizer_pos, "f")));
4825
+ }, _Tokenizer_consumeSymbol = function _Tokenizer_consumeSymbol() {
4826
+ return buildToken('Symbol', __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_consumeWhile).call(this, () => __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_curr).call(this).match(/[$\w]/)));
4827
+ }, _Tokenizer_consumeOperator = function _Tokenizer_consumeOperator() {
4828
+ var _a;
4829
+ const c = __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_curr).call(this);
4830
+ const next = __classPrivateFieldGet(this, _Tokenizer_instances, "m", _Tokenizer_peek).call(this);
4831
+ const twoCharOp = c + next;
4832
+ if (TWO_CHAR_OPERATORS.includes(twoCharOp)) {
4833
+ __classPrivateFieldSet(this, _Tokenizer_pos, __classPrivateFieldGet(this, _Tokenizer_pos, "f") + 2, "f");
4834
+ return buildToken(twoCharOp, twoCharOp);
4835
+ }
4836
+ __classPrivateFieldSet(this, _Tokenizer_pos, (_a = __classPrivateFieldGet(this, _Tokenizer_pos, "f"), _a++, _a), "f");
4837
+ return buildToken(c, c);
4838
+ }, _Tokenizer_consumeWhile = function _Tokenizer_consumeWhile(condition) {
4839
+ var _a;
4840
+ const start = __classPrivateFieldGet(this, _Tokenizer_pos, "f");
4841
+ while (__classPrivateFieldGet(this, _Tokenizer_pos, "f") < __classPrivateFieldGet(this, _Tokenizer_str, "f").length && condition()) {
4842
+ __classPrivateFieldSet(this, _Tokenizer_pos, (_a = __classPrivateFieldGet(this, _Tokenizer_pos, "f"), _a++, _a), "f");
4843
+ }
4844
+ return __classPrivateFieldGet(this, _Tokenizer_str, "f").substring(start, __classPrivateFieldGet(this, _Tokenizer_pos, "f"));
4845
+ }, _Tokenizer_curr = function _Tokenizer_curr() {
4846
+ return __classPrivateFieldGet(this, _Tokenizer_str, "f")[__classPrivateFieldGet(this, _Tokenizer_pos, "f")];
4847
+ }, _Tokenizer_prev = function _Tokenizer_prev() {
4848
+ var _a;
4849
+ return (_a = __classPrivateFieldGet(this, _Tokenizer_str, "f")[__classPrivateFieldGet(this, _Tokenizer_pos, "f") - 1]) !== null && _a !== void 0 ? _a : '';
4850
+ }, _Tokenizer_peek = function _Tokenizer_peek() {
4851
+ var _a;
4852
+ return (_a = __classPrivateFieldGet(this, _Tokenizer_str, "f")[__classPrivateFieldGet(this, _Tokenizer_pos, "f") + 1]) !== null && _a !== void 0 ? _a : '';
4853
+ };
4854
+ function buildToken(id, value) {
4855
+ return { id, value };
4856
+ }
4857
+ function isUnitToken(token) {
4858
+ if (token) {
4859
+ if (token.id === 'String') {
4860
+ return true;
4861
+ }
4862
+ if (token.id === 'Symbol' && STANDARD_UNITS.includes(token.value)) {
4863
+ return true;
4864
+ }
4865
+ }
4866
+ return false;
4867
+ }
4868
+
4869
+ var _ParserBuilder_prefixParselets, _ParserBuilder_infixParselets, _Parser_instances, _Parser_tokens, _Parser_prefixParselets, _Parser_infixParselets, _Parser_getPrecedence, _Parser_consume, _Parser_look;
4870
+ class ParserBuilder {
4871
+ constructor() {
4872
+ _ParserBuilder_prefixParselets.set(this, {});
4873
+ _ParserBuilder_infixParselets.set(this, {});
4874
+ }
4875
+ registerInfix(tokenType, parselet) {
4876
+ __classPrivateFieldGet(this, _ParserBuilder_infixParselets, "f")[tokenType] = parselet;
4877
+ return this;
4878
+ }
4879
+ registerPrefix(tokenType, parselet) {
4880
+ __classPrivateFieldGet(this, _ParserBuilder_prefixParselets, "f")[tokenType] = parselet;
4881
+ return this;
4882
+ }
4883
+ prefix(tokenType, precedence, builder) {
4884
+ return this.registerPrefix(tokenType, {
4885
+ parse(parser, token) {
4886
+ const right = parser.consumeAndParse(precedence);
4887
+ return builder(token, right);
4888
+ },
4889
+ });
4890
+ }
4891
+ infixLeft(tokenType, precedence, builder) {
4892
+ return this.registerInfix(tokenType, {
4893
+ parse(parser, left, token) {
4894
+ const right = parser.consumeAndParse(precedence);
4895
+ return builder(left, token, right);
4896
+ },
4897
+ precedence,
4898
+ });
4899
+ }
4900
+ construct(input) {
4901
+ return new Parser(tokenize(input), __classPrivateFieldGet(this, _ParserBuilder_prefixParselets, "f"), __classPrivateFieldGet(this, _ParserBuilder_infixParselets, "f"));
4902
+ }
4903
+ }
4904
+ _ParserBuilder_prefixParselets = new WeakMap(), _ParserBuilder_infixParselets = new WeakMap();
4905
+ class Parser {
4906
+ constructor(tokens, prefixParselets, infixParselets) {
4907
+ _Parser_instances.add(this);
4908
+ _Parser_tokens.set(this, void 0);
4909
+ _Parser_prefixParselets.set(this, void 0);
4910
+ _Parser_infixParselets.set(this, void 0);
4911
+ __classPrivateFieldSet(this, _Parser_tokens, tokens, "f");
4912
+ __classPrivateFieldSet(this, _Parser_prefixParselets, prefixParselets, "f");
4913
+ __classPrivateFieldSet(this, _Parser_infixParselets, infixParselets, "f");
4914
+ }
4915
+ match(expected) {
4916
+ const token = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_look).call(this);
4917
+ if ((token === null || token === void 0 ? void 0 : token.id) !== expected) {
4918
+ return false;
4919
+ }
4920
+ __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_consume).call(this);
4921
+ return true;
4922
+ }
4923
+ consumeAndParse(precedence = 100 /* Precedence.MaximumPrecedence */) {
4924
+ const token = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_consume).call(this);
4925
+ const prefix = __classPrivateFieldGet(this, _Parser_prefixParselets, "f")[token.id];
4926
+ if (!prefix) {
4927
+ throw Error(`Parse error at ${token.value}. No matching prefix parselet.`);
4928
+ }
4929
+ let left = prefix.parse(this, token);
4930
+ while (precedence > __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_getPrecedence).call(this)) {
4931
+ const next = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_consume).call(this);
4932
+ const infix = __classPrivateFieldGet(this, _Parser_infixParselets, "f")[next.id];
4933
+ left = infix.parse(this, left, next);
4934
+ }
4935
+ return left;
4936
+ }
4937
+ }
4938
+ _Parser_tokens = new WeakMap(), _Parser_prefixParselets = new WeakMap(), _Parser_infixParselets = new WeakMap(), _Parser_instances = new WeakSet(), _Parser_getPrecedence = function _Parser_getPrecedence() {
4939
+ const nextToken = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_look).call(this);
4940
+ if (!nextToken) {
4941
+ return 100 /* Precedence.MaximumPrecedence */;
4942
+ }
4943
+ const parser = __classPrivateFieldGet(this, _Parser_infixParselets, "f")[nextToken.id];
4944
+ if (parser) {
4945
+ return parser.precedence;
4946
+ }
4947
+ return 100 /* Precedence.MaximumPrecedence */;
4948
+ }, _Parser_consume = function _Parser_consume() {
4949
+ if (!__classPrivateFieldGet(this, _Parser_tokens, "f").length) {
4950
+ throw Error('Cant consume unknown more tokens.');
4951
+ }
4952
+ return __classPrivateFieldGet(this, _Parser_tokens, "f").shift();
4953
+ }, _Parser_look = function _Parser_look() {
4954
+ return __classPrivateFieldGet(this, _Parser_tokens, "f").length > 0 ? __classPrivateFieldGet(this, _Parser_tokens, "f")[0] : undefined;
4955
+ };
4956
+ const PARENTHESES_PARSELET = {
4957
+ parse(parser) {
4958
+ const expr = parser.consumeAndParse();
4959
+ if (!parser.match(')')) {
4960
+ throw new Error('Parse error: expected `)`');
4961
+ }
4962
+ return expr;
4963
+ },
4964
+ };
4965
+ const INDEXER_PARSELET = {
4966
+ parse(parser, left) {
4967
+ const expr = parser.consumeAndParse();
4968
+ if (!parser.match(']')) {
4969
+ throw new Error('Parse error: expected `]`');
4970
+ }
4971
+ return new IndexerAtom(left, expr);
4972
+ },
4973
+ precedence: 2 /* Precedence.Indexer */,
4974
+ };
4975
+ const FUNCTION_CALL_PARSELET = {
4976
+ parse(parser, left) {
4977
+ if (!(left instanceof SymbolAtom)) {
4978
+ throw new Error('Unexpected parentheses');
4979
+ }
4980
+ if (!(left.name in functions)) {
4981
+ throw new Error('Unrecognized function: ' + left.name);
4982
+ }
4983
+ const args = [];
4984
+ while (!parser.match(')')) {
4985
+ args.push(parser.consumeAndParse());
4986
+ parser.match(',');
4987
+ }
4988
+ return new FunctionAtom(left.name, args, functions[left.name]);
4989
+ },
4990
+ precedence: 0 /* Precedence.FunctionCall */,
4991
+ };
4992
+ function parseQuantity(str) {
4993
+ const parts = str.split(' ');
4994
+ const value = parseFloat(parts[0]);
4995
+ let unit = parts[1];
4996
+ if (unit && unit.startsWith("'") && unit.endsWith("'")) {
4997
+ unit = unit.substring(1, unit.length - 1);
4998
+ }
4999
+ else {
5000
+ unit = '{' + unit + '}';
5001
+ }
5002
+ return { value, unit };
5003
+ }
5004
+ const parserBuilder = new ParserBuilder()
5005
+ .registerPrefix('String', {
5006
+ parse: (_, token) => new LiteralAtom(token.value),
5007
+ })
5008
+ .registerPrefix('DateTime', {
5009
+ parse: (_, token) => new LiteralAtom(parseDateString(token.value)),
5010
+ })
5011
+ .registerPrefix('Quantity', {
5012
+ parse: (_, token) => new LiteralAtom(parseQuantity(token.value)),
5013
+ })
5014
+ .registerPrefix('Number', {
5015
+ parse: (_, token) => new LiteralAtom(parseFloat(token.value)),
5016
+ })
5017
+ .registerPrefix('Symbol', {
5018
+ parse: (_, token) => {
5019
+ if (token.value === 'false') {
5020
+ return new LiteralAtom(false);
5021
+ }
5022
+ if (token.value === 'true') {
5023
+ return new LiteralAtom(true);
5024
+ }
5025
+ return new SymbolAtom(token.value);
5026
+ },
5027
+ })
5028
+ .registerPrefix('{}', { parse: () => new EmptySetAtom() })
5029
+ .registerPrefix('(', PARENTHESES_PARSELET)
5030
+ .registerInfix('[', INDEXER_PARSELET)
5031
+ .registerInfix('(', FUNCTION_CALL_PARSELET)
5032
+ .prefix('+', 3 /* Precedence.UnaryAdd */, (_, right) => new UnaryOperatorAtom(right, (x) => x))
5033
+ .prefix('-', 3 /* Precedence.UnarySubtract */, (_, right) => new ArithemticOperatorAtom(right, right, (_, y) => -y))
5034
+ .infixLeft('.', 1 /* Precedence.Dot */, (left, _, right) => new DotAtom(left, right))
5035
+ .infixLeft('/', 4 /* Precedence.Divide */, (left, _, right) => new ArithemticOperatorAtom(left, right, (x, y) => x / y))
5036
+ .infixLeft('*', 4 /* Precedence.Multiply */, (left, _, right) => new ArithemticOperatorAtom(left, right, (x, y) => x * y))
5037
+ .infixLeft('+', 5 /* Precedence.Add */, (left, _, right) => new ArithemticOperatorAtom(left, right, (x, y) => x + y))
5038
+ .infixLeft('-', 5 /* Precedence.Subtract */, (left, _, right) => new ArithemticOperatorAtom(left, right, (x, y) => x - y))
5039
+ .infixLeft('|', 7 /* Precedence.Union */, (left, _, right) => new UnionAtom(left, right))
5040
+ .infixLeft('=', 9 /* Precedence.Equals */, (left, _, right) => new EqualsAtom(left, right))
5041
+ .infixLeft('!=', 9 /* Precedence.Equals */, (left, _, right) => new NotEqualsAtom(left, right))
5042
+ .infixLeft('~', 9 /* Precedence.Equivalent */, (left, _, right) => new EquivalentAtom(left, right))
5043
+ .infixLeft('!~', 9 /* Precedence.NotEquivalent */, (left, _, right) => new NotEquivalentAtom(left, right))
5044
+ .infixLeft('<', 8 /* Precedence.LessThan */, (left, _, right) => new ComparisonOperatorAtom(left, right, (x, y) => x < y))
5045
+ .infixLeft('<=', 8 /* Precedence.LessThanOrEquals */, (left, _, right) => new ComparisonOperatorAtom(left, right, (x, y) => x <= y))
5046
+ .infixLeft('>', 8 /* Precedence.GreaterThan */, (left, _, right) => new ComparisonOperatorAtom(left, right, (x, y) => x > y))
5047
+ .infixLeft('>=', 8 /* Precedence.GreaterThanOrEquals */, (left, _, right) => new ComparisonOperatorAtom(left, right, (x, y) => x >= y))
5048
+ .infixLeft('&', 5 /* Precedence.Ampersand */, (left, _, right) => new ConcatAtom(left, right))
5049
+ .infixLeft('Symbol', 6 /* Precedence.Is */, (left, symbol, right) => {
5050
+ switch (symbol.value) {
5051
+ case 'and':
5052
+ return new AndAtom(left, right);
5053
+ case 'as':
5054
+ return new AsAtom(left, right);
5055
+ case 'contains':
5056
+ return new ContainsAtom(left, right);
5057
+ case 'div':
5058
+ return new ArithemticOperatorAtom(left, right, (x, y) => (x / y) | 0);
5059
+ case 'in':
5060
+ return new InAtom(left, right);
5061
+ case 'is':
5062
+ return new IsAtom(left, right);
5063
+ case 'mod':
5064
+ return new ArithemticOperatorAtom(left, right, (x, y) => x % y);
5065
+ case 'or':
5066
+ return new OrAtom(left, right);
5067
+ case 'xor':
5068
+ return new XorAtom(left, right);
5069
+ default:
5070
+ throw new Error('Cannot use ' + symbol.value + ' as infix operator');
5071
+ }
5072
+ });
5073
+ /**
5074
+ * Parses a FHIRPath expression into an AST.
5075
+ * The result can be used to evaluate the expression against a resource or other object.
5076
+ * This method is useful if you know that you will evaluate the same expression many times
5077
+ * against different resources.
5078
+ * @param input The FHIRPath expression to parse.
5079
+ * @returns The AST representing the expression.
5080
+ */
5081
+ function parseFhirPath(input) {
5082
+ try {
5083
+ return new FhirPathAtom(input, parserBuilder.construct(input).consumeAndParse());
5084
+ }
5085
+ catch (error) {
5086
+ throw new Error(`FhirPathError on "${input}": ${error}`);
5087
+ }
5088
+ }
5089
+ /**
5090
+ * Evaluates a FHIRPath expression against a resource or other object.
5091
+ * @param input The FHIRPath expression to parse.
5092
+ * @param context The resource or object to evaluate the expression against.
5093
+ * @returns The result of the FHIRPath expression against the resource or object.
5094
+ */
5095
+ function evalFhirPath(input, context) {
5096
+ return parseFhirPath(input).eval(context);
5097
+ }
5098
+
2500
5099
  const SEGMENT_SEPARATOR = '\r';
2501
5100
  const FIELD_SEPARATOR = '|';
2502
5101
  const COMPONENT_SEPARATOR = '^';
@@ -2755,7 +5354,8 @@
2755
5354
  exports.createSchema = createSchema;
2756
5355
  exports.createTypeSchema = createTypeSchema;
2757
5356
  exports.created = created;
2758
- exports.deepEquals = deepEquals;
5357
+ exports.deepEquals = deepEquals$1;
5358
+ exports.evalFhirPath = evalFhirPath;
2759
5359
  exports.formatAddress = formatAddress;
2760
5360
  exports.formatFamilyName = formatFamilyName;
2761
5361
  exports.formatGivenName = formatGivenName;
@@ -2765,6 +5365,7 @@
2765
5365
  exports.getDisplayString = getDisplayString;
2766
5366
  exports.getExpressionForResourceType = getExpressionForResourceType;
2767
5367
  exports.getExtensionValue = getExtensionValue;
5368
+ exports.getIdentifier = getIdentifier;
2768
5369
  exports.getImageSrc = getImageSrc;
2769
5370
  exports.getPropertyDisplayName = getPropertyDisplayName;
2770
5371
  exports.getQuestionnaireAnswers = getQuestionnaireAnswers;
@@ -2777,16 +5378,18 @@
2777
5378
  exports.isGone = isGone;
2778
5379
  exports.isLowerCase = isLowerCase;
2779
5380
  exports.isNotFound = isNotFound;
2780
- exports.isObject = isObject;
5381
+ exports.isObject = isObject$1;
2781
5382
  exports.isOk = isOk;
2782
5383
  exports.isProfileResource = isProfileResource;
2783
5384
  exports.isStringArray = isStringArray;
2784
5385
  exports.isUUID = isUUID;
2785
5386
  exports.notFound = notFound;
2786
5387
  exports.notModified = notModified;
5388
+ exports.parseFhirPath = parseFhirPath;
2787
5389
  exports.parseSearchDefinition = parseSearchDefinition;
2788
5390
  exports.resolveId = resolveId;
2789
5391
  exports.stringify = stringify;
5392
+ exports.tokenize = tokenize;
2790
5393
 
2791
5394
  Object.defineProperty(exports, '__esModule', { value: true });
2792
5395