Skip to content

Commit cf1089d

Browse files
committed
Create singl string-normalizer function; used on unalterableColumns
1 parent 274059b commit cf1089d

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/modules/CSVModule.js

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

6+
// The standard string normalizer function
7+
function stringNormalizer(str) {
8+
return str.toLowerCase();
9+
}
10+
11+
// For translating null/nil-like values into empty strings
612
function normalizeEmptyValues(data, unalterableColumns = []) {
7-
const EMPTY_VALUES = ['null', 'nil'].map((v) => v.toLowerCase());
8-
// Flag tracking if data was normalized or not.
9-
let wasNormalized = false;
13+
const EMPTY_VALUES = ['null', 'nil'].map(stringNormalizer);
14+
const normalizedUnalterableColumns = unalterableColumns.map(stringNormalizer);
15+
// Flag tracking if empty values were normalized or not.
16+
let wasEmptyNormalized = false;
1017
const newData = data.map((row, i) => {
1118
const newRow = { ...row };
1219
// Filter out unalterable columns
13-
const columnsToNormalize = Object.keys(row).filter((col) => !unalterableColumns.includes(col));
20+
const columnsToNormalize = Object.keys(row).filter((col) => !normalizedUnalterableColumns.includes(stringNormalizer(col)));
1421
columnsToNormalize.forEach((col) => {
1522
const value = newRow[col];
1623
// If the value for this row-col combo is a value that should be empty, replace it
17-
if (EMPTY_VALUES.includes(value.toLowerCase())) {
24+
if (EMPTY_VALUES.includes(stringNormalizer(value))) {
1825
logger.debug(`NULL/NIL values '${value}' found in row-${i}, col-${col}`);
19-
wasNormalized = true;
26+
wasEmptyNormalized = true;
2027
newRow[col] = '';
2128
}
2229
});
2330
return newRow;
2431
});
2532

26-
if (wasNormalized) {
33+
if (wasEmptyNormalized) {
2734
logger.warn('NULL/NIL values found and replaced with empty-strings');
2835
}
2936
return newData;
@@ -33,7 +40,7 @@ class CSVModule {
3340
constructor(csvFilePath, unalterableColumns) {
3441
// Parse then normalize the data
3542
const parsedData = parse(fs.readFileSync(csvFilePath), {
36-
columns: (header) => header.map((column) => column.toLowerCase()),
43+
columns: (header) => header.map((column) => stringNormalizer(column)),
3744
bom: true,
3845
});
3946
this.data = normalizeEmptyValues(parsedData, unalterableColumns);
@@ -43,7 +50,7 @@ class CSVModule {
4350
logger.debug(`Get csvModule info by key '${key}'`);
4451
// return all rows if key and value aren't provided
4552
if (!key && !value) return this.data;
46-
let result = this.data.filter((d) => d[key.toLowerCase()] === value);
53+
let result = this.data.filter((d) => d[stringNormalizer(key)] === value);
4754
if (result.length === 0) {
4855
logger.warn(`CSV Record with provided key '${key}' and value was not found`);
4956
return result;

0 commit comments

Comments
 (0)