Skip to content

Commit 03a1317

Browse files
Merge branch 'feature/LF-2667/implement-getValue' into 'master'
Implement getValue See merge request lfor/fhirpath.js!27
2 parents edc186d + 52e973b commit 03a1317

File tree

7 files changed

+123
-10
lines changed

7 files changed

+123
-10
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
This log documents significant changes for each release. This project follows
44
[Semantic Versioning](http://semver.org/).
55

6+
## [3.17.0] - 2025-02-03
7+
### Added
8+
- getValue() function.
9+
610
## [3.16.4] - 2025-01-24
711
### Fixed
812
- Bug where single-quoted variable names were not supported.

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fhirpath",
3-
"version": "3.16.4",
3+
"version": "3.17.0",
44
"description": "A FHIRPath engine",
55
"main": "src/fhirpath.js",
66
"types": "src/fhirpath.d.ts",

src/fhirpath.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ engine.invocationTable = {
115115
toTime: {fn: misc.toTime},
116116
toBoolean: {fn: misc.toBoolean},
117117
toQuantity: {fn: misc.toQuantity, arity: {0: [], 1: ["String"]}},
118-
// TODO: The hasValue function should be taken into account in a separate request
119118
hasValue: {fn: misc.hasValueFn},
119+
getValue: {fn: misc.getValueFn},
120120
convertsToBoolean: {fn: misc.createConvertsToFn(misc.toBoolean, 'boolean')},
121121
convertsToInteger: {fn: misc.createConvertsToFn(misc.toInteger, 'number')},
122122
convertsToDecimal: {fn: misc.createConvertsToFn(misc.toDecimal, 'number')},

src/misc.js

+20
Original file line numberDiff line numberDiff line change
@@ -340,4 +340,24 @@ engine.hasValueFn = function(coll) {
340340
&& TypeInfo.isPrimitive(TypeInfo.fromValue(coll[0]));
341341
};
342342

343+
/**
344+
* Returns the underlying system value for the FHIR primitive if the input
345+
* collection contains a single value which is a FHIR primitive, and it has a
346+
* primitive value. Otherwise, the return value is empty (i.e. []).
347+
*
348+
* See: https://hl7.org/fhir/fhirpath.html#functions
349+
* @param {Array<*>} coll - input collection
350+
* @returns {*|[]}
351+
*/
352+
engine.getValueFn = function(coll) {
353+
if (coll.length === 1) {
354+
const node = coll[0];
355+
const v = util.valData(node);
356+
if (v != null && TypeInfo.isPrimitive(TypeInfo.fromValue(node))) {
357+
return v;
358+
}
359+
}
360+
return [];
361+
};
362+
343363
module.exports = engine;

test/cases/getValue.yaml

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
tests:
2+
- 'group: no model specified':
3+
- desc: 'collection only contains one string'
4+
inputfile: patient-example.json
5+
expression: Patient.name.given[0].getValue()
6+
result: ['Peter']
7+
8+
- desc: 'collection contains array of strings'
9+
inputfile: patient-example.json
10+
expression: Patient.name.given.getValue()
11+
result: []
12+
13+
- desc: 'collection contains object'
14+
inputfile: patient-example.json
15+
expression: Patient.name.getValue()
16+
result: []
17+
18+
- desc: 'collection only contains an element without a value but with an extension'
19+
inputfile: patient-example-3.json
20+
expression: Patient.birthDate.getValue()
21+
result: []
22+
23+
- desc: 'collection only contains an element of a non-FHIR primitive type'
24+
expression: >-
25+
'5'.getValue()
26+
result: ['5']
27+
28+
- 'group: model specified':
29+
- desc: 'collection only contains one non-primitive'
30+
inputfile: patient-example.json
31+
expression: Patient.identifier.getValue()
32+
model: r4
33+
result: []
34+
35+
- desc: 'collection only contains one string'
36+
inputfile: patient-example.json
37+
expression: Patient.name.given[0].getValue()
38+
model: 'r5'
39+
result: ['Peter']
40+
- desc: 'collection contains array of strings'
41+
inputfile: patient-example.json
42+
expression: Patient.name.given.getValue()
43+
model: 'r5'
44+
result: []
45+
46+
- desc: 'collection only contains one string'
47+
inputfile: patient-example.json
48+
expression: Patient.name.given[0].getValue()
49+
model: 'r4'
50+
result: ['Peter']
51+
- desc: 'collection contains array of strings'
52+
inputfile: patient-example.json
53+
expression: Patient.name.given.getValue()
54+
model: 'r4'
55+
result: []
56+
57+
- desc: 'collection only contains one string'
58+
inputfile: patient-example.json
59+
expression: Patient.name.given[0].getValue()
60+
model: 'stu3'
61+
result: ['Peter']
62+
- desc: 'collection contains array of strings'
63+
inputfile: patient-example.json
64+
expression: Patient.name.given.getValue()
65+
model: 'stu3'
66+
result: []
67+
68+
- desc: 'collection only contains one string'
69+
inputfile: patient-example.json
70+
expression: Patient.name.given[0].getValue()
71+
model: 'dstu2'
72+
result: ['Peter']
73+
- desc: 'collection contains array of strings'
74+
inputfile: patient-example.json
75+
expression: Patient.name.given.getValue()
76+
model: 'dstu2'
77+
result: []
78+
79+
- desc: 'collection only contains an element without a value but with an extension'
80+
inputfile: patient-example-3.json
81+
expression: Patient.birthDate.getValue()
82+
model: 'r4'
83+
result: []
84+
85+
- desc: 'collection only contains an element of a non-FHIR primitive type'
86+
expression: >-
87+
'5'.getValue()
88+
model: 'r4'
89+
result: ['5']

test/cases/hasValue.yaml

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@ tests:
55
expression: Patient.name.given[0].hasValue()
66
result: [true]
77

8-
- desc: 'collection only contains one non-primitive'
9-
inputfile: patient-example.json
10-
expression: Patient.identifier.hasValue()
11-
model: r4
12-
result: [false]
13-
148
- desc: 'collection contains array of strings'
159
inputfile: patient-example.json
1610
expression: Patient.name.given.hasValue()
@@ -32,6 +26,12 @@ tests:
3226
result: [true]
3327

3428
- 'group: model specified':
29+
- desc: 'collection only contains one non-primitive'
30+
inputfile: patient-example.json
31+
expression: Patient.identifier.hasValue()
32+
model: r4
33+
result: [false]
34+
3535
- desc: 'collection only contains one string'
3636
inputfile: patient-example.json
3737
expression: Patient.name.given[0].hasValue()

0 commit comments

Comments
 (0)