Skip to content

Commit

Permalink
resource util bug (#98)
Browse files Browse the repository at this point in the history
* resource util bug fix

* 2.2.8

* resource util bug fix
  • Loading branch information
maxi-smidt authored Nov 8, 2024
1 parent 49392f6 commit eca03a4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.2.8

* Fixed issue with `ResourceUtils.getValuesAtResourcePath` not checking if the root of the path matches the resource type.

## 2.2.7

* Added `BundleUtils.getResourceByFullUrl` to get a resource by its full url from a bundle.
Expand All @@ -8,7 +12,6 @@

* Fixed issue with `ResourceUtils.getValuesAtResourcePath` not being able to get an array with values if path exists for a top level element and is an object.


## 2.2.5

* Fixed issue with `ResourceUtils.getValuesAtResourcePath` not being able to get an array with values if path exists for a top level element and is an array.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@smile-cdr/fhirts",
"version": "2.2.7",
"version": "2.2.8",
"description": "Fhir ts/js library for frontend apps",
"license": "Apache-2.0",
"main": "dist/index.js",
Expand Down
7 changes: 7 additions & 0 deletions src/library/ResourceUtils/ResourceUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ describe("ResourceUtils", () => {

describe("#getValuesAtResourcePath()", () => {

it("should return empty array if the root of the path path does not match the resource type", () => {
// execute
const pathValues = resourceUtils.getValuesAtResourcePath(patientPayload, "Practitioner.name.given");
// validate
expect(pathValues.length).toEqual(0);
});

it("should return array with values if path exists for a top level element", () => {
// execute
const pathValues = resourceUtils.getValuesAtResourcePath(patientPayload, "Patient.gender");
Expand Down
13 changes: 8 additions & 5 deletions src/library/ResourceUtils/ResourceUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ export class ResourceUtils {
* @returns array of elements found at the provided path
*/
getValuesAtResourcePath(resource: any, elementPath: string): any[] {
const pathSections = elementPath.split(".");
const pathSections = elementPath.split('.');
if (!resource || (resource.resourceType !== pathSections[0])) return [];
return this.getValuesAtResourcePathInner(resource, pathSections);
}

private getValuesAtResourcePathInner(resource: any, pathSections: string[]): any[] {
let resourcePathValue;
for (let index = 1; index < pathSections.length; index++) {
const subPaths = pathSections[index];
Expand All @@ -86,14 +91,12 @@ export class ResourceUtils {
resultSet.push(subPathValue);
}
else {
resultSet.push(...this.getValuesAtResourcePath(subPathValue,
pathSections.slice(index).join(".")));
resultSet.push(...this.getValuesAtResourcePathInner(subPathValue, pathSections.slice(index)));
}
}
return resultSet;
} else if (typeof (resourcePathValue) === 'object') {
return this.getValuesAtResourcePath(resourcePathValue,
pathSections.slice(index).join("."));
return this.getValuesAtResourcePathInner(resourcePathValue, pathSections.slice(index));
}
} else {
break;
Expand Down

0 comments on commit eca03a4

Please sign in to comment.