Permalink
Browse files

Add 2nd preserve arguments to write to preserve parsing and inject ro…

…w text
  • Loading branch information...
1 parent 92a9687 commit 18e7b8441de91fd800115691f16c3339c91301b5 @wdavidw wdavidw committed Apr 10, 2011
View
@@ -16,6 +16,7 @@ var merge = function(obj1,obj2){
module.exports = function(){
var state = {
count: 0,
+ countWriten: 0,
field: '',
line: [],
lastC: '',
@@ -107,11 +108,11 @@ module.exports = function(){
// Writting API
- CSV.prototype.write = function(line){
- if(typeof line === 'string'){
+ CSV.prototype.write = function(line,preserve){
+ if(typeof line === 'string' && !preserve){
return parse(line);
}
- write(line);
+ write(line,preserve);
}
CSV.prototype.end = function(){
if (state.quoted) {
@@ -194,69 +195,72 @@ module.exports = function(){
// Private API
- function write(line){
- if(line !== null){
+ function write(line,preserve){
+ if(typeof line === 'undefined' || line === null){
+ return;
+ }
+ if(!preserve){
csv.emit('data',line,state.count);
- if(typeof line === 'object'){
- if(!(line instanceof Array)){
- var columns = csv.writeOptions.columns || csv.readOptions.columns;
- var _line = [];
- if(columns){
- columns.forEach(function(column, i){
- _line[i] = (typeof line[column] === 'undefined' || line[column] === null) ? '' : line[column];
- })
- }else{
- for(var column in line){
- _line.push(line[column]);
- }
+ }
+ if(typeof line === 'object'){
+ if(!(line instanceof Array)){
+ var columns = csv.writeOptions.columns || csv.readOptions.columns;
+ var _line = [];
+ if(columns){
+ columns.forEach(function(column, i){
+ _line[i] = (typeof line[column] === 'undefined' || line[column] === null) ? '' : line[column];
+ })
+ }else{
+ for(var column in line){
+ _line.push(line[column]);
}
- line = _line;
- _line = null;
}
- if(line instanceof Array){
- var newLine = state.count ? csv.writeOptions.lineBreaks || "\r" : '';
- line.forEach(function(field,i){
- if(typeof field == 'string'){
- // fine 99% of the cases, keep going
- }else if(typeof field == 'number'){
- field = '' + field;
- }else if(field instanceof Date){
- field = '' + field.getTime();
- }
- if(field){
- var containsdelimiter = field.indexOf(csv.writeOptions.delimiter || csv.readOptions.delimiter) >= 0;
- var containsQuote = field.indexOf(csv.writeOptions.quote || csv.readOptions.quote) >= 0;
- var containsLinebreak = field.indexOf("\r") >= 0 || field.indexOf("\n") >= 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));
- }
- if(containsQuote || containsdelimiter || containsLinebreak){
- field = (csv.writeOptions.quote || csv.readOptions.quote) + field + (csv.writeOptions.quote || csv.readOptions.quote);
- }
- newLine += field;
+ line = _line;
+ _line = null;
+ }
+ if(line instanceof Array){
+ var newLine = state.countWriten ? csv.writeOptions.lineBreaks || "\r" : '';
+ line.forEach(function(field,i){
+ if(typeof field == 'string'){
+ // fine 99% of the cases, keep going
+ }else if(typeof field == 'number'){
+ field = '' + field;
+ }else if(field instanceof Date){
+ field = '' + field.getTime();
+ }
+ if(field){
+ var containsdelimiter = field.indexOf(csv.writeOptions.delimiter || csv.readOptions.delimiter) >= 0;
+ var containsQuote = field.indexOf(csv.writeOptions.quote || csv.readOptions.quote) >= 0;
+ var containsLinebreak = field.indexOf("\r") >= 0 || field.indexOf("\n") >= 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));
}
- if(i!==line.length-1){
- newLine += csv.writeOptions.delimiter || csv.readOptions.delimiter;
+ if(containsQuote || containsdelimiter || containsLinebreak){
+ field = (csv.writeOptions.quote || csv.readOptions.quote) + field + (csv.writeOptions.quote || csv.readOptions.quote);
}
- });
- line = newLine;
- }
+ newLine += field;
+ }
+ if(i!==line.length-1){
+ newLine += csv.writeOptions.delimiter || csv.readOptions.delimiter;
+ }
+ });
+ line = newLine;
}
- if(state.buffer){
- 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');
+ }
+ if(state.buffer){
+ 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');
+ }
+ if(!preserve){
+ state.countWriten++;
}
- state.count++;
- state.line = [];
- state.lastC = '';
}
function parse(chars){
@@ -325,6 +329,7 @@ module.exports = function(){
break;
}
if( csv.writeOptions.lineBreaks === null ){
+ // Auto-discovery of linebreaks
csv.writeOptions.lineBreaks = c + ( c === '\r' && chars.charAt(i+1) === '\n' ? '\n' : '' );
}
if(csv.readOptions.trim || csv.readOptions.rtrim){
@@ -365,7 +370,10 @@ module.exports = function(){
line = null;
}
var line = csv.transformer ? csv.transformer(state.line, state.count) : state.line;
- write(line);
+ write(line);
+ state.count++;
+ state.line = [];
+ state.lastC = '';
}
return csv;
};
View
@@ -26,23 +26,41 @@ module.exports = {
fs.unlink(__dirname+'/transform/reorder.tmp');
});
},
- 'Test empty': function(){
+ 'Test return undefined - skip all lines': function(){
var count = 0;
csv()
- .fromPath(__dirname+'/transform/empty.in')
- .toPath(__dirname+'/transform/empty.tmp')
+ .fromPath(__dirname+'/transform/undefined.in')
+ .toPath(__dirname+'/transform/undefined.tmp')
.transform(function(data,index){
assert.strictEqual(count,index);
count++;
- return null;
})
.on('end',function(){
assert.strictEqual(2,count);
assert.equal(
- fs.readFileSync(__dirname+'/transform/empty.out').toString(),
- fs.readFileSync(__dirname+'/transform/empty.tmp').toString()
+ fs.readFileSync(__dirname+'/transform/undefined.out').toString(),
+ fs.readFileSync(__dirname+'/transform/undefined.tmp').toString()
);
- fs.unlink(__dirname+'/transform/empty.tmp');
+ fs.unlink(__dirname+'/transform/undefined.tmp');
+ });
+ },
+ 'Test return null - skip one of two lines': function(){
+ var count = 0;
+ csv()
+ .fromPath(__dirname+'/transform/null.in')
+ .toPath(__dirname+'/transform/null.tmp')
+ .transform(function(data,index){
+ assert.strictEqual(count,index);
+ count++;
+ return index%2 ? data : null;
+ })
+ .on('end',function(){
+ assert.strictEqual(6,count);
+ assert.equal(
+ fs.readFileSync(__dirname+'/transform/null.out').toString(),
+ fs.readFileSync(__dirname+'/transform/null.tmp').toString()
+ );
+ fs.unlink(__dirname+'/transform/null.tmp');
});
},
'Test return object': function(){
View
@@ -0,0 +1,6 @@
+20322051544,1979.0,8.8017226E7,ABC,45,2000-01-01
+28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
+82378392929,1972.0,8.8392926E7,FJI,23,2012-04-30
+47191084482,1978.0,8.8392926E7,2FF,23,2064-02-15
+28718040423,1973.0,8.8392926E7,FRE,23,1970-09-02
+24792823783,1971.0,8.8392926E7,POF,23,1978-06-09
View
@@ -0,0 +1,3 @@
+28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
+47191084482,1978.0,8.8392926E7,2FF,23,2064-02-15
+24792823783,1971.0,8.8392926E7,POF,23,1978-06-09
File renamed without changes.
File renamed without changes.
Oops, something went wrong.

0 comments on commit 18e7b84

Please sign in to comment.