Mercurial > hg > NetworkVis
comparison d3s_examples/python-neo4jrestclient/static/platin/lib/ucsv/csvToArray.js @ 8:18ef6948d689
new d3s examples
author | Dirk Wintergruen <dwinter@mpiwg-berlin.mpg.de> |
---|---|
date | Thu, 01 Oct 2015 17:17:27 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
7:45dad9e38c82 | 8:18ef6948d689 |
---|---|
1 //(c) http://www.bennadel.com/blog/1504-ask-ben-parsing-csv-strings-with-javascript-exec-regular-expression-command.htm | |
2 | |
3 // This will parse a delimited string into an array of | |
4 // arrays. The default delimiter is the comma, but this | |
5 // can be overriden in the second argument. | |
6 function CSVToArray( strData, strDelimiter ){ | |
7 // Check to see if the delimiter is defined. If not, | |
8 // then default to comma. | |
9 strDelimiter = (strDelimiter || ","); | |
10 | |
11 // Create a regular expression to parse the CSV values. | |
12 var objPattern = new RegExp( | |
13 ( | |
14 // Delimiters. | |
15 "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" + | |
16 | |
17 // Quoted fields. | |
18 "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" + | |
19 | |
20 // Standard fields. | |
21 "([^\"\\" + strDelimiter + "\\r\\n]*))" | |
22 ), | |
23 "gi" | |
24 ); | |
25 | |
26 | |
27 // Create an array to hold our data. Give the array | |
28 // a default empty first row. | |
29 var arrData = [[]]; | |
30 | |
31 // Create an array to hold our individual pattern | |
32 // matching groups. | |
33 var arrMatches = null; | |
34 | |
35 | |
36 // Keep looping over the regular expression matches | |
37 // until we can no longer find a match. | |
38 while (arrMatches = objPattern.exec( strData )){ | |
39 | |
40 // Get the delimiter that was found. | |
41 var strMatchedDelimiter = arrMatches[ 1 ]; | |
42 | |
43 // Check to see if the given delimiter has a length | |
44 // (is not the start of string) and if it matches | |
45 // field delimiter. If id does not, then we know | |
46 // that this delimiter is a row delimiter. | |
47 if ( | |
48 strMatchedDelimiter.length && | |
49 (strMatchedDelimiter != strDelimiter) | |
50 ){ | |
51 | |
52 // Since we have reached a new row of data, | |
53 // add an empty row to our data array. | |
54 arrData.push( [] ); | |
55 | |
56 } | |
57 | |
58 | |
59 // Now that we have our delimiter out of the way, | |
60 // let's check to see which kind of value we | |
61 // captured (quoted or unquoted). | |
62 if (arrMatches[ 2 ]){ | |
63 | |
64 // We found a quoted value. When we capture | |
65 // this value, unescape any double quotes. | |
66 var strMatchedValue = arrMatches[ 2 ].replace( | |
67 new RegExp( "\"\"", "g" ), | |
68 "\"" | |
69 ); | |
70 | |
71 } else { | |
72 | |
73 // We found a non-quoted value. | |
74 var strMatchedValue = arrMatches[ 3 ]; | |
75 | |
76 } | |
77 | |
78 | |
79 // Now that we have our value string, let's add | |
80 // it to the data array. | |
81 arrData[ arrData.length - 1 ].push( strMatchedValue ); | |
82 } | |
83 | |
84 // Return the parsed data. | |
85 return( arrData ); | |
86 } |