Permalink
Browse files

Fix empty file handling (more precisely empty buffer) appeared with n…

…ode 3.0
  • Loading branch information...
1 parent 12a5157 commit 709ccfa64a5fc997d9f66da617a4d0713f479c09 @wdavidw wdavidw committed Nov 30, 2010
Showing with 23 additions and 21 deletions.
  1. +23 −21 lib/csv.js
View
@@ -137,7 +137,7 @@ module.exports = function(){
// Private API
function parse(chars){
- chars = ''+chars;
+ chars = '' + chars;
for (var i = 0, l = chars.length; i < l; i++) {
var c = chars.charAt(i);
switch (c) {
@@ -150,7 +150,7 @@ module.exports = function(){
// if escape is same as quote, and escape is first char of a field and it's not quoted, then it is a quote
// next char should be an escape or a quote
var nextChar = chars.charAt(i + 1);
- if ( !( csv.readOptions.escape === csv.readOptions.quote && !state.field && !state.quoted )
+ if( !( csv.readOptions.escape === csv.readOptions.quote && !state.field && !state.quoted )
&& ( nextChar === csv.readOptions.escape || nextChar === csv.readOptions.quote ) ) {
i++;
isEscape = true;
@@ -198,7 +198,7 @@ module.exports = function(){
flush();
break;
default:
- if (state.commented) break;
+ if(state.commented) break;
state.field += c;
}
state.lastC = c;
@@ -207,28 +207,28 @@ module.exports = function(){
function flush(){
if(csv.readOptions.columns){
- if(state.count===0&&csv.readOptions.columns===true){
+ if(state.count === 0 && csv.readOptions.columns === true){
csv.readOptions.columns = state.line;
state.line = [];
state.lastC = '';
return;
}
var line = {};
- csv.readOptions.columns.forEach(function(column,i){
+ csv.readOptions.columns.forEach(function(column, i){
line[column] = state.line[i]||null;
})
state.line = line;
line = null;
}
- var line = csv.transformer?csv.transformer(state.line,state.count):state.line;
+ var line = csv.transformer ? csv.transformer(state.line, state.count) : state.line;
if(line !== null){
csv.emit('data',line,state.count);
if(typeof line === 'object'){
if(!(line instanceof Array)){
- var columns = csv.writeOptions.columns||csv.readOptions.columns;
+ var columns = csv.writeOptions.columns || csv.readOptions.columns;
var _line = [];
if(columns){
- columns.forEach(function(column,i){
+ columns.forEach(function(column, i){
_line[i] = line[column]||'';
})
}else{
@@ -240,7 +240,7 @@ module.exports = function(){
_line = null;
}
if(line instanceof Array){
- var newLine = state.count?csv.writeOptions.lineBreaks:'';
+ var newLine = state.count ? csv.writeOptions.lineBreaks : '';
line.forEach(function(field,i){
if(typeof field == 'string'){
// fine 99% of the cases, keep going
@@ -249,32 +249,32 @@ module.exports = function(){
}else if(field instanceof Date){
field = '' + field.getTime();
}
- var containsdelimiter = field.indexOf(csv.writeOptions.delimiter||csv.readOptions.delimiter)>=0;
- var containsQuote = field.indexOf(csv.writeOptions.quote||csv.readOptions.quote)>=0;
+ var containsdelimiter = field.indexOf(csv.writeOptions.delimiter || csv.readOptions.delimiter) >= 0;
+ var containsQuote = field.indexOf(csv.writeOptions.quote || csv.readOptions.quote) >= 0;
if(containsQuote){
field = field.replace(
- new RegExp(csv.writeOptions.quote||csv.readOptions.quote,'g')
- , (csv.writeOptions.escape||csv.readOptions.escape)
- + (csv.writeOptions.quote||csv.readOptions.quote));
+ new RegExp(csv.writeOptions.quote || csv.readOptions.quote,'g')
+ , (csv.writeOptions.escape || csv.readOptions.escape)
+ + (csv.writeOptions.quote || csv.readOptions.quote));
}
- if(containsQuote||containsdelimiter){
- field = (csv.writeOptions.quote||csv.readOptions.quote)+field+(csv.writeOptions.quote||csv.readOptions.quote);
+ if(containsQuote || containsdelimiter){
+ field = (csv.writeOptions.quote || csv.readOptions.quote) + field + (csv.writeOptions.quote || csv.readOptions.quote);
}
newLine += field;
if(i!==line.length-1){
- newLine += csv.writeOptions.delimiter||csv.readOptions.delimiter;
+ newLine += csv.writeOptions.delimiter || csv.readOptions.delimiter;
}
});
line = newLine;
}
}
if(state.buffer){
- if(state.bufferPosition+Buffer.byteLength(line,'utf8')>csv.readOptions.bufferSize){
+ if(state.bufferPosition + Buffer.byteLength(line,'utf8') > csv.readOptions.bufferSize){
csv.writeStream.write(state.buffer.slice(0, state.bufferPosition));
state.buffer = new Buffer(csv.readOptions.bufferSize);
state.bufferPosition = 0;
}
- state.bufferPosition += state.buffer.write(line,state.bufferPosition,'utf8');
+ state.bufferPosition += state.buffer.write(line, state.bufferPosition,'utf8');
}
}
state.count++;
@@ -294,10 +294,12 @@ module.exports = function(){
flush();
}
if(csv.writeStream){
- csv.writeStream.write(state.buffer.slice(0, state.bufferPosition));
+ if(state.bufferPosition !== 0){
+ csv.writeStream.write(state.buffer.slice(0, state.bufferPosition));
+ }
csv.writeStream.end();
}else{
- csv.emit('end',state.count);
+ csv.emit('end', state.count);
}
}
}

0 comments on commit 709ccfa

Please sign in to comment.