Skip to content

Commit

Permalink
Headers are not emitted on sourceless writes
Browse files Browse the repository at this point in the history
If csv is used without a source and `{header:true}`, headers are not
emitted.
  • Loading branch information
eladb committed Jul 1, 2012
1 parent 413c742 commit e66ed66
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/csv.js
Expand Up @@ -134,6 +134,9 @@ module.exports = function(){
state.line = data;
return flush();
}
if(state.count === 0 && csv.writeOptions.header === true){
write(csv.writeOptions.columns || csv.readOptions.columns);
}
write(data, preserve);
if(!transforming && !preserve){
state.count++;
Expand Down
16 changes: 14 additions & 2 deletions test/write.coffee
Expand Up @@ -106,6 +106,18 @@ describe 'write', ->
for i in [0...1000]
test.write ['Test '+i, i, '"']
test.end()
it 'should emit header even without a source', (next) ->
test = csv()
.toPath( "#{__dirname}/write/write_sourceless.tmp",
columns: [ 'col1', 'col2' ],
header: true,
lineBreaks: 'unix' )
.on 'end', ->
expect = fs.readFileSync("#{__dirname}/write/write_sourceless.out").toString()
result = fs.readFileSync("#{__dirname}/write/write_sourceless.tmp").toString()
result.should.eql expect
fs.unlink "#{__dirname}/write/write_sourceless.tmp", next



test.write col1: 'foo1', col2: 'goo1'
test.write col1: 'foo2', col2: 'goo2'
test.end()
3 changes: 3 additions & 0 deletions test/write/write_sourceless.out
@@ -0,0 +1,3 @@
col1,col2
foo1,goo1
foo2,goo2

0 comments on commit e66ed66

Please sign in to comment.