Skip to content

Commit ead7b16

Browse files
committed
can read from CSV's with empty lines and empty values
1 parent d8465ab commit ead7b16

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

src/modules/CSVFileModule.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class CSVFileModule {
1111
const parsedData = parse(fs.readFileSync(csvFilePath), {
1212
columns: (header) => header.map((column) => stringNormalizer(column)),
1313
bom: true,
14+
skip_empty_lines: true,
15+
skip_records_with_empty_values: true,
1416
});
1517
this.filePath = csvFilePath;
1618

test/modules/CSVFileModule.test.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const exampleResponse = require('./fixtures/csv-response.json');
44

55
const INVALID_MRN = 'INVALID MRN';
66
const csvFileModule = new CSVFileModule(path.join(__dirname, './fixtures/example-csv.csv'));
7-
const csvFileModuleWithBOMs = new CSVFileModule(path.join(__dirname, './fixtures/example-csv-bom.csv'));
87

98

109
describe('CSVFileModule', () => {
@@ -15,10 +14,30 @@ describe('CSVFileModule', () => {
1514
});
1615

1716
test('Reads data from CSV with a Byte Order Mark', async () => {
17+
const csvFileModuleWithBOMs = new CSVFileModule(
18+
path.join(__dirname, './fixtures/example-csv-empty-values.csv'),
19+
);
20+
1821
const data = await csvFileModuleWithBOMs.get('mrn', 'example-mrn-1');
1922
expect(data).toEqual(exampleResponse);
2023
});
2124

25+
test('Reads data from CSV with Empty Values', async () => {
26+
const csvFileModuleWithEmptyValues = new CSVFileModule(
27+
path.join(__dirname, './fixtures/example-csv-empty-values.csv'),
28+
);
29+
const data = await csvFileModuleWithEmptyValues.get('mrn', 'example-mrn-1');
30+
expect(data).toEqual(exampleResponse);
31+
});
32+
33+
test('Reads data from CSV with Empty Lines', async () => {
34+
const csvFileModuleWithEmptyLines = new CSVFileModule(
35+
path.join(__dirname, './fixtures/example-csv-empty-line.csv'),
36+
);
37+
const data = await csvFileModuleWithEmptyLines.get('mrn', 'example-mrn-1');
38+
expect(data).toEqual(exampleResponse);
39+
});
40+
2241
test('Returns multiple rows', async () => {
2342
const data = await csvFileModule.get('mrn', 'example-mrn-2');
2443
expect(data).toHaveLength(2);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mrn,trialSubjectID,enrollmentStatus,trialResearchID,trialStatus,dateRecorded
2+
example-mrn-1,subjectId-1,status-1,researchId-1,trialStatus-1,2020-01-10
3+
4+
example-mrn-2,subjectId-3,status-3,researchId-3,trialStatus-3,2020-06-10
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mrn,trialSubjectID,enrollmentStatus,trialResearchID,trialStatus,dateRecorded
2+
example-mrn-1,subjectId-1,status-1,researchId-1,trialStatus-1,2020-01-10
3+
, , , \t\t\t,,\t
4+
example-mrn-2,subjectId-3,status-3,researchId-3,trialStatus-3,2020-06-10

0 commit comments

Comments
 (0)