-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsv_parse.js
More file actions
66 lines (51 loc) · 1.25 KB
/
csv_parse.js
File metadata and controls
66 lines (51 loc) · 1.25 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
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
/**
* parseCSV( csv_string [, verbose ] )
*
* parseCSV parses a string from a csv-file, and returns an array
*/
function parseCSV( csv_string ) {
var verbose = false,
sep,
split,
i,
j,
data = [];
if (arguments.length > 1)
verbose = arguments[1];
function findSep( line ) {
// start with ; for silly excel
var i = line.indexOf(";");
if ( i != -1 ) {
if ( verbose ) console.log("separator is ; which isn't really the standard is it");
i = line.indexOf(",");
if ( i != -1 ) {
if ( verbose ) console.log("someone has put , in the integers or something!!");
}
return ";";
}
i = line.indexOf(",");
if ( i != -1 )
return ",";
}
sep = findSep( csv_string.substr( 0, csv_string.indexOf("\n", 0)) );
if ( sep != "," ) {
split = function ( line, sep ) {
var temp = line.split( sep );
// some code that fixes wild decimal separators ( , to . )
return temp.map( function( val ) { return val.replace(",","."); });
};
} else {
split = function( line, sep ) {
return line.split( sep );
};
}
i = 0;
while (i < csv_string.length) {
j = csv_string.indexOf("\n", i);
if (j == -1)
j = csv_string.length;
data.push( split( csv_string.substr( i, j-i), sep ) );
i = j +1;
}
return data;
}