-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdefault.js
48 lines (44 loc) · 1.6 KB
/
default.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
var CKI = CKI || {};
CKI.Importers = CKI.Importers || {};
CKI.Importers.default = {
textToLines: function(allText) {
// return csv text as an array of objects
var allTextLines = allText.split(/\r\n|\n/);
var headers = allTextLines[0].split(',');
for (var j=0; j<headers.length; j++) {
// string quotes from string
var cleanData = headers[j].replace(/['"]+/g, '').trim()
headers[j] = cleanData;
}
var lines = [];
for (var i=1; i<allTextLines.length; i++) {
// remove commas in any numbers
var line = allTextLines[i];
var data = line.split(',');
if (data.length == headers.length) {
var tobj = {};
for (var j=0; j<headers.length; j++) {
// string quotes from string
var cleanData = data[j].replace(/['"]+/g, '')
tobj[headers[j]] = cleanData;
}
lines.push(tobj);
}else{
console.log("skipping row")
console.log(line);
}
}
return lines;
},
parseCsvRow: function(sourceObj) {
return {
holdingType: (sourceObj["holdingType"].toLowerCase() === "long") ? "2" : "1",
reportingCategory: sourceObj["reportingCategory"],
description: sourceObj['description'],
dateAcquired: sourceObj["dateAcquired"],
dateSold: sourceObj["dateSold"],
salesPrice: sourceObj["salesPrice"],
costBasis: sourceObj["costBasis"],
}
}
};