JSFiddle - React, Tailwind, and code Playground
by chrisJamesC
JavaScript
// See http://stackoverflow.com/questions/769621/dealing-with-commas-in-a-csv-file for reference
const example ='hello, [bob, "cat"], world'
const formatCSV = (input) => {
let open = 0;
let res = "";
for(let i=0; i<input.length; i++) {
switch(input[i]) {
case "[":
open++;
if(open===1) {
res += '"[';
break;
}
open++;
case "]":
open--;
if(open===0) {
res += ']"';
break;
};
case '"':
if(open>0) {
res += '""';
break;
}
default: res+= input[i];
}
}
return res;
}
document.body.innerHTML = formatCSV(example);