Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

86 lines (78 sloc) 2.934 kb
Convert an array or an object into a CSV line.
Stringifier = (csv) ->
@csv = csv
Write a line to the written stream. Line may be an object, an array or a string
The `preserve` argument is for line which are not considered as CSV data.
Stringifier.prototype.write = (line, preserve) ->
return if typeof line is 'undefined' or line is null
# Emit the record
unless preserve
try @csv.emit 'record', line, @csv.state.count
catch e then return @csv.error e
# Convert the record into a string
line = @csv.stringifier.stringify line
# Emit the csv
@csv.emit 'data', line
@csv.state.countWriten++ unless preserve
Stringifier.prototype.stringify = (line) ->
columns = or @csv.options.from.columns
delimiter = or @csv.options.from.delimiter
quote = or @csv.options.from.quote
escape = or @csv.options.from.escape
if typeof line is 'object'
unless Array.isArray line
_line = []
if columns
for i in [0...columns.length]
column = columns[i]
_line[i] = if (typeof line[column] is 'undefined' or line[column] is null) then '' else line[column]
for column of line
_line.push line[column]
line = _line
_line = null
else if
# We are getting an array but the user want specified output columns. In
# this case, we respect the columns indexes
if Array.isArray line
newLine = if @csv.state.countWriten then or "\n" else ''
for i in [0...line.length]
field = line[i]
if typeof field is 'string'
# fine 99% of the cases, keep going
else if typeof field is 'number'
# Cast number to string
field = '' + field
else if typeof field is 'boolean'
# Cast boolean to string
field = if field then '1' else ''
else if field instanceof Date
# Cast date to timestamp string
field = '' + field.getTime()
if field
containsdelimiter = field.indexOf(delimiter) >= 0
containsQuote = field.indexOf(quote) >= 0
containsLinebreak = field.indexOf("\r") >= 0 or field.indexOf("\n") >= 0
if containsQuote
regexp = new RegExp(quote,'g')
field = field.replace(regexp, escape + quote)
if containsQuote or containsdelimiter or containsLinebreak or
field = quote + field + quote
newLine += field
if i isnt line.length - 1
newLine += delimiter
line = newLine
else if typeof line is 'number'
line = ''+line
module.exports = (csv) -> new Stringifier csv
module.exports.Stringifier = Stringifier
Jump to Line
Something went wrong with that request. Please try again.