2016-06-03 14:35:17 +03:00
|
|
|
var Promise = require('bluebird'),
|
|
|
|
csvParser = require('csv-parser'),
|
|
|
|
_ = require('lodash'),
|
2016-05-08 10:12:37 +03:00
|
|
|
fs = require('fs');
|
|
|
|
|
|
|
|
function readCSV(options) {
|
2016-06-03 14:35:17 +03:00
|
|
|
var columnsToExtract = options.columnsToExtract || [],
|
|
|
|
results = [], rows = [];
|
2016-05-08 10:12:37 +03:00
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
2016-06-03 14:35:17 +03:00
|
|
|
var readFile = fs.createReadStream(options.path);
|
|
|
|
|
|
|
|
readFile.on('err', function (err) {
|
|
|
|
reject(err);
|
|
|
|
})
|
|
|
|
.pipe(csvParser())
|
|
|
|
.on('data', function (row) {
|
|
|
|
rows.push(row);
|
|
|
|
})
|
|
|
|
.on('end', function () {
|
|
|
|
// If CSV is single column - return all values including header
|
|
|
|
var headers = _.keys(rows[0]), result = {}, columnMap = {};
|
|
|
|
if (columnsToExtract.length === 1 && headers.length === 1) {
|
|
|
|
results = _.map(rows, function (value) {
|
|
|
|
result = {};
|
|
|
|
result[columnsToExtract[0].name] = value[headers[0]];
|
|
|
|
return result;
|
|
|
|
});
|
2016-05-08 10:12:37 +03:00
|
|
|
|
2016-06-03 14:35:17 +03:00
|
|
|
// Add first row
|
|
|
|
result = {};
|
|
|
|
result[columnsToExtract[0].name] = headers[0];
|
|
|
|
results = [result].concat(results);
|
2016-05-08 10:12:37 +03:00
|
|
|
} else {
|
2016-06-03 14:35:17 +03:00
|
|
|
// If there are multiple columns in csv file
|
|
|
|
// try to match headers using lookup value
|
|
|
|
|
|
|
|
_.map(columnsToExtract, function findMatches(column) {
|
|
|
|
_.each(headers, function checkheader(header) {
|
|
|
|
if (column.lookup.test(header)) {
|
|
|
|
columnMap[column.name] = header;
|
|
|
|
}
|
|
|
|
});
|
2016-05-08 10:12:37 +03:00
|
|
|
});
|
|
|
|
|
2016-06-03 14:35:17 +03:00
|
|
|
results = _.map(rows, function evaluateRow(row) {
|
|
|
|
var result = {};
|
|
|
|
_.each(columnMap, function returnMatches(value, key) {
|
|
|
|
result[key] = row[value];
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
});
|
2016-05-08 10:12:37 +03:00
|
|
|
}
|
2016-06-03 14:35:17 +03:00
|
|
|
resolve(results);
|
2016-05-08 10:12:37 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = readCSV;
|