Permalink
Browse files

Bump version to 0.0.2; Apply quote regexp only if needed

  • Loading branch information...
1 parent a969685 commit 0813eb4305b29d4247f4c61ac8109433d28a92d2 @wdavidw committed Sep 28, 2010
Showing with 35 additions and 25 deletions.
  1. +8 −13 lib/csv.js
  2. +1 −1 package.json
  3. +22 −7 test/escape.js
  4. +2 −0 test/escape/backslash.in
  5. +2 −0 test/escape/backslash.out
  6. +0 −2 test/escape/custom.in
  7. +0 −2 test/escape/custom.out
View
@@ -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;
View
@@ -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 <david@adaltas.com>"
, "directories" : { "lib" : "./lib" }
View
@@ -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');
});
}
}
View
@@ -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
@@ -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
View
@@ -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
View
@@ -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

0 comments on commit 0813eb4

Please sign in to comment.