Permalink
Browse files

[ISSUE-44] Deal with empty columns

  • Loading branch information...
1 parent 4f65dbe commit a9e115c2085f402fc1f55f80f45caf61bcf28818 @wdavidw committed Aug 15, 2012
Showing with 33 additions and 4 deletions.
  1. +4 −4 lib/csv.js
  2. +29 −0 test/reader.coffee
View
8 lib/csv.js
@@ -4,7 +4,7 @@ var EventEmitter = require('events').EventEmitter,
fs = require('fs');
// Utils function
-var merge = function(obj1,obj2){
+var merge = function(obj1, obj2){
var r = obj1||{};
for(var key in obj2){
r[key] = obj2[key];
@@ -69,7 +69,7 @@ module.exports = function(){
// Reading API
- CSV.prototype.from = function(data,options){
+ CSV.prototype.from = function(data, options){
if(options) merge(this.readOptions,options);
var self = this;
process.nextTick(function(){
@@ -148,7 +148,7 @@ module.exports = function(){
return csv.emit('error', new Error('Quoted field not terminated'));
}
// dump open record
- if (state.field) {
+ if (state.field || state.lastC === this.readOptions.delimiter || state.lastC === this.readOptions.quote) {
if(csv.readOptions.trim || csv.readOptions.rtrim){
state.field = state.field.trimRight();
}
@@ -255,7 +255,7 @@ module.exports = function(){
state.field += c;
}
}
- if (!isEscape && (c === csv.readOptions.quote)) {
+ if (!isEscape && c === csv.readOptions.quote) {
if (state.field && !state.quoted) {
// Treat quote as a regular character
state.field += c;
View
29 test/reader.coffee
@@ -4,6 +4,35 @@ should = require 'should'
csv = require '..'
describe 'reader', ->
+
+ it 'should call data event when data is provided in from', (next) ->
+ csv().from('"1","2","3","4","5"')
+ .on 'data', (data) ->
+ data.length.should.eql 5
+ .on 'end', ->
+ next()
+
+ it 'should include empty last column', (next) ->
+ csv().from('"1","2","3","4","5",')
+ .on 'data', (data) ->
+ data.length.should.eql 6
+ .on 'end', ->
+ next()
+
+ it 'should include empty last column surrounded by quotes', (next) ->
+ csv().from('"1","2","3","4","5",""')
+ .on 'data', (data) ->
+ data.length.should.eql 6
+ .on 'end', ->
+ next()
+
+ it 'should include empty last column if followed by linebreak', (next) ->
+ csv().from('"1","2","3","4","5",""\n')
+ .on 'data', (data) ->
+ data.length.should.eql 6
+ .on 'end', ->
+ next()
+
it 'should call error event if exception is thrown in data event', (next) ->
count = 0
errors = 0

0 comments on commit a9e115c

Please sign in to comment.