Skip to content

Commit

Permalink
OCR.find_all_in_string: handle newlines and semicolons
Browse files Browse the repository at this point in the history
  • Loading branch information
henrik committed Sep 26, 2016
1 parent 7c2a046 commit a91729e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/banktools-se/ocr.rb
Expand Up @@ -63,7 +63,9 @@ def self.to_number(number, length_digit: false, pad: "")
end

def self.find_all_in_string(string, length_digit: false, pad: "")
string.scan(/\d+/).select { |candidate|
expanded_string = string + " " + string.gsub("\n", "") + " " + string.gsub(";", "")

expanded_string.scan(/\d+/).select { |candidate|
begin
to_number(candidate, length_digit: length_digit, pad: pad)
true
Expand Down
10 changes: 10 additions & 0 deletions spec/ocr_spec.rb
Expand Up @@ -82,6 +82,16 @@
expect(BankTools::SE::OCR.find_all_in_string("x1230x")).to eq [ "1230" ]
end

it "handles OCR numbers both separated and split by newlines" do
expect(BankTools::SE::OCR.find_all_in_string("1230\n4564")).to eq [ "1230", "4564", "12304564" ]
expect(BankTools::SE::OCR.find_all_in_string("45\n64")).to eq [ "4564" ]
end

it "handles OCR numbers both separated and split by semicolons" do
expect(BankTools::SE::OCR.find_all_in_string("1230;4564")).to eq [ "1230", "4564", "12304564" ]
expect(BankTools::SE::OCR.find_all_in_string("45;64")).to eq [ "4564" ]
end

it "excludes duplicates" do
expect(BankTools::SE::OCR.find_all_in_string("1230 1230 4564")).to eq [ "1230", "4564" ]
end
Expand Down

0 comments on commit a91729e

Please sign in to comment.