Skip to content

Commit 7a7c387

Browse files
committed
single point of truth for csv parsing
1 parent 605bda5 commit 7a7c387

File tree

4 files changed

+27
-25
lines changed

4 files changed

+27
-25
lines changed

src/helpers/appUtils.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const fs = require('fs');
22
const path = require('path');
3-
const parse = require('csv-parse/lib/sync');
3+
const { csvParse } = require('./csvParsingUtils');
44

55
/**
66
* Parses a provided CSV with MRN column into string array of IDs
@@ -11,10 +11,7 @@ const parse = require('csv-parse/lib/sync');
1111
function parsePatientIds(pathToCSV) {
1212
// Parse CSV for list of patient IDs
1313
const patientIdsCsvPath = path.resolve(pathToCSV);
14-
const patientIds = parse(fs.readFileSync(patientIdsCsvPath, 'utf8'), {
15-
columns: (header) => header.map((column) => column.toLowerCase()),
16-
bom: true,
17-
}).map((row) => {
14+
const patientIds = csvParse(fs.readFileSync(patientIdsCsvPath, 'utf8')).map((row) => {
1815
if (!row.mrn) {
1916
throw new Error(`${pathToCSV} has no "mrn" column`);
2017
}

src/helpers/csvParsingUtils.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const parse = require('csv-parse/lib/sync');
12
const logger = require('./logger');
23

34
// The standard string normalizer function
@@ -38,7 +39,27 @@ function normalizeEmptyValues(data, unalterableColumns = []) {
3839
return newData;
3940
}
4041

42+
// Default options for CSV parsing
43+
const DEFAULT_OPTIONS = {
44+
columns: (header) => header.map((column) => stringNormalizer(column)),
45+
// https://csv.js.org/parse/options/bom/
46+
bom: true,
47+
// https://csv.js.org/parse/options/skip_empty_lines/
48+
skip_empty_lines: true,
49+
// NOTE: This will skip any records with empty values, not just skip the empty values themselves
50+
// NOTE-2: The name of the flag changed from v4 (what we use) to v5 (what is documented)
51+
// https://csv.js.org/parse/options/skip_records_with_empty_values/
52+
skip_lines_with_empty_values: true,
53+
};
54+
55+
// Common utility for parsing CSV files
56+
function csvParse(csvData, options = {}) {
57+
return parse(csvData, { ...DEFAULT_OPTIONS, ...options });
58+
}
59+
60+
4161
module.exports = {
4262
stringNormalizer,
4363
normalizeEmptyValues,
64+
csvParse,
4465
};

src/modules/CSVFileModule.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
11
const fs = require('fs');
22
const moment = require('moment');
3-
const parse = require('csv-parse/lib/sync');
43
const logger = require('../helpers/logger');
54
const { validateCSV } = require('../helpers/csvValidator');
6-
const { stringNormalizer, normalizeEmptyValues } = require('../helpers/csvParsingUtils');
5+
const { csvParse, stringNormalizer, normalizeEmptyValues } = require('../helpers/csvParsingUtils');
76

87
class CSVFileModule {
98
constructor(csvFilePath, unalterableColumns) {
109
// Parse then normalize the data
11-
const parsedData = parse(fs.readFileSync(csvFilePath), {
12-
columns: (header) => header.map((column) => stringNormalizer(column)),
13-
// https://csv.js.org/parse/options/bom/
14-
bom: true,
15-
// https://csv.js.org/parse/options/skip_empty_lines/
16-
skip_empty_lines: true,
17-
// NOTE: This will skip any records with empty values, not just skip the empty values themselves
18-
// NOTE-2: The name of the flag changed from v4 (what we use) to v5 (what is documented)
19-
// https://csv.js.org/parse/options/skip_records_with_empty_values/
20-
skip_lines_with_empty_values: true,
21-
});
22-
10+
const parsedData = csvParse(fs.readFileSync(csvFilePath));
2311
this.filePath = csvFilePath;
2412
this.data = normalizeEmptyValues(parsedData, unalterableColumns);
2513
}

src/modules/CSVURLModule.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
const axios = require('axios');
22
const moment = require('moment');
3-
const parse = require('csv-parse/lib/sync');
43
const logger = require('../helpers/logger');
54
const { validateCSV } = require('../helpers/csvValidator');
6-
const { stringNormalizer, normalizeEmptyValues } = require('../helpers/csvParsingUtils');
5+
const { csvParse, stringNormalizer, normalizeEmptyValues } = require('../helpers/csvParsingUtils');
76

87
class CSVURLModule {
98
constructor(url, unalterableColumns) {
@@ -25,10 +24,7 @@ class CSVURLModule {
2524
});
2625
logger.debug('Web request successful');
2726
// Parse then normalize the data
28-
const parsedData = parse(csvData, {
29-
columns: (header) => header.map((column) => stringNormalizer(column)),
30-
bom: true,
31-
});
27+
const parsedData = csvParse(csvData);
3228
logger.debug('CSV Data parsing successful');
3329
this.data = normalizeEmptyValues(parsedData, this.unalterableColumns);
3430
}

0 commit comments

Comments
 (0)