Skip to content

Commit

Permalink
Version bump + new getFieldTitleMap function
Browse files Browse the repository at this point in the history
And moving getRowMap to phac_dexa export.js since it is only used there.
  • Loading branch information
ddooley committed Oct 24, 2024
1 parent 162c799 commit e9e46f7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 49 deletions.
64 changes: 16 additions & 48 deletions lib/DataHarmonizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1608,11 +1608,24 @@ class DataHarmonizer {
* @return {Dictionary<Integer>} Dictionary of all fields.
*/
getFieldNameMap(fields) {
const titleMap = {};
const map = {};
for (const [fieldIndex, field] of fields.entries()) {
titleMap[field.name] = fieldIndex;
map[field.name] = fieldIndex;
}
return titleMap;
return map;
}

/**
* Get a dictionary of source field TITLES pointing to column index
* @param {Object} fields A flat version of data.js.
* @return {Dictionary<Integer>} Dictionary of all fields.
*/
getFieldTitleMap(fields) {
const map = {};
for (const [fieldIndex, field] of fields.entries()) {
map[field.title] = fieldIndex;
}
return map;
}

/**
Expand Down Expand Up @@ -1912,51 +1925,6 @@ class DataHarmonizer {
return result;
}

/**
* Get a dictionary of empty arrays for each ExportHeader field
* FUTURE: enable it to work with hierarchic vocabulary lists
*
* @param {Array<String>} sourceRow array of values to be exported.
* @param {Array<String>} sourceFields list of source fields to examine for mappings.
* @param {Array<Array>} RuleDB list of export fields modified by rules.
* @param {Array<Array>} fields list of export fields modified by rules.
* @param {Array<Integer>} titleMap map of field names to column index.
* @param {String} prefix of export format to examine.
* @return {Array<Object>} fields Dictionary of all fields.
*/

getRowMap(sourceRow, sourceFields, RuleDB, fields, titleMap, prefix) {
for (const title of sourceFields) {
const sourceIndex = titleMap[title];
let value = sourceRow[sourceIndex]; // All text values.
// Sets source field to data value so that rules can reference it easily.
RuleDB[title] = value;
// Check to see if value is in vocabulary of given select field, and if it
// has a mapping for export to a GRDI target field above, then set target
// to value.
if (value && value.length > 0) {
const vocab_list = fields[sourceIndex]['schema:ItemList'];
if (value in vocab_list) {
const term = vocab_list[value];
// Looking for term.exportField['GRDI'] for example:
if ('exportField' in term && prefix in term.exportField) {
for (let mapping of term.exportField[prefix]) {
// Here mapping involves a value substitution
if ('value' in mapping) {
value = mapping.value;
// Changed on a copy of data, not handsongrid table
sourceRow[sourceIndex] = value;
}
if ('field' in mapping && mapping['field'] in RuleDB) {
RuleDB[mapping['field']] = value;
}
}
}
}
}
}
}

/**
* Return first and last items of a delimited string
* @param {String} value A string of values separated by delimiter.
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": "data-harmonizer",
"version": "1.7.1",
"version": "1.7.2",
"description": "A standardized spreadsheet editor and validator that can be run offline and locally",
"repository": "[email protected]:cidgoh/DataHarmonizer.git",
"license": "MIT",
Expand Down

0 comments on commit e9e46f7

Please sign in to comment.