-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvDataToArray.js
More file actions
30 lines (27 loc) · 1.15 KB
/
Copy pathcsvDataToArray.js
File metadata and controls
30 lines (27 loc) · 1.15 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
const regex = /[\,][\,]{1,}|((?<=([\,])|^)([\"](((\"){2})*|[^\"])*(?=[\"]))|[^\"\,]{1,}(?=[\,]|$))/g
const regeT = /[\t][\t]{1,}|((?<=([\t])|^)([\"](((\"){2})*|[^\"])*(?=[\"]))|[^\"\t]{1,}(?=[\t]|$))/g
const regeP = /[\|][\|]{1,}|((?<=([\|])|^)([\"](((\"){2})*|[^\"])*(?=[\"]))|[^\"\|]{1,}(?=[\|]|$))/g
function regTest(data){
var arr = []
while ((myArray = regex.exec(data)) !== null) {
//console.log(myArray[0])
if (myArray[0][0] == "\"" ) arr.push(myArray[0].slice(1))
else if (myArray[0][0] == "\"" && myArray[0].length == 1) arr.push("")
else if (myArray[0][0] == ",") {
var splitR = myArray[0].split(",")
//console.log("commas - 2 =>: ", splitR.slice(2)) //splitR.slice(2) removes "surrounding blanks"
arr = arr.concat(splitR.slice(2))
//console.log(arr)
}
else arr.push(myArray[0])
}
//console.log(arr)
return arr
}
function csvDataToArray(data) {
var lines = (data.split("\r\n").length > 1 ? data.split("\r\n") : data.split("\n"))
if(lines[lines.length-1] == '') lines.pop();
var arr = lines.map(item=>((item[0] == ',' ? [''] : []).concat(regTest(item))))
return arr
}
module.exports = csvDataToArray