From 9d44cadb5769700949ad6250552ffa7afd261d3d Mon Sep 17 00:00:00 2001 From: Jose Miguel Rivero Bruno Date: Wed, 20 Feb 2019 06:02:00 -0400 Subject: [PATCH] Prepare release v0.2.0 (#28) --- .formatter.exs | 4 + .travis.yml | 36 +- README.md | 14 +- config/.credo.exs | 7 +- config/config.exs | 2 +- lib/ex_phone_number.ex | 19 +- lib/ex_phone_number/constants/mappings.ex | 408 +++++++----- lib/ex_phone_number/constants/patterns.ex | 33 +- lib/ex_phone_number/extraction.ex | 164 +++-- lib/ex_phone_number/formatting.ex | 143 +++-- lib/ex_phone_number/metadata.ex | 109 +++- lib/ex_phone_number/metadata/number_format.ex | 38 +- .../metadata/phone_metadata.ex | 538 +++++++++++----- .../metadata/phone_number_description.ex | 38 +- lib/ex_phone_number/model/phone_number.ex | 52 +- lib/ex_phone_number/normalization.ex | 29 +- lib/ex_phone_number/parsing.ex | 116 +++- lib/ex_phone_number/utilities.ex | 8 +- lib/ex_phone_number/validation.ex | 124 ++-- mix.exs | 48 +- mix.lock | 27 +- test/ex_phone_number/extraction_test.exs | 80 ++- test/ex_phone_number/formatting_test.exs | 202 ++++-- test/ex_phone_number/metadata_test.exs | 134 ++-- .../model/phone_number_test.exs | 1 - test/ex_phone_number/parsing_test.exs | 600 +++++++++--------- test/ex_phone_number/validation_test.exs | 185 +++--- test/support/phone_number_fixture.ex | 154 ++--- test/test_helper.exs | 9 +- 29 files changed, 2063 insertions(+), 1259 deletions(-) create mode 100644 .formatter.exs diff --git a/.formatter.exs b/.formatter.exs new file mode 100644 index 0000000..19ce32b --- /dev/null +++ b/.formatter.exs @@ -0,0 +1,4 @@ +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"], + line_length: 160 +] diff --git a/.travis.yml b/.travis.yml index 95c3daa..12b66ca 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,19 +1,31 @@ language: elixir - +elixir: + - 1.4 + - 1.5 + - 1.6 + - 1.7 + - 1.8 +otp_release: + - 19.3 + - 20.3 + - 21.0 +matrix: + exclude: + - elixir: 1.4 + otp_release: 21.0 + - elixir: 1.5 + otp_release: 21.0 + - elixir: 1.7 + otp_release: 19.3 + - elixir: 1.7 + otp_release: 20.3 + - elixir: 1.8 + otp_release: 19.3 + - elixir: 1.8 + otp_release: 20.3 env: global: - MIX_ENV=test - -matrix: - include: - - elixir: 1.3.4 - env: TRAVIS_COVERAGE=true - otp_release: 18.3 - - elixir: 1.4.2 - otp_release: 18.3 - - elixir: 1.4.2 - otp_release: 19.3 - after_success: | if [ ! -z "${TRAVIS_COVERAGE}" ]; then mix coveralls.travis diff --git a/README.md b/README.md index 25a65e3..801090a 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,16 @@ # ExPhoneNumber -[![Build Status](https://travis-ci.org/socialpaymentsbv/ex_phone_number.svg?branch=develop)](https://travis-ci.org/socialpaymentsbv/ex_phone_number) +[![Build Status](https://travis-ci.org/socialpaymentsbv/ex_phone_number.svg?branch=develop)](https://travis-ci.org/socialpaymentsbv/ex_phone_number) [![Hex Version](http://img.shields.io/hexpm/v/ex_phone_number.svg?style=flat)](https://hex.pm/packages/ex_phone_number) It's a library for parsing, formatting, and validating international phone numbers. -Based on Google's [libphonenumber](https://github.com/googlei18n/libphonenumber). +Based on Google's [libphonenumber](https://github.com/googlei18n/libphonenumber) (current metadata version: 8.10.2). ## Installation 1. Add `:ex_phone_number` to your list of dependencies in `mix.exs`: ```elixir def deps do - [{:ex_phone_number, "~> 0.1"}] -end -``` - - 2. Add `:ex_phone_number` to the list of applications in `mix.exs`: -```elixir -def application do - [mod: {MyApp, []}, - applications: [..., :ex_phone_number]] + [{:ex_phone_number, "~> 0.2"}] end ``` diff --git a/config/.credo.exs b/config/.credo.exs index b9c6523..d957976 100644 --- a/config/.credo.exs +++ b/config/.credo.exs @@ -13,13 +13,10 @@ {Credo.Check.Consistency.SpaceAroundOperators}, {Credo.Check.Consistency.SpaceInParentheses}, {Credo.Check.Consistency.TabsOrSpaces}, - {Credo.Check.Design.AliasUsage, priority: :low}, {Credo.Check.Design.DuplicatedCode, mass_threshold: 16, nodes_threshold: 2}, - {Credo.Check.Design.TagTODO, exit_status: 2}, {Credo.Check.Design.TagFIXME}, - {Credo.Check.Readability.FunctionNames}, {Credo.Check.Readability.MaxLineLength, priority: :low, max_length: 160}, {Credo.Check.Readability.ModuleAttributeNames}, @@ -29,7 +26,6 @@ {Credo.Check.Readability.TrailingBlankLine}, {Credo.Check.Readability.TrailingWhiteSpace}, {Credo.Check.Readability.VariableNames}, - {Credo.Check.Refactor.ABCSize}, {Credo.Check.Refactor.CaseTrivialMatches}, {Credo.Check.Refactor.CondStatements}, @@ -41,7 +37,6 @@ {Credo.Check.Refactor.NegatedConditionsWithElse}, {Credo.Check.Refactor.Nesting}, {Credo.Check.Refactor.UnlessWithElse}, - {Credo.Check.Warning.IExPry}, {Credo.Check.Warning.IoInspect}, {Credo.Check.Warning.NameRedeclarationByAssignment}, @@ -54,7 +49,7 @@ {Credo.Check.Warning.UnusedListOperation}, {Credo.Check.Warning.UnusedStringOperation}, {Credo.Check.Warning.UnusedTupleOperation}, - {Credo.Check.Warning.OperationWithConstantResult}, + {Credo.Check.Warning.OperationWithConstantResult} ] } ] diff --git a/config/config.exs b/config/config.exs index d44877c..c76785e 100644 --- a/config/config.exs +++ b/config/config.exs @@ -3,4 +3,4 @@ use Mix.Config config :logger, backends: [:console] -import_config "#{Mix.env}.exs" +import_config "#{Mix.env()}.exs" diff --git a/lib/ex_phone_number.ex b/lib/ex_phone_number.ex index cc62d75..4ea3652 100644 --- a/lib/ex_phone_number.ex +++ b/lib/ex_phone_number.ex @@ -1,13 +1,20 @@ defmodule ExPhoneNumber do - def format(%ExPhoneNumber.Model.PhoneNumber{} = phone_number, phone_number_format) when is_atom(phone_number_format), do: ExPhoneNumber.Formatting.format(phone_number, phone_number_format) + def format(%ExPhoneNumber.Model.PhoneNumber{} = phone_number, phone_number_format) + when is_atom(phone_number_format), + do: ExPhoneNumber.Formatting.format(phone_number, phone_number_format) - def get_number_type(%ExPhoneNumber.Model.PhoneNumber{} = phone_number), do: ExPhoneNumber.Validation.get_number_type(phone_number) + def get_number_type(%ExPhoneNumber.Model.PhoneNumber{} = phone_number), + do: ExPhoneNumber.Validation.get_number_type(phone_number) - def is_possible_number?(%ExPhoneNumber.Model.PhoneNumber{} = phone_number), do: ExPhoneNumber.Validation.is_possible_number?(phone_number) + def is_possible_number?(%ExPhoneNumber.Model.PhoneNumber{} = phone_number), + do: ExPhoneNumber.Validation.is_possible_number?(phone_number) - def is_possible_number?(number, region_code) when is_binary(number), do: ExPhoneNumber.Parsing.is_possible_number?(number, region_code) + def is_possible_number?(number, region_code) when is_binary(number), + do: ExPhoneNumber.Parsing.is_possible_number?(number, region_code) - def is_valid_number?(%ExPhoneNumber.Model.PhoneNumber{} = phone_number), do: ExPhoneNumber.Validation.is_valid_number?(phone_number) + def is_valid_number?(%ExPhoneNumber.Model.PhoneNumber{} = phone_number), + do: ExPhoneNumber.Validation.is_valid_number?(phone_number) - def parse(number_to_parse, default_region), do: ExPhoneNumber.Parsing.parse(number_to_parse, default_region) + def parse(number_to_parse, default_region), + do: ExPhoneNumber.Parsing.parse(number_to_parse, default_region) end diff --git a/lib/ex_phone_number/constants/mappings.ex b/lib/ex_phone_number/constants/mappings.ex index 5e3b370..10c3134 100644 --- a/lib/ex_phone_number/constants/mappings.ex +++ b/lib/ex_phone_number/constants/mappings.ex @@ -3,46 +3,76 @@ defmodule ExPhoneNumber.Constants.Mappings do def digit_mappings() do %{ - "0" => "0", - "1" => "1", - "2" => "2", - "3" => "3", - "4" => "4", - "5" => "5", - "6" => "6", - "7" => "7", - "8" => "8", - "9" => "9", - "\uFF10" => "0", # Fullwidth digit 0 - "\uFF11" => "1", # Fullwidth digit 1 - "\uFF12" => "2", # Fullwidth digit 2 - "\uFF13" => "3", # Fullwidth digit 3 - "\uFF14" => "4", # Fullwidth digit 4 - "\uFF15" => "5", # Fullwidth digit 5 - "\uFF16" => "6", # Fullwidth digit 6 - "\uFF17" => "7", # Fullwidth digit 7 - "\uFF18" => "8", # Fullwidth digit 8 - "\uFF19" => "9", # Fullwidth digit 9 - "\u0660" => "0", # Arabic-indic digit 0 - "\u0661" => "1", # Arabic-indic digit 1 - "\u0662" => "2", # Arabic-indic digit 2 - "\u0663" => "3", # Arabic-indic digit 3 - "\u0664" => "4", # Arabic-indic digit 4 - "\u0665" => "5", # Arabic-indic digit 5 - "\u0666" => "6", # Arabic-indic digit 6 - "\u0667" => "7", # Arabic-indic digit 7 - "\u0668" => "8", # Arabic-indic digit 8 - "\u0669" => "9", # Arabic-indic digit 9 - "\u06F0" => "0", # Eastern-Arabic digit 0 - "\u06F1" => "1", # Eastern-Arabic digit 1 - "\u06F2" => "2", # Eastern-Arabic digit 2 - "\u06F3" => "3", # Eastern-Arabic digit 3 - "\u06F4" => "4", # Eastern-Arabic digit 4 - "\u06F5" => "5", # Eastern-Arabic digit 5 - "\u06F6" => "6", # Eastern-Arabic digit 6 - "\u06F7" => "7", # Eastern-Arabic digit 7 - "\u06F8" => "8", # Eastern-Arabic digit 8 - "\u06F9" => "9" # Eastern-Arabic digit 9 + "0" => "0", + "1" => "1", + "2" => "2", + "3" => "3", + "4" => "4", + "5" => "5", + "6" => "6", + "7" => "7", + "8" => "8", + "9" => "9", + # Fullwidth digit 0 + "\uFF10" => "0", + # Fullwidth digit 1 + "\uFF11" => "1", + # Fullwidth digit 2 + "\uFF12" => "2", + # Fullwidth digit 3 + "\uFF13" => "3", + # Fullwidth digit 4 + "\uFF14" => "4", + # Fullwidth digit 5 + "\uFF15" => "5", + # Fullwidth digit 6 + "\uFF16" => "6", + # Fullwidth digit 7 + "\uFF17" => "7", + # Fullwidth digit 8 + "\uFF18" => "8", + # Fullwidth digit 9 + "\uFF19" => "9", + # Arabic-indic digit 0 + "\u0660" => "0", + # Arabic-indic digit 1 + "\u0661" => "1", + # Arabic-indic digit 2 + "\u0662" => "2", + # Arabic-indic digit 3 + "\u0663" => "3", + # Arabic-indic digit 4 + "\u0664" => "4", + # Arabic-indic digit 5 + "\u0665" => "5", + # Arabic-indic digit 6 + "\u0666" => "6", + # Arabic-indic digit 7 + "\u0667" => "7", + # Arabic-indic digit 8 + "\u0668" => "8", + # Arabic-indic digit 9 + "\u0669" => "9", + # Eastern-Arabic digit 0 + "\u06F0" => "0", + # Eastern-Arabic digit 1 + "\u06F1" => "1", + # Eastern-Arabic digit 2 + "\u06F2" => "2", + # Eastern-Arabic digit 3 + "\u06F3" => "3", + # Eastern-Arabic digit 4 + "\u06F4" => "4", + # Eastern-Arabic digit 5 + "\u06F5" => "5", + # Eastern-Arabic digit 6 + "\u06F6" => "6", + # Eastern-Arabic digit 7 + "\u06F7" => "7", + # Eastern-Arabic digit 8 + "\u06F8" => "8", + # Eastern-Arabic digit 9 + "\u06F9" => "9" } end @@ -58,9 +88,9 @@ defmodule ExPhoneNumber.Constants.Mappings do "7" => "7", "8" => "8", "9" => "9", - "+" => Values.plus_sign, + "+" => Values.plus_sign(), "*" => "*" - }; + } end def alpha_mappings() do @@ -96,140 +126,170 @@ defmodule ExPhoneNumber.Constants.Mappings do def all_normalization_mappings() do %{ - "0" => "0", - "1" => "1", - "2" => "2", - "3" => "3", - "4" => "4", - "5" => "5", - "6" => "6", - "7" => "7", - "8" => "8", - "9" => "9", - "\uFF10" => "0", # Fullwidth digit 0 - "\uFF11" => "1", # Fullwidth digit 1 - "\uFF12" => "2", # Fullwidth digit 2 - "\uFF13" => "3", # Fullwidth digit 3 - "\uFF14" => "4", # Fullwidth digit 4 - "\uFF15" => "5", # Fullwidth digit 5 - "\uFF16" => "6", # Fullwidth digit 6 - "\uFF17" => "7", # Fullwidth digit 7 - "\uFF18" => "8", # Fullwidth digit 8 - "\uFF19" => "9", # Fullwidth digit 9 - "\u0660" => "0", # Arabic-indic digit 0 - "\u0661" => "1", # Arabic-indic digit 1 - "\u0662" => "2", # Arabic-indic digit 2 - "\u0663" => "3", # Arabic-indic digit 3 - "\u0664" => "4", # Arabic-indic digit 4 - "\u0665" => "5", # Arabic-indic digit 5 - "\u0666" => "6", # Arabic-indic digit 6 - "\u0667" => "7", # Arabic-indic digit 7 - "\u0668" => "8", # Arabic-indic digit 8 - "\u0669" => "9", # Arabic-indic digit 9 - "\u06F0" => "0", # Eastern-Arabic digit 0 - "\u06F1" => "1", # Eastern-Arabic digit 1 - "\u06F2" => "2", # Eastern-Arabic digit 2 - "\u06F3" => "3", # Eastern-Arabic digit 3 - "\u06F4" => "4", # Eastern-Arabic digit 4 - "\u06F5" => "5", # Eastern-Arabic digit 5 - "\u06F6" => "6", # Eastern-Arabic digit 6 - "\u06F7" => "7", # Eastern-Arabic digit 7 - "\u06F8" => "8", # Eastern-Arabic digit 8 - "\u06F9" => "9", # Eastern-Arabic digit 9 - "A" => "2", - "B" => "2", - "C" => "2", - "D" => "3", - "E" => "3", - "F" => "3", - "G" => "4", - "H" => "4", - "I" => "4", - "J" => "5", - "K" => "5", - "L" => "5", - "M" => "6", - "N" => "6", - "O" => "6", - "P" => "7", - "Q" => "7", - "R" => "7", - "S" => "7", - "T" => "8", - "U" => "8", - "V" => "8", - "W" => "9", - "X" => "9", - "Y" => "9", - "Z" => "9" + "0" => "0", + "1" => "1", + "2" => "2", + "3" => "3", + "4" => "4", + "5" => "5", + "6" => "6", + "7" => "7", + "8" => "8", + "9" => "9", + # Fullwidth digit 0 + "\uFF10" => "0", + # Fullwidth digit 1 + "\uFF11" => "1", + # Fullwidth digit 2 + "\uFF12" => "2", + # Fullwidth digit 3 + "\uFF13" => "3", + # Fullwidth digit 4 + "\uFF14" => "4", + # Fullwidth digit 5 + "\uFF15" => "5", + # Fullwidth digit 6 + "\uFF16" => "6", + # Fullwidth digit 7 + "\uFF17" => "7", + # Fullwidth digit 8 + "\uFF18" => "8", + # Fullwidth digit 9 + "\uFF19" => "9", + # Arabic-indic digit 0 + "\u0660" => "0", + # Arabic-indic digit 1 + "\u0661" => "1", + # Arabic-indic digit 2 + "\u0662" => "2", + # Arabic-indic digit 3 + "\u0663" => "3", + # Arabic-indic digit 4 + "\u0664" => "4", + # Arabic-indic digit 5 + "\u0665" => "5", + # Arabic-indic digit 6 + "\u0666" => "6", + # Arabic-indic digit 7 + "\u0667" => "7", + # Arabic-indic digit 8 + "\u0668" => "8", + # Arabic-indic digit 9 + "\u0669" => "9", + # Eastern-Arabic digit 0 + "\u06F0" => "0", + # Eastern-Arabic digit 1 + "\u06F1" => "1", + # Eastern-Arabic digit 2 + "\u06F2" => "2", + # Eastern-Arabic digit 3 + "\u06F3" => "3", + # Eastern-Arabic digit 4 + "\u06F4" => "4", + # Eastern-Arabic digit 5 + "\u06F5" => "5", + # Eastern-Arabic digit 6 + "\u06F6" => "6", + # Eastern-Arabic digit 7 + "\u06F7" => "7", + # Eastern-Arabic digit 8 + "\u06F8" => "8", + # Eastern-Arabic digit 9 + "\u06F9" => "9", + "A" => "2", + "B" => "2", + "C" => "2", + "D" => "3", + "E" => "3", + "F" => "3", + "G" => "4", + "H" => "4", + "I" => "4", + "J" => "5", + "K" => "5", + "L" => "5", + "M" => "6", + "N" => "6", + "O" => "6", + "P" => "7", + "Q" => "7", + "R" => "7", + "S" => "7", + "T" => "8", + "U" => "8", + "V" => "8", + "W" => "9", + "X" => "9", + "Y" => "9", + "Z" => "9" } end def all_plus_number_grouping_symbols() do %{ - "0" => "0", - "1" => "1", - "2" => "2", - "3" => "3", - "4" => "4", - "5" => "5", - "6" => "6", - "7" => "7", - "8" => "8", - "9" => "9", - "A" => "A", - "B" => "B", - "C" => "C", - "D" => "D", - "E" => "E", - "F" => "F", - "G" => "G", - "H" => "H", - "I" => "I", - "J" => "J", - "K" => "K", - "L" => "L", - "M" => "M", - "N" => "N", - "O" => "O", - "P" => "P", - "Q" => "Q", - "R" => "R", - "S" => "S", - "T" => "T", - "U" => "U", - "V" => "V", - "W" => "W", - "X" => "X", - "Y" => "Y", - "Z" => "Z", - "a" => "A", - "b" => "B", - "c" => "C", - "d" => "D", - "e" => "E", - "f" => "F", - "g" => "G", - "h" => "H", - "i" => "I", - "j" => "J", - "k" => "K", - "l" => "L", - "m" => "M", - "n" => "N", - "o" => "O", - "p" => "P", - "q" => "Q", - "r" => "R", - "s" => "S", - "t" => "T", - "u" => "U", - "v" => "V", - "w" => "W", - "x" => "X", - "y" => "Y", - "z" => "Z", - "-" => "-", + "0" => "0", + "1" => "1", + "2" => "2", + "3" => "3", + "4" => "4", + "5" => "5", + "6" => "6", + "7" => "7", + "8" => "8", + "9" => "9", + "A" => "A", + "B" => "B", + "C" => "C", + "D" => "D", + "E" => "E", + "F" => "F", + "G" => "G", + "H" => "H", + "I" => "I", + "J" => "J", + "K" => "K", + "L" => "L", + "M" => "M", + "N" => "N", + "O" => "O", + "P" => "P", + "Q" => "Q", + "R" => "R", + "S" => "S", + "T" => "T", + "U" => "U", + "V" => "V", + "W" => "W", + "X" => "X", + "Y" => "Y", + "Z" => "Z", + "a" => "A", + "b" => "B", + "c" => "C", + "d" => "D", + "e" => "E", + "f" => "F", + "g" => "G", + "h" => "H", + "i" => "I", + "j" => "J", + "k" => "K", + "l" => "L", + "m" => "M", + "n" => "N", + "o" => "O", + "p" => "P", + "q" => "Q", + "r" => "R", + "s" => "S", + "t" => "T", + "u" => "U", + "v" => "V", + "w" => "W", + "x" => "X", + "y" => "Y", + "z" => "Z", + "-" => "-", "\uFF0D" => "-", "\u2010" => "-", "\u2011" => "-", @@ -238,12 +298,12 @@ defmodule ExPhoneNumber.Constants.Mappings do "\u2014" => "-", "\u2015" => "-", "\u2212" => "-", - "/" => "/", + "/" => "/", "\uFF0F" => "/", - " " => " ", + " " => " ", "\u3000" => " ", "\u2060" => " ", - "." => ".", + "." => ".", "\uFF0E" => "." } end diff --git a/lib/ex_phone_number/constants/patterns.ex b/lib/ex_phone_number/constants/patterns.ex index 5c99dbd..ba17e84 100644 --- a/lib/ex_phone_number/constants/patterns.ex +++ b/lib/ex_phone_number/constants/patterns.ex @@ -34,15 +34,20 @@ defmodule ExPhoneNumber.Constants.Patterns do def valid_alpha_phone_pattern(), do: ~r/(?:.*?[A-Za-z]){3}.*/u - def min_length_phone_number_pattern(), do: "[" <> valid_digits() <> "]{" <> Integer.to_string(Values.min_length_for_nsn) <> "}" + def min_length_phone_number_pattern(), + do: "[" <> valid_digits() <> "]{" <> Integer.to_string(Values.min_length_for_nsn()) <> "}" def valid_phone_number() do - "[" <> plus_chars() <> "]*(?:[" <> + "[" <> + plus_chars() <> + "]*(?:[" <> valid_punctuation() <> - Values.star_sign <> "]*[" <> - valid_digits() <> "]){3,}[" <> + Values.star_sign() <> + "]*[" <> + valid_digits() <> + "]){3,}[" <> valid_punctuation() <> - Values.star_sign <> + Values.star_sign() <> valid_alpha() <> valid_digits() <> "]*" end @@ -52,14 +57,16 @@ defmodule ExPhoneNumber.Constants.Patterns do def capturing_extn_digits(), do: "([" <> valid_digits() <> "]{1,7})" def extn_patterns_for_parsing() do - Values.rfc3966_extn_prefix <> - capturing_extn_digits() <> "|" <> - "[ \u00A0\\t,]*" <> - "(?:e?xt(?:ensi(?:o\u0301?|\u00F3))?n?|\uFF45?\uFF58\uFF54\uFF4E?|" <> - "[,x\uFF58#\uFF03~\uFF5E]|int|anexo|\uFF49\uFF4E\uFF54)" <> - "[:\\.\uFF0E]?[ \u00A0\\t,-]*" <> - capturing_extn_digits() <> "#?|" <> - "[- ]+([" <> valid_digits() <> "]{1,5})#" + Values.rfc3966_extn_prefix() <> + capturing_extn_digits() <> + "|" <> + "[ \u00A0\\t,]*" <> + "(?:e?xt(?:ensi(?:o\u0301?|\u00F3))?n?|\uFF45?\uFF58\uFF54\uFF4E?|" <> + "[,x\uFF58#\uFF03~\uFF5E]|int|anexo|\uFF49\uFF4E\uFF54)" <> + "[:\\.\uFF0E]?[ \u00A0\\t,-]*" <> + capturing_extn_digits() <> + "#?|" <> + "[- ]+([" <> valid_digits() <> "]{1,5})#" end def extn_pattern(), do: ~r/(?:#{extn_patterns_for_parsing()})$/iu diff --git a/lib/ex_phone_number/extraction.ex b/lib/ex_phone_number/extraction.ex index f98ead7..5b2bcb8 100644 --- a/lib/ex_phone_number/extraction.ex +++ b/lib/ex_phone_number/extraction.ex @@ -18,11 +18,13 @@ defmodule ExPhoneNumber.Extraction do extract_country_code(1, full_number) end end + def extract_country_code(index, full_number) when is_number(index) and is_binary(full_number) do - if not(index <= Values.max_length_country_code and index <= String.length(full_number)) do + if not (index <= Values.max_length_country_code() and index <= String.length(full_number)) do {0, ""} else split = String.split_at(full_number, index) + case Integer.parse(elem(split, 0)) do {int, _bin} -> if Metadata.is_valid_country_code?(int) do @@ -30,51 +32,81 @@ defmodule ExPhoneNumber.Extraction do else extract_country_code(index + 1, full_number) end - :error -> {0, ""} + + :error -> + {0, ""} end end end def extract_possible_number(number_to_parse) do - case Regex.run(Patterns.valid_start_char_pattern, number_to_parse, return: :index) do + case Regex.run(Patterns.valid_start_char_pattern(), number_to_parse, return: :index) do [{index, _length} | _tail] -> {_head, tail} = String.split_at(number_to_parse, index) + tail - |> split_at_match_and_return_head(Patterns.unwanted_end_char_pattern) - |> split_at_match_and_return_head(Patterns.second_number_start_pattern) - nil -> "" + |> split_at_match_and_return_head(Patterns.unwanted_end_char_pattern()) + |> split_at_match_and_return_head(Patterns.second_number_start_pattern()) + + nil -> + "" end end - def maybe_extract_country_code(number, _metadata, _keep_raw_input) when length(number) == 0, do: {false, number, %{country_code: 0}} - def maybe_extract_country_code(number, metadata, keep_raw_input) when is_binary(number) and is_boolean(keep_raw_input) do + def maybe_extract_country_code(number, _metadata, _keep_raw_input) when length(number) == 0, + do: {false, number, %{country_code: 0}} + + def maybe_extract_country_code(number, metadata, keep_raw_input) + when is_binary(number) and is_boolean(keep_raw_input) do possible_country_idd_prefix = if not is_nil(metadata), do: metadata.international_prefix + possible_country_idd_prefix = if is_nil(possible_country_idd_prefix), do: "NonMatch", else: possible_country_idd_prefix + {country_code_source, full_number} = maybe_strip_international_prefix_and_normalize(number, possible_country_idd_prefix) - phone_number = if keep_raw_input, do: %PhoneNumber{country_code_source: country_code_source}, else: %PhoneNumber{} - if country_code_source != CountryCodeSource.from_default_country do - if String.length(full_number) <= Values.min_length_for_nsn do - {false, ErrorMessages.too_short_after_idd} + + phone_number = + if keep_raw_input, + do: %PhoneNumber{country_code_source: country_code_source}, + else: %PhoneNumber{} + + if country_code_source != CountryCodeSource.from_default_country() do + if String.length(full_number) <= Values.min_length_for_nsn() do + {false, ErrorMessages.too_short_after_idd()} else {country_code, national_number} = extract_country_code(full_number) + if country_code != 0 do {true, national_number, %{phone_number | country_code: country_code}} else - {false, ErrorMessages.invalid_country_code} + {false, ErrorMessages.invalid_country_code()} end end else if not is_nil(metadata) do country_code_string = Integer.to_string(metadata.country_code) + if String.starts_with?(full_number, country_code_string) do {_, potential_national_number} = String.split_at(full_number, String.length(country_code_string)) + {result, _, possible_national_number} = maybe_strip_national_prefix_and_carrier_code(potential_national_number, metadata) + potential_national_number = if result, do: possible_national_number, else: potential_national_number + if (not matches_entirely?(metadata.general.national_number_pattern, full_number) and - matches_entirely?(metadata.general.national_number_pattern, potential_national_number)) - or test_number_length(full_number, metadata) == ValidationResults.too_long do - phone_number = if keep_raw_input, do: %{phone_number | country_code: metadata.country_code, country_code_source: CountryCodeSource.from_number_without_plus_sign}, - else: phone_number + matches_entirely?( + metadata.general.national_number_pattern, + potential_national_number + )) or + test_number_length(full_number, metadata) == ValidationResults.too_long() do + phone_number = + if keep_raw_input, + do: %{ + phone_number + | country_code: metadata.country_code, + country_code_source: CountryCodeSource.from_number_without_plus_sign() + }, + else: phone_number + {true, potential_national_number, %{phone_number | country_code: metadata.country_code}} else {true, full_number, %{phone_number | country_code: 0}} @@ -89,55 +121,87 @@ defmodule ExPhoneNumber.Extraction do end def maybe_strip_extension(number) do - case Regex.run(Patterns.extn_pattern, number, return: :index) do + case Regex.run(Patterns.extn_pattern(), number, return: :index) do [{index, _} | tail] -> {phone_number_head, _} = String.split_at(number, index) + if is_viable_phone_number?(phone_number_head) do - {match_index, match_length} = Enum.find(tail, fn {match_index, match_length} -> - if match_index > 0 do - match = Kernel.binary_part(number, match_index, match_length) - match != "" - else - false - end - end) + {match_index, match_length} = + Enum.find(tail, fn {match_index, match_length} -> + if match_index > 0 do + match = Kernel.binary_part(number, match_index, match_length) + match != "" + else + false + end + end) + ext = Kernel.binary_part(number, match_index, match_length) {ext, phone_number_head} else {"", number} end - nil -> {"", number} + + nil -> + {"", number} end end - def maybe_strip_international_prefix_and_normalize(number, _possible_country_idd_prefix) when length(number) == 0, do: {CountryCodeSource.from_default_country, ""} + def maybe_strip_international_prefix_and_normalize(number, _possible_country_idd_prefix) + when length(number) == 0, + do: {CountryCodeSource.from_default_country(), ""} + def maybe_strip_international_prefix_and_normalize(number, possible_country_idd_prefix) do - if Regex.match?(Patterns.leading_plus_chars_pattern, number) do - stripped_plus_sign = Regex.replace(Patterns.leading_plus_chars_pattern, number, "") - {CountryCodeSource.from_number_with_plus_sign, normalize(stripped_plus_sign)} + if Regex.match?(Patterns.leading_plus_chars_pattern(), number) do + stripped_plus_sign = Regex.replace(Patterns.leading_plus_chars_pattern(), number, "") + {CountryCodeSource.from_number_with_plus_sign(), normalize(stripped_plus_sign)} else case Regex.compile(possible_country_idd_prefix) do - {:error, _} -> {CountryCodeSource.from_default_country, ""} + {:error, _} -> + {CountryCodeSource.from_default_country(), ""} + {:ok, regex} -> normalized_number = normalize(number) + case parse_prefix_as_idd(regex, normalized_number) do - {false, _} -> {CountryCodeSource.from_default_country, normalized_number} - {true, stripped_idd} -> {CountryCodeSource.from_number_with_idd, stripped_idd} + {false, _} -> {CountryCodeSource.from_default_country(), normalized_number} + {true, stripped_idd} -> {CountryCodeSource.from_number_with_idd(), stripped_idd} end end end end def maybe_strip_national_prefix_and_carrier_code(number, %PhoneMetadata{} = metadata) do - maybe_strip_national_prefix_and_carrier_code(number, metadata.national_prefix_for_parsing, metadata.national_prefix_transform_rule, metadata.general.national_number_pattern) + maybe_strip_national_prefix_and_carrier_code( + number, + metadata.national_prefix_for_parsing, + metadata.national_prefix_transform_rule, + metadata.general.national_number_pattern + ) end - def maybe_strip_national_prefix_and_carrier_code(number, national_prefix_for_parsing, _national_prefix_transform_rule, _general_national_number_pattern) - when is_nil(number) or is_nil(national_prefix_for_parsing), do: {false, "", number} - def maybe_strip_national_prefix_and_carrier_code(number, national_prefix_for_parsing, national_prefix_transform_rule, general_national_number_pattern) - when is_binary(number) and is_binary(national_prefix_for_parsing) do + + def maybe_strip_national_prefix_and_carrier_code( + number, + national_prefix_for_parsing, + _national_prefix_transform_rule, + _general_national_number_pattern + ) + when is_nil(number) or is_nil(national_prefix_for_parsing), + do: {false, "", number} + + def maybe_strip_national_prefix_and_carrier_code( + number, + national_prefix_for_parsing, + national_prefix_transform_rule, + general_national_number_pattern + ) + when is_binary(number) and is_binary(national_prefix_for_parsing) do prefix_pattern = ~r/^(?:#{national_prefix_for_parsing})/ + case Regex.run(prefix_pattern, number) do - nil -> {false, "", number} + nil -> + {false, "", number} + matches -> is_viable_original_number = matches_entirely?(general_national_number_pattern, number) number_of_groups = if length(matches) == 1, do: 1, else: length(matches) - 1 @@ -146,16 +210,23 @@ defmodule ExPhoneNumber.Extraction do is_nil_transform_rule = is_nil_or_empty?(national_prefix_transform_rule) is_nil_match = is_nil_or_empty?(match) no_transform = is_nil_transform_rule or is_nil_match + if no_transform do - if is_viable_original_number and not matches_entirely?(general_national_number_pattern, number_stripped) do + if is_viable_original_number and + not matches_entirely?(general_national_number_pattern, number_stripped) do {false, "", number} else - carrier_code = if length(matches) > 0 and not is_nil(Enum.at(matches, number_of_groups)), do: Enum.at(matches, 1) + carrier_code = + if length(matches) > 0 and not is_nil(Enum.at(matches, number_of_groups)), + do: Enum.at(matches, 1) + {true, carrier_code, number_stripped} end else transformed_number = Regex.replace(prefix_pattern, number, national_prefix_transform_rule) - if is_viable_original_number and not matches_entirely?(general_national_number_pattern, transformed_number) do + + if is_viable_original_number and + not matches_entirely?(general_national_number_pattern, transformed_number) do {false, "", number} else carrier_code = if length(matches) > 0, do: Enum.at(matches, 1) @@ -170,10 +241,11 @@ defmodule ExPhoneNumber.Extraction do [{index, match_length} | _tail] -> if index == 0 do {_, number_tail} = String.split_at(number, match_length) - matches = Regex.run(Patterns.capturing_digit_pattern, number_tail) + matches = Regex.run(Patterns.capturing_digit_pattern(), number_tail) match_is_nil = is_nil(matches) capture = if match_is_nil, do: "", else: Enum.at(matches, 1) normalized_group = normalize_digits_only(capture) + if normalized_group == "0" do {false, number} else @@ -182,7 +254,9 @@ defmodule ExPhoneNumber.Extraction do else {false, number} end - nil -> {false, number} + + nil -> + {false, number} end end end diff --git a/lib/ex_phone_number/formatting.ex b/lib/ex_phone_number/formatting.ex index 3740b62..d44a24a 100644 --- a/lib/ex_phone_number/formatting.ex +++ b/lib/ex_phone_number/formatting.ex @@ -8,51 +8,75 @@ defmodule ExPhoneNumber.Formatting do alias ExPhoneNumber.Model.PhoneNumber def choose_formatting_pattern_for_number(available_formats, national_number) do - Enum.find(available_formats, fn(number_format) -> - leading_digits_pattern_size = length(number_format.leading_digits_pattern) - match_index = if not (leading_digits_pattern_size == 0) do + Enum.find(available_formats, fn number_format -> + leading_digits_pattern_size = length(number_format.leading_digits_pattern) + + match_index = + if not (leading_digits_pattern_size == 0) do last_leading_digits_pattern = List.last(number_format.leading_digits_pattern) + case Regex.run(last_leading_digits_pattern, national_number, return: :index) do - nil -> - 1 + nil -> -1 [{match_index, _match_length} | _tail] -> match_index end else - - 1 - end - if leading_digits_pattern_size == 0 or match_index == 0 do - matches_entirely?(number_format.pattern, national_number) + -1 end + + if leading_digits_pattern_size == 0 or match_index == 0 do + matches_entirely?(number_format.pattern, national_number) + end end) end - def format(%PhoneNumber{} = phone_number, phone_number_format) when is_atom(phone_number_format) do + def format(%PhoneNumber{} = phone_number, phone_number_format) + when is_atom(phone_number_format) do if phone_number.national_number == 0 and not is_nil_or_empty?(phone_number.raw_input) do phone_number.raw_input else national_significant_number = PhoneNumber.get_national_significant_number(phone_number) - if PhoneNumberFormats.e164 == phone_number_format do - prefix_number_with_country_calling_code(phone_number.country_code, phone_number_format, national_significant_number, "") + + if PhoneNumberFormats.e164() == phone_number_format do + prefix_number_with_country_calling_code( + phone_number.country_code, + phone_number_format, + national_significant_number, + "" + ) else if not Metadata.is_valid_country_code?(phone_number.country_code) do national_significant_number else region_code = Metadata.get_region_code_for_country_code(phone_number.country_code) + metadata = Metadata.get_for_region_code_or_calling_code(phone_number.country_code, region_code) + formatted_extension = maybe_get_formatted_extension(phone_number, metadata, phone_number_format) + formatted_national_number = format_nsn(national_significant_number, metadata, phone_number_format) - prefix_number_with_country_calling_code(phone_number.country_code, phone_number_format, formatted_national_number, formatted_extension) + + prefix_number_with_country_calling_code( + phone_number.country_code, + phone_number_format, + formatted_national_number, + formatted_extension + ) end end end end def format_nsn(number, %PhoneMetadata{} = metadata, number_format, carrier_code \\ nil) do - available_formats = if length(metadata.intl_number_format) == 0 or PhoneNumberFormats.national == number_format do - metadata.number_format - else - metadata.intl_number_format - end + available_formats = + if length(metadata.intl_number_format) == 0 or + PhoneNumberFormats.national() == number_format do + metadata.number_format + else + metadata.intl_number_format + end + formatting_pattern = choose_formatting_pattern_for_number(available_formats, number) + if is_nil(formatting_pattern) do number else @@ -62,51 +86,98 @@ defmodule ExPhoneNumber.Formatting do def format_nsn_using_pattern(number, formatting_pattern, phone_number_format, carrier_code) do formatted_national_number = - if PhoneNumberFormats.national == phone_number_format - and not is_nil_or_empty?(carrier_code) and not is_nil_or_empty?(formatting_pattern.domestic_carrier_code_formatting_rule) do - carrier_code_rule = Regex.replace(Patterns.cc_pattern, formatting_pattern.domestic_carrier_code_formatting_rule, carrier_code) - number_rule = Regex.replace(Patterns.first_group_pattern, formatting_pattern.format, carrier_code_rule) + if PhoneNumberFormats.national() == phone_number_format and + not is_nil_or_empty?(carrier_code) and + not is_nil_or_empty?(formatting_pattern.domestic_carrier_code_formatting_rule) do + carrier_code_rule = + Regex.replace( + Patterns.cc_pattern(), + formatting_pattern.domestic_carrier_code_formatting_rule, + carrier_code + ) + + number_rule = + Regex.replace( + Patterns.first_group_pattern(), + formatting_pattern.format, + carrier_code_rule + ) + Regex.replace(formatting_pattern.pattern, number, number_rule) else - if PhoneNumberFormats.national == phone_number_format and not is_nil_or_empty?(formatting_pattern.national_prefix_formatting_rule) do - number_rule = Regex.replace(Patterns.first_group_pattern, formatting_pattern.format, formatting_pattern.national_prefix_formatting_rule, global: false) + if PhoneNumberFormats.national() == phone_number_format and + not is_nil_or_empty?(formatting_pattern.national_prefix_formatting_rule) do + number_rule = + Regex.replace( + Patterns.first_group_pattern(), + formatting_pattern.format, + formatting_pattern.national_prefix_formatting_rule, + global: false + ) + Regex.replace(formatting_pattern.pattern, number, number_rule) else Regex.replace(formatting_pattern.pattern, number, formatting_pattern.format) end end - if PhoneNumberFormats.rfc3966 == phone_number_format do - new_formatted_national_number = Regex.replace(~r/^#{Patterns.separator_pattern.source}/, formatted_national_number, "") - Regex.replace(Patterns.separator_pattern, new_formatted_national_number, "-") + + if PhoneNumberFormats.rfc3966() == phone_number_format do + new_formatted_national_number = Regex.replace(~r/^#{Patterns.separator_pattern().source}/, formatted_national_number, "") + + Regex.replace(Patterns.separator_pattern(), new_formatted_national_number, "-") else formatted_national_number end end - def maybe_get_formatted_extension(%PhoneNumber{} = phone_number, %PhoneMetadata{} = metadata, phone_number_format) when is_atom(phone_number_format) do + def maybe_get_formatted_extension( + %PhoneNumber{} = phone_number, + %PhoneMetadata{} = metadata, + phone_number_format + ) + when is_atom(phone_number_format) do if not PhoneNumber.has_extension?(phone_number) or String.length(phone_number.extension) == 0 do "" else - if PhoneNumberFormats.rfc3966 == phone_number_format do - Values.rfc3966_extn_prefix <> phone_number.extension + if PhoneNumberFormats.rfc3966() == phone_number_format do + Values.rfc3966_extn_prefix() <> phone_number.extension else if PhoneMetadata.has_preferred_extn_prefix?(metadata) do metadata.preferred_extn_prefix <> phone_number.extension else - Values.default_extn_prefix <> phone_number.extension + Values.default_extn_prefix() <> phone_number.extension end end end end - def prefix_number_with_country_calling_code(country_code, phone_number_format, formatted_national_number, formatted_extension) do + def prefix_number_with_country_calling_code( + country_code, + phone_number_format, + formatted_national_number, + formatted_extension + ) do country_code_string = Integer.to_string(country_code) + cond do - PhoneNumberFormats.e164 == phone_number_format -> Values.plus_sign <> country_code_string <> formatted_national_number <> formatted_extension - PhoneNumberFormats.international == phone_number_format -> Values.plus_sign <> country_code_string <> " " <> formatted_national_number <> formatted_extension - PhoneNumberFormats.rfc3966 == phone_number_format -> Values.rfc3966_prefix <> Values.plus_sign <> country_code_string <> "-" <> formatted_national_number <> formatted_extension - PhoneNumberFormats.national == phone_number_format -> formatted_national_number <> formatted_extension - true -> formatted_national_number <> formatted_extension + PhoneNumberFormats.e164() == phone_number_format -> + Values.plus_sign() <> + country_code_string <> formatted_national_number <> formatted_extension + + PhoneNumberFormats.international() == phone_number_format -> + Values.plus_sign() <> + country_code_string <> " " <> formatted_national_number <> formatted_extension + + PhoneNumberFormats.rfc3966() == phone_number_format -> + Values.rfc3966_prefix() <> + Values.plus_sign() <> + country_code_string <> "-" <> formatted_national_number <> formatted_extension + + PhoneNumberFormats.national() == phone_number_format -> + formatted_national_number <> formatted_extension + + true -> + formatted_national_number <> formatted_extension end end end diff --git a/lib/ex_phone_number/metadata.ex b/lib/ex_phone_number/metadata.ex index a8ce519..dee992d 100644 --- a/lib/ex_phone_number/metadata.ex +++ b/lib/ex_phone_number/metadata.ex @@ -8,16 +8,20 @@ defmodule ExPhoneNumber.Metadata do alias ExPhoneNumber.Model.PhoneNumber @resources_dir "./resources" - @xml_file if Mix.env == :test, do: "PhoneNumberMetadataForTesting.xml", else: "PhoneNumberMetadata.xml" + @xml_file if Mix.env() == :test, + do: "PhoneNumberMetadataForTesting.xml", + else: "PhoneNumberMetadata.xml" @document_path Path.join([@resources_dir, @xml_file]) @external_resource @document_path document = File.read!(@document_path) + metadata_collection = - document |> xpath( + document + |> xpath( ~x"//phoneNumberMetadata/territories/territory"el, territory: ~x"." |> transform_by(&PhoneMetadata.from_xpath_node/1) - ) + ) Module.register_attribute(__MODULE__, :list_region_code_to_metadata, accumulate: true) Module.register_attribute(__MODULE__, :list_country_code_to_region_code, accumulate: true) @@ -30,29 +34,45 @@ defmodule ExPhoneNumber.Metadata do country_code = Map.get(phone_metadata, :country_code) country_code_atom = String.to_atom(Integer.to_string(country_code)) - Module.put_attribute(__MODULE__, :list_country_code_to_region_code, {country_code_atom, phone_metadata.id}) + + Module.put_attribute( + __MODULE__, + :list_country_code_to_region_code, + {country_code_atom, phone_metadata.id} + ) end list_cctrc = Module.get_attribute(__MODULE__, :list_country_code_to_region_code) uniq_keys_cctrc = Enum.uniq(Keyword.keys(list_cctrc)) - map_cctrc = Enum.reduce(uniq_keys_cctrc, %{}, fn(key, acc) -> - {new_key, _} = Integer.parse(Atom.to_string(key)) - Map.put(acc, new_key, Keyword.get_values(list_cctrc, key)) - end) + + map_cctrc = + Enum.reduce(uniq_keys_cctrc, %{}, fn key, acc -> + {new_key, _} = Integer.parse(Atom.to_string(key)) + Map.put(acc, new_key, Keyword.get_values(list_cctrc, key)) + end) + defp country_code_to_region_code_map() do unquote(Macro.escape(map_cctrc)) end + Module.delete_attribute(__MODULE__, :list_country_code_to_region_code) list_rctm = Module.get_attribute(__MODULE__, :list_region_code_to_metadata) uniq_keys_rctm = Enum.uniq(Keyword.keys(list_rctm)) - map_rctm = Enum.reduce(uniq_keys_rctm, %{}, fn(key, acc) -> Map.put(acc, Atom.to_string(key), Keyword.get(list_rctm, key)) end) + + map_rctm = + Enum.reduce(uniq_keys_rctm, %{}, fn key, acc -> + Map.put(acc, Atom.to_string(key), Keyword.get(list_rctm, key)) + end) + defp region_code_to_metadata_map() do unquote(Macro.escape(map_rctm)) end + Module.delete_attribute(__MODULE__, :list_region_code_to_metadata) def get_country_code_for_region_code(nil), do: 0 + def get_country_code_for_region_code(region_code) when is_binary(region_code) do if not is_valid_region_code?(region_code) do 0 @@ -63,6 +83,7 @@ defmodule ExPhoneNumber.Metadata do def get_country_code_for_valid_region(region_code) when is_binary(region_code) do metadata = get_for_region_code(region_code) + if metadata do metadata.country_code else @@ -70,25 +91,29 @@ defmodule ExPhoneNumber.Metadata do end end - def get_for_non_geographical_region(calling_code) when is_number(calling_code), do: get_for_non_geographical_region(Integer.to_string(calling_code)) + def get_for_non_geographical_region(calling_code) when is_number(calling_code), + do: get_for_non_geographical_region(Integer.to_string(calling_code)) + def get_for_non_geographical_region(region_code) when is_binary(region_code) do get_for_region_code(region_code) end def get_for_region_code(nil), do: nil + def get_for_region_code(region_code) do region_code_to_metadata_map()[String.upcase(region_code)] end def get_for_region_code_or_calling_code(calling_code, region_code) do - if region_code == Values.region_code_for_non_geo_entity do + if region_code == Values.region_code_for_non_geo_entity() do get_for_non_geographical_region(calling_code) else get_for_region_code(region_code) end end - def get_ndd_prefix_for_region_code(region_code, strip_non_digits) when is_binary(region_code) and is_boolean(strip_non_digits) do + def get_ndd_prefix_for_region_code(region_code, strip_non_digits) + when is_binary(region_code) and is_boolean(strip_non_digits) do if is_nil(metadata = get_for_region_code(region_code)) do nil else @@ -106,17 +131,21 @@ defmodule ExPhoneNumber.Metadata do def get_region_code_for_country_code(country_code) when is_number(country_code) do region_codes = country_code_to_region_code_map()[country_code] - if is_nil(region_codes) do - Values.unknown_region + + if is_nil(region_codes) do + Values.unknown_region() else - main_country = Enum.find(region_codes, fn(region_code) -> - metadata = region_code_to_metadata_map()[region_code] - if is_nil(metadata) do - false - else - metadata.main_country_for_code - end - end) + main_country = + Enum.find(region_codes, fn region_code -> + metadata = region_code_to_metadata_map()[region_code] + + if is_nil(metadata) do + false + else + metadata.main_country_for_code + end + end) + if is_nil(main_country) do Enum.at(Enum.reverse(region_codes), 0) else @@ -126,8 +155,10 @@ defmodule ExPhoneNumber.Metadata do end def get_region_code_for_number(nil), do: nil + def get_region_code_for_number(%PhoneNumber{} = phone_number) do regions = country_code_to_region_code_map()[phone_number.country_code] + if is_nil(regions) do nil else @@ -139,7 +170,8 @@ defmodule ExPhoneNumber.Metadata do end end - defp get_region_code_for_number_from_region_list(%PhoneNumber{} = phone_number, region_codes) when is_list(region_codes) do + defp get_region_code_for_number_from_region_list(%PhoneNumber{} = phone_number, region_codes) + when is_list(region_codes) do region_codes = if_gb_regions_ensure_gb_first(region_codes) national_number = PhoneNumber.get_national_significant_number(phone_number) find_matching_region_code(region_codes, national_number) @@ -158,59 +190,76 @@ defmodule ExPhoneNumber.Metadata do end def get_supported_regions() do - Enum.filter(Map.keys(region_code_to_metadata_map()), fn(key) -> Integer.parse(key) == :error end) + Enum.filter(Map.keys(region_code_to_metadata_map()), fn key -> + Integer.parse(key) == :error + end) end def get_supported_global_network_calling_codes() do - region_codes_as_strings = Enum.filter(Map.keys(region_code_to_metadata_map()), fn(key) -> Integer.parse(key) != :error end) - Enum.map(region_codes_as_strings, fn(calling_code) -> + region_codes_as_strings = + Enum.filter(Map.keys(region_code_to_metadata_map()), fn key -> + Integer.parse(key) != :error + end) + + Enum.map(region_codes_as_strings, fn calling_code -> {number, _} = Integer.parse(calling_code) number end) end def is_nanpa_country?(nil), do: false + def is_nanpa_country?(region_code) when is_binary(region_code) do - String.upcase(region_code) in country_code_to_region_code_map()[Values.nanpa_country_code] + String.upcase(region_code) in country_code_to_region_code_map()[Values.nanpa_country_code()] end def is_supported_global_network_calling_code?(calling_code) when is_number(calling_code) do not is_nil(region_code_to_metadata_map()[Integer.to_string(calling_code)]) end + def is_supported_global_network_calling_code?(_), do: false def is_supported_region?(region_code) when is_binary(region_code) do not is_nil(region_code_to_metadata_map()[String.upcase(region_code)]) end + def is_supported_region?(_), do: false def is_valid_country_code?(nil), do: false + def is_valid_country_code?(country_code) when is_number(country_code) do not is_nil(country_code_to_region_code_map()[country_code]) end def is_valid_region_code?(nil), do: false + def is_valid_region_code?(region_code) when is_binary(region_code) do - Integer.parse(region_code) == :error and not is_nil(region_code_to_metadata_map()[String.upcase(region_code)]) + Integer.parse(region_code) == :error and + not is_nil(region_code_to_metadata_map()[String.upcase(region_code)]) end defp find_matching_region_code([], _), do: nil + defp find_matching_region_code([head | tail], national_number) do region_code = find_matching_region_code(head, national_number) + if region_code do region_code else find_matching_region_code(tail, national_number) end end - defp find_matching_region_code(region_code, national_number) when is_binary(region_code) and is_binary(national_number) do + + defp find_matching_region_code(region_code, national_number) + when is_binary(region_code) and is_binary(national_number) do metadata = get_for_region_code(region_code) + if PhoneMetadata.has_leading_digits(metadata) do if match_at_start?(national_number, metadata.leading_digits) do region_code end else - if get_number_type_helper(national_number, metadata) != PhoneNumberTypes.unknown do + if get_number_type_helper(national_number, metadata) != PhoneNumberTypes.unknown() do region_code end end diff --git a/lib/ex_phone_number/metadata/number_format.ex b/lib/ex_phone_number/metadata/number_format.ex index b6bd96e..2e12abb 100644 --- a/lib/ex_phone_number/metadata/number_format.ex +++ b/lib/ex_phone_number/metadata/number_format.ex @@ -1,19 +1,28 @@ defmodule ExPhoneNumber.Metadata.NumberFormat do - defstruct pattern: nil, # string - format: nil, # string - leading_digits_pattern: nil, # string - national_prefix_formatting_rule: nil, # string - national_prefix_optional_when_formatting: nil, # boolean - domestic_carrier_code_formatting_rule: nil, # string - intl_format: nil # string + # string + defstruct pattern: nil, + # string + format: nil, + # string + leading_digits_pattern: nil, + # string + national_prefix_formatting_rule: nil, + # boolean + national_prefix_optional_when_formatting: nil, + # string + domestic_carrier_code_formatting_rule: nil, + # string + intl_format: nil import SweetXml alias ExPhoneNumber.Metadata.NumberFormat def from_xpath_node(nil), do: nil + def from_xpath_node(xpath_node) do kwlist = - xpath_node |> xmap( + xpath_node + |> xmap( pattern: ~x"./@pattern"s |> transform_by(&normalize_pattern/1), format: ~x"./format/text()"s |> transform_by(&normalize_rule/1), leading_digits_pattern: [ @@ -25,14 +34,19 @@ defmodule ExPhoneNumber.Metadata.NumberFormat do domestic_carrier_code_formatting_rule: ~x"./@carrierCodeFormattingRule"o |> transform_by(&normalize_string/1), intl_format: ~x"./intlFormat/text()"o |> transform_by(&normalize_rule/1) ) + struct(%NumberFormat{}, kwlist) end defp normalize_boolean(nil), do: nil - defp normalize_boolean(true_char_list) when is_list(true_char_list) and length(true_char_list) == 4, do: true + + defp normalize_boolean(true_char_list) + when is_list(true_char_list) and length(true_char_list) == 4, + do: true defp normalize_pattern(nil), do: nil defp normalize_pattern(string) when length(string) == 0, do: nil + defp normalize_pattern(string) when is_binary(string) do string |> String.split(["\n", " "], trim: true) @@ -41,13 +55,17 @@ defmodule ExPhoneNumber.Metadata.NumberFormat do end defp normalize_string(nil), do: nil + defp normalize_string(char_list) when is_list(char_list) do char_list |> List.to_string() end defp normalize_rule(nil), do: nil - defp normalize_rule(char_list) when is_list(char_list), do: char_list |> List.to_string() |> normalize_rule() + + defp normalize_rule(char_list) when is_list(char_list), + do: char_list |> List.to_string() |> normalize_rule() + defp normalize_rule(string) when is_binary(string) do string |> String.replace(~r/\$(\d)/, "\\\\g{\\g{1}}") diff --git a/lib/ex_phone_number/metadata/phone_metadata.ex b/lib/ex_phone_number/metadata/phone_metadata.ex index 0101144..e9d65e5 100644 --- a/lib/ex_phone_number/metadata/phone_metadata.ex +++ b/lib/ex_phone_number/metadata/phone_metadata.ex @@ -1,34 +1,64 @@ defmodule ExPhoneNumber.Metadata.PhoneMetadata do - defstruct id: nil, # string - country_code: nil, # number - leading_digits: nil, # string - international_prefix: nil, # string - preferred_international_prefix: nil, # string - national_prefix: nil, # string - national_prefix_for_parsing: nil, # string - national_prefix_transform_rule: nil, # string - national_prefix_formatting_rule: nil, # string - national_prefix_optional_when_formatting: nil, # string - preferred_extn_prefix: nil, # string - main_country_for_code: nil, # boolean - mobile_number_portable_region: nil, # boolean - carrier_code_formatting_rule: nil, # string - general: nil, # %PhoneNumberDescription{} - fixed_line: nil, # %PhoneNumberDescription{} - mobile: nil, # %PhoneNumberDescription{} - toll_free: nil, # %PhoneNumberDescription{} - premium_rate: nil, # %PhoneNumberDescription{} - shared_cost: nil, # %PhoneNumberDescription{} - personal_number: nil, # %PhoneNumberDescription{} - voip: nil, # %PhoneNumberDescription{} - pager: nil, # %PhoneNumberDescription{} - uan: nil, # %PhoneNumberDescription{} - voicemail: nil, # %PhoneNumberDescription{} - no_international_dialing: nil, # %PhoneNumberDescription{} - same_mobile_and_fixed_line_pattern: nil, # boolean - available_formats: [], # [%NumberFormat{}] - number_format: [], # [%NumberFormat{}] - intl_number_format: [] # [%NumberFormat{}] + # string + defstruct id: nil, + # number + country_code: nil, + # string + leading_digits: nil, + # string + international_prefix: nil, + # string + preferred_international_prefix: nil, + # string + national_prefix: nil, + # string + national_prefix_for_parsing: nil, + # string + national_prefix_transform_rule: nil, + # string + national_prefix_formatting_rule: nil, + # string + national_prefix_optional_when_formatting: nil, + # string + preferred_extn_prefix: nil, + # boolean + main_country_for_code: nil, + # boolean + mobile_number_portable_region: nil, + # string + carrier_code_formatting_rule: nil, + # %PhoneNumberDescription{} + general: nil, + # %PhoneNumberDescription{} + fixed_line: nil, + # %PhoneNumberDescription{} + mobile: nil, + # %PhoneNumberDescription{} + toll_free: nil, + # %PhoneNumberDescription{} + premium_rate: nil, + # %PhoneNumberDescription{} + shared_cost: nil, + # %PhoneNumberDescription{} + personal_number: nil, + # %PhoneNumberDescription{} + voip: nil, + # %PhoneNumberDescription{} + pager: nil, + # %PhoneNumberDescription{} + uan: nil, + # %PhoneNumberDescription{} + voicemail: nil, + # %PhoneNumberDescription{} + no_international_dialing: nil, + # boolean + same_mobile_and_fixed_line_pattern: nil, + # [%NumberFormat{}] + available_formats: [], + # [%NumberFormat{}] + number_format: [], + # [%NumberFormat{}] + intl_number_format: [] import SweetXml import ExPhoneNumber.Utilities @@ -44,81 +74,90 @@ defmodule ExPhoneNumber.Metadata.PhoneMetadata do kwlist = xpath_node |> xmap( - id: ~x"./@id"s, - country_code: ~x"./@countryCode"i, - leading_digits: ~x"./@leadingDigits"o |> transform_by(&normalize_pattern/1), - international_prefix: ~x"./@internationalPrefix"s, - preferred_international_prefix: ~x"./@preferredInternationalPrefix"o |> transform_by(&normalize_string/1), - national_prefix: ~x"./@nationalPrefix"s, - national_prefix_for_parsing: ~x"./@nationalPrefixForParsing"o |> transform_by(&normalize_string/1), - national_prefix_transform_rule: ~x"./@nationalPrefixTransformRule"o |> transform_by(&normalize_rule/1), - national_prefix_formatting_rule: ~x"./@nationalPrefixFormattingRule"s, - preferred_extn_prefix: ~x"./@preferredExtnPrefix"o |> transform_by(&normalize_string/1), - main_country_for_code: ~x"./@mainCountryForCode"o |> transform_by(&normalize_boolean/1), - mobile_number_portable_region: ~x"./@mobileNumberPortableRegion"o |> transform_by(&normalize_boolean/1), - carrier_code_formatting_rule: ~x"./@carrierCodeFormattingRule"s, - general: ~x"./generalDesc"e |> transform_by(&PhoneNumberDescription.from_xpath_node/1), - fixed_line: ~x"./fixedLine"e |> transform_by(&PhoneNumberDescription.from_xpath_node/1), - mobile: ~x"./mobile"e |> transform_by(&PhoneNumberDescription.from_xpath_node/1), - toll_free: ~x"./tollFree"o |> transform_by(&PhoneNumberDescription.from_xpath_node/1), - premium_rate: ~x"./premiumRate"e |> transform_by(&PhoneNumberDescription.from_xpath_node/1), - shared_cost: ~x"./sharedCost"e |> transform_by(&PhoneNumberDescription.from_xpath_node/1), - personal_number: ~x"./personalNumber"e |> transform_by(&PhoneNumberDescription.from_xpath_node/1), - voip: ~x"./voip"e |> transform_by(&PhoneNumberDescription.from_xpath_node/1), - pager: ~x"./pager"e |> transform_by(&PhoneNumberDescription.from_xpath_node/1), - uan: ~x"./uan"e |> transform_by(&PhoneNumberDescription.from_xpath_node/1), - voicemail: ~x"./voicemail"e |> transform_by(&PhoneNumberDescription.from_xpath_node/1), - no_international_dialing: ~x"./noInternationalDialling"e |> transform_by(&PhoneNumberDescription.from_xpath_node/1), - available_formats: [ - ~x"./availableFormats/numberFormat"el, - number_format: ~x"."e |> transform_by(&NumberFormat.from_xpath_node/1) - ] - ) - - kwlist = if(is_map(kwlist) && is_map(kwlist.general)) do - put_in(kwlist.general.possible_lengths, general_possible_lengths(kwlist)) - else - kwlist - end + id: ~x"./@id"s, + country_code: ~x"./@countryCode"i, + leading_digits: ~x"./@leadingDigits"o |> transform_by(&normalize_pattern/1), + international_prefix: ~x"./@internationalPrefix"s, + preferred_international_prefix: ~x"./@preferredInternationalPrefix"o |> transform_by(&normalize_string/1), + national_prefix: ~x"./@nationalPrefix"s, + national_prefix_for_parsing: ~x"./@nationalPrefixForParsing"o |> transform_by(&normalize_string/1), + national_prefix_transform_rule: ~x"./@nationalPrefixTransformRule"o |> transform_by(&normalize_rule/1), + national_prefix_formatting_rule: ~x"./@nationalPrefixFormattingRule"s, + preferred_extn_prefix: ~x"./@preferredExtnPrefix"o |> transform_by(&normalize_string/1), + main_country_for_code: ~x"./@mainCountryForCode"o |> transform_by(&normalize_boolean/1), + mobile_number_portable_region: ~x"./@mobileNumberPortableRegion"o |> transform_by(&normalize_boolean/1), + carrier_code_formatting_rule: ~x"./@carrierCodeFormattingRule"s, + general: ~x"./generalDesc"e |> transform_by(&PhoneNumberDescription.from_xpath_node/1), + fixed_line: ~x"./fixedLine"e |> transform_by(&PhoneNumberDescription.from_xpath_node/1), + mobile: ~x"./mobile"e |> transform_by(&PhoneNumberDescription.from_xpath_node/1), + toll_free: ~x"./tollFree"o |> transform_by(&PhoneNumberDescription.from_xpath_node/1), + premium_rate: ~x"./premiumRate"e |> transform_by(&PhoneNumberDescription.from_xpath_node/1), + shared_cost: ~x"./sharedCost"e |> transform_by(&PhoneNumberDescription.from_xpath_node/1), + personal_number: ~x"./personalNumber"e |> transform_by(&PhoneNumberDescription.from_xpath_node/1), + voip: ~x"./voip"e |> transform_by(&PhoneNumberDescription.from_xpath_node/1), + pager: ~x"./pager"e |> transform_by(&PhoneNumberDescription.from_xpath_node/1), + uan: ~x"./uan"e |> transform_by(&PhoneNumberDescription.from_xpath_node/1), + voicemail: ~x"./voicemail"e |> transform_by(&PhoneNumberDescription.from_xpath_node/1), + no_international_dialing: + ~x"./noInternationalDialling"e + |> transform_by(&PhoneNumberDescription.from_xpath_node/1), + available_formats: [ + ~x"./availableFormats/numberFormat"el, + number_format: ~x"."e |> transform_by(&NumberFormat.from_xpath_node/1) + ] + ) + + kwlist = + if(is_map(kwlist) && is_map(kwlist.general)) do + put_in(kwlist.general.possible_lengths, general_possible_lengths(kwlist)) + else + kwlist + end struct(%PhoneMetadata{}, kwlist) end defp general_possible_lengths(kwlist) do - [ - :fixed_line, - :mobile, - :toll_free, - :premium_rate, - :shared_cost, - :personal_number, - :voip, - :pager, - :uan, - :voicemail, - :no_international_dialing, - ] - |> Enum.flat_map(fn type -> - kwlist - |> Map.get(type) - |> get_possible_length() - end) - |> Enum.reject(&is_nil/1) - |> Enum.sort - |> Enum.uniq + [ + :fixed_line, + :mobile, + :toll_free, + :premium_rate, + :shared_cost, + :personal_number, + :voip, + :pager, + :uan, + :voicemail, + :no_international_dialing + ] + |> Enum.flat_map(fn type -> + kwlist + |> Map.get(type) + |> get_possible_length() + end) + |> Enum.reject(&is_nil/1) + |> Enum.sort() + |> Enum.uniq() end defp get_possible_length(nil), do: [] - defp get_possible_length(phone_number_description = %PhoneNumberDescription{}), do: phone_number_description.possible_lengths || [] + + defp get_possible_length(phone_number_description = %PhoneNumberDescription{}), + do: phone_number_description.possible_lengths || [] defp normalize_rule(nil), do: nil - defp normalize_rule(char_list) when is_list(char_list), do: char_list |> List.to_string() |> normalize_rule() + + defp normalize_rule(char_list) when is_list(char_list), + do: char_list |> List.to_string() |> normalize_rule() + defp normalize_rule(string) when is_binary(string) do string |> String.replace(~r/\$(\d)/, "\\\\g{\\g{1}}") end defp normalize_pattern(nil), do: nil + defp normalize_pattern(char_list) when is_list(char_list) do char_list |> List.to_string() @@ -128,13 +167,17 @@ defmodule ExPhoneNumber.Metadata.PhoneMetadata do end defp normalize_string(nil), do: nil + defp normalize_string(char_list) when is_list(char_list) do char_list |> List.to_string() end defp normalize_boolean(nil), do: false - defp normalize_boolean(true_char_list) when is_list(true_char_list) and length(true_char_list) == 4, do: true + + defp normalize_boolean(true_char_list) + when is_list(true_char_list) and length(true_char_list) == 4, + do: true defp get_map_key(%PhoneMetadata{} = phone_metadata) do if Integer.parse(phone_metadata.id) != :error do @@ -145,6 +188,7 @@ defmodule ExPhoneNumber.Metadata.PhoneMetadata do end def is_main_country_for_code?(nil), do: nil + def is_main_country_for_code?(%PhoneMetadata{} = phone_metadata) do not is_nil(phone_metadata.main_country_for_code) and phone_metadata.main_country_for_code end @@ -197,116 +241,251 @@ defmodule ExPhoneNumber.Metadata.PhoneMetadata do end def put_default_values(phone_metadata = %PhoneMetadata{}) do - Logger.debug "----- Territory -----" - Logger.debug "#{inspect(phone_metadata)}" - Logger.debug "region_code: #{inspect(phone_metadata.id)}" + Logger.debug("----- Territory -----") + Logger.debug("#{inspect(phone_metadata)}") + Logger.debug("region_code: #{inspect(phone_metadata.id)}") phone_metadata = if has_national_prefix?(phone_metadata) do - Logger.debug "has national_prefix" + Logger.debug("has national_prefix") + if not has_national_prefix_for_parsing?(phone_metadata) do - Logger.debug "has not national_prefix_for_parsing" - Logger.debug "national_prefix_for_parsing: #{inspect(phone_metadata.national_prefix)}" + Logger.debug("has not national_prefix_for_parsing") + Logger.debug("national_prefix_for_parsing: #{inspect(phone_metadata.national_prefix)}") %{phone_metadata | national_prefix_for_parsing: phone_metadata.national_prefix} else phone_metadata end else - Logger.debug "has not national_prefix" + Logger.debug("has not national_prefix") phone_metadata end - phone_metadata = %{phone_metadata | national_prefix_formatting_rule: get_national_prefix_formatting_rule(phone_metadata)} - Logger.debug "national_prefix_formatting_rule: #{inspect(phone_metadata.national_prefix_formatting_rule)}" + phone_metadata = %{ + phone_metadata + | national_prefix_formatting_rule: get_national_prefix_formatting_rule(phone_metadata) + } - phone_metadata = %{phone_metadata | carrier_code_formatting_rule: get_domestic_carrier_code_formatting_rule(phone_metadata)} - Logger.debug "carrier_code_formatting_rule: #{inspect(phone_metadata.carrier_code_formatting_rule)}" + Logger.debug("national_prefix_formatting_rule: #{inspect(phone_metadata.national_prefix_formatting_rule)}") + + phone_metadata = %{ + phone_metadata + | carrier_code_formatting_rule: get_domestic_carrier_code_formatting_rule(phone_metadata) + } - Logger.debug "available_formats count: #{inspect(length(phone_metadata.available_formats))}" + Logger.debug("carrier_code_formatting_rule: #{inspect(phone_metadata.carrier_code_formatting_rule)}") + + Logger.debug("available_formats count: #{inspect(length(phone_metadata.available_formats))}") formats = get_number_format(phone_metadata) - number_format = List.foldl(formats, [], fn(x, acc) -> - ele = destructure_to_number_format(x) - if is_nil(ele), do: acc, else: acc ++ [ele] - end) - Logger.debug "number_format count: #{inspect(length(number_format))}" - intl_number_format = List.foldl(formats, [], fn(x, acc) -> - ele = destructure_to_intl_number_format(x) - if is_nil(ele), do: acc, else: acc ++ [ele] - end) - Logger.debug "intl_number_format count: #{inspect(length(intl_number_format))}" - phone_metadata = %{phone_metadata | number_format: number_format, intl_number_format: intl_number_format} - phone_metadata = %{phone_metadata | general: process_phone_number_description(phone_metadata.general, phone_metadata)} - phone_metadata = %{phone_metadata | fixed_line: process_phone_number_description(phone_metadata.fixed_line, phone_metadata)} - phone_metadata = %{phone_metadata | mobile: process_phone_number_description(phone_metadata.mobile, phone_metadata)} - phone_metadata = %{phone_metadata | toll_free: process_other_phone_number_description(phone_metadata.toll_free, phone_metadata)} - phone_metadata = %{phone_metadata | premium_rate: process_other_phone_number_description(phone_metadata.premium_rate, phone_metadata)} - phone_metadata = %{phone_metadata | shared_cost: process_other_phone_number_description(phone_metadata.shared_cost, phone_metadata)} - phone_metadata = %{phone_metadata | voip: process_other_phone_number_description(phone_metadata.voip, phone_metadata)} - phone_metadata = %{phone_metadata | personal_number: process_other_phone_number_description(phone_metadata.personal_number, phone_metadata)} - phone_metadata = %{phone_metadata | pager: process_other_phone_number_description(phone_metadata.pager, phone_metadata)} - phone_metadata = %{phone_metadata | uan: process_other_phone_number_description(phone_metadata.uan, phone_metadata)} - phone_metadata = %{phone_metadata | voicemail: process_other_phone_number_description(phone_metadata.voicemail, phone_metadata)} - phone_metadata = %{phone_metadata | no_international_dialing: process_other_phone_number_description(phone_metadata.no_international_dialing, phone_metadata)} - phone_metadata = %{phone_metadata | same_mobile_and_fixed_line_pattern: phone_metadata.mobile.national_number_pattern == phone_metadata.fixed_line.national_number_pattern} + + number_format = + List.foldl(formats, [], fn x, acc -> + ele = destructure_to_number_format(x) + if is_nil(ele), do: acc, else: acc ++ [ele] + end) + + Logger.debug("number_format count: #{inspect(length(number_format))}") + + intl_number_format = + List.foldl(formats, [], fn x, acc -> + ele = destructure_to_intl_number_format(x) + if is_nil(ele), do: acc, else: acc ++ [ele] + end) + + Logger.debug("intl_number_format count: #{inspect(length(intl_number_format))}") + + phone_metadata = %{ + phone_metadata + | number_format: number_format, + intl_number_format: intl_number_format + } + + phone_metadata = %{ + phone_metadata + | general: process_phone_number_description(phone_metadata.general, phone_metadata) + } + + phone_metadata = %{ + phone_metadata + | fixed_line: process_phone_number_description(phone_metadata.fixed_line, phone_metadata) + } + + phone_metadata = %{ + phone_metadata + | mobile: process_phone_number_description(phone_metadata.mobile, phone_metadata) + } + + phone_metadata = %{ + phone_metadata + | toll_free: process_other_phone_number_description(phone_metadata.toll_free, phone_metadata) + } + + phone_metadata = %{ + phone_metadata + | premium_rate: process_other_phone_number_description(phone_metadata.premium_rate, phone_metadata) + } + + phone_metadata = %{ + phone_metadata + | shared_cost: process_other_phone_number_description(phone_metadata.shared_cost, phone_metadata) + } + + phone_metadata = %{ + phone_metadata + | voip: process_other_phone_number_description(phone_metadata.voip, phone_metadata) + } + + phone_metadata = %{ + phone_metadata + | personal_number: process_other_phone_number_description(phone_metadata.personal_number, phone_metadata) + } + + phone_metadata = %{ + phone_metadata + | pager: process_other_phone_number_description(phone_metadata.pager, phone_metadata) + } + + phone_metadata = %{ + phone_metadata + | uan: process_other_phone_number_description(phone_metadata.uan, phone_metadata) + } + + phone_metadata = %{ + phone_metadata + | voicemail: process_other_phone_number_description(phone_metadata.voicemail, phone_metadata) + } + + phone_metadata = %{ + phone_metadata + | no_international_dialing: + process_other_phone_number_description( + phone_metadata.no_international_dialing, + phone_metadata + ) + } + + phone_metadata = %{ + phone_metadata + | same_mobile_and_fixed_line_pattern: + phone_metadata.mobile.national_number_pattern == + phone_metadata.fixed_line.national_number_pattern + } + {get_map_key(phone_metadata), phone_metadata} end - def get_national_prefix_formatting_rule(%PhoneMetadata{national_prefix: prefix, national_prefix_formatting_rule: rule}), do: get_national_prefix_formatting_rule(prefix, rule) - def get_national_prefix_formatting_rule(%NumberFormat{national_prefix_formatting_rule: rule}, prefix), do: get_national_prefix_formatting_rule(prefix, rule) + def get_national_prefix_formatting_rule(%PhoneMetadata{ + national_prefix: prefix, + national_prefix_formatting_rule: rule + }), + do: get_national_prefix_formatting_rule(prefix, rule) + + def get_national_prefix_formatting_rule( + %NumberFormat{national_prefix_formatting_rule: rule}, + prefix + ), + do: get_national_prefix_formatting_rule(prefix, rule) + def get_national_prefix_formatting_rule(prefix, rule) do rule = Regex.replace(~r/\$NP/, rule, prefix, global: false) Regex.replace(~r/\$FG/, rule, "\\\\g{1}", global: false) end - def get_domestic_carrier_code_formatting_rule(%PhoneMetadata{national_prefix: prefix, carrier_code_formatting_rule: rule}), do: get_domestic_carrier_code_formatting_rule(prefix, rule) - def get_domestic_carrier_code_formatting_rule(%NumberFormat{domestic_carrier_code_formatting_rule: rule}, prefix), do: get_domestic_carrier_code_formatting_rule(prefix, rule) + def get_domestic_carrier_code_formatting_rule(%PhoneMetadata{ + national_prefix: prefix, + carrier_code_formatting_rule: rule + }), + do: get_domestic_carrier_code_formatting_rule(prefix, rule) + + def get_domestic_carrier_code_formatting_rule( + %NumberFormat{domestic_carrier_code_formatting_rule: rule}, + prefix + ), + do: get_domestic_carrier_code_formatting_rule(prefix, rule) + def get_domestic_carrier_code_formatting_rule(prefix, rule) do rule = Regex.replace(~r/\$FG/, rule, "\\\\g{1}", global: false) Regex.replace(~r/\$NP/, rule, prefix, global: false) end - def get_number_format(%PhoneMetadata{} = phone_metadata), do: get_number_format(phone_metadata.available_formats, phone_metadata) + def get_number_format(%PhoneMetadata{} = phone_metadata), + do: get_number_format(phone_metadata.available_formats, phone_metadata) + def get_number_format([head | tail], %PhoneMetadata{} = phone_metadata) do [get_number_format(head, phone_metadata) | get_number_format(tail, phone_metadata)] end + def get_number_format(%{number_format: number_format}, %PhoneMetadata{} = phone_metadata) do - Logger.debug "---> number_format" - Logger.debug "\t#{inspect(number_format)}" + Logger.debug("---> number_format") + Logger.debug("\t#{inspect(number_format)}") + + number_format = + Map.merge( + number_format, + if not is_nil_or_empty?(number_format.national_prefix_formatting_rule) do + %{ + national_prefix_formatting_rule: get_national_prefix_formatting_rule(number_format, phone_metadata.national_prefix) + } + else + %{national_prefix_formatting_rule: phone_metadata.national_prefix_formatting_rule} + end + ) - number_format = Map.merge(number_format, - if not is_nil_or_empty?(number_format.national_prefix_formatting_rule) do - %{national_prefix_formatting_rule: get_national_prefix_formatting_rule(number_format, phone_metadata.national_prefix)} - else - %{national_prefix_formatting_rule: phone_metadata.national_prefix_formatting_rule} - end) - Logger.debug "\tnational_prefix_formatting_rule: #{inspect(number_format.national_prefix_formatting_rule)}" + Logger.debug("\tnational_prefix_formatting_rule: #{inspect(number_format.national_prefix_formatting_rule)}") - number_format = Map.merge(number_format, - if is_nil_or_empty?(number_format.national_prefix_optional_when_formatting) do - %{national_prefix_optional_when_formatting: has_national_prefix_optional_when_formatting?(phone_metadata)} - else - %{} - end) - Logger.debug ~s"\tnational_prefix_optional_when_formatting: #{inspect(number_format.national_prefix_optional_when_formatting)}" + number_format = + Map.merge( + number_format, + if is_nil_or_empty?(number_format.national_prefix_optional_when_formatting) do + %{ + national_prefix_optional_when_formatting: has_national_prefix_optional_when_formatting?(phone_metadata) + } + else + %{} + end + ) + + Logger.debug(~s"\tnational_prefix_optional_when_formatting: #{inspect(number_format.national_prefix_optional_when_formatting)}") + + number_format = + Map.merge( + number_format, + if not is_nil_or_empty?(number_format.domestic_carrier_code_formatting_rule) do + %{ + domestic_carrier_code_formatting_rule: + get_domestic_carrier_code_formatting_rule( + number_format, + phone_metadata.national_prefix + ) + } + else + %{domestic_carrier_code_formatting_rule: phone_metadata.carrier_code_formatting_rule} + end + ) - number_format = Map.merge(number_format, - if not is_nil_or_empty?(number_format.domestic_carrier_code_formatting_rule) do - %{domestic_carrier_code_formatting_rule: get_domestic_carrier_code_formatting_rule(number_format, phone_metadata.national_prefix)} - else - %{domestic_carrier_code_formatting_rule: phone_metadata.carrier_code_formatting_rule} - end) - Logger.debug ~s"\tdomestic_carrier_code_formatting_rule: #{inspect(number_format.domestic_carrier_code_formatting_rule)}" + Logger.debug(~s"\tdomestic_carrier_code_formatting_rule: #{inspect(number_format.domestic_carrier_code_formatting_rule)}") + + number_format = + Map.merge( + number_format, + %{ + leading_digits_pattern: + List.wrap( + Enum.reduce(number_format.leading_digits_pattern, [], fn %{pattern: pattern}, _ -> + pattern + end) + ) + } + ) - number_format = Map.merge(number_format, - %{leading_digits_pattern: List.wrap(Enum.reduce(number_format.leading_digits_pattern, [], fn(%{pattern: pattern}, _) -> pattern end))} - ) - Logger.debug ~s"\tleading_digits_pattern: #{inspect(number_format.leading_digits_pattern)}" + Logger.debug(~s"\tleading_digits_pattern: #{inspect(number_format.leading_digits_pattern)}") intl_number_format = get_intl_format(number_format) - Logger.debug "---> intl_number_format" - Logger.debug "\t#{inspect(intl_number_format)}" + Logger.debug("---> intl_number_format") + Logger.debug("\t#{inspect(intl_number_format)}") %{number_format: number_format, intl_number_format: intl_number_format} end + def get_number_format([], _), do: [] def get_intl_format(%NumberFormat{} = number_format) do @@ -314,8 +493,12 @@ defmodule ExPhoneNumber.Metadata.PhoneMetadata do if is_nil_or_empty?(number_format.intl_format) do Map.merge(%NumberFormat{}, number_format) else - intl_number_format = %NumberFormat{pattern: number_format.pattern, leading_digits_pattern: number_format.leading_digits_pattern} - if number_format.intl_format == Values.description_default_pattern do + intl_number_format = %NumberFormat{ + pattern: number_format.pattern, + leading_digits_pattern: number_format.leading_digits_pattern + } + + if number_format.intl_format == Values.description_default_pattern() do intl_number_format else Map.merge(intl_number_format, %{format: number_format.intl_format}) @@ -334,29 +517,45 @@ defmodule ExPhoneNumber.Metadata.PhoneMetadata do def destructure_to_intl_number_format([]), do: [] def destructure_to_intl_number_format(%{intl_number_format: number_format}), do: number_format - def process_phone_number_description(nil, nil), do: process_phone_number_description(%PhoneNumberDescription{}, %PhoneNumberDescription{}) - def process_phone_number_description(nil, %PhoneMetadata{general: general}), do: process_phone_number_description(%PhoneNumberDescription{}, general) - def process_phone_number_description(%PhoneNumberDescription{} = description, nil), do: process_phone_number_description(description, %PhoneNumberDescription{}) - def process_phone_number_description(%PhoneNumberDescription{} = description, %PhoneMetadata{general: general}), do: process_phone_number_description(description, general) - def process_phone_number_description(%PhoneNumberDescription{} = description, %PhoneNumberDescription{} = general) do + def process_phone_number_description(nil, nil), + do: process_phone_number_description(%PhoneNumberDescription{}, %PhoneNumberDescription{}) + + def process_phone_number_description(nil, %PhoneMetadata{general: general}), + do: process_phone_number_description(%PhoneNumberDescription{}, general) + + def process_phone_number_description(%PhoneNumberDescription{} = description, nil), + do: process_phone_number_description(description, %PhoneNumberDescription{}) + + def process_phone_number_description(%PhoneNumberDescription{} = description, %PhoneMetadata{ + general: general + }), + do: process_phone_number_description(description, general) + + def process_phone_number_description( + %PhoneNumberDescription{} = description, + %PhoneNumberDescription{} = general + ) do national_number_pattern = if is_nil_or_empty?(description.national_number_pattern) do general.national_number_pattern else description.national_number_pattern end + possible_lengths = if is_nil_or_empty?(description.possible_lengths) do general.possible_lengths else description.possible_lengths end + example_number = if is_nil_or_empty?(description.example_number) do general.example_number else description.example_number end + %PhoneNumberDescription{ national_number_pattern: national_number_pattern, possible_lengths: possible_lengths, @@ -366,7 +565,10 @@ defmodule ExPhoneNumber.Metadata.PhoneMetadata do def process_other_phone_number_description(description, %PhoneMetadata{general: general}) do if is_nil(description) do - %PhoneNumberDescription{national_number_pattern: Values.description_default_pattern, possible_lengths: Values.description_default_length} + %PhoneNumberDescription{ + national_number_pattern: Values.description_default_pattern(), + possible_lengths: Values.description_default_length() + } else process_phone_number_description(description, general) end diff --git a/lib/ex_phone_number/metadata/phone_number_description.ex b/lib/ex_phone_number/metadata/phone_number_description.ex index 696d7d2..d794a97 100644 --- a/lib/ex_phone_number/metadata/phone_number_description.ex +++ b/lib/ex_phone_number/metadata/phone_number_description.ex @@ -1,12 +1,16 @@ defmodule ExPhoneNumber.Metadata.PhoneNumberDescription do - defstruct national_number_pattern: nil, # string - possible_lengths: nil, # list - example_number: nil # string + # string + defstruct national_number_pattern: nil, + # list + possible_lengths: nil, + # string + example_number: nil import SweetXml alias ExPhoneNumber.Metadata.PhoneNumberDescription def from_xpath_node(nil), do: nil + def from_xpath_node(xpath_node) do kwlist = xmap( @@ -20,8 +24,8 @@ defmodule ExPhoneNumber.Metadata.PhoneNumberDescription do possible_lengths = (kwlist.local_possible_lengths || []) |> Enum.concat(kwlist.national_possible_lengths || []) - |> Enum.sort - |> Enum.uniq + |> Enum.sort() + |> Enum.uniq() struct(%PhoneNumberDescription{}, %{ national_number_pattern: kwlist.national_number_pattern, @@ -33,30 +37,34 @@ defmodule ExPhoneNumber.Metadata.PhoneNumberDescription do defp clean_string(string) when is_binary(string) do string |> String.split(["\n", " "], trim: true) - |> List.to_string + |> List.to_string() end defp normalize_string(nil), do: nil - defp normalize_string(char_list) when is_list(char_list), do: List.to_string(char_list) |> clean_string + + defp normalize_string(char_list) when is_list(char_list), + do: List.to_string(char_list) |> clean_string defp normalize_pattern(nil), do: nil + defp normalize_pattern(char_list) when is_list(char_list) do char_list - |> List.to_string + |> List.to_string() |> clean_string |> Regex.compile!() end defp normalize_range(nil), do: nil + defp normalize_range(char_list) when is_list(char_list) do char_list - |> List.to_string + |> List.to_string() |> clean_string |> String.split(",") |> Enum.map(&range_to_list/1) - |> List.flatten - |> Enum.sort - |> Enum.uniq + |> List.flatten() + |> Enum.sort() + |> Enum.uniq() end defp range_to_list(range_or_number) do @@ -68,8 +76,10 @@ defmodule ExPhoneNumber.Metadata.PhoneNumberDescription do |> Enum.map(fn n -> String.to_integer(n) end) Range.new(range_start, range_end) - |> Enum.to_list - _ -> String.to_integer(range_or_number) + |> Enum.to_list() + + _ -> + String.to_integer(range_or_number) end end end diff --git a/lib/ex_phone_number/model/phone_number.ex b/lib/ex_phone_number/model/phone_number.ex index 7cdf7f5..81b0eec 100644 --- a/lib/ex_phone_number/model/phone_number.ex +++ b/lib/ex_phone_number/model/phone_number.ex @@ -1,12 +1,20 @@ defmodule ExPhoneNumber.Model.PhoneNumber do - defstruct country_code: nil, # number - national_number: nil, # number - extension: nil, # string - italian_leading_zero: nil, # boolean - number_of_leading_zeros: nil, # number - raw_input: nil, # string - country_code_source: nil, # atom - preferred_domestic_carrier_code: nil # string + # number + defstruct country_code: nil, + # number + national_number: nil, + # string + extension: nil, + # boolean + italian_leading_zero: nil, + # number + number_of_leading_zeros: nil, + # string + raw_input: nil, + # atom + country_code_source: nil, + # string + preferred_domestic_carrier_code: nil alias ExPhoneNumber.Model.PhoneNumber alias ExPhoneNumber.Constants.CountryCodeSource @@ -62,11 +70,12 @@ defmodule ExPhoneNumber.Model.PhoneNumber do end def get_national_significant_number(phone_number = %PhoneNumber{}) do - national_number = if has_national_number?(phone_number) do - phone_number.national_number - else - "" - end + national_number = + if has_national_number?(phone_number) do + phone_number.national_number + else + "" + end if has_italian_leading_zero?(phone_number) and phone_number.italian_leading_zero do upper_bound = get_number_of_leading_zeros_or_default(phone_number) @@ -80,9 +89,20 @@ defmodule ExPhoneNumber.Model.PhoneNumber do def set_italian_leading_zeros(national_number, %PhoneNumber{} = phone_number) do if String.length(national_number) > 1 and String.at(national_number, 0) == "0" do phone_number = %{phone_number | italian_leading_zero: true} - number_of_leading_zeros = Enum.reduce_while(String.graphemes(national_number), 0, fn ele, acc -> if ele == "0", do: {:cont, acc + 1}, else: {:halt, acc} end) - number_of_leading_zeros = if String.ends_with?(national_number, "0"), do: number_of_leading_zeros - 1, else: number_of_leading_zeros - if number_of_leading_zeros > 1, do: %{phone_number | number_of_leading_zeros: number_of_leading_zeros}, else: phone_number + + number_of_leading_zeros = + Enum.reduce_while(String.graphemes(national_number), 0, fn ele, acc -> + if ele == "0", do: {:cont, acc + 1}, else: {:halt, acc} + end) + + number_of_leading_zeros = + if String.ends_with?(national_number, "0"), + do: number_of_leading_zeros - 1, + else: number_of_leading_zeros + + if number_of_leading_zeros > 1, + do: %{phone_number | number_of_leading_zeros: number_of_leading_zeros}, + else: phone_number else phone_number end diff --git a/lib/ex_phone_number/normalization.ex b/lib/ex_phone_number/normalization.ex index a6276a2..3e8f9e1 100644 --- a/lib/ex_phone_number/normalization.ex +++ b/lib/ex_phone_number/normalization.ex @@ -4,7 +4,7 @@ defmodule ExPhoneNumber.Normalization do alias ExPhoneNumber.Constants.Patterns def convert_alpha_chars_in_number(number) do - normalize_helper(number, Mappings.all_normalization_mappings, false) + normalize_helper(number, Mappings.all_normalization_mappings(), false) end def match_at_start?(string, pattern) when is_binary(string) and is_map(pattern) do @@ -15,22 +15,25 @@ defmodule ExPhoneNumber.Normalization do end def normalize(number) do - if matches_entirely?(Patterns.valid_alpha_phone_pattern, number) do - normalize_helper(number, Mappings.all_normalization_mappings, true) + if matches_entirely?(Patterns.valid_alpha_phone_pattern(), number) do + normalize_helper(number, Mappings.all_normalization_mappings(), true) else normalize_digits_only(number) end end def normalize_digits_only(number) do - normalize_helper(number, Mappings.digit_mappings, true) + normalize_helper(number, Mappings.digit_mappings(), true) end - def normalize_helper(number, normalization_replacements, remove_non_matches) when is_binary(number) and is_map(normalization_replacements) and is_boolean(remove_non_matches) do + def normalize_helper(number, normalization_replacements, remove_non_matches) + when is_binary(number) and is_map(normalization_replacements) and + is_boolean(remove_non_matches) do number |> String.codepoints() - |> Enum.reduce([], fn(char, list) -> + |> Enum.reduce([], fn char, list -> new_char = Map.get(normalization_replacements, String.upcase(char)) + if new_char do list ++ [new_char] else @@ -44,21 +47,27 @@ defmodule ExPhoneNumber.Normalization do |> List.to_string() end - def split_at_match_and_return_head(string, pattern) when is_binary(string) and is_map(pattern) do + def split_at_match_and_return_head(string, pattern) + when is_binary(string) and is_map(pattern) do case Regex.run(pattern, string, return: :index) do [{index, _length} | _tail] -> {head, _tail} = String.split_at(string, index) head - nil -> string + + nil -> + string end end - def split_at_match_and_return_head(string, pattern) when is_binary(string) and is_binary(pattern) do + def split_at_match_and_return_head(string, pattern) + when is_binary(string) and is_binary(pattern) do case :binary.match(string, pattern) do {pos, _length} -> {head, _tail} = String.split_at(string, pos) head - :nomatch -> string + + :nomatch -> + string end end end diff --git a/lib/ex_phone_number/parsing.ex b/lib/ex_phone_number/parsing.ex index c6ad004..be82af6 100644 --- a/lib/ex_phone_number/parsing.ex +++ b/lib/ex_phone_number/parsing.ex @@ -11,29 +11,31 @@ defmodule ExPhoneNumber.Parsing do def build_national_number_for_parsing(number_to_parse) do number_to_parse - |> String.split(Values.rfc3966_phone_context, parts: 2) + |> String.split(Values.rfc3966_phone_context(), parts: 2) |> case do [number_head, number_tail] -> - if String.starts_with?(number_tail, Values.plus_sign) do + if String.starts_with?(number_tail, Values.plus_sign()) do case String.split(number_tail, ";", parts: 2) do [phone_context_head, _] -> phone_context_head [_] -> number_tail end else "" - end - <> - case String.split(number_head, Values.rfc3966_prefix, parts: 2) do - [_, national_number_tail] -> national_number_tail - [_] -> number_head - end - [_] -> extract_possible_number(number_to_parse) + end <> + case String.split(number_head, Values.rfc3966_prefix(), parts: 2) do + [_, national_number_tail] -> national_number_tail + [_] -> number_head + end + + [_] -> + extract_possible_number(number_to_parse) end - |> split_at_match_and_return_head(Values.rfc3966_isdn_subaddress) + |> split_at_match_and_return_head(Values.rfc3966_isdn_subaddress()) end def check_region_for_parsing(number_to_parse, default_region) do - Metadata.is_valid_region_code?(default_region) or Regex.match?(Patterns.leading_plus_chars_pattern, number_to_parse) + Metadata.is_valid_region_code?(default_region) or + Regex.match?(Patterns.leading_plus_chars_pattern(), number_to_parse) end def is_possible_number?(number, region_code) when is_binary(number) do @@ -47,18 +49,25 @@ defmodule ExPhoneNumber.Parsing do parse_helper(number_to_parse, default_region, false, true) end - def parse_helper(number_to_parse, _default_region, _keep_raw_input, _check_region) when is_nil(number_to_parse), do: {:error, ErrorMessages.not_a_number} - def parse_helper(number_to_parse, default_region, keep_raw_input, check_region) when is_binary(number_to_parse) and is_boolean(keep_raw_input) and is_boolean(check_region) do + def parse_helper(number_to_parse, _default_region, _keep_raw_input, _check_region) + when is_nil(number_to_parse), + do: {:error, ErrorMessages.not_a_number()} + + def parse_helper(number_to_parse, default_region, keep_raw_input, check_region) + when is_binary(number_to_parse) and is_boolean(keep_raw_input) and is_boolean(check_region) do results_tuple = case validate_length(number_to_parse) do - {:error, message} -> {:error, message} + {:error, message} -> + {:error, message} + {:ok, number_to_parse} -> national_number = build_national_number_for_parsing(number_to_parse) + if not is_viable_phone_number?(national_number) do - {:error, ErrorMessages.not_a_number} + {:error, ErrorMessages.not_a_number()} else if check_region and not check_region_for_parsing(national_number, default_region) do - {:error, ErrorMessages.invalid_country_code} + {:error, ErrorMessages.invalid_country_code()} else {:ok, national_number, default_region} end @@ -68,37 +77,66 @@ defmodule ExPhoneNumber.Parsing do results_tuple = if :ok == elem(results_tuple, 0) do {_, national_number, _} = results_tuple + phone_number = if keep_raw_input, do: %PhoneNumber{raw_input: number_to_parse}, else: %PhoneNumber{} + {ext, national_number} = maybe_strip_extension(national_number) + phone_number = if not is_nil_or_empty?(ext), do: %{phone_number | extension: ext}, else: phone_number + region_metadata = Metadata.get_for_region_code(default_region) + case maybe_extract_country_code(national_number, region_metadata, keep_raw_input) do {true, normalized_national_number, phone_number_extract} -> if phone_number_extract.country_code != 0 do phone_number_region_code = Metadata.get_region_code_for_country_code(phone_number_extract.country_code) - phone_number_region_metadata = if phone_number_region_code != default_region do - Metadata.get_for_region_code_or_calling_code(phone_number_extract.country_code, phone_number_region_code) + + phone_number_region_metadata = + if phone_number_region_code != default_region do + Metadata.get_for_region_code_or_calling_code( + phone_number_extract.country_code, + phone_number_region_code + ) else region_metadata end + {:ok, normalized_national_number, phone_number_region_metadata, Map.merge(phone_number, phone_number_extract, &update_if_nil/3)} else new_normalized_national_number = normalize(national_number) - phone_number_extract = %{phone_number_extract | country_code: region_metadata.country_code} - phone_number_extract = if keep_raw_input, do: %{phone_number_extract | country_code_source: nil}, else: phone_number_extract + + phone_number_extract = %{ + phone_number_extract + | country_code: region_metadata.country_code + } + + phone_number_extract = + if keep_raw_input, + do: %{phone_number_extract | country_code_source: nil}, + else: phone_number_extract + {:ok, new_normalized_national_number, region_metadata, Map.merge(phone_number, phone_number_extract, &update_if_nil/3)} end + {false, message} -> - if message == ErrorMessages.invalid_country_code and Regex.match?(Patterns.leading_plus_chars_pattern, national_number) do - national_number_without_plus_sign = String.replace(national_number, Patterns.leading_plus_chars_pattern, "") - case maybe_extract_country_code(national_number_without_plus_sign, region_metadata, keep_raw_input) do + if message == ErrorMessages.invalid_country_code() and + Regex.match?(Patterns.leading_plus_chars_pattern(), national_number) do + national_number_without_plus_sign = String.replace(national_number, Patterns.leading_plus_chars_pattern(), "") + + case maybe_extract_country_code( + national_number_without_plus_sign, + region_metadata, + keep_raw_input + ) do {true, normalized_national_number, phone_number_extract} -> if phone_number_extract.country_code == 0 do {:error, message} else {:ok, normalized_national_number, region_metadata, Map.merge(phone_number, phone_number_extract, &update_if_nil/3)} end - {false, message} -> {:error, message} + + {false, message} -> + {:error, message} end else {:error, message} @@ -111,12 +149,25 @@ defmodule ExPhoneNumber.Parsing do results_tuple = if :ok == elem(results_tuple, 0) do {_, normalized_national_number, metadata, phone_number} = results_tuple + if not is_nil(metadata) do case maybe_strip_national_prefix_and_carrier_code(normalized_national_number, metadata) do {true, carrier_code, potential_national_number} -> - national_number = if not is_shorter_than_possible_normal_number?(metadata, potential_national_number), do: potential_national_number, else: normalized_national_number - phone_number = if keep_raw_input, do: %{phone_number | preferred_domestic_carrier_code: carrier_code}, else: phone_number + national_number = + if not is_shorter_than_possible_normal_number?( + metadata, + potential_national_number + ), + do: potential_national_number, + else: normalized_national_number + + phone_number = + if keep_raw_input, + do: %{phone_number | preferred_domestic_carrier_code: carrier_code}, + else: phone_number + {:ok, national_number, phone_number} + {false, _, _} -> {:ok, normalized_national_number, phone_number} end @@ -130,16 +181,21 @@ defmodule ExPhoneNumber.Parsing do results_tuple = if :ok == elem(results_tuple, 0) do {_, normalized_national_number, phone_number} = results_tuple + cond do - String.length(normalized_national_number) < Values.min_length_for_nsn -> - {:error, ErrorMessages.too_short_nsn} - String.length(normalized_national_number) > Values.max_length_for_nsn -> - {:error, ErrorMessages.too_long} + String.length(normalized_national_number) < Values.min_length_for_nsn() -> + {:error, ErrorMessages.too_short_nsn()} + + String.length(normalized_national_number) > Values.max_length_for_nsn() -> + {:error, ErrorMessages.too_long()} + true -> phone_number_add = PhoneNumber.set_italian_leading_zeros(normalized_national_number, phone_number) + case Integer.parse(normalized_national_number) do {int, _} -> {:ok, %{phone_number_add | national_number: int}} + :error -> {:ok, phone_number_add} end diff --git a/lib/ex_phone_number/utilities.ex b/lib/ex_phone_number/utilities.ex index 523ed2e..998a6a6 100644 --- a/lib/ex_phone_number/utilities.ex +++ b/lib/ex_phone_number/utilities.ex @@ -6,8 +6,10 @@ defmodule ExPhoneNumber.Utilities do def is_nil_or_empty?(string) when is_binary(string), do: String.length(string) == 0 def is_nil_or_empty?(_), do: false - def is_number_matching_description?(number, %PhoneNumberDescription{} = description) when is_binary(number) do - if description.possible_lengths == Values.description_default_length or description.national_number_pattern == Values.description_default_pattern do + def is_number_matching_description?(number, %PhoneNumberDescription{} = description) + when is_binary(number) do + if description.possible_lengths == Values.description_default_length() or + description.national_number_pattern == Values.description_default_pattern() do false else matches_entirely?(description.national_number_pattern, number) @@ -15,8 +17,10 @@ defmodule ExPhoneNumber.Utilities do end def matches_entirely?(nil, _string), do: false + def matches_entirely?(regex, string) do regex = ~r/^(?:#{regex.source})$/ + case Regex.run(regex, string, return: :index) do [{_index, length} | _tail] -> Kernel.byte_size(string) == length _ -> false diff --git a/lib/ex_phone_number/validation.ex b/lib/ex_phone_number/validation.ex index b835a85..82cc4da 100644 --- a/lib/ex_phone_number/validation.ex +++ b/lib/ex_phone_number/validation.ex @@ -11,9 +11,11 @@ defmodule ExPhoneNumber.Validation do def get_number_type(%PhoneNumber{} = phone_number) do region_code = Metadata.get_region_code_for_number(phone_number) + metadata = Metadata.get_for_region_code_or_calling_code(phone_number.country_code, region_code) + if metadata == nil do - PhoneNumberTypes.unknown + PhoneNumberTypes.unknown() else national_significant_number = PhoneNumber.get_national_significant_number(phone_number) get_number_type_helper(national_significant_number, metadata) @@ -22,42 +24,66 @@ defmodule ExPhoneNumber.Validation do def get_number_type_helper(national_number, metadata = %PhoneMetadata{}) do cond do - not is_number_matching_description?(national_number, metadata.general) -> PhoneNumberTypes.unknown - is_number_matching_description?(national_number, metadata.premium_rate) -> PhoneNumberTypes.premium_rate - is_number_matching_description?(national_number, metadata.toll_free) -> PhoneNumberTypes.toll_free - is_number_matching_description?(national_number, metadata.shared_cost) -> PhoneNumberTypes.shared_cost - is_number_matching_description?(national_number, metadata.voip) -> PhoneNumberTypes.voip - is_number_matching_description?(national_number, metadata.personal_number) -> PhoneNumberTypes.personal_number - is_number_matching_description?(national_number, metadata.pager) -> PhoneNumberTypes.pager - is_number_matching_description?(national_number, metadata.uan) -> PhoneNumberTypes.uan - is_number_matching_description?(national_number, metadata.voicemail) -> PhoneNumberTypes.voicemail + not is_number_matching_description?(national_number, metadata.general) -> + PhoneNumberTypes.unknown() + + is_number_matching_description?(national_number, metadata.premium_rate) -> + PhoneNumberTypes.premium_rate() + + is_number_matching_description?(national_number, metadata.toll_free) -> + PhoneNumberTypes.toll_free() + + is_number_matching_description?(national_number, metadata.shared_cost) -> + PhoneNumberTypes.shared_cost() + + is_number_matching_description?(national_number, metadata.voip) -> + PhoneNumberTypes.voip() + + is_number_matching_description?(national_number, metadata.personal_number) -> + PhoneNumberTypes.personal_number() + + is_number_matching_description?(national_number, metadata.pager) -> + PhoneNumberTypes.pager() + + is_number_matching_description?(national_number, metadata.uan) -> + PhoneNumberTypes.uan() + + is_number_matching_description?(national_number, metadata.voicemail) -> + PhoneNumberTypes.voicemail() + is_number_matching_description?(national_number, metadata.fixed_line) -> if metadata.same_mobile_and_fixed_line_pattern do - PhoneNumberTypes.fixed_line_or_mobile + PhoneNumberTypes.fixed_line_or_mobile() else if is_number_matching_description?(national_number, metadata.mobile) do - PhoneNumberTypes.fixed_line_or_mobile + PhoneNumberTypes.fixed_line_or_mobile() else - PhoneNumberTypes.fixed_line + PhoneNumberTypes.fixed_line() end end - is_number_matching_description?(national_number, metadata.mobile) -> PhoneNumberTypes.mobile - true -> PhoneNumberTypes.unknown + + is_number_matching_description?(national_number, metadata.mobile) -> + PhoneNumberTypes.mobile() + + true -> + PhoneNumberTypes.unknown() end end def is_number_geographical?(%PhoneNumber{} = phone_number) do number_type = get_number_type(phone_number) - number_type == PhoneNumberTypes.fixed_line or number_type == PhoneNumberTypes.fixed_line_or_mobile + + number_type == PhoneNumberTypes.fixed_line() or + number_type == PhoneNumberTypes.fixed_line_or_mobile() end def is_possible_number?(%PhoneNumber{} = number) do - ValidationResults.is_possible == is_possible_number_with_reason?(number) + ValidationResults.is_possible() == is_possible_number_with_reason?(number) end def is_possible_number_with_reason?(%PhoneNumber{} = number) do if not Metadata.is_valid_country_code?(number.country_code) do - ValidationResults.invalid_country_code + ValidationResults.invalid_country_code() else region_code = Metadata.get_region_code_for_country_code(number.country_code) metadata = Metadata.get_for_region_code_or_calling_code(number.country_code, region_code) @@ -67,7 +93,7 @@ defmodule ExPhoneNumber.Validation do end def is_shorter_than_possible_normal_number?(metadata, number) do - test_number_length(number, metadata) == ValidationResults.too_short + test_number_length(number, metadata) == ValidationResults.too_short() end def is_valid_number?(%PhoneNumber{} = number) do @@ -76,31 +102,37 @@ defmodule ExPhoneNumber.Validation do end def is_valid_number_for_region?(%PhoneNumber{} = _number, nil), do: false - def is_valid_number_for_region?(%PhoneNumber{} = number, region_code) when is_binary(region_code) do + + def is_valid_number_for_region?(%PhoneNumber{} = number, region_code) + when is_binary(region_code) do metadata = Metadata.get_for_region_code_or_calling_code(number.country_code, region_code) - is_invalid_code = Values.region_code_for_non_geo_entity != region_code and number.country_code != Metadata.get_country_code_for_valid_region(region_code) + + is_invalid_code = + Values.region_code_for_non_geo_entity() != region_code and + number.country_code != Metadata.get_country_code_for_valid_region(region_code) + if is_nil(metadata) or is_invalid_code do false else national_significant_number = PhoneNumber.get_national_significant_number(number) - get_number_type_helper(national_significant_number, metadata) != PhoneNumberTypes.unknown + get_number_type_helper(national_significant_number, metadata) != PhoneNumberTypes.unknown() end end def is_viable_phone_number?(phone_number) do - if String.length(phone_number) < Values.min_length_for_nsn do + if String.length(phone_number) < Values.min_length_for_nsn() do false else - matches_entirely?(Patterns.valid_phone_number_pattern, phone_number) + matches_entirely?(Patterns.valid_phone_number_pattern(), phone_number) end end def test_number_length(number, metadata) do - test_number_length_for_type(number, metadata, PhoneNumberTypes.unknown) + test_number_length_for_type(number, metadata, PhoneNumberTypes.unknown()) end def validate_length(number_to_parse) do - if String.length(number_to_parse) > Values.max_input_string_length do + if String.length(number_to_parse) > Values.max_input_string_length() do {:error, ErrorMessages.too_long()} else {:ok, number_to_parse} @@ -110,9 +142,9 @@ defmodule ExPhoneNumber.Validation do defp test_number_length_for_type(number, metadata, type) do possible_lengths = if type == PhoneNumberTypes.fixed_line_or_mobile() do - possible_lengths_by_type(metadata, PhoneNumberTypes.fixed_line()) - ++ possible_lengths_by_type(metadata, PhoneNumberTypes.mobile()) - |> Enum.uniq + (possible_lengths_by_type(metadata, PhoneNumberTypes.fixed_line()) ++ + possible_lengths_by_type(metadata, PhoneNumberTypes.mobile())) + |> Enum.uniq() else possible_lengths_by_type(metadata, type) end @@ -121,16 +153,20 @@ defmodule ExPhoneNumber.Validation do max_length = Enum.max(possible_lengths) if(min_length == -1) do - ValidationResults.invalid_length + ValidationResults.invalid_length() else case String.length(number) do - actual_length when (actual_length < min_length) -> ValidationResults.too_short - actual_length when (actual_length > max_length) -> ValidationResults.too_long + actual_length when actual_length < min_length -> + ValidationResults.too_short() + + actual_length when actual_length > max_length -> + ValidationResults.too_long() + actual_length -> if Enum.member?(possible_lengths, actual_length) do - ValidationResults.is_possible + ValidationResults.is_possible() else - ValidationResults.invalid_length + ValidationResults.invalid_length() end end end @@ -149,16 +185,16 @@ defmodule ExPhoneNumber.Validation do defp get_number_description_by_type(%PhoneMetadata{} = metadata, type) do cond do - type == PhoneNumberTypes.premium_rate -> metadata.premium_rate - type == PhoneNumberTypes.toll_free -> metadata.toll_free - type == PhoneNumberTypes.mobile -> metadata.mobile - type == PhoneNumberTypes.fixed_line -> metadata.fixed_line - type == PhoneNumberTypes.shared_cost -> metadata.shared_cost - type == PhoneNumberTypes.voip -> metadata.voip - type == PhoneNumberTypes.personal_number -> metadata.personal_number - type == PhoneNumberTypes.pager -> metadata.pager - type == PhoneNumberTypes.uan -> metadata.uan - type == PhoneNumberTypes.voicemail -> metadata.voicemail + type == PhoneNumberTypes.premium_rate() -> metadata.premium_rate + type == PhoneNumberTypes.toll_free() -> metadata.toll_free + type == PhoneNumberTypes.mobile() -> metadata.mobile + type == PhoneNumberTypes.fixed_line() -> metadata.fixed_line + type == PhoneNumberTypes.shared_cost() -> metadata.shared_cost + type == PhoneNumberTypes.voip() -> metadata.voip + type == PhoneNumberTypes.personal_number() -> metadata.personal_number + type == PhoneNumberTypes.pager() -> metadata.pager + type == PhoneNumberTypes.uan() -> metadata.uan + type == PhoneNumberTypes.voicemail() -> metadata.voicemail true -> metadata.general end end diff --git a/mix.exs b/mix.exs index ec1cb10..7b636cc 100644 --- a/mix.exs +++ b/mix.exs @@ -1,39 +1,43 @@ defmodule ExPhoneNumber.Mixfile do use Mix.Project - @version "0.1.2" + @version "0.2.0" def project do - [app: :ex_phone_number, - version: @version, - elixir: "~> 1.3", - build_embedded: Mix.env == :prod, - start_permanent: Mix.env == :prod, - test_coverage: [tool: ExCoveralls], - preferred_cli_env: [coveralls: :test, "coveralls.travis": :test], - deps: deps(), - package: package(), - description: description(), - name: "ExPhoneNumber", - source_url: "https://github.com/socialpaymentsbv/ex_phone_number", - homepage_url: "https://github.com/socialpaymentsbv/ex_phone_number"] + [ + app: :ex_phone_number, + version: @version, + elixir: "~> 1.4", + build_embedded: Mix.env() == :prod, + start_permanent: Mix.env() == :prod, + test_coverage: [tool: ExCoveralls], + preferred_cli_env: [coveralls: :test, "coveralls.travis": :test], + deps: deps(), + package: package(), + description: description(), + name: "ExPhoneNumber", + source_url: "https://github.com/socialpaymentsbv/ex_phone_number", + homepage_url: "https://github.com/socialpaymentsbv/ex_phone_number" + ] end def application do - [applications: [:logger]] + [extra_applications: [:logger]] end defp deps do - [{:sweet_xml, "~> 0.6.5"}, - {:ex_doc, "~> 0.14", only: :dev, runtime: false}, - {:ex_spec, "~> 2.0", only: :test}, - {:excoveralls, "~> 0.6", only: :test}, - {:credo, "~> 0.7", only: [:dev, :test]}] + [ + {:sweet_xml, "~> 0.6.5"}, + {:ex_doc, "~> 0.18.0", only: :dev, runtime: false}, + {:ex_spec, "~> 2.0", only: :test}, + {:excoveralls, "~> 0.10", only: :test}, + {:credo, "~> 1.0.0", only: [:dev, :test], runtime: false} + ] end defp description do - "A library for parsing, formatting, and validating international phone numbers. " <> - "Based on Google's libphonenumber." + "A library for parsing, formatting, and validating international phone numbers. " <> + "Based on Google's libphonenumber." end defp package do diff --git a/mix.lock b/mix.lock index bc2d5ab..3c3e29a 100644 --- a/mix.lock +++ b/mix.lock @@ -1,15 +1,18 @@ -%{"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"}, - "certifi": {:hex, :certifi, "1.1.0", "c9b71a547016c2528a590ccfc28de786c7edb74aafa17446b84f54e04efc00ee", [:rebar3], [], "hexpm"}, - "credo": {:hex, :credo, "0.7.3", "9827ab04002186af1aec014a811839a06f72aaae6cd5eed3919b248c8767dbf3", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}], "hexpm"}, - "earmark": {:hex, :earmark, "1.2.0", "bf1ce17aea43ab62f6943b97bd6e3dc032ce45d4f787504e3adf738e54b42f3a", [:mix], [], "hexpm"}, - "ex_doc": {:hex, :ex_doc, "0.15.1", "d5f9d588fd802152516fccfdb96d6073753f77314fcfee892b15b6724ca0d596", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm"}, +%{ + "bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"}, + "certifi": {:hex, :certifi, "2.4.2", "75424ff0f3baaccfd34b1214184b6ef616d89e420b258bb0a5ea7d7bc628f7f0", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"}, + "credo": {:hex, :credo, "1.0.2", "88bc918f215168bf6ce7070610a6173c45c82f32baa08bdfc80bf58df2d103b6", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"}, + "earmark": {:hex, :earmark, "1.3.1", "73812f447f7a42358d3ba79283cfa3075a7580a3a2ed457616d6517ac3738cb9", [:mix], [], "hexpm"}, + "ex_doc": {:hex, :ex_doc, "0.18.4", "4406b8891cecf1352f49975c6d554e62e4341ceb41b9338949077b0d4a97b949", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm"}, "ex_spec": {:hex, :ex_spec, "2.0.1", "8bdbd6fa85995fbf836ed799571d44be6f9ebbcace075209fd0ad06372c111cf", [:mix], [], "hexpm"}, - "excoveralls": {:hex, :excoveralls, "0.6.3", "894bf9254890a4aac1d1165da08145a72700ff42d8cb6ce8195a584cb2a4b374", [:mix], [{:exjsx, "~> 3.0", [hex: :exjsx, repo: "hexpm", optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"}, - "exjsx": {:hex, :exjsx, "3.2.1", "1bc5bf1e4fd249104178f0885030bcd75a4526f4d2a1e976f4b428d347614f0f", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, repo: "hexpm", optional: false]}], "hexpm"}, - "hackney": {:hex, :hackney, "1.8.0", "8388a22f4e7eb04d171f2cf0285b217410f266d6c13a4c397a6c22ab823a486c", [:rebar3], [{:certifi, "1.1.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "4.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"}, - "idna": {:hex, :idna, "4.0.0", "10aaa9f79d0b12cf0def53038547855b91144f1bfcc0ec73494f38bb7b9c4961", [:rebar3], [], "hexpm"}, - "jsx": {:hex, :jsx, "2.8.2", "7acc7d785b5abe8a6e9adbde926a24e481f29956dd8b4df49e3e4e7bcc92a018", [:mix, :rebar3], [], "hexpm"}, + "excoveralls": {:hex, :excoveralls, "0.10.5", "7c912c4ec0715a6013647d835c87cde8154855b9b84e256bc7a63858d5f284e3", [:mix], [{:hackney, "~> 1.13", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"}, + "hackney": {:hex, :hackney, "1.15.0", "287a5d2304d516f63e56c469511c42b016423bcb167e61b611f6bad47e3ca60e", [:rebar3], [{:certifi, "2.4.2", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.4", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"}, + "idna": {:hex, :idna, "6.0.0", "689c46cbcdf3524c44d5f3dde8001f364cd7608a99556d8fbd8239a5798d4c10", [:rebar3], [{:unicode_util_compat, "0.4.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"}, + "jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"}, "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"}, "mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], [], "hexpm"}, - "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], [], "hexpm"}, - "sweet_xml": {:hex, :sweet_xml, "0.6.5", "dd9cde443212b505d1b5f9758feb2000e66a14d3c449f04c572f3048c66e6697", [:mix], [], "hexpm"}} + "parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm"}, + "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm"}, + "sweet_xml": {:hex, :sweet_xml, "0.6.5", "dd9cde443212b505d1b5f9758feb2000e66a14d3c449f04c572f3048c66e6697", [:mix], [], "hexpm"}, + "unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm"}, +} diff --git a/test/ex_phone_number/extraction_test.exs b/test/ex_phone_number/extraction_test.exs index 5246c4c..c6b5b4c 100644 --- a/test/ex_phone_number/extraction_test.exs +++ b/test/ex_phone_number/extraction_test.exs @@ -58,18 +58,22 @@ defmodule ExPhoneNumber.ExtractionTest do national_number_pattern: ~r/\d{4,8}/ } } + {:ok, metadata: metadata} end it "should strip national prefix", state do {result, _, number} = maybe_strip_national_prefix_and_carrier_code("34356778", state[:metadata]) + assert result assert "356778" == number end it "should strip national prefix only once", state do {_, _, number} = maybe_strip_national_prefix_and_carrier_code("34356778", state[:metadata]) + {result, _, number} = maybe_strip_national_prefix_and_carrier_code(number, state[:metadata]) + refute result assert "356778" == number end @@ -97,9 +101,11 @@ defmodule ExPhoneNumber.ExtractionTest do national_number_pattern: ~r/\d{4,8}/ } } + {result, carrier_code, number} = maybe_strip_national_prefix_and_carrier_code("08122123456", metadata) + assert result - assert "81" == carrier_code + assert "81" == carrier_code assert "22123456" == number end end @@ -113,6 +119,7 @@ defmodule ExPhoneNumber.ExtractionTest do national_number_pattern: ~r/\d{4,8}/ } } + {result, _, number} = maybe_strip_national_prefix_and_carrier_code("031123", metadata) assert result assert "5315123" == number @@ -124,41 +131,47 @@ defmodule ExPhoneNumber.ExtractionTest do context "case 1" do it "should strip international prefix" do {result, number} = maybe_strip_international_prefix_and_normalize("0034567700-3898003", "00[39]") - assert result == CountryCodeSource.from_number_with_idd + + assert result == CountryCodeSource.from_number_with_idd() assert "45677003898003" == number end it "should return correct CountryCodeSource" do {_, number} = maybe_strip_international_prefix_and_normalize("0034567700-3898003", "00[39]") + {result, _} = maybe_strip_international_prefix_and_normalize(number, "00[39]") - assert result == CountryCodeSource.from_default_country + assert result == CountryCodeSource.from_default_country() end end context "case 2" do it "should strip international prefix" do {result, number} = maybe_strip_international_prefix_and_normalize("00945677003898003", "00[39]") - assert result == CountryCodeSource.from_number_with_idd + + assert result == CountryCodeSource.from_number_with_idd() assert "45677003898003" == number end it "should strip international prefix when is broken up by spaces" do {result, number} = maybe_strip_international_prefix_and_normalize("00 9 45677003898003", "00[39]") - assert result == CountryCodeSource.from_number_with_idd + + assert result == CountryCodeSource.from_number_with_idd() assert "45677003898003" == number end it "should return correct CountryCodeSource" do {_, number} = maybe_strip_international_prefix_and_normalize("00 9 45677003898003", "00[39]") + {result, _} = maybe_strip_international_prefix_and_normalize(number, "00[39]") - assert result == CountryCodeSource.from_default_country + assert result == CountryCodeSource.from_default_country() end end context "case 3" do it "should strip international prefix when contains leading plus sign" do {result, number} = maybe_strip_international_prefix_and_normalize("+45677003898003", "00[39]") - assert result == CountryCodeSource.from_number_with_plus_sign + + assert result == CountryCodeSource.from_number_with_plus_sign() assert "45677003898003" == number end end @@ -166,13 +179,14 @@ defmodule ExPhoneNumber.ExtractionTest do context "number contains leading zero" do it "should not strip leading zero" do {result, number} = maybe_strip_international_prefix_and_normalize("0090112-3123", "00[39]") - assert result == CountryCodeSource.from_default_country + + assert result == CountryCodeSource.from_default_country() assert "00901123123" == number end it "should not strip leading zero when includes spaces" do {result, _} = maybe_strip_international_prefix_and_normalize("009 0-112-3123", "00[39]") - assert result == CountryCodeSource.from_default_country + assert result == CountryCodeSource.from_default_country() end end end @@ -180,68 +194,74 @@ defmodule ExPhoneNumber.ExtractionTest do describe ".maybe_extract_country_code/3" do context "case 1" do it "should return correct values" do - metadata = Metadata.get_for_region_code(RegionCodeFixture.us) - {result, number, phone_number} = maybe_extract_country_code("011112-3456789", metadata, true) + metadata = Metadata.get_for_region_code(RegionCodeFixture.us()) + + {result, number, phone_number} = maybe_extract_country_code("011112-3456789", metadata, true) + assert result assert 1 == phone_number.country_code assert "123456789" = number - assert CountryCodeSource.from_number_with_idd == phone_number.country_code_source + assert CountryCodeSource.from_number_with_idd() == phone_number.country_code_source end end context "case 2" do it "should return correct values" do - metadata = Metadata.get_for_region_code(RegionCodeFixture.us) - {result, _, phone_number} = maybe_extract_country_code("+6423456789", metadata, true) + metadata = Metadata.get_for_region_code(RegionCodeFixture.us()) + {result, _, phone_number} = maybe_extract_country_code("+6423456789", metadata, true) assert result assert 64 == phone_number.country_code - assert CountryCodeSource.from_number_with_plus_sign == phone_number.country_code_source + assert CountryCodeSource.from_number_with_plus_sign() == phone_number.country_code_source end end context "case 3" do it "should return correct values" do - metadata = Metadata.get_for_region_code(RegionCodeFixture.us) - {result, _, phone_number} = maybe_extract_country_code("+80012345678", metadata, true) + metadata = Metadata.get_for_region_code(RegionCodeFixture.us()) + {result, _, phone_number} = maybe_extract_country_code("+80012345678", metadata, true) assert result assert 800 == phone_number.country_code - assert CountryCodeSource.from_number_with_plus_sign == phone_number.country_code_source + assert CountryCodeSource.from_number_with_plus_sign() == phone_number.country_code_source end end context "case 4" do it "should return correct values" do - metadata = Metadata.get_for_region_code(RegionCodeFixture.us) - {result, _, phone_number} = maybe_extract_country_code("2345-6789", metadata, true) + metadata = Metadata.get_for_region_code(RegionCodeFixture.us()) + {result, _, phone_number} = maybe_extract_country_code("2345-6789", metadata, true) assert result assert 0 == phone_number.country_code - assert CountryCodeSource.from_default_country == phone_number.country_code_source + assert CountryCodeSource.from_default_country() == phone_number.country_code_source end end context "case 5" do it "should return correct values" do - metadata = Metadata.get_for_region_code(RegionCodeFixture.us) - {result, message} = maybe_extract_country_code("0119991123456789", metadata, true) + metadata = Metadata.get_for_region_code(RegionCodeFixture.us()) + {result, message} = maybe_extract_country_code("0119991123456789", metadata, true) refute result - assert message == ErrorMessages.invalid_country_code + assert message == ErrorMessages.invalid_country_code() end end context "case 6" do it "should return correct values" do - metadata = Metadata.get_for_region_code(RegionCodeFixture.us) + metadata = Metadata.get_for_region_code(RegionCodeFixture.us()) {result, _, phone_number} = maybe_extract_country_code("(1 610) 619 4466", metadata, true) assert result assert 1 = phone_number.country_code - assert CountryCodeSource.from_number_without_plus_sign == phone_number.country_code_source + + assert CountryCodeSource.from_number_without_plus_sign() == + phone_number.country_code_source end end context "case 7" do it "should return correct values" do - metadata = Metadata.get_for_region_code(RegionCodeFixture.us) + metadata = Metadata.get_for_region_code(RegionCodeFixture.us()) + {result, _, phone_number} = maybe_extract_country_code("(1 610) 619 4466", metadata, false) + assert result assert 1 = phone_number.country_code assert is_nil(phone_number.country_code_source) @@ -250,7 +270,7 @@ defmodule ExPhoneNumber.ExtractionTest do context "case 8" do it "should return correct values" do - metadata = Metadata.get_for_region_code(RegionCodeFixture.us) + metadata = Metadata.get_for_region_code(RegionCodeFixture.us()) {result, _, phone_number} = maybe_extract_country_code("(1 610) 619 446", metadata, false) assert result assert 0 = phone_number.country_code @@ -260,11 +280,11 @@ defmodule ExPhoneNumber.ExtractionTest do context "case 9" do it "should return correct values" do - metadata = Metadata.get_for_region_code(RegionCodeFixture.us) + metadata = Metadata.get_for_region_code(RegionCodeFixture.us()) {result, _, phone_number} = maybe_extract_country_code("(1 610) 619", metadata, true) assert result assert 0 = phone_number.country_code - assert CountryCodeSource.from_default_country == phone_number.country_code_source + assert CountryCodeSource.from_default_country() == phone_number.country_code_source end end end diff --git a/test/ex_phone_number/formatting_test.exs b/test/ex_phone_number/formatting_test.exs index ade7067..a176162 100644 --- a/test/ex_phone_number/formatting_test.exs +++ b/test/ex_phone_number/formatting_test.exs @@ -9,281 +9,353 @@ defmodule ExPhoneNumber.FormattingTest do describe ".format/2" do context "US number" do it "should return correct value #1" do - assert "650 253 0000" == format(PhoneNumberFixture.us_number, PhoneNumberFormats.national) + assert "650 253 0000" == + format(PhoneNumberFixture.us_number(), PhoneNumberFormats.national()) end it "should return correct value #2" do - assert "+1 650 253 0000" == format(PhoneNumberFixture.us_number, PhoneNumberFormats.international) + assert "+1 650 253 0000" == + format(PhoneNumberFixture.us_number(), PhoneNumberFormats.international()) end it "should return correct value #3" do - assert "800 253 0000" == format(PhoneNumberFixture.us_tollfree, PhoneNumberFormats.national) + assert "800 253 0000" == + format(PhoneNumberFixture.us_tollfree(), PhoneNumberFormats.national()) end it "should return correct value #4" do - assert "+1 800 253 0000" == format(PhoneNumberFixture.us_tollfree, PhoneNumberFormats.international) + assert "+1 800 253 0000" == + format(PhoneNumberFixture.us_tollfree(), PhoneNumberFormats.international()) end it "should return correct value #5" do - assert "900 253 0000" == format(PhoneNumberFixture.us_premium, PhoneNumberFormats.national) + assert "900 253 0000" == + format(PhoneNumberFixture.us_premium(), PhoneNumberFormats.national()) end it "should return correct value #6" do - assert "+1 900 253 0000" == format(PhoneNumberFixture.us_premium, PhoneNumberFormats.international) + assert "+1 900 253 0000" == + format(PhoneNumberFixture.us_premium(), PhoneNumberFormats.international()) end it "should return correct value #7" do - assert "tel:+1-900-253-0000" == format(PhoneNumberFixture.us_premium, PhoneNumberFormats.rfc3966) + assert "tel:+1-900-253-0000" == + format(PhoneNumberFixture.us_premium(), PhoneNumberFormats.rfc3966()) end it "should return correct value #8" do - assert "000-000-0000" == format(PhoneNumberFixture.us_spoof_with_raw_input, PhoneNumberFormats.national) + assert "000-000-0000" == + format( + PhoneNumberFixture.us_spoof_with_raw_input(), + PhoneNumberFormats.national() + ) end it "should return correct value #9" do - assert "0" == format(PhoneNumberFixture.us_spoof, PhoneNumberFormats.national) + assert "0" == format(PhoneNumberFixture.us_spoof(), PhoneNumberFormats.national()) end end context "BS number" do it "should return correct value #1" do - assert "242 365 1234" == format(PhoneNumberFixture.bs_number, PhoneNumberFormats.national) + assert "242 365 1234" == + format(PhoneNumberFixture.bs_number(), PhoneNumberFormats.national()) end it "should return correct value #2" do - assert "+1 242 365 1234" == format(PhoneNumberFixture.bs_number, PhoneNumberFormats.international) + assert "+1 242 365 1234" == + format(PhoneNumberFixture.bs_number(), PhoneNumberFormats.international()) end end context "GB number" do it "should return correct value #1" do - assert "(020) 7031 3000" == format(PhoneNumberFixture.gb_number, PhoneNumberFormats.national) + assert "(020) 7031 3000" == + format(PhoneNumberFixture.gb_number(), PhoneNumberFormats.national()) end it "should return correct value #2" do - assert "+44 20 7031 3000" == format(PhoneNumberFixture.gb_number, PhoneNumberFormats.international) + assert "+44 20 7031 3000" == + format(PhoneNumberFixture.gb_number(), PhoneNumberFormats.international()) end it "should return correct value #3" do - assert "(07912) 345 678" == format(PhoneNumberFixture.gb_mobile, PhoneNumberFormats.national) + assert "(07912) 345 678" == + format(PhoneNumberFixture.gb_mobile(), PhoneNumberFormats.national()) end it "should return correct value #4" do - assert "+44 7912 345 678" == format(PhoneNumberFixture.gb_mobile, PhoneNumberFormats.international) + assert "+44 7912 345 678" == + format(PhoneNumberFixture.gb_mobile(), PhoneNumberFormats.international()) end end context "DE number" do it "should return correct value #1" do - assert "030/1234" == format(PhoneNumberFixture.de_number2, PhoneNumberFormats.national) + assert "030/1234" == + format(PhoneNumberFixture.de_number2(), PhoneNumberFormats.national()) end it "should return correct value #2" do - assert "+49 30/1234" == format(PhoneNumberFixture.de_number2, PhoneNumberFormats.international) + assert "+49 30/1234" == + format(PhoneNumberFixture.de_number2(), PhoneNumberFormats.international()) end it "should return correct value #3" do - assert "tel:+49-30-1234" == format(PhoneNumberFixture.de_number2, PhoneNumberFormats.rfc3966) + assert "tel:+49-30-1234" == + format(PhoneNumberFixture.de_number2(), PhoneNumberFormats.rfc3966()) end it "should return correct value #4" do - assert "0291 123" == format(PhoneNumberFixture.de_number3, PhoneNumberFormats.national) + assert "0291 123" == + format(PhoneNumberFixture.de_number3(), PhoneNumberFormats.national()) end it "should return correct value #5" do - assert "+49 291 123" == format(PhoneNumberFixture.de_number3, PhoneNumberFormats.international) + assert "+49 291 123" == + format(PhoneNumberFixture.de_number3(), PhoneNumberFormats.international()) end it "should return correct value #6" do - assert "0291 12345678" == format(PhoneNumberFixture.de_number4, PhoneNumberFormats.national) + assert "0291 12345678" == + format(PhoneNumberFixture.de_number4(), PhoneNumberFormats.national()) end it "should return correct value #7" do - assert "+49 291 12345678" == format(PhoneNumberFixture.de_number4, PhoneNumberFormats.international) + assert "+49 291 12345678" == + format(PhoneNumberFixture.de_number4(), PhoneNumberFormats.international()) end it "should return correct value #8" do - assert "09123 12345" == format(PhoneNumberFixture.de_number5, PhoneNumberFormats.national) + assert "09123 12345" == + format(PhoneNumberFixture.de_number5(), PhoneNumberFormats.national()) end it "should return correct value #9" do - assert "+49 9123 12345" == format(PhoneNumberFixture.de_number5, PhoneNumberFormats.international) + assert "+49 9123 12345" == + format(PhoneNumberFixture.de_number5(), PhoneNumberFormats.international()) end it "should return correct value #10" do - assert "08021 2345" == format(PhoneNumberFixture.de_number6, PhoneNumberFormats.national) + assert "08021 2345" == + format(PhoneNumberFixture.de_number6(), PhoneNumberFormats.national()) end it "should return correct value #11" do - assert "+49 8021 2345" == format(PhoneNumberFixture.de_number6, PhoneNumberFormats.international) + assert "+49 8021 2345" == + format(PhoneNumberFixture.de_number6(), PhoneNumberFormats.international()) end it "should return correct value #12" do - assert "1234" == format(PhoneNumberFixture.de_short_number, PhoneNumberFormats.national) + assert "1234" == + format(PhoneNumberFixture.de_short_number(), PhoneNumberFormats.national()) end it "should return correct value #13" do - assert "+49 1234" == format(PhoneNumberFixture.de_short_number, PhoneNumberFormats.international) + assert "+49 1234" == + format(PhoneNumberFixture.de_short_number(), PhoneNumberFormats.international()) end it "should return correct value #14" do - assert "04134 1234" == format(PhoneNumberFixture.de_number7, PhoneNumberFormats.national) + assert "04134 1234" == + format(PhoneNumberFixture.de_number7(), PhoneNumberFormats.national()) end end context "IT numbers" do it "should return correct value #1" do - assert "02 3661 8300" == format(PhoneNumberFixture.it_number, PhoneNumberFormats.national) + assert "02 3661 8300" == + format(PhoneNumberFixture.it_number(), PhoneNumberFormats.national()) end it "should return correct value #2" do - assert "+39 02 3661 8300" == format(PhoneNumberFixture.it_number, PhoneNumberFormats.international) + assert "+39 02 3661 8300" == + format(PhoneNumberFixture.it_number(), PhoneNumberFormats.international()) end it "should return correct value #3" do - assert "+390236618300" == format(PhoneNumberFixture.it_number, PhoneNumberFormats.e164) + assert "+390236618300" == + format(PhoneNumberFixture.it_number(), PhoneNumberFormats.e164()) end it "should return correct value #4" do - assert "345 678 901" == format(PhoneNumberFixture.it_mobile, PhoneNumberFormats.national) + assert "345 678 901" == + format(PhoneNumberFixture.it_mobile(), PhoneNumberFormats.national()) end it "should return correct value #5" do - assert "+39 345 678 901" == format(PhoneNumberFixture.it_mobile, PhoneNumberFormats.international) + assert "+39 345 678 901" == + format(PhoneNumberFixture.it_mobile(), PhoneNumberFormats.international()) end it "should return correct value #6" do - assert "+39345678901" == format(PhoneNumberFixture.it_mobile, PhoneNumberFormats.e164) + assert "+39345678901" == format(PhoneNumberFixture.it_mobile(), PhoneNumberFormats.e164()) end end context "AU numbers" do it "should return correct value #1" do - assert "02 3661 8300" == format(PhoneNumberFixture.au_number, PhoneNumberFormats.national) + assert "02 3661 8300" == + format(PhoneNumberFixture.au_number(), PhoneNumberFormats.national()) end it "should return correct value #2" do - assert "+61 2 3661 8300" == format(PhoneNumberFixture.au_number, PhoneNumberFormats.international) + assert "+61 2 3661 8300" == + format(PhoneNumberFixture.au_number(), PhoneNumberFormats.international()) end it "should return correct value #3" do - assert "+61236618300" == format(PhoneNumberFixture.au_number, PhoneNumberFormats.e164) + assert "+61236618300" == format(PhoneNumberFixture.au_number(), PhoneNumberFormats.e164()) end it "should return correct value #4" do - assert "1800 123 456" == format(PhoneNumberFixture.au_number2, PhoneNumberFormats.national) + assert "1800 123 456" == + format(PhoneNumberFixture.au_number2(), PhoneNumberFormats.national()) end it "should return correct value #5" do - assert "+61 1800 123 456" == format(PhoneNumberFixture.au_number2, PhoneNumberFormats.international) + assert "+61 1800 123 456" == + format(PhoneNumberFixture.au_number2(), PhoneNumberFormats.international()) end it "should return correct value #6" do - assert "+611800123456" == format(PhoneNumberFixture.au_number2, PhoneNumberFormats.e164) + assert "+611800123456" == + format(PhoneNumberFixture.au_number2(), PhoneNumberFormats.e164()) end end context "AR numbers" do it "should return correct value #1" do - assert "011 8765-4321" == format(PhoneNumberFixture.ar_number, PhoneNumberFormats.national) + assert "011 8765-4321" == + format(PhoneNumberFixture.ar_number(), PhoneNumberFormats.national()) end it "should return correct value #2" do - assert "+54 11 8765-4321" == format(PhoneNumberFixture.ar_number, PhoneNumberFormats.international) + assert "+54 11 8765-4321" == + format(PhoneNumberFixture.ar_number(), PhoneNumberFormats.international()) end it "should return correct value #3" do - assert "+541187654321" == format(PhoneNumberFixture.ar_number, PhoneNumberFormats.e164) + assert "+541187654321" == + format(PhoneNumberFixture.ar_number(), PhoneNumberFormats.e164()) end it "should return correct value #4" do - assert "011 15 8765-4321" == format(PhoneNumberFixture.ar_mobile, PhoneNumberFormats.national) + assert "011 15 8765-4321" == + format(PhoneNumberFixture.ar_mobile(), PhoneNumberFormats.national()) end it "should return correct value #5" do - assert "+54 9 11 8765 4321" == format(PhoneNumberFixture.ar_mobile, PhoneNumberFormats.international) + assert "+54 9 11 8765 4321" == + format(PhoneNumberFixture.ar_mobile(), PhoneNumberFormats.international()) end it "should return correct value #6" do - assert "+5491187654321" == format(PhoneNumberFixture.ar_mobile, PhoneNumberFormats.e164) + assert "+5491187654321" == + format(PhoneNumberFixture.ar_mobile(), PhoneNumberFormats.e164()) end end context "MX numbers" do it "should return correct value #1" do - assert "045 234 567 8900" == format(PhoneNumberFixture.mx_mobile1, PhoneNumberFormats.national) + assert "045 234 567 8900" == + format(PhoneNumberFixture.mx_mobile1(), PhoneNumberFormats.national()) end it "should return correct value #2" do - assert "+52 1 234 567 8900" == format(PhoneNumberFixture.mx_mobile1, PhoneNumberFormats.international) + assert "+52 1 234 567 8900" == + format(PhoneNumberFixture.mx_mobile1(), PhoneNumberFormats.international()) end it "should return correct value #3" do - assert "+5212345678900" == format(PhoneNumberFixture.mx_mobile1, PhoneNumberFormats.e164) + assert "+5212345678900" == + format(PhoneNumberFixture.mx_mobile1(), PhoneNumberFormats.e164()) end it "should return correct value #4" do - assert "045 55 1234 5678" == format(PhoneNumberFixture.mx_mobile2, PhoneNumberFormats.national) + assert "045 55 1234 5678" == + format(PhoneNumberFixture.mx_mobile2(), PhoneNumberFormats.national()) end it "should return correct value #5" do - assert "+52 1 55 1234 5678" == format(PhoneNumberFixture.mx_mobile2, PhoneNumberFormats.international) + assert "+52 1 55 1234 5678" == + format(PhoneNumberFixture.mx_mobile2(), PhoneNumberFormats.international()) end it "should return correct value #6" do - assert "+5215512345678" == format(PhoneNumberFixture.mx_mobile2, PhoneNumberFormats.e164) + assert "+5215512345678" == + format(PhoneNumberFixture.mx_mobile2(), PhoneNumberFormats.e164()) end it "should return correct value #7" do - assert "01 33 1234 5678" == format(PhoneNumberFixture.mx_number1, PhoneNumberFormats.national) + assert "01 33 1234 5678" == + format(PhoneNumberFixture.mx_number1(), PhoneNumberFormats.national()) end it "should return correct value #8" do - assert "+52 33 1234 5678" == format(PhoneNumberFixture.mx_number1, PhoneNumberFormats.international) + assert "+52 33 1234 5678" == + format(PhoneNumberFixture.mx_number1(), PhoneNumberFormats.international()) end it "should return correct value #9" do - assert "+523312345678" == format(PhoneNumberFixture.mx_number1, PhoneNumberFormats.e164) + assert "+523312345678" == + format(PhoneNumberFixture.mx_number1(), PhoneNumberFormats.e164()) end it "should return correct value #10" do - assert "01 821 123 4567" == format(PhoneNumberFixture.mx_number2, PhoneNumberFormats.national) + assert "01 821 123 4567" == + format(PhoneNumberFixture.mx_number2(), PhoneNumberFormats.national()) end it "should return correct value #11" do - assert "+52 821 123 4567" == format(PhoneNumberFixture.mx_number2, PhoneNumberFormats.international) + assert "+52 821 123 4567" == + format(PhoneNumberFixture.mx_number2(), PhoneNumberFormats.international()) end it "should return correct value #12" do - assert "+528211234567" == format(PhoneNumberFixture.mx_number2, PhoneNumberFormats.e164) + assert "+528211234567" == + format(PhoneNumberFixture.mx_number2(), PhoneNumberFormats.e164()) end end context "E164 numbers" do it "should return correct value #0" do - assert "+16502530000" == format(PhoneNumberFixture.us_number, PhoneNumberFormats.e164) + assert "+16502530000" == format(PhoneNumberFixture.us_number(), PhoneNumberFormats.e164()) end it "should return correct value #1" do - assert "+4930123456" == format(PhoneNumberFixture.de_number, PhoneNumberFormats.e164) + assert "+4930123456" == format(PhoneNumberFixture.de_number(), PhoneNumberFormats.e164()) end it "should return correct value #2" do - assert "+80012345678" == format(PhoneNumberFixture.international_toll_free, PhoneNumberFormats.e164) + assert "+80012345678" == + format(PhoneNumberFixture.international_toll_free(), PhoneNumberFormats.e164()) end end context "Numbers with extensions" do it "should return correct value #0" do - assert "03-331 6005 ext. 1234" == format(%{PhoneNumberFixture.nz_number | extension: "1234"}, PhoneNumberFormats.national) + assert "03-331 6005 ext. 1234" == + format( + %{PhoneNumberFixture.nz_number() | extension: "1234"}, + PhoneNumberFormats.national() + ) end it "should return correct value #1" do - assert "tel:+64-3-331-6005;ext=1234" == format(%{PhoneNumberFixture.nz_number | extension: "1234"}, PhoneNumberFormats.rfc3966) + assert "tel:+64-3-331-6005;ext=1234" == + format( + %{PhoneNumberFixture.nz_number() | extension: "1234"}, + PhoneNumberFormats.rfc3966() + ) end it "should return correct value #2" do - assert "650 253 0000 extn. 4567" == format(%{PhoneNumberFixture.us_number | extension: "4567"}, PhoneNumberFormats.national) + assert "650 253 0000 extn. 4567" == + format( + %{PhoneNumberFixture.us_number() | extension: "4567"}, + PhoneNumberFormats.national() + ) end end end diff --git a/test/ex_phone_number/metadata_test.exs b/test/ex_phone_number/metadata_test.exs index edfed3c..f3b3e4a 100644 --- a/test/ex_phone_number/metadata_test.exs +++ b/test/ex_phone_number/metadata_test.exs @@ -11,11 +11,11 @@ defmodule ExPhoneNumber.MetadataTest do describe ".get_for_region_code/1" do context "US region_code" do setup do - {:ok, us_metadata: get_for_region_code(RegionCodeFixture.us)} + {:ok, us_metadata: get_for_region_code(RegionCodeFixture.us())} end it "returns valid id", state do - assert RegionCodeFixture.us == state[:us_metadata].id + assert RegionCodeFixture.us() == state[:us_metadata].id end it "returns valid country_code", state do @@ -43,11 +43,12 @@ defmodule ExPhoneNumber.MetadataTest do end it "returns valid general.national_number_pattern", state do - assert ~r/[13-689]\d{9}|2[0-35-9]\d{8}/ == state[:us_metadata].general.national_number_pattern + assert ~r/[13-689]\d{9}|2[0-35-9]\d{8}/ == + state[:us_metadata].general.national_number_pattern end it "returns valid general.possible_lengths", state do - assert [7,10] == state[:us_metadata].general.possible_lengths + assert [7, 10] == state[:us_metadata].general.possible_lengths end it "returns valid toll_free.possible_lengths", state do @@ -59,21 +60,23 @@ defmodule ExPhoneNumber.MetadataTest do end it "returns valid shared_cost.national_number_pattern", state do - assert Values.description_default_pattern == state[:us_metadata].shared_cost.national_number_pattern + assert Values.description_default_pattern() == + state[:us_metadata].shared_cost.national_number_pattern end it "returns valid shared_cost.possible_lengths", state do - assert Values.description_default_length == state[:us_metadata].shared_cost.possible_lengths + assert Values.description_default_length() == + state[:us_metadata].shared_cost.possible_lengths end end context "DE region_code" do setup do - {:ok, de_metadata: get_for_region_code(RegionCodeFixture.de)} + {:ok, de_metadata: get_for_region_code(RegionCodeFixture.de())} end it "returns valid id", state do - assert RegionCodeFixture.de == state[:de_metadata].id + assert RegionCodeFixture.de() == state[:de_metadata].id end it "returns valid country_code", state do @@ -97,19 +100,22 @@ defmodule ExPhoneNumber.MetadataTest do end it "returns valid number_format(5).leading_digits_pattern(0)", state do - assert ~r/900/ == Enum.at(Enum.at(state[:de_metadata].number_format, 5).leading_digits_pattern, 0) + assert ~r/900/ == + Enum.at(Enum.at(state[:de_metadata].number_format, 5).leading_digits_pattern, 0) end it "returns valid number_format(5).pattern", state do - assert ~r/(\d{3})(\d{3,4})(\d{4})/ == Enum.at(state[:de_metadata].number_format, 5).pattern + assert ~r/(\d{3})(\d{3,4})(\d{4})/ == + Enum.at(state[:de_metadata].number_format, 5).pattern end it "returns valid number_format(5).format", state do - assert "\\g{1} \\g{2} \\g{3}" == Enum.at(state[:de_metadata].number_format, 5).format + assert "\\g{1} \\g{2} \\g{3}" == Enum.at(state[:de_metadata].number_format, 5).format end it "returns valid fixed_line.national_number_pattern", state do - assert ~r/(?:[24-6]\d{2}|3[03-9]\d|[789](?:0[2-9]|[1-9]\d))\d{1,8}/ == state[:de_metadata].fixed_line.national_number_pattern + assert ~r/(?:[24-6]\d{2}|3[03-9]\d|[789](?:0[2-9]|[1-9]\d))\d{1,8}/ == + state[:de_metadata].fixed_line.national_number_pattern end it "returns valid fixed_line.possible_lengths", state do @@ -125,17 +131,18 @@ defmodule ExPhoneNumber.MetadataTest do end it "returns valid premium_rate.national_number_pattern", state do - assert ~r/900([135]\d{6}|9\d{7})/ == state[:de_metadata].premium_rate.national_number_pattern + assert ~r/900([135]\d{6}|9\d{7})/ == + state[:de_metadata].premium_rate.national_number_pattern end end context "AR region_code" do setup do - {:ok, ar_metadata: get_for_region_code(RegionCodeFixture.ar)} + {:ok, ar_metadata: get_for_region_code(RegionCodeFixture.ar())} end it "returns valid id", state do - assert RegionCodeFixture.ar == state[:ar_metadata].id + assert RegionCodeFixture.ar() == state[:ar_metadata].id end it "returns valid country_code", state do @@ -163,15 +170,18 @@ defmodule ExPhoneNumber.MetadataTest do end it "returns valid number_format(3).pattern", state do - assert ~r/(\d)(\d{4})(\d{2})(\d{4})/ == Enum.at(state[:ar_metadata].number_format, 3).pattern + assert ~r/(\d)(\d{4})(\d{2})(\d{4})/ == + Enum.at(state[:ar_metadata].number_format, 3).pattern end it "returns valid intl_number_format(3).pattern", state do - assert ~r/(\d)(\d{4})(\d{2})(\d{4})/ == Enum.at(state[:ar_metadata].intl_number_format, 3).pattern + assert ~r/(\d)(\d{4})(\d{2})(\d{4})/ == + Enum.at(state[:ar_metadata].intl_number_format, 3).pattern end it "returns valid intl_number_format(3).format", state do - assert "\\g{1} \\g{2} \\g{3} \\g{4}" == Enum.at(state[:ar_metadata].intl_number_format, 3).format + assert "\\g{1} \\g{2} \\g{3} \\g{4}" == + Enum.at(state[:ar_metadata].intl_number_format, 3).format end end end @@ -183,7 +193,7 @@ defmodule ExPhoneNumber.MetadataTest do end it "returns valid id", state do - assert RegionCodeFixture.un001 == state[:un001_metadata].id + assert RegionCodeFixture.un001() == state[:un001_metadata].id end it "returns valid country_code", state do @@ -215,17 +225,17 @@ defmodule ExPhoneNumber.MetadataTest do describe ".is_supported_region?/1" do context "US" do it "returns true" do - assert is_supported_region?(RegionCodeFixture.us) + assert is_supported_region?(RegionCodeFixture.us()) end end context "001" do it "returns false" do - refute is_supported_region?(RegionCodeFixture.un001) + refute is_supported_region?(RegionCodeFixture.un001()) end it "returns false for calling code" do - refute Enum.any?(get_supported_regions(), fn(region_code) -> region_code == "800" end) + refute Enum.any?(get_supported_regions(), fn region_code -> region_code == "800" end) end end end @@ -239,7 +249,9 @@ defmodule ExPhoneNumber.MetadataTest do context "test all the calling codes" do it "contains all the calling codes" do - assert Enum.all?(get_supported_global_network_calling_codes(), fn(calling_code) -> get_region_code_for_country_code(calling_code) == RegionCodeFixture.un001 end) + assert Enum.all?(get_supported_global_network_calling_codes(), fn calling_code -> + get_region_code_for_country_code(calling_code) == RegionCodeFixture.un001() + end) end end end @@ -261,31 +273,31 @@ defmodule ExPhoneNumber.MetadataTest do describe ".get_region_code_for_country_code/1" do context "1" do it "returns correct value" do - assert RegionCodeFixture.us == get_region_code_for_country_code(1) + assert RegionCodeFixture.us() == get_region_code_for_country_code(1) end end context "44" do it "returns correct value" do - assert RegionCodeFixture.gb == get_region_code_for_country_code(44) + assert RegionCodeFixture.gb() == get_region_code_for_country_code(44) end end context "49" do it "returns correct value" do - assert RegionCodeFixture.de == get_region_code_for_country_code(49) + assert RegionCodeFixture.de() == get_region_code_for_country_code(49) end end context "800" do it "returns correct value" do - assert RegionCodeFixture.un001 == get_region_code_for_country_code(800) + assert RegionCodeFixture.un001() == get_region_code_for_country_code(800) end end context "979" do it "returns correct value" do - assert RegionCodeFixture.un001 == get_region_code_for_country_code(979) + assert RegionCodeFixture.un001() == get_region_code_for_country_code(979) end end end @@ -294,29 +306,29 @@ defmodule ExPhoneNumber.MetadataTest do context "1" do it "should contains US BS" do list = get_region_codes_for_country_code(1) - assert Enum.any?(list, &(&1 == RegionCodeFixture.us)) - assert Enum.any?(list, &(&1 == RegionCodeFixture.bs)) + assert Enum.any?(list, &(&1 == RegionCodeFixture.us())) + assert Enum.any?(list, &(&1 == RegionCodeFixture.bs())) end end context "44" do it "should contains GB" do list = get_region_codes_for_country_code(44) - assert Enum.any?(list, &(&1 == RegionCodeFixture.gb)) + assert Enum.any?(list, &(&1 == RegionCodeFixture.gb())) end end context "49" do it "should contains DE" do list = get_region_codes_for_country_code(49) - assert Enum.any?(list, &(&1 == RegionCodeFixture.de)) + assert Enum.any?(list, &(&1 == RegionCodeFixture.de())) end end context "800" do it "should contains 001" do list = get_region_codes_for_country_code(800) - assert Enum.any?(list, &(&1 == RegionCodeFixture.un001)) + assert Enum.any?(list, &(&1 == RegionCodeFixture.un001())) end end @@ -330,37 +342,37 @@ defmodule ExPhoneNumber.MetadataTest do describe ".get_country_code_for_region_code/1" do context "US" do it "returns correct value" do - assert 1 == get_country_code_for_region_code RegionCodeFixture.us + assert 1 == get_country_code_for_region_code(RegionCodeFixture.us()) end end context "NZ" do it "returns correct value" do - assert 64 == get_country_code_for_region_code RegionCodeFixture.nz + assert 64 == get_country_code_for_region_code(RegionCodeFixture.nz()) end end context "nil" do it "returns correct value" do - assert 0 == get_country_code_for_region_code nil + assert 0 == get_country_code_for_region_code(nil) end end context "ZZ" do it "returns correct value" do - assert 0 == get_country_code_for_region_code RegionCodeFixture.zz + assert 0 == get_country_code_for_region_code(RegionCodeFixture.zz()) end end context "001" do it "returns correct value" do - assert 0 == get_country_code_for_region_code RegionCodeFixture.un001 + assert 0 == get_country_code_for_region_code(RegionCodeFixture.un001()) end end context "CS" do it "returns correct value" do - assert 0 == get_country_code_for_region_code "CS" + assert 0 == get_country_code_for_region_code("CS") end end end @@ -368,35 +380,41 @@ defmodule ExPhoneNumber.MetadataTest do describe ".get_region_code_for_number/1" do context "BS" do it "return correct value" do - assert RegionCodeFixture.bs == get_region_code_for_number(PhoneNumberFixture.bs_number) + assert RegionCodeFixture.bs() == + get_region_code_for_number(PhoneNumberFixture.bs_number()) end end context "YT" do it "return correct value" do - assert RegionCodeFixture.yt == get_region_code_for_number(PhoneNumberFixture.re_number_invalid) + assert RegionCodeFixture.yt() == + get_region_code_for_number(PhoneNumberFixture.re_number_invalid()) end end context "US" do it "return correct value" do - assert RegionCodeFixture.us == get_region_code_for_number(PhoneNumberFixture.us_number) + assert RegionCodeFixture.us() == + get_region_code_for_number(PhoneNumberFixture.us_number()) end end context "GB" do it "return correct value" do - assert RegionCodeFixture.gb == get_region_code_for_number(PhoneNumberFixture.gb_mobile) + assert RegionCodeFixture.gb() == + get_region_code_for_number(PhoneNumberFixture.gb_mobile()) end end context "001" do it "return correct value for International Toll Free" do - assert RegionCodeFixture.un001 == get_region_code_for_number(PhoneNumberFixture.international_toll_free) + assert RegionCodeFixture.un001() == + get_region_code_for_number(PhoneNumberFixture.international_toll_free()) end it "return correct value for Universal Premium Rate" do - assert RegionCodeFixture.un001 == get_region_code_for_number(PhoneNumberFixture.universal_premium_rate) + assert RegionCodeFixture.un001() == + get_region_code_for_number(PhoneNumberFixture.universal_premium_rate()) end end end @@ -404,31 +422,31 @@ defmodule ExPhoneNumber.MetadataTest do describe ".get_ndd_prefix_for_region_code/2" do context "US, false" do it "returns the correct value" do - assert "1" == get_ndd_prefix_for_region_code(RegionCodeFixture.us, false) + assert "1" == get_ndd_prefix_for_region_code(RegionCodeFixture.us(), false) end end context "BS, false" do it "returns the correct value" do - assert "1" == get_ndd_prefix_for_region_code(RegionCodeFixture.bs, false) + assert "1" == get_ndd_prefix_for_region_code(RegionCodeFixture.bs(), false) end end context "NZ, false" do it "returns the correct value" do - assert "0" == get_ndd_prefix_for_region_code(RegionCodeFixture.nz, false) + assert "0" == get_ndd_prefix_for_region_code(RegionCodeFixture.nz(), false) end end context "A0, false" do it "returns the correct value" do - assert "0~0" == get_ndd_prefix_for_region_code(RegionCodeFixture.ao, false) + assert "0~0" == get_ndd_prefix_for_region_code(RegionCodeFixture.ao(), false) end end context "A0, true" do it "returns the correct value" do - assert "00" == get_ndd_prefix_for_region_code(RegionCodeFixture.ao, true) + assert "00" == get_ndd_prefix_for_region_code(RegionCodeFixture.ao(), true) end end @@ -440,19 +458,19 @@ defmodule ExPhoneNumber.MetadataTest do context "ZZ, false" do it "returns the correct value" do - refute get_ndd_prefix_for_region_code(RegionCodeFixture.zz, false) + refute get_ndd_prefix_for_region_code(RegionCodeFixture.zz(), false) end end context "UN001, false" do it "returns the correct value" do - refute get_ndd_prefix_for_region_code(RegionCodeFixture.un001, false) + refute get_ndd_prefix_for_region_code(RegionCodeFixture.un001(), false) end end context "CS, false" do it "returns the correct value" do - refute get_ndd_prefix_for_region_code(RegionCodeFixture.cs, false) + refute get_ndd_prefix_for_region_code(RegionCodeFixture.cs(), false) end end end @@ -460,31 +478,31 @@ defmodule ExPhoneNumber.MetadataTest do describe ".is_nanpa_country?/1" do context "US" do it "returns true" do - assert is_nanpa_country?(RegionCodeFixture.us) + assert is_nanpa_country?(RegionCodeFixture.us()) end end context "BS" do it "returns true" do - assert is_nanpa_country?(RegionCodeFixture.bs) + assert is_nanpa_country?(RegionCodeFixture.bs()) end end context "DE" do it "returns false" do - refute is_nanpa_country?(RegionCodeFixture.de) + refute is_nanpa_country?(RegionCodeFixture.de()) end end context "ZZ" do it "returns false" do - refute is_nanpa_country?(RegionCodeFixture.zz) + refute is_nanpa_country?(RegionCodeFixture.zz()) end end context "UN001" do it "returns false" do - refute is_nanpa_country?(RegionCodeFixture.un001) + refute is_nanpa_country?(RegionCodeFixture.un001()) end end diff --git a/test/ex_phone_number/model/phone_number_test.exs b/test/ex_phone_number/model/phone_number_test.exs index 3b2de27..1473882 100644 --- a/test/ex_phone_number/model/phone_number_test.exs +++ b/test/ex_phone_number/model/phone_number_test.exs @@ -29,6 +29,5 @@ defmodule ExPhoneNumber.Model.PhoneNumberTest do assert "12345678" == get_national_significant_number(international_toll_free()) end end - end end diff --git a/test/ex_phone_number/parsing_test.exs b/test/ex_phone_number/parsing_test.exs index 69e3a60..e31f7cf 100644 --- a/test/ex_phone_number/parsing_test.exs +++ b/test/ex_phone_number/parsing_test.exs @@ -9,78 +9,77 @@ defmodule ExPhoneNumber.ParsingTest do describe ".is_possible_number?/2" do context "US region" do it "should return true #1" do - assert is_possible_number?("+1 650 253 0000", RegionCodeFixture.us) + assert is_possible_number?("+1 650 253 0000", RegionCodeFixture.us()) end it "should return true #2" do - assert is_possible_number?("+1 650 GOO OGLE", RegionCodeFixture.us) + assert is_possible_number?("+1 650 GOO OGLE", RegionCodeFixture.us()) end it "should return true #3" do - assert is_possible_number?("(650) 253-0000", RegionCodeFixture.us) + assert is_possible_number?("(650) 253-0000", RegionCodeFixture.us()) end it "should return true #4" do - assert is_possible_number?("253-0000", RegionCodeFixture.us) + assert is_possible_number?("253-0000", RegionCodeFixture.us()) end it "should return false #1" do - refute is_possible_number?("+1 650 253 00000", RegionCodeFixture.us) + refute is_possible_number?("+1 650 253 00000", RegionCodeFixture.us()) end it "should return false #2" do - refute is_possible_number?("(650) 253-00000", RegionCodeFixture.us) + refute is_possible_number?("(650) 253-00000", RegionCodeFixture.us()) end it "should return false #3" do - refute is_possible_number?("I want a Pizza", RegionCodeFixture.us) + refute is_possible_number?("I want a Pizza", RegionCodeFixture.us()) end it "should return false #4" do - refute is_possible_number?("253-000", RegionCodeFixture.us) + refute is_possible_number?("253-000", RegionCodeFixture.us()) end end context "GB region" do it "should return true #1" do - assert is_possible_number?("+1 650 253 0000", RegionCodeFixture.gb) + assert is_possible_number?("+1 650 253 0000", RegionCodeFixture.gb()) end it "should return true #2" do - assert is_possible_number?("+44 20 7031 3000", RegionCodeFixture.gb) + assert is_possible_number?("+44 20 7031 3000", RegionCodeFixture.gb()) end it "should return true #3" do - assert is_possible_number?("(020) 7031 3000", RegionCodeFixture.gb) + assert is_possible_number?("(020) 7031 3000", RegionCodeFixture.gb()) end it "should return true #4" do - assert is_possible_number?("7031 3000", RegionCodeFixture.gb) + assert is_possible_number?("7031 3000", RegionCodeFixture.gb()) end it "should return false #1" do - refute is_possible_number?("1 3000", RegionCodeFixture.gb) + refute is_possible_number?("1 3000", RegionCodeFixture.gb()) end it "should return false #2" do - refute is_possible_number?("+44 300", RegionCodeFixture.gb) + refute is_possible_number?("+44 300", RegionCodeFixture.gb()) end - end context "NZ region" do it "should return true #1" do - assert is_possible_number?("3331 6005", RegionCodeFixture.nz) + assert is_possible_number?("3331 6005", RegionCodeFixture.nz()) end end context "UN001 region" do it "should return true #1" do - assert is_possible_number?("+800 1234 5678", RegionCodeFixture.un001) + assert is_possible_number?("+800 1234 5678", RegionCodeFixture.un001()) end it "should return false #1" do - refute is_possible_number?("+800 1234 5678 9", RegionCodeFixture.un001) + refute is_possible_number?("+800 1234 5678 9", RegionCodeFixture.un001()) end end end @@ -88,859 +87,884 @@ defmodule ExPhoneNumber.ParsingTest do describe ".parse/2" do context "NZ number" do it "should return correct value #1" do - {result, phone_number} = parse("033316005", RegionCodeFixture.nz) + {result, phone_number} = parse("033316005", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end it "should return correct value #2" do - {result, phone_number} = parse("33316005", RegionCodeFixture.nz) + {result, phone_number} = parse("33316005", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end it "should return correct value #3" do - {result, phone_number} = parse("03-331 6005", RegionCodeFixture.nz) + {result, phone_number} = parse("03-331 6005", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end it "should return correct value #4" do - {result, phone_number} = parse("03 331 6005", RegionCodeFixture.nz) + {result, phone_number} = parse("03 331 6005", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end it "should return correct value #5" do - {result, phone_number} = parse("tel:03-331-6005;phone-context=+64", RegionCodeFixture.nz) + {result, phone_number} = parse("tel:03-331-6005;phone-context=+64", RegionCodeFixture.nz()) + assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end it "should return correct value #6" do - {result, phone_number} = parse("tel:331-6005;phone-context=+64-3", RegionCodeFixture.nz) + {result, phone_number} = parse("tel:331-6005;phone-context=+64-3", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end it "should return correct value #7" do - {result, phone_number} = parse("tel:331-6005;phone-context=+64-3", RegionCodeFixture.us) + {result, phone_number} = parse("tel:331-6005;phone-context=+64-3", RegionCodeFixture.us()) assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end it "should return correct value #8" do - {result, phone_number} = parse("My number is tel:03-331-6005;phone-context=+64", RegionCodeFixture.nz) + {result, phone_number} = parse("My number is tel:03-331-6005;phone-context=+64", RegionCodeFixture.nz()) + assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end it "should return correct value #9" do - {result, phone_number} = parse("tel:03-331-6005;phone-context=+64;a=%A1", RegionCodeFixture.nz) + {result, phone_number} = parse("tel:03-331-6005;phone-context=+64;a=%A1", RegionCodeFixture.nz()) + assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end it "should return correct value #10" do - {result, phone_number} = parse("tel:03-331-6005;isub=12345;phone-context=+64", RegionCodeFixture.nz) + {result, phone_number} = parse("tel:03-331-6005;isub=12345;phone-context=+64", RegionCodeFixture.nz()) + assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end it "should return correct value #11" do - {result, phone_number} = parse("tel:+64-3-331-6005;isub=12345", RegionCodeFixture.nz) + {result, phone_number} = parse("tel:+64-3-331-6005;isub=12345", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end it "should return correct value #12" do - {result, phone_number} = parse("03-331-6005;phone-context=+64", RegionCodeFixture.nz) + {result, phone_number} = parse("03-331-6005;phone-context=+64", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end it "should return correct value #13" do - {result, phone_number} = parse("0064 3 331 6005", RegionCodeFixture.nz) + {result, phone_number} = parse("0064 3 331 6005", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end it "should return correct value #14" do - {result, phone_number} = parse("01164 3 331 6005", RegionCodeFixture.us) + {result, phone_number} = parse("01164 3 331 6005", RegionCodeFixture.us()) assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end it "should return correct value #15" do - {result, phone_number} = parse("+64 3 331 6005", RegionCodeFixture.us) + {result, phone_number} = parse("+64 3 331 6005", RegionCodeFixture.us()) assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end it "should return correct value #16" do - {result, phone_number} = parse("+01164 3 331 6005", RegionCodeFixture.us) + {result, phone_number} = parse("+01164 3 331 6005", RegionCodeFixture.us()) assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end it "should return correct value #17" do - {result, phone_number} = parse("+0064 3 331 6005", RegionCodeFixture.nz) + {result, phone_number} = parse("+0064 3 331 6005", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end it "should return correct value #18" do - {result, phone_number} = parse("+ 00 64 3 331 6005", RegionCodeFixture.nz) + {result, phone_number} = parse("+ 00 64 3 331 6005", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end end context "NZ number3" do it "should return correct value" do - {result, phone_number} = parse("64(0)64123456", RegionCodeFixture.nz) + {result, phone_number} = parse("64(0)64123456", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.nz_number3 == phone_number + assert PhoneNumberFixture.nz_number3() == phone_number end end context "NZ short number" do it "should return correct value" do - {result, phone_number} = parse("12", RegionCodeFixture.nz) + {result, phone_number} = parse("12", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.nz_short_number == phone_number + assert PhoneNumberFixture.nz_short_number() == phone_number end end context "DE number" do it "should return correct value" do - {result, phone_number} = parse("301/23456", RegionCodeFixture.de) + {result, phone_number} = parse("301/23456", RegionCodeFixture.de()) assert :ok == result - assert PhoneNumberFixture.de_number == phone_number + assert PhoneNumberFixture.de_number() == phone_number end end context "JP star number" do it "should return correct value" do - {result, phone_number} = parse("+81 *2345", RegionCodeFixture.jp) + {result, phone_number} = parse("+81 *2345", RegionCodeFixture.jp()) assert :ok == result - assert PhoneNumberFixture.jp_star_number == phone_number + assert PhoneNumberFixture.jp_star_number() == phone_number end end context "US number2" do it "should return correct value" do - {result, phone_number} = parse("123-456-7890", RegionCodeFixture.us) + {result, phone_number} = parse("123-456-7890", RegionCodeFixture.us()) assert :ok == result - assert PhoneNumberFixture.us_number2 == phone_number + assert PhoneNumberFixture.us_number2() == phone_number end end context "US local number" do it "should return correct value #1" do - {result, phone_number} = parse("tel:253-0000;phone-context=www.google.com", RegionCodeFixture.us) + {result, phone_number} = parse("tel:253-0000;phone-context=www.google.com", RegionCodeFixture.us()) + assert :ok == result - assert PhoneNumberFixture.us_local_number == phone_number + assert PhoneNumberFixture.us_local_number() == phone_number end it "should return correct value #2" do - {result, phone_number} = parse("tel:253-0000;isub=12345;phone-context=www.google.com", RegionCodeFixture.us) + {result, phone_number} = parse("tel:253-0000;isub=12345;phone-context=www.google.com", RegionCodeFixture.us()) + assert :ok == result - assert PhoneNumberFixture.us_local_number == phone_number + assert PhoneNumberFixture.us_local_number() == phone_number end it "should return correct value #3" do - {result, phone_number} = parse("tel:2530000;isub=12345;phone-context=1-650", RegionCodeFixture.us) + {result, phone_number} = parse("tel:2530000;isub=12345;phone-context=1-650", RegionCodeFixture.us()) + assert :ok == result - assert PhoneNumberFixture.us_local_number == phone_number + assert PhoneNumberFixture.us_local_number() == phone_number end it "should return correct value #4" do - {result, phone_number} = parse("tel:2530000;isub=12345;phone-context=1234.com", RegionCodeFixture.us) + {result, phone_number} = parse("tel:2530000;isub=12345;phone-context=1234.com", RegionCodeFixture.us()) + assert :ok == result - assert PhoneNumberFixture.us_local_number == phone_number + assert PhoneNumberFixture.us_local_number() == phone_number end end context "AR numbers" do it "should return correct value #1" do - {result, phone_number} = parse("+54 9 343 555 1212", RegionCodeFixture.ar) + {result, phone_number} = parse("+54 9 343 555 1212", RegionCodeFixture.ar()) assert :ok == result - assert PhoneNumberFixture.ar_mobile2 == phone_number + assert PhoneNumberFixture.ar_mobile2() == phone_number end it "should return correct value #2" do - {result, phone_number} = parse("0343 15 555 1212", RegionCodeFixture.ar) + {result, phone_number} = parse("0343 15 555 1212", RegionCodeFixture.ar()) assert :ok == result - assert PhoneNumberFixture.ar_mobile2 == phone_number + assert PhoneNumberFixture.ar_mobile2() == phone_number end it "should return correct value #3" do - {result, phone_number} = parse("+54 9 3715 65 4320", RegionCodeFixture.ar) + {result, phone_number} = parse("+54 9 3715 65 4320", RegionCodeFixture.ar()) assert :ok == result - assert PhoneNumberFixture.ar_mobile3 == phone_number + assert PhoneNumberFixture.ar_mobile3() == phone_number end it "should return correct value #4" do - {result, phone_number} = parse("03715 15 65 4320", RegionCodeFixture.ar) + {result, phone_number} = parse("03715 15 65 4320", RegionCodeFixture.ar()) assert :ok == result - assert PhoneNumberFixture.ar_mobile3 == phone_number + assert PhoneNumberFixture.ar_mobile3() == phone_number end it "should return correct value #5" do - {result, phone_number} = parse("911 876 54321", RegionCodeFixture.ar) + {result, phone_number} = parse("911 876 54321", RegionCodeFixture.ar()) assert :ok == result - assert PhoneNumberFixture.ar_mobile == phone_number + assert PhoneNumberFixture.ar_mobile() == phone_number end it "should return correct value #6" do - {result, phone_number} = parse("+54 11 8765 4321", RegionCodeFixture.ar) + {result, phone_number} = parse("+54 11 8765 4321", RegionCodeFixture.ar()) assert :ok == result - assert PhoneNumberFixture.ar_number == phone_number + assert PhoneNumberFixture.ar_number() == phone_number end it "should return correct value #7" do - {result, phone_number} = parse("011 8765 4321", RegionCodeFixture.ar) + {result, phone_number} = parse("011 8765 4321", RegionCodeFixture.ar()) assert :ok == result - assert PhoneNumberFixture.ar_number == phone_number + assert PhoneNumberFixture.ar_number() == phone_number end it "should return correct value #8" do - {result, phone_number} = parse("+54 3715 65 4321", RegionCodeFixture.ar) + {result, phone_number} = parse("+54 3715 65 4321", RegionCodeFixture.ar()) assert :ok == result - assert PhoneNumberFixture.ar_number3 == phone_number + assert PhoneNumberFixture.ar_number3() == phone_number end it "should return correct value #9" do - {result, phone_number} = parse("03715 65 4321", RegionCodeFixture.ar) + {result, phone_number} = parse("03715 65 4321", RegionCodeFixture.ar()) assert :ok == result - assert PhoneNumberFixture.ar_number3 == phone_number + assert PhoneNumberFixture.ar_number3() == phone_number end it "should return correct value #10" do - {result, phone_number} = parse("023 1234 0000", RegionCodeFixture.ar) + {result, phone_number} = parse("023 1234 0000", RegionCodeFixture.ar()) assert :ok == result - assert PhoneNumberFixture.ar_number4 == phone_number + assert PhoneNumberFixture.ar_number4() == phone_number end it "should return correct value #11" do - {result, phone_number} = parse("+54 23 1234 0000", RegionCodeFixture.ar) + {result, phone_number} = parse("+54 23 1234 0000", RegionCodeFixture.ar()) assert :ok == result - assert PhoneNumberFixture.ar_number4 == phone_number + assert PhoneNumberFixture.ar_number4() == phone_number end it "should return correct value #12" do - {result, phone_number} = parse("01187654321", RegionCodeFixture.ar) + {result, phone_number} = parse("01187654321", RegionCodeFixture.ar()) assert :ok == result - assert PhoneNumberFixture.ar_number == phone_number + assert PhoneNumberFixture.ar_number() == phone_number end it "should return correct value #13" do - {result, phone_number} = parse("(0) 1187654321", RegionCodeFixture.ar) + {result, phone_number} = parse("(0) 1187654321", RegionCodeFixture.ar()) assert :ok == result - assert PhoneNumberFixture.ar_number == phone_number + assert PhoneNumberFixture.ar_number() == phone_number end it "should return correct value #14" do - {result, phone_number} = parse("0 1187654321", RegionCodeFixture.ar) + {result, phone_number} = parse("0 1187654321", RegionCodeFixture.ar()) assert :ok == result - assert PhoneNumberFixture.ar_number == phone_number + assert PhoneNumberFixture.ar_number() == phone_number end it "should return correct value #15" do - {result, phone_number} = parse("(0xx) 1187654321", RegionCodeFixture.ar) + {result, phone_number} = parse("(0xx) 1187654321", RegionCodeFixture.ar()) assert :ok == result - assert PhoneNumberFixture.ar_number == phone_number + assert PhoneNumberFixture.ar_number() == phone_number end it "should return correct value #16" do - {result, phone_number} = parse("011xx5481429712", RegionCodeFixture.us) + {result, phone_number} = parse("011xx5481429712", RegionCodeFixture.us()) assert :ok == result - assert PhoneNumberFixture.ar_number5 == phone_number + assert PhoneNumberFixture.ar_number5() == phone_number end end context "MX numbers" do it "should return correct value #1" do - {result, phone_number} = parse("+52 (449)978-0001", RegionCodeFixture.mx) + {result, phone_number} = parse("+52 (449)978-0001", RegionCodeFixture.mx()) assert :ok == result - assert PhoneNumberFixture.mx_number3 == phone_number + assert PhoneNumberFixture.mx_number3() == phone_number end it "should return correct value #2" do - {result, phone_number} = parse("01 (449)978-0001", RegionCodeFixture.mx) + {result, phone_number} = parse("01 (449)978-0001", RegionCodeFixture.mx()) assert :ok == result - assert PhoneNumberFixture.mx_number3 == phone_number + assert PhoneNumberFixture.mx_number3() == phone_number end it "should return correct value #3" do - {result, phone_number} = parse("(449)978-0001", RegionCodeFixture.mx) + {result, phone_number} = parse("(449)978-0001", RegionCodeFixture.mx()) assert :ok == result - assert PhoneNumberFixture.mx_number3 == phone_number + assert PhoneNumberFixture.mx_number3() == phone_number end it "should return correct value #4" do - {result, phone_number} = parse("+52 1 33 1234-5678", RegionCodeFixture.mx) + {result, phone_number} = parse("+52 1 33 1234-5678", RegionCodeFixture.mx()) assert :ok == result - assert PhoneNumberFixture.mx_number4 == phone_number + assert PhoneNumberFixture.mx_number4() == phone_number end it "should return correct value #5" do - {result, phone_number} = parse("044 (33) 1234-5678", RegionCodeFixture.mx) + {result, phone_number} = parse("044 (33) 1234-5678", RegionCodeFixture.mx()) assert :ok == result - assert PhoneNumberFixture.mx_number4 == phone_number + assert PhoneNumberFixture.mx_number4() == phone_number end it "should return correct value #6" do - {result, phone_number} = parse("045 33 1234-5678", RegionCodeFixture.mx) + {result, phone_number} = parse("045 33 1234-5678", RegionCodeFixture.mx()) assert :ok == result - assert PhoneNumberFixture.mx_number4 == phone_number + assert PhoneNumberFixture.mx_number4() == phone_number end end context "Number with alpha chars" do it "should return correct value #1" do - {result, phone_number} = parse("0800 DDA 005", RegionCodeFixture.nz) + {result, phone_number} = parse("0800 DDA 005", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.nz_toll_free == phone_number + assert PhoneNumberFixture.nz_toll_free() == phone_number end it "should return correct value #2" do - {result, phone_number} = parse("0900 DDA 6005", RegionCodeFixture.nz) + {result, phone_number} = parse("0900 DDA 6005", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.nz_premium == phone_number + assert PhoneNumberFixture.nz_premium() == phone_number end it "should return correct value #3" do - {result, phone_number} = parse("0900 332 6005a", RegionCodeFixture.nz) + {result, phone_number} = parse("0900 332 6005a", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.nz_premium == phone_number + assert PhoneNumberFixture.nz_premium() == phone_number end it "should return correct value #4" do - {result, phone_number} = parse("0900 332 600a5", RegionCodeFixture.nz) + {result, phone_number} = parse("0900 332 600a5", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.nz_premium == phone_number + assert PhoneNumberFixture.nz_premium() == phone_number end it "should return correct value #5" do - {result, phone_number} = parse("0900 332 600A5", RegionCodeFixture.nz) + {result, phone_number} = parse("0900 332 600A5", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.nz_premium == phone_number + assert PhoneNumberFixture.nz_premium() == phone_number end it "should return correct value #6" do - {result, phone_number} = parse("0900 a332 600A5", RegionCodeFixture.nz) + {result, phone_number} = parse("0900 a332 600A5", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.nz_premium == phone_number + assert PhoneNumberFixture.nz_premium() == phone_number end end context "Number with international prefix" do it "should return correct value #1" do - {result, phone_number} = parse("+1 (650) 253-0000", RegionCodeFixture.nz) + {result, phone_number} = parse("+1 (650) 253-0000", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.us_number == phone_number + assert PhoneNumberFixture.us_number() == phone_number end it "should return correct value #2" do - {result, phone_number} = parse("011 800 1234 5678", RegionCodeFixture.us) + {result, phone_number} = parse("011 800 1234 5678", RegionCodeFixture.us()) assert :ok == result - assert PhoneNumberFixture.international_toll_free == phone_number + assert PhoneNumberFixture.international_toll_free() == phone_number end it "should return correct value #3" do - {result, phone_number} = parse("1-650-253-0000", RegionCodeFixture.us) + {result, phone_number} = parse("1-650-253-0000", RegionCodeFixture.us()) assert :ok == result - assert PhoneNumberFixture.us_number == phone_number + assert PhoneNumberFixture.us_number() == phone_number end it "should return correct value #4" do - {result, phone_number} = parse("0011-650-253-0000", RegionCodeFixture.sg) + {result, phone_number} = parse("0011-650-253-0000", RegionCodeFixture.sg()) assert :ok == result - assert PhoneNumberFixture.us_number == phone_number + assert PhoneNumberFixture.us_number() == phone_number end it "should return correct value #5" do - {result, phone_number} = parse("0081-650-253-0000", RegionCodeFixture.sg) + {result, phone_number} = parse("0081-650-253-0000", RegionCodeFixture.sg()) assert :ok == result - assert PhoneNumberFixture.us_number == phone_number + assert PhoneNumberFixture.us_number() == phone_number end it "should return correct value #6" do - {result, phone_number} = parse("0191-650-253-0000", RegionCodeFixture.sg) + {result, phone_number} = parse("0191-650-253-0000", RegionCodeFixture.sg()) assert :ok == result - assert PhoneNumberFixture.us_number == phone_number + assert PhoneNumberFixture.us_number() == phone_number end it "should return correct value #7" do - {result, phone_number} = parse("0~01-650-253-0000", RegionCodeFixture.pl) + {result, phone_number} = parse("0~01-650-253-0000", RegionCodeFixture.pl()) assert :ok == result - assert PhoneNumberFixture.us_number == phone_number + assert PhoneNumberFixture.us_number() == phone_number end it "should return correct value #8" do - {result, phone_number} = parse("++1 (650) 253-0000", RegionCodeFixture.pl) + {result, phone_number} = parse("++1 (650) 253-0000", RegionCodeFixture.pl()) assert :ok == result - assert PhoneNumberFixture.us_number == phone_number + assert PhoneNumberFixture.us_number() == phone_number end end context "Non Ascii" do it "should return correct value #1" do - {result, phone_number} = parse("\uFF0B1 (650) 253-0000", RegionCodeFixture.sg) + {result, phone_number} = parse("\uFF0B1 (650) 253-0000", RegionCodeFixture.sg()) assert :ok == result - assert PhoneNumberFixture.us_number == phone_number + assert PhoneNumberFixture.us_number() == phone_number end it "should return correct value #2" do - {result, phone_number} = parse("1 (650) 253\u00AD-0000", RegionCodeFixture.us) + {result, phone_number} = parse("1 (650) 253\u00AD-0000", RegionCodeFixture.us()) assert :ok == result - assert PhoneNumberFixture.us_number == phone_number + assert PhoneNumberFixture.us_number() == phone_number end it "should return correct value #3" do - {result, phone_number} = parse("\uFF0B\uFF11\u3000\uFF08\uFF16\uFF15\uFF10\uFF09\u3000\uFF12\uFF15\uFF13\uFF0D\uFF10\uFF10\uFF10\uFF10", RegionCodeFixture.sg) + {result, phone_number} = + parse( + "\uFF0B\uFF11\u3000\uFF08\uFF16\uFF15\uFF10\uFF09\u3000\uFF12\uFF15\uFF13\uFF0D\uFF10\uFF10\uFF10\uFF10", + RegionCodeFixture.sg() + ) + assert :ok == result - assert PhoneNumberFixture.us_number == phone_number + assert PhoneNumberFixture.us_number() == phone_number end it "should return correct value #4" do - {result, phone_number} = parse("\uFF0B\uFF11\u3000\uFF08\uFF16\uFF15\uFF10\uFF09\u3000\uFF12\uFF15\uFF13\u30FC\uFF10\uFF10\uFF10\uFF10", RegionCodeFixture.sg) + {result, phone_number} = + parse( + "\uFF0B\uFF11\u3000\uFF08\uFF16\uFF15\uFF10\uFF09\u3000\uFF12\uFF15\uFF13\u30FC\uFF10\uFF10\uFF10\uFF10", + RegionCodeFixture.sg() + ) + assert :ok == result - assert PhoneNumberFixture.us_number == phone_number + assert PhoneNumberFixture.us_number() == phone_number end end context "With Leading Zero" do it "should return correct value #1" do - {result, phone_number} = parse("+39 02-36618 300", RegionCodeFixture.nz) + {result, phone_number} = parse("+39 02-36618 300", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.it_number == phone_number + assert PhoneNumberFixture.it_number() == phone_number end it "should return correct value #2" do - {result, phone_number} = parse("02-36618 300", RegionCodeFixture.it) + {result, phone_number} = parse("02-36618 300", RegionCodeFixture.it()) assert :ok == result - assert PhoneNumberFixture.it_number == phone_number + assert PhoneNumberFixture.it_number() == phone_number end it "should return correct value #3" do - {result, phone_number} = parse("345 678 901", RegionCodeFixture.it) + {result, phone_number} = parse("345 678 901", RegionCodeFixture.it()) assert :ok == result - assert PhoneNumberFixture.it_mobile == phone_number + assert PhoneNumberFixture.it_mobile() == phone_number end end context "Invalid numbers" do it "should match the error message #1" do - {result, message} = parse("This is not a phone number", RegionCodeFixture.nz) + {result, message} = parse("This is not a phone number", RegionCodeFixture.nz()) assert :error == result - assert ErrorMessages.not_a_number == message + assert ErrorMessages.not_a_number() == message end it "should match the error message #2" do - {result, message} = parse("1 Still not a number", RegionCodeFixture.nz) + {result, message} = parse("1 Still not a number", RegionCodeFixture.nz()) assert :error == result - assert ErrorMessages.not_a_number == message + assert ErrorMessages.not_a_number() == message end it "should match the error message #3" do - {result, message} = parse("1 MICROSOFT", RegionCodeFixture.nz) + {result, message} = parse("1 MICROSOFT", RegionCodeFixture.nz()) assert :error == result - assert ErrorMessages.not_a_number == message + assert ErrorMessages.not_a_number() == message end it "should match the error message #4" do - {result, message} = parse("12 MICROSOFT", RegionCodeFixture.nz) + {result, message} = parse("12 MICROSOFT", RegionCodeFixture.nz()) assert :error == result - assert ErrorMessages.not_a_number == message + assert ErrorMessages.not_a_number() == message end it "should match the error message #5" do - {result, message} = parse("01495 72553301873 810104", RegionCodeFixture.gb) + {result, message} = parse("01495 72553301873 810104", RegionCodeFixture.gb()) assert :error == result - assert ErrorMessages.too_long == message + assert ErrorMessages.too_long() == message end it "should match the error message #6" do - {result, message} = parse("+---", RegionCodeFixture.de) + {result, message} = parse("+---", RegionCodeFixture.de()) assert :error == result - assert ErrorMessages.not_a_number == message + assert ErrorMessages.not_a_number() == message end it "should match the error message #7" do - {result, message} = parse("+***", RegionCodeFixture.de) + {result, message} = parse("+***", RegionCodeFixture.de()) assert :error == result - assert ErrorMessages.not_a_number == message + assert ErrorMessages.not_a_number() == message end it "should match the error message #8" do - {result, message} = parse("+*******91", RegionCodeFixture.de) + {result, message} = parse("+*******91", RegionCodeFixture.de()) assert :error == result - assert ErrorMessages.not_a_number == message + assert ErrorMessages.not_a_number() == message end it "should match the error message #9" do - {result, message} = parse("+49 0", RegionCodeFixture.de) + {result, message} = parse("+49 0", RegionCodeFixture.de()) assert :error == result - assert ErrorMessages.too_short_nsn == message + assert ErrorMessages.too_short_nsn() == message end it "should match the error message #10" do - {result, message} = parse("+210 3456 56789", RegionCodeFixture.nz) + {result, message} = parse("+210 3456 56789", RegionCodeFixture.nz()) assert :error == result - assert ErrorMessages.invalid_country_code == message + assert ErrorMessages.invalid_country_code() == message end it "should match the error message #11" do - {result, message} = parse("+ 00 210 3 331 6005", RegionCodeFixture.nz) + {result, message} = parse("+ 00 210 3 331 6005", RegionCodeFixture.nz()) assert :error == result - assert ErrorMessages.invalid_country_code == message + assert ErrorMessages.invalid_country_code() == message end it "should match the error message #12" do - {result, message} = parse("123 456 7890", RegionCodeFixture.zz) + {result, message} = parse("123 456 7890", RegionCodeFixture.zz()) assert :error == result - assert ErrorMessages.invalid_country_code == message + assert ErrorMessages.invalid_country_code() == message end it "should match the error message #13" do - {result, message} = parse("123 456 7890", RegionCodeFixture.cs) + {result, message} = parse("123 456 7890", RegionCodeFixture.cs()) assert :error == result - assert ErrorMessages.invalid_country_code == message + assert ErrorMessages.invalid_country_code() == message end it "should match the error message #14" do {result, message} = parse("123 456 7890", nil) assert :error == result - assert ErrorMessages.invalid_country_code == message + assert ErrorMessages.invalid_country_code() == message end it "should match the error message #15" do - {result, message} = parse("0044------", RegionCodeFixture.gb) + {result, message} = parse("0044------", RegionCodeFixture.gb()) assert :error == result - assert ErrorMessages.too_short_after_idd == message + assert ErrorMessages.too_short_after_idd() == message end it "should match the error message #16" do - {result, message} = parse("0044", RegionCodeFixture.gb) + {result, message} = parse("0044", RegionCodeFixture.gb()) assert :error == result - assert ErrorMessages.too_short_after_idd == message + assert ErrorMessages.too_short_after_idd() == message end it "should match the error message #17" do - {result, message} = parse("011", RegionCodeFixture.us) + {result, message} = parse("011", RegionCodeFixture.us()) assert :error == result - assert ErrorMessages.too_short_after_idd == message + assert ErrorMessages.too_short_after_idd() == message end it "should match the error message #18" do - {result, message} = parse("0119", RegionCodeFixture.us) + {result, message} = parse("0119", RegionCodeFixture.us()) assert :error == result - assert ErrorMessages.too_short_after_idd == message + assert ErrorMessages.too_short_after_idd() == message end it "should match the error message #19" do - {result, message} = parse("", RegionCodeFixture.zz) + {result, message} = parse("", RegionCodeFixture.zz()) assert :error == result - assert ErrorMessages.not_a_number == message + assert ErrorMessages.not_a_number() == message end it "should match the error message #20" do - {result, message} = parse(nil, RegionCodeFixture.zz) + {result, message} = parse(nil, RegionCodeFixture.zz()) assert :error == result - assert ErrorMessages.not_a_number == message + assert ErrorMessages.not_a_number() == message end it "should match the error message #21" do - {result, message} = parse(nil, RegionCodeFixture.us) + {result, message} = parse(nil, RegionCodeFixture.us()) assert :error == result - assert ErrorMessages.not_a_number == message + assert ErrorMessages.not_a_number() == message end it "should match the error message #22" do - {result, message} = parse("tel:555-1234;phone-context=www.google.com", RegionCodeFixture.zz) + {result, message} = parse("tel:555-1234;phone-context=www.google.com", RegionCodeFixture.zz()) + assert :error == result - assert ErrorMessages.invalid_country_code == message + assert ErrorMessages.invalid_country_code() == message end it "should match the error message #23" do - {result, message} = parse("tel:555-1234;phone-context=1-331", RegionCodeFixture.zz) + {result, message} = parse("tel:555-1234;phone-context=1-331", RegionCodeFixture.zz()) assert :error == result - assert ErrorMessages.invalid_country_code == message + assert ErrorMessages.invalid_country_code() == message end end context "With plus sign with no region" do it "should match the phone number #1" do - {result, phone_number} = parse("+64 3 331 6005", RegionCodeFixture.zz) + {result, phone_number} = parse("+64 3 331 6005", RegionCodeFixture.zz()) assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end it "should match the phone number #2" do - {result, phone_number} = parse("\uFF0B64 3 331 6005", RegionCodeFixture.zz) + {result, phone_number} = parse("\uFF0B64 3 331 6005", RegionCodeFixture.zz()) assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end it "should match the phone number #3" do - {result, phone_number} = parse("Tel: +64 3 331 6005", RegionCodeFixture.zz) + {result, phone_number} = parse("Tel: +64 3 331 6005", RegionCodeFixture.zz()) assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end it "should match the phone number #4" do {result, phone_number} = parse("+64 3 331 6005", nil) assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end it "should match the phone number #5" do {result, phone_number} = parse("+800 1234 5678", nil) assert :ok == result - assert PhoneNumberFixture.international_toll_free == phone_number + assert PhoneNumberFixture.international_toll_free() == phone_number end it "should match the phone number #6" do {result, phone_number} = parse("+979 123 456 789", nil) assert :ok == result - assert PhoneNumberFixture.universal_premium_rate == phone_number + assert PhoneNumberFixture.universal_premium_rate() == phone_number end it "should match the phone number #7" do - {result, phone_number} = parse("tel:03-331-6005;phone-context=+64", RegionCodeFixture.zz) + {result, phone_number} = parse("tel:03-331-6005;phone-context=+64", RegionCodeFixture.zz()) + assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end it "should match the phone number #8" do - {result, phone_number} = parse(" tel:03-331-6005;phone-context=+64", RegionCodeFixture.zz) + {result, phone_number} = parse(" tel:03-331-6005;phone-context=+64", RegionCodeFixture.zz()) + assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end it "should match the phone number #9" do - {result, phone_number} = parse("tel:03-331-6005;isub=12345;phone-context=+64", RegionCodeFixture.zz) + {result, phone_number} = parse("tel:03-331-6005;isub=12345;phone-context=+64", RegionCodeFixture.zz()) + assert :ok == result - assert PhoneNumberFixture.nz_number == phone_number + assert PhoneNumberFixture.nz_number() == phone_number end end context "Too short if national prefix stripped" do it "should match the phone number #1" do - {result, phone_number} = parse("8123", RegionCodeFixture.by) + {result, phone_number} = parse("8123", RegionCodeFixture.by()) assert :ok == result - assert PhoneNumberFixture.by_number == phone_number + assert PhoneNumberFixture.by_number() == phone_number end it "should match the phone number #2" do - {result, phone_number} = parse("81234", RegionCodeFixture.by) + {result, phone_number} = parse("81234", RegionCodeFixture.by()) assert :ok == result - assert PhoneNumberFixture.by_number2 == phone_number + assert PhoneNumberFixture.by_number2() == phone_number end it "should match the phone number #3" do - {result, phone_number} = parse("812345", RegionCodeFixture.by) + {result, phone_number} = parse("812345", RegionCodeFixture.by()) assert :ok == result - assert PhoneNumberFixture.by_number3 == phone_number + assert PhoneNumberFixture.by_number3() == phone_number end it "should match the phone number #4" do - {result, phone_number} = parse("8123456", RegionCodeFixture.by) + {result, phone_number} = parse("8123456", RegionCodeFixture.by()) assert :ok == result - assert PhoneNumberFixture.by_number4 == phone_number + assert PhoneNumberFixture.by_number4() == phone_number end end context "Number with extension" do it "should match the phone number #1" do - {result, phone_number} = parse("03 331 6005 ext 3456", RegionCodeFixture.nz) + {result, phone_number} = parse("03 331 6005 ext 3456", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.nz_number4 == phone_number + assert PhoneNumberFixture.nz_number4() == phone_number end it "should match the phone number #2" do - {result, phone_number} = parse("03-3316005x3456", RegionCodeFixture.nz) + {result, phone_number} = parse("03-3316005x3456", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.nz_number4 == phone_number + assert PhoneNumberFixture.nz_number4() == phone_number end it "should match the phone number #3" do - {result, phone_number} = parse("03-3316005 int.3456", RegionCodeFixture.nz) + {result, phone_number} = parse("03-3316005 int.3456", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.nz_number4 == phone_number + assert PhoneNumberFixture.nz_number4() == phone_number end it "should match the phone number #4" do - {result, phone_number} = parse("03 3316005 #3456", RegionCodeFixture.nz) + {result, phone_number} = parse("03 3316005 #3456", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.nz_number4 == phone_number + assert PhoneNumberFixture.nz_number4() == phone_number end it "should match the phone number #5" do - {result, phone_number} = parse("1800 six-flags", RegionCodeFixture.us) + {result, phone_number} = parse("1800 six-flags", RegionCodeFixture.us()) assert :ok == result - assert PhoneNumberFixture.alpha_numeric_number == phone_number + assert PhoneNumberFixture.alpha_numeric_number() == phone_number end it "should match the phone number #6" do - {result, phone_number} = parse("1800 SIX FLAGS", RegionCodeFixture.us) + {result, phone_number} = parse("1800 SIX FLAGS", RegionCodeFixture.us()) assert :ok == result - assert PhoneNumberFixture.alpha_numeric_number == phone_number + assert PhoneNumberFixture.alpha_numeric_number() == phone_number end it "should match the phone number #7" do - {result, phone_number} = parse("0~0 1800 7493 5247", RegionCodeFixture.pl) + {result, phone_number} = parse("0~0 1800 7493 5247", RegionCodeFixture.pl()) assert :ok == result - assert PhoneNumberFixture.alpha_numeric_number == phone_number + assert PhoneNumberFixture.alpha_numeric_number() == phone_number end it "should match the phone number #8" do - {result, phone_number} = parse("(1800) 7493.5247", RegionCodeFixture.us) + {result, phone_number} = parse("(1800) 7493.5247", RegionCodeFixture.us()) assert :ok == result - assert PhoneNumberFixture.alpha_numeric_number == phone_number + assert PhoneNumberFixture.alpha_numeric_number() == phone_number end it "should match the phone number #9" do - {result, phone_number} = parse("0~0 1800 7493 5247 ~1234", RegionCodeFixture.pl) + {result, phone_number} = parse("0~0 1800 7493 5247 ~1234", RegionCodeFixture.pl()) assert :ok == result - assert PhoneNumberFixture.alpha_numeric_number2 == phone_number + assert PhoneNumberFixture.alpha_numeric_number2() == phone_number end it "should match the phone number #10" do - {result, phone_number} = parse("+44 2034567890x456", RegionCodeFixture.nz) + {result, phone_number} = parse("+44 2034567890x456", RegionCodeFixture.nz()) assert :ok == result - assert PhoneNumberFixture.gb_number2 == phone_number + assert PhoneNumberFixture.gb_number2() == phone_number end it "should match the phone number #11" do - {result, phone_number} = parse("+44 2034567890x456", RegionCodeFixture.gb) + {result, phone_number} = parse("+44 2034567890x456", RegionCodeFixture.gb()) assert :ok == result - assert PhoneNumberFixture.gb_number2 == phone_number + assert PhoneNumberFixture.gb_number2() == phone_number end it "should match the phone number #12" do - {result, phone_number} = parse("+44 2034567890 x456", RegionCodeFixture.gb) + {result, phone_number} = parse("+44 2034567890 x456", RegionCodeFixture.gb()) assert :ok == result - assert PhoneNumberFixture.gb_number2 == phone_number + assert PhoneNumberFixture.gb_number2() == phone_number end it "should match the phone number #13" do - {result, phone_number} = parse("+44 2034567890 X456", RegionCodeFixture.gb) + {result, phone_number} = parse("+44 2034567890 X456", RegionCodeFixture.gb()) assert :ok == result - assert PhoneNumberFixture.gb_number2 == phone_number + assert PhoneNumberFixture.gb_number2() == phone_number end it "should match the phone number #14" do - {result, phone_number} = parse("+44 2034567890 X 456", RegionCodeFixture.gb) + {result, phone_number} = parse("+44 2034567890 X 456", RegionCodeFixture.gb()) assert :ok == result - assert PhoneNumberFixture.gb_number2 == phone_number + assert PhoneNumberFixture.gb_number2() == phone_number end it "should match the phone number #15" do - {result, phone_number} = parse("+44 2034567890 X 456", RegionCodeFixture.gb) + {result, phone_number} = parse("+44 2034567890 X 456", RegionCodeFixture.gb()) assert :ok == result - assert PhoneNumberFixture.gb_number2 == phone_number + assert PhoneNumberFixture.gb_number2() == phone_number end it "should match the phone number #16" do - {result, phone_number} = parse("44 2034567890 x 456 ", RegionCodeFixture.gb) + {result, phone_number} = parse("44 2034567890 x 456 ", RegionCodeFixture.gb()) assert :ok == result - assert PhoneNumberFixture.gb_number2 == phone_number + assert PhoneNumberFixture.gb_number2() == phone_number end it "should match the phone number #17" do - {result, phone_number} = parse("+44 2034567890 X 456", RegionCodeFixture.gb) + {result, phone_number} = parse("+44 2034567890 X 456", RegionCodeFixture.gb()) assert :ok == result - assert PhoneNumberFixture.gb_number2 == phone_number + assert PhoneNumberFixture.gb_number2() == phone_number end it "should match the phone number #18" do - {result, phone_number} = parse("+44-2034567890;ext=456", RegionCodeFixture.gb) + {result, phone_number} = parse("+44-2034567890;ext=456", RegionCodeFixture.gb()) assert :ok == result - assert PhoneNumberFixture.gb_number2 == phone_number + assert PhoneNumberFixture.gb_number2() == phone_number end it "should match the phone number #19" do - {result, phone_number} = parse("tel:2034567890;ext=456;phone-context=+44", RegionCodeFixture.zz) + {result, phone_number} = parse("tel:2034567890;ext=456;phone-context=+44", RegionCodeFixture.zz()) + assert :ok == result - assert PhoneNumberFixture.gb_number2 == phone_number + assert PhoneNumberFixture.gb_number2() == phone_number end it "should match the phone number #20" do - {result, phone_number} = parse("+442034567890\uFF45\uFF58\uFF54\uFF4E456", RegionCodeFixture.zz) + {result, phone_number} = parse("+442034567890\uFF45\uFF58\uFF54\uFF4E456", RegionCodeFixture.zz()) + assert :ok == result - assert PhoneNumberFixture.gb_number2 == phone_number + assert PhoneNumberFixture.gb_number2() == phone_number end it "should match the phone number #21" do - {result, phone_number} = parse("+442034567890\uFF58\uFF54\uFF4E456", RegionCodeFixture.zz) + {result, phone_number} = parse("+442034567890\uFF58\uFF54\uFF4E456", RegionCodeFixture.zz()) + assert :ok == result - assert PhoneNumberFixture.gb_number2 == phone_number + assert PhoneNumberFixture.gb_number2() == phone_number end it "should match the phone number #22" do - {result, phone_number} = parse("+442034567890\uFF58\uFF54456", RegionCodeFixture.zz) + {result, phone_number} = parse("+442034567890\uFF58\uFF54456", RegionCodeFixture.zz()) assert :ok == result - assert PhoneNumberFixture.gb_number2 == phone_number + assert PhoneNumberFixture.gb_number2() == phone_number end it "should match the phone number #23" do - {result, phone_number} = parse("(212)123-1234 x508/x1234", RegionCodeFixture.us) + {result, phone_number} = parse("(212)123-1234 x508/x1234", RegionCodeFixture.us()) assert :ok == result - assert PhoneNumberFixture.us_number_with_extension == phone_number + assert PhoneNumberFixture.us_number_with_extension() == phone_number end it "should match the phone number #24" do - {result, phone_number} = parse("(212)123-1234 x508/ x1234", RegionCodeFixture.us) + {result, phone_number} = parse("(212)123-1234 x508/ x1234", RegionCodeFixture.us()) assert :ok == result - assert PhoneNumberFixture.us_number_with_extension == phone_number + assert PhoneNumberFixture.us_number_with_extension() == phone_number end it "should match the phone number #25" do - {result, phone_number} = parse("(212)123-1234 x508\\x1234", RegionCodeFixture.us) + {result, phone_number} = parse("(212)123-1234 x508\\x1234", RegionCodeFixture.us()) assert :ok == result - assert PhoneNumberFixture.us_number_with_extension == phone_number + assert PhoneNumberFixture.us_number_with_extension() == phone_number end it "should match the phone number #26" do - {result, phone_number} = parse("+1 (645) 123 1234-910#", RegionCodeFixture.us) + {result, phone_number} = parse("+1 (645) 123 1234-910#", RegionCodeFixture.us()) assert :ok == result - assert PhoneNumberFixture.us_number_with_extension2 == phone_number + assert PhoneNumberFixture.us_number_with_extension2() == phone_number end it "should match the phone number #27" do - {result, phone_number} = parse("+1 (645) 123 1234 ext. 910#", RegionCodeFixture.us) + {result, phone_number} = parse("+1 (645) 123 1234 ext. 910#", RegionCodeFixture.us()) assert :ok == result - assert PhoneNumberFixture.us_number_with_extension2 == phone_number + assert PhoneNumberFixture.us_number_with_extension2() == phone_number end end context "Italian leading zero" do it "should match the phone number #1" do - {result, phone_number} = parse("011", RegionCodeFixture.au) + {result, phone_number} = parse("011", RegionCodeFixture.au()) assert :ok == result - assert PhoneNumberFixture.au_leading_zero == phone_number + assert PhoneNumberFixture.au_leading_zero() == phone_number end it "should match the phone number #2" do - {result, phone_number} = parse("001", RegionCodeFixture.au) + {result, phone_number} = parse("001", RegionCodeFixture.au()) assert :ok == result - assert PhoneNumberFixture.au_leading_zero2 == phone_number + assert PhoneNumberFixture.au_leading_zero2() == phone_number end it "should match the phone number #3" do - {result, phone_number} = parse("000", RegionCodeFixture.au) + {result, phone_number} = parse("000", RegionCodeFixture.au()) assert :ok == result - assert PhoneNumberFixture.au_leading_zero3 == phone_number + assert PhoneNumberFixture.au_leading_zero3() == phone_number end it "should match the phone number #4" do - {result, phone_number} = parse("0000", RegionCodeFixture.au) + {result, phone_number} = parse("0000", RegionCodeFixture.au()) assert :ok == result - assert PhoneNumberFixture.au_leading_zero4 == phone_number + assert PhoneNumberFixture.au_leading_zero4() == phone_number end end end diff --git a/test/ex_phone_number/validation_test.exs b/test/ex_phone_number/validation_test.exs index 1db425a..7ae27b5 100644 --- a/test/ex_phone_number/validation_test.exs +++ b/test/ex_phone_number/validation_test.exs @@ -11,49 +11,49 @@ defmodule ExPhoneNumber.ValidationTest do describe ".is_possible_number?/1" do context "US number" do it "should return true" do - assert is_possible_number?(PhoneNumberFixture.us_number) + assert is_possible_number?(PhoneNumberFixture.us_number()) end end context "US local number" do it "should return true" do - assert is_possible_number?(PhoneNumberFixture.us_local_number) + assert is_possible_number?(PhoneNumberFixture.us_local_number()) end end context "GB number" do it "should return true" do - assert is_possible_number?(PhoneNumberFixture.gb_number) + assert is_possible_number?(PhoneNumberFixture.gb_number()) end end context "International Toll Free" do it "should return true" do - assert is_possible_number?(PhoneNumberFixture.international_toll_free) + assert is_possible_number?(PhoneNumberFixture.international_toll_free()) end end context "NANPA short number" do it "should return false" do - refute is_possible_number?(PhoneNumberFixture.nanpa_short_number) + refute is_possible_number?(PhoneNumberFixture.nanpa_short_number()) end end context "US long number" do it "should return false" do - refute is_possible_number?(PhoneNumberFixture.us_long_number) + refute is_possible_number?(PhoneNumberFixture.us_long_number()) end end context "GB short number" do it "should return false" do - refute is_possible_number?(PhoneNumberFixture.gb_short_number) + refute is_possible_number?(PhoneNumberFixture.gb_short_number()) end end context "International Toll Free too long" do it "should return false" do - refute is_possible_number?(PhoneNumberFixture.international_toll_free_too_long) + refute is_possible_number?(PhoneNumberFixture.international_toll_free_too_long()) end end end @@ -61,43 +61,50 @@ defmodule ExPhoneNumber.ValidationTest do describe ".is_possible_number_with_reason?/1" do context "US number" do it "should return correct value" do - assert ValidationResults.is_possible == is_possible_number_with_reason?(PhoneNumberFixture.us_number) + assert ValidationResults.is_possible() == + is_possible_number_with_reason?(PhoneNumberFixture.us_number()) end end context "US local number" do it "should return correct value" do - assert ValidationResults.is_possible == is_possible_number_with_reason?(PhoneNumberFixture.us_number) + assert ValidationResults.is_possible() == + is_possible_number_with_reason?(PhoneNumberFixture.us_number()) end end context "US long number" do it "should return correct value" do - assert ValidationResults.too_long == is_possible_number_with_reason?(PhoneNumberFixture.us_long_number) + assert ValidationResults.too_long() == + is_possible_number_with_reason?(PhoneNumberFixture.us_long_number()) end end context "Invalid country code" do it "should return correct value" do - assert ValidationResults.invalid_country_code == is_possible_number_with_reason?(PhoneNumberFixture.unknown_country_code2) + assert ValidationResults.invalid_country_code() == + is_possible_number_with_reason?(PhoneNumberFixture.unknown_country_code2()) end end context "US short number" do it "should return correct value" do - assert ValidationResults.too_short == is_possible_number_with_reason?(PhoneNumberFixture.nanpa_short_number) + assert ValidationResults.too_short() == + is_possible_number_with_reason?(PhoneNumberFixture.nanpa_short_number()) end end context "SG number 2" do it "should return correct value" do - assert ValidationResults.is_possible == is_possible_number_with_reason?(PhoneNumberFixture.sg_number2) + assert ValidationResults.is_possible() == + is_possible_number_with_reason?(PhoneNumberFixture.sg_number2()) end end context "International Toll Free long number" do it "should return correct value" do - assert ValidationResults.too_long == is_possible_number_with_reason?(PhoneNumberFixture.international_toll_free_too_long) + assert ValidationResults.too_long() == + is_possible_number_with_reason?(PhoneNumberFixture.international_toll_free_too_long()) end end end @@ -149,61 +156,61 @@ defmodule ExPhoneNumber.ValidationTest do describe ".is_valid_number/1" do context "test US number" do it "returns true" do - assert is_valid_number?(PhoneNumberFixture.us_number) + assert is_valid_number?(PhoneNumberFixture.us_number()) end end context "test IT number" do it "returns true" do - assert is_valid_number?(PhoneNumberFixture.it_number) + assert is_valid_number?(PhoneNumberFixture.it_number()) end end context "test GB mobile" do it "returns true" do - assert is_valid_number?(PhoneNumberFixture.gb_mobile) + assert is_valid_number?(PhoneNumberFixture.gb_mobile()) end end context "test International Toll Free" do it "returns true" do - assert is_valid_number?(PhoneNumberFixture.international_toll_free) + assert is_valid_number?(PhoneNumberFixture.international_toll_free()) end end context "test Universal Premium Rate" do it "returns true" do - assert is_valid_number?(PhoneNumberFixture.universal_premium_rate) + assert is_valid_number?(PhoneNumberFixture.universal_premium_rate()) end end context "test NZ number 2" do it "returns true" do - assert is_valid_number?(PhoneNumberFixture.nz_number2) + assert is_valid_number?(PhoneNumberFixture.nz_number2()) end end context "test invalid BS number" do it "returns false" do - refute is_valid_number?(PhoneNumberFixture.bs_number_invalid) + refute is_valid_number?(PhoneNumberFixture.bs_number_invalid()) end end context "test IT invalid" do it "returns false" do - refute is_valid_number?(PhoneNumberFixture.it_invalid) + refute is_valid_number?(PhoneNumberFixture.it_invalid()) end end context "test GB invalid" do it "returns false" do - refute is_valid_number?(PhoneNumberFixture.gb_invalid) + refute is_valid_number?(PhoneNumberFixture.gb_invalid()) end end context "test DE invalid" do it "returns false" do - refute is_valid_number?(PhoneNumberFixture.de_invalid) + refute is_valid_number?(PhoneNumberFixture.de_invalid()) end it "returns false #2" do @@ -215,17 +222,17 @@ defmodule ExPhoneNumber.ValidationTest do context "test NZ invalid" do it "returns false" do - refute is_valid_number?(PhoneNumberFixture.nz_invalid) + refute is_valid_number?(PhoneNumberFixture.nz_invalid()) end end context "test country code invalid" do it "returns false" do - refute is_valid_number?(PhoneNumberFixture.unknown_country_code) + refute is_valid_number?(PhoneNumberFixture.unknown_country_code()) end it "returns false #2" do - refute is_valid_number?(PhoneNumberFixture.unknown_country_code2) + refute is_valid_number?(PhoneNumberFixture.unknown_country_code2()) end end end @@ -233,81 +240,102 @@ defmodule ExPhoneNumber.ValidationTest do describe ".is_valid_number_for_region?/2" do context "test BS number" do it "returns true" do - assert is_valid_number?(PhoneNumberFixture.bs_number) + assert is_valid_number?(PhoneNumberFixture.bs_number()) end it "returns true #2" do - assert is_valid_number_for_region?(PhoneNumberFixture.bs_number, RegionCodeFixture.bs) + assert is_valid_number_for_region?(PhoneNumberFixture.bs_number(), RegionCodeFixture.bs()) end it "returns false" do - refute is_valid_number_for_region?(PhoneNumberFixture.bs_number, RegionCodeFixture.us) + refute is_valid_number_for_region?(PhoneNumberFixture.bs_number(), RegionCodeFixture.us()) end end context "test RE number" do it "returns true" do - assert is_valid_number?(PhoneNumberFixture.re_number) + assert is_valid_number?(PhoneNumberFixture.re_number()) end it "returns true #2" do - assert is_valid_number_for_region?(PhoneNumberFixture.re_number, RegionCodeFixture.re) + assert is_valid_number_for_region?(PhoneNumberFixture.re_number(), RegionCodeFixture.re()) end it "returns false" do - refute is_valid_number_for_region?(PhoneNumberFixture.re_number, RegionCodeFixture.yt) + refute is_valid_number_for_region?(PhoneNumberFixture.re_number(), RegionCodeFixture.yt()) end end context "test RE number invalid" do it "returns false" do - refute is_valid_number?(PhoneNumberFixture.re_number_invalid) + refute is_valid_number?(PhoneNumberFixture.re_number_invalid()) end it "returns false #2" do - refute is_valid_number_for_region?(PhoneNumberFixture.re_number_invalid, RegionCodeFixture.re) + refute is_valid_number_for_region?( + PhoneNumberFixture.re_number_invalid(), + RegionCodeFixture.re() + ) end it "returns false #3" do - refute is_valid_number_for_region?(PhoneNumberFixture.re_number_invalid, RegionCodeFixture.yt) + refute is_valid_number_for_region?( + PhoneNumberFixture.re_number_invalid(), + RegionCodeFixture.yt() + ) end end context "test YT number" do it "returns true" do - assert is_valid_number?(PhoneNumberFixture.yt_number) + assert is_valid_number?(PhoneNumberFixture.yt_number()) end it "returns true #2" do - assert is_valid_number_for_region?(PhoneNumberFixture.yt_number, RegionCodeFixture.yt) + assert is_valid_number_for_region?(PhoneNumberFixture.yt_number(), RegionCodeFixture.yt()) end it "returns false" do - refute is_valid_number_for_region?(PhoneNumberFixture.yt_number, RegionCodeFixture.re) + refute is_valid_number_for_region?(PhoneNumberFixture.yt_number(), RegionCodeFixture.re()) end end context "test multi country number" do it "returns true" do - assert is_valid_number_for_region?(PhoneNumberFixture.re_yt_number, RegionCodeFixture.re) + assert is_valid_number_for_region?( + PhoneNumberFixture.re_yt_number(), + RegionCodeFixture.re() + ) end it "returns true #2" do - assert is_valid_number_for_region?(PhoneNumberFixture.re_yt_number, RegionCodeFixture.yt) + assert is_valid_number_for_region?( + PhoneNumberFixture.re_yt_number(), + RegionCodeFixture.yt() + ) end end context "test International Toll Free number" do it "returns true" do - assert is_valid_number_for_region?(PhoneNumberFixture.international_toll_free, RegionCodeFixture.un001) + assert is_valid_number_for_region?( + PhoneNumberFixture.international_toll_free(), + RegionCodeFixture.un001() + ) end it "returns false #1" do - refute is_valid_number_for_region?(PhoneNumberFixture.international_toll_free, RegionCodeFixture.us) + refute is_valid_number_for_region?( + PhoneNumberFixture.international_toll_free(), + RegionCodeFixture.us() + ) end it "returns false #2" do - refute is_valid_number_for_region?(PhoneNumberFixture.international_toll_free, RegionCodeFixture.zz) + refute is_valid_number_for_region?( + PhoneNumberFixture.international_toll_free(), + RegionCodeFixture.zz() + ) end end end @@ -315,19 +343,19 @@ defmodule ExPhoneNumber.ValidationTest do describe ".is_number_geographical?/1" do context "test BS mobile" do it "returns false" do - refute is_number_geographical?(PhoneNumberFixture.bs_mobile) + refute is_number_geographical?(PhoneNumberFixture.bs_mobile()) end end context "test AU number" do it "returns true" do - assert is_number_geographical?(PhoneNumberFixture.au_number) + assert is_number_geographical?(PhoneNumberFixture.au_number()) end end context "test International Toll Free number" do it "returns false" do - refute is_number_geographical?(PhoneNumberFixture.international_toll_free) + refute is_number_geographical?(PhoneNumberFixture.international_toll_free()) end end end @@ -335,149 +363,156 @@ defmodule ExPhoneNumber.ValidationTest do describe ".get_number_type/1" do context "test IT premium" do it "returns true" do - assert get_number_type(PhoneNumberFixture.it_premium) == PhoneNumberTypes.premium_rate + assert get_number_type(PhoneNumberFixture.it_premium()) == PhoneNumberTypes.premium_rate() end end context "test GB premium" do it "returns true" do - assert get_number_type(PhoneNumberFixture.gb_premium) == PhoneNumberTypes.premium_rate + assert get_number_type(PhoneNumberFixture.gb_premium()) == PhoneNumberTypes.premium_rate() end end context "test DE premium" do it "returns true" do - assert get_number_type(PhoneNumberFixture.de_premium) == PhoneNumberTypes.premium_rate + assert get_number_type(PhoneNumberFixture.de_premium()) == PhoneNumberTypes.premium_rate() end it "returns true #" do - assert get_number_type(PhoneNumberFixture.de_premium2) == PhoneNumberTypes.premium_rate + assert get_number_type(PhoneNumberFixture.de_premium2()) == + PhoneNumberTypes.premium_rate() end end context "test Universal Premium Rate" do it "returns true" do - assert get_number_type(PhoneNumberFixture.universal_premium_rate) == PhoneNumberTypes.premium_rate + assert get_number_type(PhoneNumberFixture.universal_premium_rate()) == + PhoneNumberTypes.premium_rate() end end context "test US toll free" do it "returns true" do - assert get_number_type(PhoneNumberFixture.us_tollfree2) == PhoneNumberTypes.toll_free + assert get_number_type(PhoneNumberFixture.us_tollfree2()) == PhoneNumberTypes.toll_free() end end context "test IT toll free" do it "returns true" do - assert get_number_type(PhoneNumberFixture.it_toll_free) == PhoneNumberTypes.toll_free + assert get_number_type(PhoneNumberFixture.it_toll_free()) == PhoneNumberTypes.toll_free() end end context "test GB toll free" do it "returns true" do - assert get_number_type(PhoneNumberFixture.gb_toll_free) == PhoneNumberTypes.toll_free + assert get_number_type(PhoneNumberFixture.gb_toll_free()) == PhoneNumberTypes.toll_free() end end context "test DE toll free" do it "returns true" do - assert get_number_type(PhoneNumberFixture.de_toll_free) == PhoneNumberTypes.toll_free + assert get_number_type(PhoneNumberFixture.de_toll_free()) == PhoneNumberTypes.toll_free() end end context "test International Toll Free" do it "returns true" do - assert get_number_type(PhoneNumberFixture.international_toll_free) == PhoneNumberTypes.toll_free + assert get_number_type(PhoneNumberFixture.international_toll_free()) == + PhoneNumberTypes.toll_free() end end context "test BS mobile" do it "returns true" do - assert get_number_type(PhoneNumberFixture.bs_mobile) == PhoneNumberTypes.mobile + assert get_number_type(PhoneNumberFixture.bs_mobile()) == PhoneNumberTypes.mobile() end end context "test GB mobile" do it "returns true" do - assert get_number_type(PhoneNumberFixture.gb_mobile) == PhoneNumberTypes.mobile + assert get_number_type(PhoneNumberFixture.gb_mobile()) == PhoneNumberTypes.mobile() end end context "test IT mobile" do it "returns true" do - assert get_number_type(PhoneNumberFixture.it_mobile) == PhoneNumberTypes.mobile + assert get_number_type(PhoneNumberFixture.it_mobile()) == PhoneNumberTypes.mobile() end end context "test AR mobile" do it "returns true" do - assert get_number_type(PhoneNumberFixture.ar_mobile) == PhoneNumberTypes.mobile + assert get_number_type(PhoneNumberFixture.ar_mobile()) == PhoneNumberTypes.mobile() end end context "test DE mobile" do it "returns true" do - assert get_number_type(PhoneNumberFixture.de_mobile) == PhoneNumberTypes.mobile + assert get_number_type(PhoneNumberFixture.de_mobile()) == PhoneNumberTypes.mobile() end end context "test BS number" do it "returns true" do - assert get_number_type(PhoneNumberFixture.bs_number) == PhoneNumberTypes.fixed_line + assert get_number_type(PhoneNumberFixture.bs_number()) == PhoneNumberTypes.fixed_line() end end context "test IT number" do it "returns true" do - assert get_number_type(PhoneNumberFixture.it_number) == PhoneNumberTypes.fixed_line + assert get_number_type(PhoneNumberFixture.it_number()) == PhoneNumberTypes.fixed_line() end end context "test GB number" do it "returns true" do - assert get_number_type(PhoneNumberFixture.gb_number) == PhoneNumberTypes.fixed_line + assert get_number_type(PhoneNumberFixture.gb_number()) == PhoneNumberTypes.fixed_line() end end context "test DE number" do it "returns true" do - assert get_number_type(PhoneNumberFixture.de_number) == PhoneNumberTypes.fixed_line + assert get_number_type(PhoneNumberFixture.de_number()) == PhoneNumberTypes.fixed_line() end end context "test US number" do it "returns true" do - assert get_number_type(PhoneNumberFixture.us_number) == PhoneNumberTypes.fixed_line_or_mobile + assert get_number_type(PhoneNumberFixture.us_number()) == + PhoneNumberTypes.fixed_line_or_mobile() end end context "test AR number 2" do it "returns true" do - assert get_number_type(PhoneNumberFixture.ar_number2) == PhoneNumberTypes.fixed_line_or_mobile + assert get_number_type(PhoneNumberFixture.ar_number2()) == + PhoneNumberTypes.fixed_line_or_mobile() end end context "test GB shared cost" do it "returns true" do - assert get_number_type(PhoneNumberFixture.gb_shared_cost) == PhoneNumberTypes.shared_cost + assert get_number_type(PhoneNumberFixture.gb_shared_cost()) == + PhoneNumberTypes.shared_cost() end end context "test GB voip" do it "returns true" do - assert get_number_type(PhoneNumberFixture.gb_voip) == PhoneNumberTypes.voip + assert get_number_type(PhoneNumberFixture.gb_voip()) == PhoneNumberTypes.voip() end end context "test GB personal number" do it "returns true" do - assert get_number_type(PhoneNumberFixture.gb_personal_number) == PhoneNumberTypes.personal_number + assert get_number_type(PhoneNumberFixture.gb_personal_number()) == + PhoneNumberTypes.personal_number() end end context "test US local number" do it "returns true" do - assert get_number_type(PhoneNumberFixture.us_local_number) == PhoneNumberTypes.unknown + assert get_number_type(PhoneNumberFixture.us_local_number()) == PhoneNumberTypes.unknown() end end end @@ -492,7 +527,9 @@ defmodule ExPhoneNumber.ValidationTest do context "length larger than Constants.Value.max_input_string_length" do it "returns {:error, message}" do - subject = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890x" + subject = + "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890x" + assert {:error, _} = validate_length(subject) end end diff --git a/test/support/phone_number_fixture.ex b/test/support/phone_number_fixture.ex index 84cf44d..c495f61 100644 --- a/test/support/phone_number_fixture.ex +++ b/test/support/phone_number_fixture.ex @@ -4,14 +4,14 @@ defmodule PhoneNumberFixture do def alpha_numeric_number() do %PhoneNumber{ country_code: 1, - national_number: 80074935247 + national_number: 80_074_935_247 } end def alpha_numeric_number2() do %PhoneNumber{ country_code: 1, - national_number: 80074935247, + national_number: 80_074_935_247, extension: "1234" } end @@ -19,98 +19,98 @@ defmodule PhoneNumberFixture do def ae_uan() do %PhoneNumber{ country_code: 971, - national_number: 600123456 + national_number: 600_123_456 } end def ar_mobile() do %PhoneNumber{ country_code: 54, - national_number: 91187654321 + national_number: 91_187_654_321 } end def ar_mobile2() do %PhoneNumber{ country_code: 54, - national_number: 93435551212 + national_number: 93_435_551_212 } end def ar_mobile3() do %PhoneNumber{ country_code: 54, - national_number: 93715654320 + national_number: 93_715_654_320 } end def ar_number() do %PhoneNumber{ country_code: 54, - national_number: 1187654321 + national_number: 1_187_654_321 } end def ar_number2() do %PhoneNumber{ country_code: 54, - national_number: 1987654321 + national_number: 1_987_654_321 } end def ar_number3() do %PhoneNumber{ country_code: 54, - national_number: 3715654321 + national_number: 3_715_654_321 } end def ar_number4() do %PhoneNumber{ country_code: 54, - national_number: 2312340000 + national_number: 2_312_340_000 } end def ar_number5() do %PhoneNumber{ country_code: 54, - national_number: 81429712 + national_number: 81_429_712 } end def au_number() do %PhoneNumber{ country_code: 61, - national_number: 236618300 + national_number: 236_618_300 } end def au_number2() do %PhoneNumber{ country_code: 61, - national_number: 1800123456 + national_number: 1_800_123_456 } end def bs_mobile() do %PhoneNumber{ country_code: 1, - national_number: 2423570000 + national_number: 2_423_570_000 } end def bs_number() do %PhoneNumber{ country_code: 1, - national_number: 2423651234 + national_number: 2_423_651_234 } end def bs_number_invalid() do %PhoneNumber{ country_code: 1, - national_number: 2421232345 + national_number: 2_421_232_345 } end @@ -131,63 +131,63 @@ defmodule PhoneNumberFixture do def by_number3() do %PhoneNumber{ country_code: 375, - national_number: 812345 + national_number: 812_345 } end def by_number4() do %PhoneNumber{ country_code: 375, - national_number: 123456 + national_number: 123_456 } end def de_number() do %PhoneNumber{ country_code: 49, - national_number: 30123456 + national_number: 30_123_456 } end def de_number2() do %PhoneNumber{ country_code: 49, - national_number: 301234 + national_number: 301_234 } end def de_number3() do %PhoneNumber{ country_code: 49, - national_number: 291123 + national_number: 291_123 } end def de_number4() do %PhoneNumber{ country_code: 49, - national_number: 29112345678 + national_number: 29_112_345_678 } end def de_number5() do %PhoneNumber{ country_code: 49, - national_number: 912312345 + national_number: 912_312_345 } end def de_number6() do %PhoneNumber{ country_code: 49, - national_number: 80212345 + national_number: 80_212_345 } end def de_number7() do %PhoneNumber{ country_code: 49, - national_number: 41341234 + national_number: 41_341_234 } end @@ -201,28 +201,28 @@ defmodule PhoneNumberFixture do def de_premium() do %PhoneNumber{ country_code: 49, - national_number: 9001654321 + national_number: 9_001_654_321 } end def de_premium2() do %PhoneNumber{ country_code: 49, - national_number: 90091234567 + national_number: 90_091_234_567 } end def de_toll_free() do %PhoneNumber{ country_code: 49, - national_number: 8001234567 + national_number: 8_001_234_567 } end def de_mobile() do %PhoneNumber{ country_code: 49, - national_number: 15123456789 + national_number: 15_123_456_789 } end @@ -236,56 +236,56 @@ defmodule PhoneNumberFixture do def gb_mobile() do %PhoneNumber{ country_code: 44, - national_number: 7912345678 + national_number: 7_912_345_678 } end def gb_number() do %PhoneNumber{ country_code: 44, - national_number: 2070313000 + national_number: 2_070_313_000 } end def gb_premium() do %PhoneNumber{ country_code: 44, - national_number: 9187654321 + national_number: 9_187_654_321 } end def gb_toll_free() do %PhoneNumber{ country_code: 44, - national_number: 8012345678 + national_number: 8_012_345_678 } end def gb_shared_cost() do %PhoneNumber{ country_code: 44, - national_number: 8431231234 + national_number: 8_431_231_234 } end def gb_voip() do %PhoneNumber{ country_code: 44, - national_number: 5631231234 + national_number: 5_631_231_234 } end def gb_personal_number() do %PhoneNumber{ country_code: 44, - national_number: 7031231234 + national_number: 7_031_231_234 } end def gb_invalid() do %PhoneNumber{ country_code: 44, - national_number: 791234567 + national_number: 791_234_567 } end @@ -299,7 +299,7 @@ defmodule PhoneNumberFixture do def gb_number2() do %PhoneNumber{ country_code: 44, - national_number: 2034567890, + national_number: 2_034_567_890, extension: "456" } end @@ -307,36 +307,36 @@ defmodule PhoneNumberFixture do def it_mobile() do %PhoneNumber{ country_code: 39, - national_number: 345678901 + national_number: 345_678_901 } end def it_number() do %PhoneNumber{ country_code: 39, - national_number: 236618300, - italian_leading_zero: true, + national_number: 236_618_300, + italian_leading_zero: true } end def it_premium() do %PhoneNumber{ country_code: 39, - national_number: 892123 + national_number: 892_123 } end def it_toll_free() do %PhoneNumber{ country_code: 39, - national_number: 803123 + national_number: 803_123 } end def it_invalid() do %PhoneNumber{ country_code: 39, - national_number: 23661830000, + national_number: 23_661_830_000, italian_leading_zero: true } end @@ -386,70 +386,70 @@ defmodule PhoneNumberFixture do def mx_mobile1() do %PhoneNumber{ country_code: 52, - national_number: 12345678900 + national_number: 12_345_678_900 } end def mx_mobile2() do %PhoneNumber{ country_code: 52, - national_number: 15512345678 + national_number: 15_512_345_678 } end def mx_number1() do %PhoneNumber{ country_code: 52, - national_number: 3312345678 + national_number: 3_312_345_678 } end def mx_number2() do %PhoneNumber{ country_code: 52, - national_number: 8211234567 + national_number: 8_211_234_567 } end def mx_number3() do %PhoneNumber{ country_code: 52, - national_number: 4499780001 + national_number: 4_499_780_001 } end def mx_number4() do %PhoneNumber{ country_code: 52, - national_number: 13312345678 + national_number: 13_312_345_678 } end def nz_number() do %PhoneNumber{ country_code: 64, - national_number: 33316005 + national_number: 33_316_005 } end def nz_number2() do %PhoneNumber{ country_code: 64, - national_number: 21387835 + national_number: 21_387_835 } end def nz_number3() do %PhoneNumber{ country_code: 64, - national_number: 64123456 + national_number: 64_123_456 } end def nz_invalid() do %PhoneNumber{ country_code: 64, - national_number: 3316005 + national_number: 3_316_005 } end @@ -463,21 +463,21 @@ defmodule PhoneNumberFixture do def nz_toll_free() do %PhoneNumber{ country_code: 64, - national_number: 800332005 + national_number: 800_332_005 } end def nz_premium() do %PhoneNumber{ country_code: 64, - national_number: 9003326005 + national_number: 9_003_326_005 } end def nz_number4() do %PhoneNumber{ country_code: 64, - national_number: 33316005, + national_number: 33_316_005, extension: "3456" } end @@ -485,91 +485,91 @@ defmodule PhoneNumberFixture do def re_number() do %PhoneNumber{ country_code: 262, - national_number: 262123456 + national_number: 262_123_456 } end def re_number_invalid() do %PhoneNumber{ country_code: 262, - national_number: 269123456 + national_number: 269_123_456 } end def re_yt_number() do %PhoneNumber{ country_code: 262, - national_number: 800123456 + national_number: 800_123_456 } end def sg_number() do %PhoneNumber{ country_code: 65, - national_number: 65218000 + national_number: 65_218_000 } end def sg_number2() do %PhoneNumber{ country_code: 65, - national_number: 1234567890 + national_number: 1_234_567_890 } end def us_number2() do %PhoneNumber{ country_code: 1, - national_number: 1234567890 + national_number: 1_234_567_890 } end def us_long_number() do %PhoneNumber{ country_code: 1, - national_number: 65025300001 + national_number: 65_025_300_001 } end def us_number() do %PhoneNumber{ country_code: 1, - national_number: 6502530000 + national_number: 6_502_530_000 } end def us_premium() do %PhoneNumber{ country_code: 1, - national_number: 9002530000 + national_number: 9_002_530_000 } end def us_local_number() do %PhoneNumber{ country_code: 1, - national_number: 2530000 + national_number: 2_530_000 } end def us_short_by_one_number() do %PhoneNumber{ country_code: 1, - national_number: 650253000 + national_number: 650_253_000 } end def us_tollfree() do %PhoneNumber{ country_code: 1, - national_number: 8002530000 + national_number: 8_002_530_000 } end def us_tollfree2() do %PhoneNumber{ country_code: 1, - national_number: 8881234567 + national_number: 8_881_234_567 } end @@ -591,7 +591,7 @@ defmodule PhoneNumberFixture do def us_number_with_extension() do %PhoneNumber{ country_code: 1, - national_number: 2121231234, + national_number: 2_121_231_234, extension: "508" } end @@ -599,7 +599,7 @@ defmodule PhoneNumberFixture do def us_number_with_extension2() do %PhoneNumber{ country_code: 1, - national_number: 6451231234, + national_number: 6_451_231_234, extension: "910" } end @@ -607,35 +607,35 @@ defmodule PhoneNumberFixture do def yt_number() do %PhoneNumber{ country_code: 262, - national_number: 269601234 + national_number: 269_601_234 } end def international_toll_free() do %PhoneNumber{ country_code: 800, - national_number: 12345678 + national_number: 12_345_678 } end def international_toll_free_too_long() do %PhoneNumber{ country_code: 800, - national_number: 123456789 + national_number: 123_456_789 } end def universal_premium_rate() do %PhoneNumber{ country_code: 979, - national_number: 123456789 + national_number: 123_456_789 } end def nanpa_short_number() do %PhoneNumber{ country_code: 1, - national_number: 253000 + national_number: 253_000 } end diff --git a/test/test_helper.exs b/test/test_helper.exs index 4a23070..6ef6019 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,6 +1,7 @@ -ExUnit.start +ExUnit.start() {:ok, files} = File.ls("./test/support") -Enum.each files, fn(file) -> - Code.require_file "support/#{file}", __DIR__ -end + +Enum.each(files, fn file -> + Code.require_file("support/#{file}", __DIR__) +end)