Skip to content

Commit

Permalink
added function pro properly add columns where needed preventing need …
Browse files Browse the repository at this point in the history
…for the dont-freak-out boolean in csv parse
  • Loading branch information
rhettl committed Sep 12, 2016
1 parent de971ce commit cebe312
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ var parseRow = function (row, del) {
: row.split(del)
;
};
var joinRow = function (row, del) {
return del === ','
? csvrow.stringify(row)
: row.join(del)
;
};

var readContents = function (file, cb) {
error.ifNot(file, 'Must include file path');
Expand Down Expand Up @@ -169,6 +175,28 @@ var removeHeaderRows = function (file, cb) {
.nodeify(cb);
};

var normalizeColumnCounts = function (file, cb) {
return readContents(file)
.then(function (data) {
return determineDelimiter(data).then(function (del) {
// console.log(data);
let lineChar = determineLineChar(data);
let rows = data
.split(lineChar)
.map(r => parseRow(r, del))
;
let max = rows.reduce((max, r) => r.length > max ? r.length : max, 0);

rows = rows.map(r => r.length < max ? r.concat(new Array(max - r.length).join('.').split('.')) : r);

return rows
.map(r => joinRow(r, del))
.join(lineChar)
;
});
})
.nodeify(cb);
};

var xlsxToCSV = function (file, cb) {
error.ifNot(file, 'Must include file path');
Expand Down Expand Up @@ -262,11 +290,9 @@ var _svToCSV = function (file, del, cb) { // eslint-disable-line no-underscore-d
.then(function (data) {
var def = q.defer();

// console.log(data)

csv.parse(data, {
delimiter: del,
relax_column_count: true
// relax_column_count: true
}, function (err, rows) {
if (err) {
def.reject(err);
Expand Down Expand Up @@ -304,6 +330,7 @@ var checkTypeAndConvert = function (filePath, cb) {
case fileType.XLSX:
return xlsxToCSV(filePath)
.then(removeHeaderRows)
.then(normalizeColumnCounts)
.then(csvToCSV);

case fileType.CSV:
Expand Down Expand Up @@ -332,6 +359,7 @@ var httpHandler = function (req, res) {
res.send(csvOut);
})
.fail(function (err) {
console.error(err, err.stack);
res.send(err);
})
.done()
Expand All @@ -345,6 +373,7 @@ module.exports = {

getFileType,
removeHeaderRows,
normalizeColumnCounts,

xlsxToCSV,
xmlToCSV,
Expand Down

0 comments on commit cebe312

Please sign in to comment.