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