Skip to content

Commit 4de2f9e

Browse files
committed
Updated 'attributes' to be 'columns'; added test ensuring that unalterableColumns works as expected
1 parent 164e9c0 commit 4de2f9e

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

src/extractors/BaseCSVExtractor.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ const { validateCSV } = require('../helpers/csvValidator');
55
const logger = require('../helpers/logger');
66

77
class BaseCSVExtractor extends Extractor {
8-
constructor({ filePath, csvSchema, unalterableAttributes }) {
8+
constructor({ filePath, csvSchema, unalterableColumns }) {
99
super();
10-
this.unalterableAttributes = unalterableAttributes || [];
10+
this.unalterableColumns = unalterableColumns || [];
1111
this.csvSchema = csvSchema;
1212
this.filePath = path.resolve(filePath);
13-
this.csvModule = new CSVModule(this.filePath, this.unalterableAttributes);
13+
this.csvModule = new CSVModule(this.filePath, this.unalterableColumns);
1414
}
1515

1616
validate() {

src/extractors/CSVPatientExtractor.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ function joinAndReformatData(patientData) {
5555

5656
class CSVPatientExtractor extends BaseCSVExtractor {
5757
constructor({ filePath, mask = [] }) {
58-
// Define CSV attributes whose values should never be altered
59-
const unalterableAttributes = ['familyName', 'givenName'];
60-
super({ filePath, csvSchema: CSVPatientSchema, unalterableAttributes });
58+
// Define CSV Columns whose values should never be altered
59+
const unalterableColumns = ['familyName', 'givenName'];
60+
super({ filePath, csvSchema: CSVPatientSchema, unalterableColumns });
6161
this.mask = mask;
6262
}
6363

src/modules/CSVModule.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,32 @@ const moment = require('moment');
33
const parse = require('csv-parse/lib/sync');
44
const logger = require('../helpers/logger');
55

6-
function normalizeEmptyValues(data, unalterableAttributes = []) {
6+
function normalizeEmptyValues(data, unalterableColumns = []) {
77
const EMPTY_VALUES = ['null', 'nil'];
88

99
return data.map((row) => {
1010
const newRow = { ...row };
11-
// Filter out unalterable attributes
12-
const attributesToNormalize = Object.keys(row).filter((attr) => !unalterableAttributes.includes(attr));
13-
attributesToNormalize.forEach((attr) => {
14-
const value = newRow[attr];
15-
// If the value for this row-attr combo is a value that should be empty, replace it
11+
// Filter out unalterable columns
12+
const columnsToNormalize = Object.keys(row).filter((col) => !unalterableColumns.includes(col));
13+
columnsToNormalize.forEach((col) => {
14+
const value = newRow[col];
15+
// If the value for this row-col combo is a value that should be empty, replace it
1616
if (EMPTY_VALUES.includes(value.toLowerCase())) {
17-
newRow[attr] = '';
17+
newRow[col] = '';
1818
}
1919
});
2020
return newRow;
2121
});
2222
}
2323

2424
class CSVModule {
25-
constructor(csvFilePath, unalterableAttributes) {
25+
constructor(csvFilePath, unalterableColumns) {
2626
// Parse then normalize the data
2727
const parsedData = parse(fs.readFileSync(csvFilePath), {
2828
columns: (header) => header.map((column) => column.toLowerCase()),
2929
bom: true,
3030
});
31-
this.data = normalizeEmptyValues(parsedData, unalterableAttributes);
31+
this.data = normalizeEmptyValues(parsedData, unalterableColumns);
3232
}
3333

3434
async get(key, value, fromDate, toDate) {

test/modules/CSVModule.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ describe('CSVModule', () => {
7272
});
7373
});
7474

75+
it('Should not modify unalterableColumns, regardless of their value', () => {
76+
const data = [{ key: 'null' }, { key: 'NULL' }, { key: 'nuLL' }, { key: 'nil' }, { key: 'NIL' }, { key: 'NIl' }];
77+
const normalizedData = normalizeEmptyValues(data, ['key']);
78+
normalizedData.forEach((d) => {
79+
expect(d.key).not.toBe('');
80+
});
81+
});
82+
7583
it('Should leave all other values uneffected, regardless of case', () => {
7684
const data = [{ key: 'anything' }, { key: 'any' }, { key: 'thing' }];
7785
const normalizedData = normalizeEmptyValues(data);

0 commit comments

Comments
 (0)