From 1aae24ed16adb4e9e1b24e8fbdd03ee48a936462 Mon Sep 17 00:00:00 2001 From: Caleb Anderson Date: Wed, 19 Aug 2015 21:11:17 -0400 Subject: [PATCH 1/2] added conditional extension delimiter (characters that only appear when the extension is present) --- README.md | 3 +++ lib/phone.rb | 2 ++ test/phone_test.rb | 12 ++++++++++++ 3 files changed, 17 insertions(+) diff --git a/README.md b/README.md index f862212..477ba74 100644 --- a/README.md +++ b/README.md @@ -63,12 +63,14 @@ When given a string, it interpolates the string with the following fields: * %f - first @@n1_length characters of number (configured through Phoner::Phone.n1_length), default is 3 (512) * %l - last characters of number (5486) * %x - the extension number +* %d{} - characters in brackets will only appear if extension is present ```ruby pn = Phoner::Phone.parse('+385915125486') pn.to_s # => "+385915125486" pn.format("%A/%f-%l") # => "091/512-5486" pn.format("+ %c (%a) %n") # => "+ 385 (91) 5125486" +pn.format('(%a) %n%d{ ext.}%x') # => "(91) 5125486" ``` When given a symbol it is used as a lookup for the format in the Phoner::Phone.named_formats hash. @@ -77,6 +79,7 @@ When given a symbol it is used as a lookup for the format in the Phoner::Pho pn.format(:europe) # => "+385 (0) 91 512 5486" pn.format(:us) # => "(234) 123-4567" pn.format(:default_with_extension) # => "+3851234567x143" +pn.format('(%a) %n%d{ ext.}%x') # => "(385) 1234567 ext.143" ``` You can add your own custom named formats like so: diff --git a/lib/phone.rb b/lib/phone.rb index e5169e9..6d79225 100644 --- a/lib/phone.rb +++ b/lib/phone.rb @@ -338,6 +338,8 @@ def format_number(fmt) # TODO: When we drop 1.8.7 we can pass the hash in as an arg to #gsub fmt.gsub(FORMAT_TOKENS) do |match| replacements[match.to_s] + end.gsub(/%d\{(.*?)\}/) do + extension.nil? ? '' : $1 end end end diff --git a/test/phone_test.rb b/test/phone_test.rb index 638f366..a678a9d 100644 --- a/test/phone_test.rb +++ b/test/phone_test.rb @@ -145,6 +145,18 @@ def test_format_with_symbol_specifier assert_equal pn.format(:europe), '+385 (0) 91 512 5486' end + def test_format_with_conditional_extension_with_extension + Phoner::Phone.default_country_code = nil + pn = Phoner::Phone.new '5125486', '191', '385', '242' + assert_equal pn.format('+ %c (%a) %n%d{ #}%x'), '+ 385 (191) 5125486 #242' + end + + def test_format_with_conditional_extension_without_extension + Phoner::Phone.default_country_code = nil + pn = Phoner::Phone.new '5125486', '191', '385' + assert_equal pn.format('+ %c (%a) %n%d{ #}%x'), '+ 385 (191) 5125486' + end + def test_validity assert true, Phoner::Phone.valid?("+17788827175") end From f7965787003773ddb2b6e2b9240e98030c5b0aaf Mon Sep 17 00:00:00 2001 From: Caleb Anderson Date: Wed, 19 Aug 2015 21:40:03 -0400 Subject: [PATCH 2/2] fixed category where conditional delimiter appeared in README --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 477ba74..1b46378 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,12 @@ pn = Phoner::Phone.parse('+385915125486') pn.to_s # => "+385915125486" pn.format("%A/%f-%l") # => "091/512-5486" pn.format("+ %c (%a) %n") # => "+ 385 (91) 5125486" -pn.format('(%a) %n%d{ ext.}%x') # => "(91) 5125486" +pn.format("+ %c (%a) %n%d{ ext.}%x") # => "+ 385 (91) 5125486" +``` +```ruby +pn = Phoner::Phone.parse('+1-800-555-0125x443') +pn.format("%c (%a) %n") # => "1 (800) 5550125" +pn.format("%c (%a) %n%d{ ext.}%x") # => "1 (800) 5550125 ext.443" ``` When given a symbol it is used as a lookup for the format in the Phoner::Phone.named_formats hash. @@ -79,7 +84,7 @@ When given a symbol it is used as a lookup for the format in the Phoner::Pho pn.format(:europe) # => "+385 (0) 91 512 5486" pn.format(:us) # => "(234) 123-4567" pn.format(:default_with_extension) # => "+3851234567x143" -pn.format('(%a) %n%d{ ext.}%x') # => "(385) 1234567 ext.143" + ``` You can add your own custom named formats like so: