Last active
August 29, 2015 14:14
-
-
Save nramirez/0c274c9c101114555eea to your computer and use it in GitHub Desktop.
Complex CSV Parser
This file contains hidden or 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 parseCSV(input, separator, quote) { | |
separator = separator || ','; | |
quote = quote || '"'; | |
var tempWord ='', c ='', temp = [], | |
closingPosition, result =[], specialCharacters = '$\\', | |
doubleQuoteRegex = new RegExp(quote+quote, 'g'); | |
if (specialCharacters.indexOf(quote) > -1) { | |
doubleQuoteRegex = new RegExp('\\' +quote+ '\\' +quote, 'g'); | |
}; | |
//This will replace the double quote just for one and ignore first and last quote | |
String.prototype.clear = function(){ | |
var self = this +''; | |
var tempClean = self.replace(doubleQuoteRegex,quote); | |
if (tempClean[0] === quote) { | |
tempClean = tempClean.slice(1); | |
}; | |
if (tempClean[tempClean.length-1] == quote) { | |
tempClean = tempClean.slice(0,self.length-1); | |
}; | |
return tempClean; | |
}; | |
//Helper to get the next quote input | |
var appendAndPushNext = function(position,noPush){ | |
var tempInput = input.slice(0, closingPosition); | |
temp.push(tempInput.clear()); | |
if (!noPush) { | |
result.push(temp); | |
temp =[]; | |
}; | |
input = input.slice(closingPosition+position); | |
} | |
do{ | |
c = input[0]; | |
if(c == quote){ | |
closingPosition = input.indexOf(quote + separator,1); | |
if (closingPosition === -1) { | |
closingPosition = input.indexOf(quote + '\n',1); | |
if (closingPosition === -1) { | |
closingPosition = input.indexOf(quote,1); | |
appendAndPushNext(1); | |
} else{ | |
appendAndPushNext(2);//ignoring "+separator or "+\n | |
}; | |
} else { | |
appendAndPushNext(2, true);//ignoring "+separator or "+\n | |
} | |
} else if (c == separator || (c === '\n')) { | |
temp.push(tempWord); | |
tempWord = ''; | |
input = input.slice(1); | |
if (c === '\n') { | |
result.push(temp); | |
temp=[]; | |
}; | |
} else { | |
tempWord+=c; | |
input = input.slice(1) | |
} | |
} while(input.length); | |
if (temp.length > 0) { | |
temp.push(tempWord); | |
result.push(temp); | |
}; | |
return result.length === 0 ? [[""]] : result; //If it's equal zero is an empty input | |
} | |
/* | |
console.log(parseCSV('1,2,3')); | |
console.log(parseCSV('one,"two wraps\nonto ""two"" lines",three\n4,,6')); | |
console.log(parseCSV('1,2,3,4,5,6\n7,8\n9,10,11,12')) | |
console.log(parseCSV('1,2,3\n\n4,5,6')) | |
console.log(parseCSV('')) | |
console.log(parseCSV('one,",,,,,..two,,,,,\n,,,,,,",three\n4,,6')); | |
console.log(parseCSV('1,"two ""quote""",3\n4,5,6')); | |
//Expected: [["1","2","3\tthree"],["4","5","6"]] | |
console.log(parseCSV('1 2 "3 three"\n4 5 6', ' ')); | |
//Expected: [["a $string$ using $ as the quote","multi\nline",""],["1.2","3","4"]] | |
console.log(parseCSV('$a $$string$$ using $$ as the quote$.$multi\nline$.\n$1.2$.3.4','.','$')) | |
//Expected: [["a \\string\\ using \\ as the quote","multi\nline",""],["1","2","3.4"]] | |
console.log(parseCSV('\\a \\\\string\\\\ using \\\\ as the quote\\.\\multi\nline\\.\n1.2.\\3.4\\','.','\\')); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment