Created
March 8, 2020 17:39
-
-
Save jvadillo/0787742bd56320000a48e8368c7be9cd to your computer and use it in GitHub Desktop.
Split CSV
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
function splitCsv(str) { | |
return str.split(',').reduce((accum,curr)=>{ | |
if(accum.isConcatting) { | |
accum.soFar[accum.soFar.length-1] += ','+curr | |
} else { | |
accum.soFar.push(curr) | |
} | |
if(curr.split('"').length % 2 == 0) { | |
accum.isConcatting= !accum.isConcatting | |
} | |
return accum; | |
},{soFar:[],isConcatting:false}).soFar | |
} | |
console.log(splitCsv('asdf,"a,d",fdsa'),' should be ',['asdf','"a,d"','fdsa']) | |
console.log(splitCsv(',asdf,,fds,'),' should be ',['','asdf','','fds','']) | |
console.log(splitCsv('asdf,"a,,,d",fdsa'),' should be ',['asdf','"a,,,d"','fdsa']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment