Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion lib/csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1057,10 +1057,29 @@ def filter(input=nil, output=nil, **options)
out_options[key] = value
end
end

# build input and output wrappers
input = new(input || ARGF, **in_options)
input = new(input || ARGF, **in_options)
output = new(output || $stdout, **out_options)

# process headers
need_manual_header_output =
(in_options[:headers] and
out_options[:headers] == true and
out_options[:write_headers])
if need_manual_header_output
first_row = input.shift
if first_row
if first_row.is_a?(Row)
headers = first_row.headers
yield headers
output << headers
end
yield first_row
output << first_row
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds headers twice with the following case:

  def test_filter_with_headers
    input = <<-CSV
foo,0
bar,1
baz,2
    CSV
    output = ""
    CSV.filter(input, output, headers: ["Name", "Value"], output_write_headers: true) do |row|
      row
    end
    assert_equal(input, output)
  end


# read, yield, write
input.each do |row|
yield row
Expand Down
67 changes: 66 additions & 1 deletion test/csv/interface/test_read_write.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class TestCSVInterfaceReadWrite < Test::Unit::TestCase
extend DifferentOFS

def test_filter
input = <<-CSV
input = <<-CSV.freeze
1;2;3
4;5
CSV
Expand All @@ -24,6 +24,71 @@ def test_filter
CSV
end

def test_filter_headers_true
input = <<-CSV.freeze
Name,Value
foo,0
bar,1
baz,2
CSV
output = ""
CSV.filter(input, output, headers: true) do |row|
row[0] += "X"
row[1] = row[1].to_i + 1
end
assert_equal(<<-CSV, output)
fooX,1
barX,2
bazX,3
CSV
end

def test_filter_headers_true_write_headers
input = <<-CSV.freeze
Name,Value
foo,0
bar,1
baz,2
CSV
output = ""
CSV.filter(input, output, headers: true, out_write_headers: true) do |row|
if row.is_a?(Array)
row[0] += "X"
row[1] += "Y"
else
row[0] += "X"
row[1] = row[1].to_i + 1
end
end
assert_equal(<<-CSV, output)
NameX,ValueY
fooX,1
barX,2
bazX,3
CSV
end

def test_filter_headers_array_write_headers
input = <<-CSV.freeze
foo,0
bar,1
baz,2
CSV
output = ""
CSV.filter(input, output,
headers: ["Name", "Value"],
out_write_headers: true) do |row|
row[0] += "X"
row[1] = row[1].to_i + 1
end
assert_equal(<<-CSV, output)
Name,Value
fooX,1
barX,2
bazX,3
CSV
end

def test_instance_same
data = ""
assert_equal(CSV.instance(data, col_sep: ";").object_id,
Expand Down