Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Cater for double carriage return characters when removing empty values

  • Loading branch information...
commit 706740be5d2befb70b342088bb09db4d50e93bf3 1 parent 95c5062
@chrismhilton chrismhilton authored
View
5 lib/smarter_csv/smarter_csv.rb
@@ -113,7 +113,10 @@ def SmarterCSV.process(input, options={}, &block) # first parameter: filename
eval('hash.delete(:"")')
end
- hash.delete_if{|k,v| v.nil? || v =~ /^\s*$/} if options[:remove_empty_values]
+ # remove empty values using the same regexp as used by the rails blank? method
+ # which caters for double \n and \r\n characters such as "1\r\n\r\n2" whereas the original check (v =~ /^\s*$/) does not
+ hash.delete_if{|k,v| v.nil? || v !~ /[^[:space:]]/} if options[:remove_empty_values]
+
hash.delete_if{|k,v| ! v.nil? && v =~ /^(\d+|\d+\.\d+)$/ && v.to_f == 0} if options[:remove_zero_values] # values are typically Strings!
hash.delete_if{|k,v| v =~ options[:remove_values_matching]} if options[:remove_values_matching]
if options[:convert_values_to_numeric]
View
5 spec/fixtures/empty.csv
@@ -0,0 +1,5 @@
+not empty 1,not empty 2,not empty 3,empty 1,empty 2 "1234
+56
+
+666
+",?," ? ",," "
View
13 spec/smarter_csv/remove_empty_values_spec.rb
@@ -0,0 +1,13 @@
+require 'spec_helper'
+
+fixture_path = 'spec/fixtures'
+
+describe 'be_able_to' do
+ it 'remove_empty_values' do
+ options = {:row_sep => :auto, :remove_empty_values => true}
+ data = SmarterCSV.process("#{fixture_path}/empty.csv", options)
+ data.size.should == 1
+ data[0].keys.should == [:not_empty_1, :not_empty_2, :not_empty_3]
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.