Permalink
Browse files

New quoted write option to quote all fields

  • Loading branch information...
1 parent 6b56514 commit 75a8ffa26db7b1face1c2631ead2283bf98a5e30 David Worms committed Jul 20, 2012
Showing with 35 additions and 13 deletions.
  1. +3 −1 lib/csv.js
  2. +3 −0 readme.md
  3. +21 −8 test/quotes.coffee
  4. +2 −0 test/quotes/contains_quotes.in
  5. +2 −0 test/quotes/contains_quotes.out
  6. +2 −2 test/quotes/quoted.in
  7. +2 −2 test/quotes/quoted.out
View
@@ -47,6 +47,7 @@ module.exports = function(){
this.writeOptions = {
delimiter: null,
quote: null,
+ quoted: false,
escape: null,
columns: null,
header: false,
@@ -442,7 +443,8 @@ module.exports = function(){
, (csv.writeOptions.escape || csv.readOptions.escape)
+ (csv.writeOptions.quote || csv.readOptions.quote));
}
- if(containsQuote || containsdelimiter || containsLinebreak){
+ console.log('quoted', csv.writeOptions.quoted);
+ if(containsQuote || containsdelimiter || containsLinebreak || csv.writeOptions.quoted){
field = (csv.writeOptions.quote || csv.readOptions.quote) + field + (csv.writeOptions.quote || csv.readOptions.quote);
}
newLine += field;
View
@@ -132,6 +132,9 @@ Options are:
- *quote*
Default to the quote read option.
+- *quoted*
+ Boolean, default to false, quote all the fields even if not required.
+
- *escape*
Default to the escape read option.
View
@@ -15,7 +15,7 @@ describe 'quotes', ->
result = fs.readFileSync("#{__dirname}/quotes/regular.tmp").toString()
result.should.eql expect
fs.unlink "#{__dirname}/quotes/regular.tmp"
- it 'Test quotes with delimiter', ->
+ it 'should read quoted values containing delimiters and write around quote only the value containing delimiters', ->
csv()
.fromPath("#{__dirname}/quotes/delimiter.in")
.toPath("#{__dirname}/quotes/delimiter.tmp")
@@ -44,23 +44,23 @@ describe 'quotes', ->
result = fs.readFileSync("#{__dirname}/quotes/empty_value.tmp").toString()
result.should.eql expect
fs.unlink "#{__dirname}/quotes/empty_value.tmp"
- it 'Test quoted quote', ->
+ it 'should read values with quotes, escaped as double quotes, and write empty values as not quoted', ->
csv()
- .fromPath "#{__dirname}/quotes/quoted.in",
+ .fromPath "#{__dirname}/quotes/contains_quotes.in",
quote: '"',
escape: '"',
- .toPath("#{__dirname}/quotes/quoted.tmp")
+ .toPath("#{__dirname}/quotes/contains_quotes.tmp")
.on 'data', (data,index) ->
data.length.should.eql 5
if index is 0
data[1].should.eql '"'
data[4].should.eql '"ok"'
.on 'end', ->
- expect = fs.readFileSync("#{__dirname}/quotes/quoted.out").toString()
- result = fs.readFileSync("#{__dirname}/quotes/quoted.tmp").toString()
+ expect = fs.readFileSync("#{__dirname}/quotes/contains_quotes.out").toString()
+ result = fs.readFileSync("#{__dirname}/quotes/contains_quotes.tmp").toString()
result.should.eql expect
- fs.unlink "#{__dirname}/quotes/quoted.tmp"
- it 'Test quoted linebreak', ->
+ fs.unlink "#{__dirname}/quotes/contains_quotes.tmp"
+ it 'should accept line breaks inside quotes', ->
csv()
.fromPath "#{__dirname}/quotes/linebreak.in",
quote: '"',
@@ -109,3 +109,16 @@ describe 'quotes', ->
e.message.should.match /Invalid closing quote/
fs.unlink "#{__dirname}/quotes/invalidstring.tmp"
next()
+ it 'should quotes all fields', (next) ->
+ csv()
+ .fromPath("#{__dirname}/quotes/quoted.in")
+ .toPath( "#{__dirname}/quotes/quoted.tmp", quoted: true )
+ .on 'end', ->
+ expect = fs.readFileSync("#{__dirname}/quotes/quoted.out").toString()
+ result = fs.readFileSync("#{__dirname}/quotes/quoted.tmp").toString()
+ result.should.eql expect
+ fs.unlink "#{__dirname}/quotes/quoted.tmp"
+ next()
+ .on 'error', (e) ->
+ false.should.be.ok
+
@@ -0,0 +1,2 @@
+20322051544,"""",8.8017226E7,45,"""ok"""
+"",1974,8.8392926E7,"",""
@@ -0,0 +1,2 @@
+20322051544,"""",8.8017226E7,45,"""ok"""
+,1974,8.8392926E7,,
View
@@ -1,2 +1,2 @@
-20322051544,"""",8.8017226E7,45,"""ok"""
-"",1974,8.8392926E7,"",""
+20322051544,"1979.0",8.801"7226E7,ABC
+"283928""98392",1974.0,8.8392926E7,DEF
View
@@ -1,2 +1,2 @@
-20322051544,"""",8.8017226E7,45,"""ok"""
-,1974,8.8392926E7,,
+"20322051544","1979.0","8.801""7226E7","ABC"
+"283928""98392","1974.0","8.8392926E7","DEF"

0 comments on commit 75a8ffa

Please sign in to comment.