From f2725eaf96a4cde0961ca21d769f3f7656a71d87 Mon Sep 17 00:00:00 2001 From: Mat-Sedkowski Date: Thu, 25 Jul 2024 14:13:34 +0200 Subject: [PATCH] Exclude phone numbers starting with + sign from sanitisation --- lib/csv-safe.rb | 6 ++++++ spec/csv_safe_spec.rb | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/csv-safe.rb b/lib/csv-safe.rb index 2a799a7..50c26fa 100644 --- a/lib/csv-safe.rb +++ b/lib/csv-safe.rb @@ -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 @@ -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 diff --git a/spec/csv_safe_spec.rb b/spec/csv_safe_spec.rb index fba9f47..b120cfa 100644 --- a/spec/csv_safe_spec.rb +++ b/spec/csv_safe_spec.rb @@ -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 @@ -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