-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCSVToJSON.js
More file actions
35 lines (32 loc) · 1.07 KB
/
CSVToJSON.js
File metadata and controls
35 lines (32 loc) · 1.07 KB
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
var fs = require('fs')
var path = require('path')
function lineToJSON (headings, lines) {
var result = {};
for (var i = 0; i < headings.length; i++) {
// Remove vestigal double quotes from beginning
// and end of line strings
var line = lines[i].replace(/^"|"$/g, '')
result[headings[i]] = line;
}
return result;
}
function convert (filepath) {
var fpath = path.join(__dirname, filepath)
var jsonArr = [];
var file = fs.readFileSync(fpath, 'utf8')
// Replace newlines to enforce the same style for newline
// characters across operating systems.
var lines = file.replace(/(\r\n|\r|\n)/g, '\n')
.split(/\r?\n/g);
var headings = lines.shift();
lines.forEach(function(line) {
if (line) {
// positive lookahead regex to avoid splitting on commas
// that appear within double-quotes
jsonArr.push(lineToJSON(headings.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/),
line.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/)))
}
});
return jsonArr
}
module.exports = convert