Skip to content

Commit

Permalink
Ignore rows with just spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Léo Frachet committed Apr 4, 2018
1 parent 5366c87 commit f15caf2
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions helpers/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,35 +87,43 @@ function processRows(gtfs, tableName, indexKeys, rows, shouldThrow) {
checkThatKeysIncludeIndexKeys(sortedKeys, indexKeys, tableName);

eachWithLog(`Importation:${tableName}`, rows, (row, index) => {
if (index !== 0 && row && row.length > 0) {
const arrayOfValues = fromCsvStringToArray(row, tableName, gtfs).map(key => key.trim());

if (arrayOfValues !== null) {
const item = sortedKeys.reduce((accumulator, key, i) => {
accumulator[key] = arrayOfValues[i];
return accumulator;
}, {});

if (sortedKeys.length !== arrayOfValues.length) {
if (shouldThrow === true) {
throw new Error(`Invalid raw in table ${tableName}: ${JSON.stringify(item)}`);
}

process.notices.addWarning(__filename, `Row not valid in table: ${JSON.stringify(item)}`);
return;
if (index === 0 || !row || !row.trim) {
return;
}

row = row.trim();

if (row.length === 0) {
return;
}

const arrayOfValues = fromCsvStringToArray(row, tableName, gtfs).map(key => key.trim());

if (arrayOfValues !== null) {
const item = sortedKeys.reduce((accumulator, key, i) => {
accumulator[key] = arrayOfValues[i];
return accumulator;
}, {});

if (sortedKeys.length !== arrayOfValues.length) {
if (shouldThrow === true) {
throw new Error(`Invalid raw in table ${tableName}: ${JSON.stringify(item)}`);
}

if (indexKeys.indexKey) {
table.set(item[indexKeys.indexKey], item);
} else if (indexKeys.firstIndexKey && indexKeys.secondIndexKey) {
if (table.has(item[indexKeys.firstIndexKey]) === false) {
table.set(item[indexKeys.firstIndexKey], new Map());
}
process.notices.addWarning(__filename, `Row not valid in table: ${JSON.stringify(item)}`);
return;
}

table.get(item[indexKeys.firstIndexKey]).set(item[indexKeys.secondIndexKey], item);
} else if (indexKeys.singleton) {
table = item;
if (indexKeys.indexKey) {
table.set(item[indexKeys.indexKey], item);
} else if (indexKeys.firstIndexKey && indexKeys.secondIndexKey) {
if (table.has(item[indexKeys.firstIndexKey]) === false) {
table.set(item[indexKeys.firstIndexKey], new Map());
}

table.get(item[indexKeys.firstIndexKey]).set(item[indexKeys.secondIndexKey], item);
} else if (indexKeys.singleton) {
table = item;
}
}

Expand Down

0 comments on commit f15caf2

Please sign in to comment.