Last active
December 14, 2015 06:29
-
-
Save dglittle/5042589 to your computer and use it in GitHub Desktop.
csv parser and outputter
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
_.csv = function (data, headers) { | |
if (headers === undefined) headers = true | |
if (typeof(data) == 'object') { | |
var s = [] | |
function escapeCsv(s) { | |
if (s.match(/[,"\n]/)) | |
return '"' + s.replace(/"/g, '""') + '"' | |
return s | |
} | |
function escapeLine(a) { | |
return _.map(_.values(a), escapeCsv).join(',') | |
} | |
if (headers) | |
s.push(escapeLine(_.keys(data[0]))) | |
_.each(data, function (row) { | |
s.push(escapeLine(row)) | |
}) | |
return s.join('\n') | |
} else if (typeof(data) == 'string') { | |
function unescapeCsv(s) { | |
if (s[0] == '"') | |
return s.slice(1, s.length - 1).replace(/""/g, '"') | |
return s | |
} | |
var bins = [] | |
var bin = [] | |
var re = /(^|,|\n)("(""|[^"])*"|[^,\r\n]*)/g | |
var r | |
while (r = re.exec(data)) { | |
if (r[1] == '\n') { | |
bins.push(bin) | |
bin = [] | |
} | |
bin.push(unescapeCsv(r[2])) | |
} | |
if (bin.length > 0) | |
bins.push(bin) | |
var last = bins[bins.length - 1] | |
if (last.length == 1 && last[0] == "") bins.pop() | |
if (headers) { | |
headers = bins[0] | |
bins = _.map(bins.slice(1), function (e) { return _.object(headers, e) }) | |
} | |
return bins | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment