Skip to content

Instantly share code, notes, and snippets.

@oieioi
Last active August 29, 2015 14:07
Show Gist options
  • Save oieioi/c7d70d56cf6983335d33 to your computer and use it in GitHub Desktop.
Save oieioi/c7d70d56cf6983335d33 to your computer and use it in GitHub Desktop.
Convert Backbone.Collection to CSV String
class ConvertCSV extends Backbone.Collection
makeCSVStr: (headers) ->
body = @map (model) ->
headers.map((header)->
'"' + "#{model.get(header)}".replace(/"/g, '""') + '"').join ","
[headers.map((header)-> "\"#{header}\"").join ','].concat(body).join('\n')
con = new ConvertCSV [
{hoge: "\"", fuga: 4, bar: 0}
{hoge: "", fuga: 3, bar: 0}
{hoge: " \n \r\n 1", fuga: 2, bar: 0}
{hoge: 4, fuga: 1, bar: 0}
]
console.log con.makeCSVStr ['hoge', 'fuga']
console.log con.makeCSVStr ['fuga', 'hoge']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment