Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve parsing of BankTransactionReference #819

Merged
merged 1 commit into from
Feb 12, 2021
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
6 changes: 3 additions & 3 deletions lib/bank_transaction_reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class BankTransactionReference

# parses a string from a bank transaction field
def self.parse(data)
m = /(^|\s)FS(?<group>\d+)(\.(?<user>\d+))?(?<parts>([A-Za-z]+\d+(\.\d+)?)+)(\s|$)/.match(data)
m = /(^|[^\w\.])FS(?<group>\d+)(\.(?<user>\d+))?(?<parts>([A-Za-z]+\d+(\.\d+)?)+)([^\w\.]|$)/.match(data)
return unless m

parts = {}
Expand All @@ -12,8 +12,8 @@ def self.parse(data)
parts[category] = value
end

ret = { group: m[:group], parts: parts }
ret[:user] = m[:user] if m[:user]
ret = { group: m[:group].to_i, parts: parts }
ret[:user] = m[:user].to_i if m[:user]
return ret
end

Expand Down
60 changes: 48 additions & 12 deletions spec/lib/bank_transaction_reference_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,76 @@
expect(BankTransactionReference.parse('xFS1A1')).to be nil
end

it 'returns nil for .FS1A1' do
expect(BankTransactionReference.parse('.FS1A1')).to be nil
end

it 'returns nil for FS1A1x' do
expect(BankTransactionReference.parse('FS1A1x')).to be nil
end

it 'returns correct value for FS1A1' do
expect(BankTransactionReference.parse('FS1A1')).to be { { group: 1, parts: { A: 1 } } }
it 'returns nil for FS1A1.' do
expect(BankTransactionReference.parse('FS1A1.')).to be nil
end

it 'returns correct value for FS1A1' do
expect(BankTransactionReference.parse('FS1.2A3')).to be { { group: 1, user: 2, parts: { A: 3 } } }
expect(BankTransactionReference.parse('FS1A1')).to match({ group: 1, parts: { "A" => 1 } })
end

it 'returns correct value for FS1.2A3' do
expect(BankTransactionReference.parse('FS1.2A3')).to match({ group: 1, user: 2, parts: { "A" => 3 } })
end

it 'returns correct value for FS1A2B3' do
expect(BankTransactionReference.parse('FS1A2B3C4')).to be { { group: 1, parts: { A: 2, B: 3, C: 4 } } }
it 'returns correct value for FS1A2B3C4' do
expect(BankTransactionReference.parse('FS1A2B3C4')).to match({ group: 1, parts: { "A" => 2, "B" => 3, "C" => 4 } })
end

it 'returns correct value for FS1A2B3A4' do
expect(BankTransactionReference.parse('FS1A2B3C4')).to be { { group: 1, parts: { A: 6, B: 3 } } }
expect(BankTransactionReference.parse('FS1A2B3A4')).to match({ group: 1, parts: { "A" => 6, "B" => 3 } })
end

it 'returns correct value for FS1A2.34B5.67C8.90' do
expect(BankTransactionReference.parse('FS1A2B3C4')).to be { { group: 1, parts: { A: 2.34, B: 5.67, C: 8.90 } } }
expect(BankTransactionReference.parse('FS1A2.34B5.67C8.90')).to match({ group: 1, parts: { "A" => 2.34, "B" => 5.67, "C" => 8.90 } })
end

it 'returns correct value for FS123A456 with comma-separated prefix' do
expect(BankTransactionReference.parse('x,FS123A456')).to match({ group: 123, parts: { "A" => 456 } })
end

it 'returns correct value for FS123A456 with minus-separated prefix' do
expect(BankTransactionReference.parse('x-FS123A456')).to match({ group: 123, parts: { "A" => 456 } })
end

it 'returns correct value for FS123A456 with semicolon-separated prefix' do
expect(BankTransactionReference.parse('x;FS123A456')).to match({ group: 123, parts: { "A" => 456 } })
end

it 'returns correct value for FS123A456 with space-separated prefix' do
expect(BankTransactionReference.parse('x FS123A456')).to match({ group: 123, parts: { "A" => 456 } })
end

it 'returns correct value for FS234A567 with comma-separated suffix' do
expect(BankTransactionReference.parse('FS234A567,x')).to match({ group: 234, parts: { "A" => 567 } })
end

it 'returns correct value for FS234A567 with minus-separated suffix' do
expect(BankTransactionReference.parse('FS234A567-x')).to match({ group: 234, parts: { "A" => 567 } })
end

it 'returns correct value for FS234A567 with space-separated suffix' do
expect(BankTransactionReference.parse('FS234A567 x')).to match({ group: 234, parts: { "A" => 567 } })
end

it 'returns correct value for FS123A456 with prefix' do
expect(BankTransactionReference.parse('x FS123A456')).to be { { group: 123, parts: { A: 456 } } }
it 'returns correct value for FS234A567 with semicolon-separated suffix' do
expect(BankTransactionReference.parse('FS234A567;x')).to match({ group: 234, parts: { "A" => 567 } })
end

it 'returns correct value for FS234A567 with suffix' do
expect(BankTransactionReference.parse('FS234A567 x')).to be { { group: 234, parts: { A: 567 } } }
it 'returns correct value for FS234A567 with minus-separated suffix' do
expect(BankTransactionReference.parse('FS234A567-x')).to match({ group: 234, parts: { "A" => 567 } })
end

it 'returns correct value for FS34.56A67.89 with prefix and suffix' do
expect(BankTransactionReference.parse('x FS34.56A67.89 x')).to be { { group: 34, user: 56, parts: { A: 67.89 } } }
expect(BankTransactionReference.parse('prefix FS34.56A67.89, suffix')).to match({ group: 34, user: 56, parts: { "A" => 67.89 } })
end

end