Skip to content

Commit

Permalink
Merge branch 'release/0.8.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
dermatologist committed May 17, 2021
2 parents 8399398 + 2a09a73 commit 9a33090
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "fhirformjs",
"description": "FHIR Questionnaire To Form Converter for rendering",
"version": "0.8.0",
"version": "0.8.1",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
33 changes: 18 additions & 15 deletions src/resp-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,37 @@ export const FhirJsonResp = (
};

// https://stackoverflow.com/questions/15523514/find-by-key-deep-in-a-nested-array
const getObject = function(theObject: Object | Object[], theProperty: string) {
const getObject = function(theObject: Object | Object[], theProperty: string, returnObjects?: boolean) {
var result = null;
if (theObject instanceof Array) {
for (var i = 0; i < theObject.length; i++) {
result = getObject(theObject[i], theProperty);
// end of the road of value is a string
if (typeof theObject[i] === "string") {
break;
}

result = getObject(theObject[i], theProperty, returnObjects);
if (result) {
break;
}
}
} else {
for (var prop in theObject) {
//console.log(prop + ': ' + theObject[prop]);
if (prop === theProperty) {
if (theObject[prop] instanceof Object) {
} else {
return theObject[prop];
}
if (prop === theProperty && (!(theObject[prop] instanceof Object) || returnObjects) ) {
return theObject[prop];
}
if (
theObject[prop] instanceof Object ||
theObject[prop] instanceof Array
) {
result = getObject(theObject[prop], theProperty);
result = getObject(theObject[prop], theProperty, returnObjects);
if (result) {
break;
}
}
}
}

return result;
};

Expand All @@ -72,19 +74,20 @@ const formValueToFhirAnswer = (
fhirElement: R4.IQuestionnaireResponse_Answer,
jsonSchema: FhirForm['schema'],
linkId: string
) =>
) =>
supportedValueTypes.reduce(
(answer: Array<{ [x: string]: any }>, propertyName) => {
if (fhirElement && fhirElement.hasOwnProperty(propertyName)) {
const enumNames = jsonSchema.properties[linkId]?.enumNames;
const linkProperties = getObject(jsonSchema.properties, linkId, true)
const enumNames = linkProperties?.enumNames;
if (enumNames) {
const valueIndex = jsonSchema.properties[linkId].enum.indexOf(
const valueIndex = linkProperties.enum.indexOf(
formDataValue
);
answer.push({
[propertyName]: {
code: formDataValue,
display: enumNames[valueIndex],
display: enumNames[valueIndex] || null,
},
});
} else {
Expand All @@ -94,6 +97,6 @@ const formValueToFhirAnswer = (
}
}
return answer;
},
},
[]
);
);
2 changes: 1 addition & 1 deletion test/resp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const formData =
'{"1": true,"2": {"2.1": "Male", "2.2": "12/12/2020", "2.3": "Canada", "2.4": "Married"},"3": {"3.1": true,"3.2": true}}';

const parsedResponse: R4.IQuestionnaireResponse = JSON.parse(
' {"resourceType":"QuestionnaireResponse","item":[{"linkId":"1","text":"Do you have allergies?","answer":[{"valueBoolean":true}]},{"linkId":"sex","text":"Sex","answer":[{"valueCoding":null}]},{"linkId":"2.2","text":"What is your date of birth?","answer":[{"valueDate":"12/12/2020"}]},{"linkId":"2.3","text":"What is your country of birth?","answer":[{"valueString":"Canada"}]},{"linkId":"2.4","text":"What is your marital status?","answer":[{"valueString":"Married"}]},{"linkId":"3.1","text":"Do you smoke?","answer":[{"valueBoolean":true}]},{"linkId":"3.2","text":"Do you drink alchohol?","answer":[{"valueBoolean":true}]}],"status":"in-progress"}'
' {"resourceType":"QuestionnaireResponse","item":[{"linkId":"1","text":"Do you have allergies?","answer":[{"valueBoolean":true}]},{"linkId":"sex","text":"Sex","answer":[{"valueCoding":{"code":null,"display":null}}]},{"linkId":"2.2","text":"What is your date of birth?","answer":[{"valueDate":"12/12/2020"}]},{"linkId":"2.3","text":"What is your country of birth?","answer":[{"valueString":"Canada"}]},{"linkId":"2.4","text":"What is your marital status?","answer":[{"valueString":"Married"}]},{"linkId":"3.1","text":"Do you smoke?","answer":[{"valueBoolean":true}]},{"linkId":"3.2","text":"Do you drink alchohol?","answer":[{"valueBoolean":true}]}],"status":"in-progress"}'
);

describe('map', () => {
Expand Down

0 comments on commit 9a33090

Please sign in to comment.