2017-12-15 00:07:53 +03:00
|
|
|
const Promise = require('bluebird'),
|
2016-06-03 14:35:17 +03:00
|
|
|
csvParser = require('csv-parser'),
|
|
|
|
_ = require('lodash'),
|
2017-12-13 21:15:29 +03:00
|
|
|
fs = require('fs-extra');
|
2016-05-08 10:12:37 +03:00
|
|
|
|
2017-12-15 00:07:53 +03:00
|
|
|
module.exports = 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);
|
|
|
|
})
|
2017-11-01 16:44:54 +03:00
|
|
|
.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;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// If there are multiple columns in csv file
|
|
|
|
// try to match headers using lookup value
|
2016-06-03 14:35:17 +03:00
|
|
|
|
2017-11-01 16:44:54 +03:00
|
|
|
_.map(columnsToExtract, function findMatches(column) {
|
|
|
|
_.each(headers, function checkheader(header) {
|
|
|
|
if (column.lookup.test(header)) {
|
|
|
|
columnMap[column.name] = header;
|
|
|
|
}
|
|
|
|
});
|
2016-06-03 14:35:17 +03:00
|
|
|
});
|
2016-05-08 10:12:37 +03:00
|
|
|
|
2017-11-01 16:44:54 +03:00
|
|
|
results = _.map(rows, function evaluateRow(row) {
|
|
|
|
var result = {};
|
|
|
|
_.each(columnMap, function returnMatches(value, key) {
|
|
|
|
result[key] = row[value];
|
|
|
|
});
|
|
|
|
return result;
|
2016-06-03 14:35:17 +03:00
|
|
|
});
|
2017-11-01 16:44:54 +03:00
|
|
|
}
|
|
|
|
resolve(results);
|
|
|
|
});
|
2016-05-08 10:12:37 +03:00
|
|
|
});
|
2017-12-15 00:07:53 +03:00
|
|
|
};
|