Permalink
Browse files

Fix header option when columns is defined as a read option

  • Loading branch information...
1 parent 18699be commit 06806326ed00237e847fb5659e6cd5120767a7e4 @wdavidw committed Apr 3, 2012
View
@@ -319,9 +319,6 @@ module.exports = function(){
// Called by the `parse` function on each line. It will then call `write`
function flush(){
- if(state.count === 0 && csv.writeOptions.header === true){
- write(csv.writeOptions.columns || csv.readOptions.columns);
- }
if(csv.readOptions.columns){
if(state.count === 0 && csv.readOptions.columns === true){
csv.readOptions.columns = state.line;
@@ -337,6 +334,9 @@ module.exports = function(){
state.line = line;
line = null;
}
+ if(state.count === 0 && csv.writeOptions.header === true){
+ write(csv.writeOptions.columns || csv.readOptions.columns);
+ }
var line;
if(csv.transformer){
transforming = true;
View
@@ -90,20 +90,6 @@ describe 'columns', ->
result.should.eql expect
fs.unlink "#{__dirname}/columns/out_named.tmp"
next()
- it 'should print names if header', (next) ->
- csv()
- .fromPath("#{__dirname}/columns/header.in")
- .toPath("#{__dirname}/columns/header.tmp",
- header: true
- columns: ["FIELD_1", "FIELD_2"]
- )
- .on 'end', (count) ->
- count.should.eql 2
- expect = fs.readFileSync("#{__dirname}/columns/header.out").toString()
- result = fs.readFileSync("#{__dirname}/columns/header.tmp").toString()
- result.should.eql expect
- fs.unlink "#{__dirname}/columns/header.tmp"
- next()
View
@@ -0,0 +1,43 @@
+
+# Test CSV - Copyright David Worms <open@adaltas.com> (BSD Licensed)
+
+fs = require 'fs'
+should = require 'should'
+csv = require '..'
+
+describe 'header', ->
+ it 'should print headers with defined write columns', (next) ->
+ csv()
+ .fromPath("#{__dirname}/header/defwcols.in")
+ .toPath("#{__dirname}/header/defwcols.tmp",
+ header: true
+ columns: ["FIELD_1", "FIELD_2"]
+ )
+ .on 'end', (count) ->
+ count.should.eql 2
+ expect = fs.readFileSync("#{__dirname}/header/defwcols.out").toString()
+ result = fs.readFileSync("#{__dirname}/header/defwcols.tmp").toString()
+ result.should.eql expect
+ fs.unlink "#{__dirname}/header/defwcols.tmp"
+ next()
+ it 'should print headers with true read columns and defined write columns', (next) ->
+ csv()
+ .fromPath("#{__dirname}/header/truercols_defwcols.in",
+ columns: true
+ )
+ .toPath("#{__dirname}/header/truercols_defwcols.tmp",
+ header: true
+ columns: ["FIELD_1", "FIELD_2"]
+ )
+ .on 'end', (count) ->
+ count.should.eql 2
+ expect = fs.readFileSync("#{__dirname}/header/truercols_defwcols.out").toString()
+ result = fs.readFileSync("#{__dirname}/header/truercols_defwcols.tmp").toString()
+ result.should.eql expect
+ fs.unlink "#{__dirname}/header/truercols_defwcols.tmp"
+ next()
+
+
+
+
+
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,3 @@
+FIELD_1,FIELD_2,FIELD_3,FIELD_4,FIELD_5,FIELD_6
+20322051544,1979,8.8017226E7,ABC,45,2000-01-01
+28392898392,1974,8.8392926E7,DEF,23,2050-11-27
@@ -0,0 +1,3 @@
+FIELD_1,FIELD_2
+20322051544,1979
+28392898392,1974

0 comments on commit 0680632

Please sign in to comment.