-
Notifications
You must be signed in to change notification settings - Fork 0
/
readCsv.js
executable file
·77 lines (66 loc) · 2.56 KB
/
readCsv.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var fs = require('fs');
var _ = require('lodash');
var csv = require('fast-csv');
var csvsync = require('csvsync');
function readCsv(csvFile, options, callback) {
options = options || { };
if(!callback) {
//treat as synchronous
var csopts = null;
if(options) {
if(Object.keys(options).join() == 'leaveCaseAlone') {
//ignore, this is already the case
} else {
throw new Error('Only leaveCaseAlone is available for synchronous csv read');
}
}
var returnRows = csvsync.parse(fs.readFileSync(csvFile).toString(), csopts);
return returnRows;
} else {
var csvTable = [];
var columns = null;
var fileStream = fs.createReadStream(csvFile);
var csvStream = csv(options.csvOptions)
.on("data", function(data){
csvTable.push(data);
})
.on("end", function(){
processCsv();
csvTable = [];
});
fileStream.pipe(csvStream);
var returnRows = [];
function processCsv() {
csvTable.forEach(function(row, index) {
if(index == 0) {
columns = row;
} else {
var r = { };
columns.forEach(function(col, ci) {
if(options.renameColumns) {
if(options.renameColumns[col]) {
col = options.renameColumns[col];
r[col] = row[ci];
} else {
return; //omit
}
} else {
if(options.simplifyNames) {
col = col.charAt(0).toLowerCase() + col.substring(1).split(' ').join('').split('-').join('');
} else if(!options.leaveCaseAlone) {
col = col.charAt(0).toLowerCase() + col.substring(1);
}
r[col] = row[ci];
}
if(options.columnsToArray && _.includes(options.columnsToArray, col)) {
r[col] = (r[col].trim().length == 0 ? null : r[col].split(',').map(function(v) { return v.trim(); }));
}
});
returnRows.push(r);
}
});
callback(returnRows);
}
}
}
module.exports = readCsv;