-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSVLoader.js
97 lines (90 loc) · 2.59 KB
/
CSVLoader.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
function CSVLoader () {
this._csvString = "";
this._csvDelimiter = ",";
}
CSVLoader.prototype.getArray = function () {
return this._CSVToArray(this._csvString);
}
CSVLoader.prototype.getObject = function() {
return this._ArrayToObject(this._CSVToArray());
}
/**
* Load stringData into our CSV object
* @param {string} csv
* @param {string} delimiter(OPTIONAL)
*/
CSVLoader.prototype.LoadString = function ( csv, delimiter ) {
this._csvString = csv;
this._csvDelimiter = (delimiter || ",");
}
/**
* Make an ajax call to fileURL and load string data into loader object
* @param {string} filename
* @param {string} delimiter(OPTIONAL)
*/
CSVLoader.prototype.LoadFile = function ( filename, delimiter ) {
this._csvDelimiter = (delimiter || ",");
var ajax = new XMLHttpRequest();
ajax.open("GET", filename, true);
ajax.onreadystatechange = function() {
this._csvString = ajax.responseText;
};
}
/**
* Takes in a 2d Array and returns an object using row 1 of 2d array as
* object keys.
* @param {2D Array} my2dArray
* @return {Object}
*/
CSVLoader.prototype._ArrayToObject = function ( my2dArray ) {
var myObject = {};
for(var y = 0; y < my2dArray[0].length; y++) {
myObject[my2dArray[0][y]] = [];
}
var col = 0;
for(var property in myObject) {
for(var x = 1; x < my2dArray.length; x++) {
myObject[property].push(my2dArray[x][col]);
}
col++;
}
return myObject;
}
/**
* Takes in a .csv formated string and formats it into a 2d array
* @param {string} strData
* @param {string} strDelimiter (OPTIONAL)
* @return {2D Array}
*/
CSVLoader.prototype._CSVToArray = function ( strData, strDelimiter ){
strDelimiter = (strDelimiter || ",");
var objPattern = new RegExp(
(
"(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
"(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
"([^\"\\" + strDelimiter + "\\r\\n]*))"
),
"gi"
);
var arrData = [[]];
var arrMatches = null;
while (arrMatches = objPattern.exec( strData )){
var strMatchedDelimiter = arrMatches[ 1 ];
if (
strMatchedDelimiter.length &&
(strMatchedDelimiter != strDelimiter)
){
arrData.push( [] );
}
if (arrMatches[ 2 ]){
var strMatchedValue = arrMatches[ 2 ].replace(
new RegExp( "\"\"", "g" ),
"\""
);
} else {
var strMatchedValue = arrMatches[ 3 ];
}
arrData[ arrData.length - 1 ].push( strMatchedValue );
}
return( arrData );
}