Skip to content

Commit

Permalink
Bump version to 0.0.2; Apply quote regexp only if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Sep 28, 2010
1 parent a969685 commit 0813eb4
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 25 deletions.
21 changes: 8 additions & 13 deletions lib/csv.js
Expand Up @@ -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:
Expand All @@ -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++;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion 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 <david@adaltas.com>"
, "directories" : { "lib" : "./lib" }
Expand Down
29 changes: 22 additions & 7 deletions test/escape.js
Expand Up @@ -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(),
Expand All @@ -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');
});
}
}
2 changes: 2 additions & 0 deletions 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
2 changes: 2 additions & 0 deletions 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
2 changes: 0 additions & 2 deletions test/escape/custom.in

This file was deleted.

2 changes: 0 additions & 2 deletions test/escape/custom.out

This file was deleted.

0 comments on commit 0813eb4

Please sign in to comment.