Last active
August 29, 2015 13:57
-
-
Save JosePedroDias/9893423 to your computer and use it in GitHub Desktop.
Proper TSV/CSV parsing. toObjects uses first line as header and converts lines into objects. sep defaults to '\t', use ',' instead for 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
var parseXSV = function(str, toObjects, sep) { | |
if (!sep) { sep = '\t'; } | |
var chars = str.split(''); | |
var lst = []; | |
var elem = []; | |
var field = []; | |
var inComplexField = false; | |
var c, p; // current, prev | |
for (var i = 0, f = chars.length; i < f; ++i) { | |
c = chars[i]; | |
if (c === '\r') { // this if is optional :) | |
continue; | |
} | |
else if (!inComplexField && c === '"' && (p === undefined || p === sep || p === '\n')) { | |
inComplexField = true; | |
} | |
else if (!inComplexField && (c === sep || c === '\n')) { | |
elem.push( field.join('').replace(/""/g, '"') ); | |
field = []; | |
if (c === '\n') { | |
lst.push(elem); | |
elem = []; | |
} | |
} | |
else if (inComplexField && p === '"' && (c === sep || c === '\n')) { | |
field.pop(); | |
elem.push( field.join('').replace(/""/g, '"') ); | |
field = []; | |
inComplexField = false; | |
if (c === '\n') { | |
lst.push(elem); | |
elem = []; | |
} | |
} | |
else { | |
field.push(c); | |
} | |
p = c; | |
} | |
if (!toObjects) { | |
return cb(null, lst); | |
} | |
var names = lst.shift(); | |
return lst.map(function(l) { | |
var o = {}; | |
names.forEach(function(name, idx) { | |
o[name] = l[idx]; | |
}); | |
return o; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment