Phone (phone v0.5.4)
Phone is a real telephone number parser, that will help you get useful information from numbers.
How to use
Very simple to use:
iex> Phone.parse("555132345678")
{:ok, %{a2: "BR", a3: "BRA", country: "Brazil", international_code: "55", area_code: "51", number: "32345678", area_abbreviation: "RS", area_type: "state", area_name: "Rio Grande do Sul"}}
Link to this section Summary
Functions
Parses a string or integer and returns a map with information about that number.
Same as parse/1
but the number doesn't have the international code, instead you specify country as an atom with two-letters code.
Same as parse/1
, except it raises on error.
Same as parse/2
, except it raises on error.
Returns true
if the number can be parsed, otherwise returns false
.
Link to this section Functions
parse(number)
Specs
parse(String.t()) :: {:ok, Map.t()}
parse(pos_integer()) :: {:ok, Map.t()}
parse(String.t()) :: boolean()
parse(pos_integer()) :: boolean()
Parses a string or integer and returns a map with information about that number.
iex> Phone.parse("555132345678")
{:ok, %{a2: "BR", a3: "BRA", country: "Brazil", international_code: "55", area_code: "51", number: "32345678", area_abbreviation: "RS", area_type: "state", area_name: "Rio Grande do Sul"}}
iex> Phone.parse("+55(51)3234-5678")
{:ok, %{a2: "BR", a3: "BRA", country: "Brazil", international_code: "55", area_code: "51", number: "32345678", area_abbreviation: "RS", area_type: "state", area_name: "Rio Grande do Sul"}}
iex> Phone.parse("55 51 3234-5678")
{:ok, %{a2: "BR", a3: "BRA", country: "Brazil", international_code: "55", area_code: "51", number: "32345678", area_abbreviation: "RS", area_type: "state", area_name: "Rio Grande do Sul"}}
iex> Phone.parse(555132345678)
{:ok, %{a2: "BR", a3: "BRA", country: "Brazil", international_code: "55", area_code: "51", number: "32345678", area_abbreviation: "RS", area_type: "state", area_name: "Rio Grande do Sul"}}
parse(number, atom)
Specs
parse(String.t(), Atom.t()) :: {:ok, Map.t()}
parse(pos_integer(), Atom.t()) :: {:ok, Map.t()}
Same as parse/1
but the number doesn't have the international code, instead you specify country as an atom with two-letters code.
For NANP countries you can use the atom :nanp
or two-letter codes for any country in NANP.
For United Kingdom is possible to use the more known acronym :uk
or the official two-letter code :gb
.
iex> Phone.parse("5132345678", :br)
{:ok, %{a2: "BR", a3: "BRA", country: "Brazil", international_code: "55", area_code: "51", number: "32345678", area_abbreviation: "RS", area_type: "state", area_name: "Rio Grande do Sul"}}
iex> Phone.parse("(51)3234-5678", :br)
{:ok, %{a2: "BR", a3: "BRA", country: "Brazil", international_code: "55", area_code: "51", number: "32345678", area_abbreviation: "RS", area_type: "state", area_name: "Rio Grande do Sul"}}
iex> Phone.parse("51 3234-5678", :br)
{:ok, %{a2: "BR", a3: "BRA", country: "Brazil", international_code: "55", area_code: "51", number: "32345678", area_abbreviation: "RS", area_type: "state", area_name: "Rio Grande do Sul"}}
iex> Phone.parse(5132345678, :br)
{:ok, %{a2: "BR", a3: "BRA", country: "Brazil", international_code: "55", area_code: "51", number: "32345678", area_abbreviation: "RS", area_type: "state", area_name: "Rio Grande do Sul"}}
parse!(number)
Specs
parse!(String.t()) :: Map.t()
parse!(pos_integer()) :: Map.t()
Same as parse/1
, except it raises on error.
iex> Phone.parse!("555132345678")
%{a2: "BR", a3: "BRA", country: "Brazil", international_code: "55", area_code: "51", number: "32345678", area_abbreviation: "RS", area_type: "state", area_name: "Rio Grande do Sul"}
iex> Phone.parse!("+55(51)3234-5678")
%{a2: "BR", a3: "BRA", country: "Brazil", international_code: "55", area_code: "51", number: "32345678", area_abbreviation: "RS", area_type: "state", area_name: "Rio Grande do Sul"}
iex> Phone.parse!("55 51 3234-5678")
%{a2: "BR", a3: "BRA", country: "Brazil", international_code: "55", area_code: "51", number: "32345678", area_abbreviation: "RS", area_type: "state", area_name: "Rio Grande do Sul"}
iex> Phone.parse!(555132345678)
%{a2: "BR", a3: "BRA", country: "Brazil", international_code: "55", area_code: "51", number: "32345678", area_abbreviation: "RS", area_type: "state", area_name: "Rio Grande do Sul"}
parse!(number, atom)
Specs
parse!(String.t(), Atom.t()) :: Map.t()
parse!(pos_integer(), Atom.t()) :: Map.t()
Same as parse/2
, except it raises on error.
iex> Phone.parse!("5132345678", :br)
%{a2: "BR", a3: "BRA", country: "Brazil", international_code: "55", area_code: "51", number: "32345678", area_abbreviation: "RS", area_type: "state", area_name: "Rio Grande do Sul"}
iex> Phone.parse!("(51)3234-5678", :br)
%{a2: "BR", a3: "BRA", country: "Brazil", international_code: "55", area_code: "51", number: "32345678", area_abbreviation: "RS", area_type: "state", area_name: "Rio Grande do Sul"}
iex> Phone.parse!("51 3234-5678", :br)
%{a2: "BR", a3: "BRA", country: "Brazil", international_code: "55", area_code: "51", number: "32345678", area_abbreviation: "RS", area_type: "state", area_name: "Rio Grande do Sul"}
iex> Phone.parse!(5132345678, :br)
%{a2: "BR", a3: "BRA", country: "Brazil", international_code: "55", area_code: "51", number: "32345678", area_abbreviation: "RS", area_type: "state", area_name: "Rio Grande do Sul"}
valid?(number)
Returns true
if the number can be parsed, otherwise returns false
.
iex> Phone.valid?("555132345678")
true
iex> Phone.valid?("+55(51)3234-5678")
true
iex> Phone.valid?("55 51 3234-5678")
true
iex> Phone.valid?(555132345678)
true