Skip to content

Commit 5095086

Browse files
Update formDataToJson.js to form codejson object recursively
Signed-off-by: Natalia Luzuriaga <natalia.luzuriaga@cms.hhs.gov>
1 parent 4c2f93e commit 5095086

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

js/formDataToJson.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ async function retrieveFile(filePath) {
1515
}
1616

1717
function isMultiSelect(obj) {
18+
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) return false;
19+
1820
for (const key in obj) {
1921
if (typeof obj[key] !== 'boolean') {
2022
return false;
@@ -32,25 +34,27 @@ function getSelectedOptions(options) {
3234
selectedOptions.push(key);
3335
}
3436
}
37+
3538
return selectedOptions;
3639
}
3740

3841
// Populates fields with form data
39-
function populateObject(data, schema) {
42+
function populateObject(data, fields) {
4043
let reorderedObject = {}
4144

42-
// Array of fields following proper order of fields in schema
43-
const fields = Object.keys(schema.properties.items);
44-
45-
for (const key of fields) {
46-
let value = data[key];
45+
for (const field of fields) {
46+
let value = data[field];
4747

4848
// Adjusts value accordingly if multi-select field
4949
if ((typeof value === "object" && isMultiSelect(value))) {
5050
value = getSelectedOptions(value);
5151
}
52+
// Recurses if multi-field object
53+
else if (typeof value === 'object' && value !== null && Object.keys(value).length > 1) {
54+
value = populateObject(value, Object.keys(value));
55+
}
5256

53-
reorderedObject[key] = value;
57+
reorderedObject[field] = value;
5458
}
5559

5660
return reorderedObject;
@@ -68,7 +72,7 @@ async function populateCodeJson(data) {
6872

6973
// Populates fields with form data
7074
if (schema) {
71-
codeJson = populateObject(data, schema);
75+
codeJson = populateObject(data, Object.keys(schema.properties));
7276
} else {
7377
console.error("Failed to retrieve JSON data.");
7478
}

0 commit comments

Comments
 (0)