Created
June 25, 2018 15:17
-
-
Save postpostscript/c88b6aa3759918378ec0738e5279b746 to your computer and use it in GitHub Desktop.
jsonToCsv.js
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 jsonToCsv(headers, rows, rowSep="\r\n") { | |
function escape(value) { | |
return `"${value.toString().replace('"', '""')}"` | |
} | |
function row(values) { | |
return values.map(escape).join(',').replace(/(,"")+$/, '') | |
} | |
return row(headers) + rowSep + rows.map(row => { | |
return headers.map(header => row[header] || '') | |
}).map(row).join(rowSep) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment