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

Exclude phone numbers starting with + sign from sanitisation #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 6 additions & 0 deletions lib/csv-safe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def <<(row)
private

def starts_with_special_character?(str)
return false if str.start_with?('+') && phone_number?(str)

str.start_with?("-", "=", "+", "@", "%", "|", "\r", "\t")
end

Expand Down Expand Up @@ -58,4 +60,8 @@ def sanitize_row(row)
row.map { |field| sanitize_field(field) }
end
end

def phone_number?(value)
value.match?(/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{2,3}?[-\s\.]?[0-9]{2,3}[-\s\.]?[0-9]{2,4}$/)
end
end
18 changes: 18 additions & 0 deletions spec/csv_safe_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@
end
end

context 'with a field that is a phone number' do
let(:field) { '+353 89 999 9999' }
it { should eq field }
it 'should not error' do
expect { subject }.to_not raise_error
end
end

# TODO: this file is too big?

context 'with a field that is a non-String' do
Expand Down Expand Up @@ -204,6 +212,16 @@ def arr_to_line(arr)
end
end

context 'with a row that contains phone numbers' do
let(:row) do
['+353 89 999 9999', '+353899999999', '+353-999-999-999', '+353 899 99 99']
end
it { should eq arr_to_line(row) }
it 'should not raise an error' do
expect { subject }.to_not raise_error
end
end

context 'with a row that requires sanitization' do
context 'because it starts with an @' do
let(:row) do
Expand Down