diff --git a/lib/csv.js b/lib/csv.js index 616c18435..2195896fa 100644 --- a/lib/csv.js +++ b/lib/csv.js @@ -140,7 +140,6 @@ module.exports = function(){ chars = ''+chars; for (var i = 0, l = chars.length; i < l; i++) { var c = chars.charAt(i); -// console.log(c); switch (c) { case csv.readOptions.escape: case csv.readOptions.quote: @@ -151,9 +150,6 @@ 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); -// console.log('-------------- '+c+' '+nextChar+' |'+''+'|'); -// console.log('-------------- '+(!( 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++; @@ -247,27 +243,26 @@ module.exports = function(){ var newLine = state.count?csv.writeOptions.lineBreaks:''; line.forEach(function(field,i){ if(typeof field == 'string'){ + // fine 99% of the cases, keep going }else if(typeof field == 'number'){ - field = ''+field; + field = '' + field; }else if(field instanceof Date){ - field = ''+field.getTime(); + 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; - // http://simonwillison.net/2006/Jan/20/escape/ - RegExp.escape = function(text) { - return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); - } - var quote = new RegExp(csv.writeOptions.quote||csv.readOptions.quote,'g'); if(containsQuote){ - field = field.replace(quote,(csv.writeOptions.escape||csv.readOptions.escape)+(csv.writeOptions.quote||csv.readOptions.quote)); + 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){ 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; diff --git a/package.json b/package.json index 7be360095..4f63b0279 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { "name": "csv" -, "version": "0.0.1" +, "version": "0.0.2" , "description": "CSV parser with simple api, full of options and tested against large datasets." , "author": "David Worms " , "directories" : { "lib" : "./lib" } diff --git a/test/escape.js b/test/escape.js index b74383353..cedc5cec5 100644 --- a/test/escape.js +++ b/test/escape.js @@ -8,8 +8,16 @@ module.exports = { // Note: we only escape quote and escape character 'Test default': function(assert){ csv() - .fromPath(__dirname+'/escape/default.in') + .fromPath(__dirname+'/escape/default.in',{ + escape: '"' + }) .toPath(__dirname+'/escape/default.tmp') + .on('data',function(data,index){ + if(index===0){ + assert.equal('19"79.0',data[1]); + assert.equal('A"B"C',data[3]); + } + }) .on('end',function(){ assert.equal( fs.readFileSync(__dirname+'/escape/default.out').toString(), @@ -20,16 +28,23 @@ module.exports = { }, 'Test backslash': function(assert){ csv() - .fromPath(__dirname+'/escape/custom.in',{ + .fromPath(__dirname+'/escape/backslash.in',{ escape: '\\' }) - .toPath(__dirname+'/escape/custom.tmp') - .on('end',function(){ + .toPath(__dirname+'/escape/backslash.tmp') + .on('data',function(data,index){ + if(index===0){ + assert.equal('19"79.0',data[1]); + assert.equal('A"B"C',data[3]); + } + }) + .on('end',function(count){ + assert.strictEqual(2,count); assert.equal( - fs.readFileSync(__dirname+'/escape/custom.out').toString(), - fs.readFileSync(__dirname+'/escape/custom.tmp').toString() + fs.readFileSync(__dirname+'/escape/backslash.out').toString(), + fs.readFileSync(__dirname+'/escape/backslash.tmp').toString() ); - fs.unlink(__dirname+'/escape/custom.tmp'); + fs.unlink(__dirname+'/escape/backslash.tmp'); }); } } \ No newline at end of file diff --git a/test/escape/backslash.in b/test/escape/backslash.in new file mode 100644 index 000000000..582e28ad5 --- /dev/null +++ b/test/escape/backslash.in @@ -0,0 +1,2 @@ +20322051544,"19\"79.0",8.8017226E7,"A\"B\"C",45,2000-01-01 +28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27 diff --git a/test/escape/backslash.out b/test/escape/backslash.out new file mode 100644 index 000000000..41972650d --- /dev/null +++ b/test/escape/backslash.out @@ -0,0 +1,2 @@ +20322051544,"19\"79.0",8.8017226E7,"A\"B\"C",45,2000-01-01 +28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27 \ No newline at end of file diff --git a/test/escape/custom.in b/test/escape/custom.in deleted file mode 100644 index 0eba6f3d0..000000000 --- a/test/escape/custom.in +++ /dev/null @@ -1,2 +0,0 @@ -20322051544,"19\"79.0",8.8017226E7,ABC,45,2000-01-01 -28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27 diff --git a/test/escape/custom.out b/test/escape/custom.out deleted file mode 100644 index 610326474..000000000 --- a/test/escape/custom.out +++ /dev/null @@ -1,2 +0,0 @@ -20322051544,"19\"79.0",8.8017226E7,ABC,45,2000-01-01 -28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27 \ No newline at end of file