-
-
Save plbowers/7560ae793613ee839151624182133159 to your computer and use it in GitHub Desktop.
forked and (1) added header capability and (2) added escaped characters more consistently for https://stackoverflow.com/questions/1293147/javascript-code-to-parse-csv-data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const csvStringToArray = (strData, header=true) => | |
{ | |
//const objPattern = new RegExp(("(\\,|\\r?\\n|\\r|^)(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|([^\\,\\r\\n]*))"),"gi"); | |
const objPattern = new RegExp(("(\\,|\\r?\\n|\\r|^)(?:\"((?:\\\\.|\"\"|[^\\\\\"])*)\"|([^\\,\"\\r\\n]*))"),"gi"); | |
let arrMatches = null, arrData = [[]]; | |
while (arrMatches = objPattern.exec(strData)){ | |
if (arrMatches[1].length && arrMatches[1] !== ",") arrData.push([]); | |
arrData[arrData.length - 1].push(arrMatches[2] ? | |
arrMatches[2].replace(new RegExp( "[\\\\\"](.)", "g" ), '$1') : | |
arrMatches[3]); | |
} | |
if (header) { | |
hData = arrData.shift(); | |
hashData = arrData.map(row => { | |
let i = 0; | |
return hData.reduce( | |
(acc, key) => { | |
acc[key] = row[i++]; | |
return acc; | |
}, | |
{} | |
); | |
}); | |
return hashData; | |
} else { | |
return arrData; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<input type="checkbox" name="hasHeader" id="hasHeader" value="1" checked="checked" />Has Header<br /> | |
<textarea id="foo" name="foo" cols="80" rows="10" /> | |
h1,h2,h3,h4,h5 | |
a,b,c,d,e | |
a,b,,d, | |
first,"second | |
with | |
multi-line",third,fourth,fifth | |
1,2,3,4,5,extra field that is ignored/lost if using headers | |
g,"h with ""embedded quotes"" asdf",i,j,k | |
l,"m with \"escaped quotes\" asdf",n,o,p | |
q,"r with \,escaped \,commas asdf",s,t,u | |
x,y,z</textarea> | |
<button onclick="doIt()">doIt</button> | |
<h3>Results</h3> | |
<div id="results"> | |
</div> | |
</body> | |
<script> | |
const csvStringToArray = (strData, header=true) => | |
{ | |
//const objPattern = new RegExp(("(\\,|\\r?\\n|\\r|^)(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|([^\\,\\r\\n]*))"),"gi"); | |
const objPattern = new RegExp(("(\\,|\\r?\\n|\\r|^)(?:\"((?:\\\\.|\"\"|[^\\\\\"])*)\"|([^\\,\"\\r\\n]*))"),"gi"); | |
let arrMatches = null, arrData = [[]]; | |
while (arrMatches = objPattern.exec(strData)){ | |
if (arrMatches[1].length && arrMatches[1] !== ",") arrData.push([]); | |
arrData[arrData.length - 1].push(arrMatches[2] ? | |
arrMatches[2].replace(new RegExp( "[\\\\\"](.)", "g" ), '$1') : | |
arrMatches[3]); | |
} | |
if (header) { | |
hData = arrData.shift(); | |
hashData = arrData.map(row => { | |
let i = 0; | |
return hData.reduce( | |
(acc, key) => { | |
acc[key] = row[i++]; | |
return acc; | |
}, | |
{} | |
); | |
}); | |
return hashData; | |
} else { | |
return arrData; | |
} | |
} | |
function doIt() { | |
var h = document.getElementById('hasHeader'); | |
var v = document.getElementById('foo'); | |
var r = document.getElementById('results'); | |
//console.log(h, h.checked); | |
var a = csvStringToArray(v.value, h.checked); | |
r.innerHTML = JSON.stringify(a, function(k, v) { return v === undefined ? '<i>(undefined)</i>' : v; }).replace(/},{/g, '},<br />\n{'); | |
console.log(a); | |
} | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For some reason, this https://gist.github.com/Jezternz/c8e9fafc2c114e079829974e3764db75 becomes unresponsive, I ended up using papa parse https://www.papaparse.com/ , works great on large csv dataset