Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mathieuprog/mobile_number_format
Parse and validate mobile phone numbers
https://github.com/mathieuprog/mobile_number_format
Last synced: about 2 months ago
JSON representation
Parse and validate mobile phone numbers
- Host: GitHub
- URL: https://github.com/mathieuprog/mobile_number_format
- Owner: mathieuprog
- License: apache-2.0
- Created: 2021-05-14T14:28:01.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-05-08T10:05:49.000Z (over 1 year ago)
- Last Synced: 2024-10-29T08:45:13.585Z (2 months ago)
- Language: Elixir
- Size: 177 KB
- Stars: 8
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MobileNumberFormat
This library helps you parsing and validating mobile phone numbers.
It relies on the data available in Google's [libphonenumber](https://github.com/google/libphonenumber) library. Version `0.4.0` uses the data from version `v8.13.11` of `libphonenumber`.
### `parse/1`
```elixir
assert {:ok,
%{
country_code: "BE",
country_calling_code: "32",
national_number: "455123456",
trimmed_national_number_input: "455/12.34.56"
}} =
MobileNumberFormat.parse(%{
country_calling_code: 32,
national_number: "0455/12.34.56"
})
```The `parse/1` function receives a map that may contain any of these keys:
* `country_code`
For instance `"BE"`.* `country_calling_code`
For instance `"32"`, `"+32"` or `32`.
Local international call prefixes are not supported (only the prefix `+` is supported).* `national_number`
For instance the Belgian national number `"455/12.34.56"`. National prefixes are supported `"0455/12.34.56"`.* `international_number`
For instance `"+32 455/12.34.56"`. A national prefix may be added in parentheses `"+32 (0)455/12.34.56"`.In case the number is valid, the tuple `{:ok, data}` is returned where `data` is a map containing the 4 key/values mentioned above.
In case of error, the following atoms may be returned:
* `:invalid_country_code`
* `:invalid_country_calling_code`
* `:invalid_number`
* `:insufficient_data`### `valid_number?/1`
Same as `parse/1` but returns `true` or `false` whether the number is valid or not.
### `valid_country_calling_code?/1`
Checks whether the given country calling code is valid or not.
### `valid_country_code?/1`
Checks whether the given ISO country code is valid or not.
### `formatting_data_per_territory/0`
Returns a list of all the formatting rules. Example of formatting rules (for Belgium):
```elixir
%{
country_calling_code: "32",
example: "470123456",
country_code: "BE",
national_prefix: "0",
possible_lengths: [9],
regex: ~r/4[5-9]\d{7}/
}
```## Ecto schema
The library provides an embedded schema `MobileNumberFormat.MobileNumber`.
## Installation
Add `mobile_number_format` for Elixir as a dependency in your `mix.exs` file:
```elixir
def deps do
[
{:mobile_number_format, "~> 0.4.0"}
]
end
```## HexDocs
HexDocs documentation can be found at [https://hexdocs.pm/mobile_number_format](https://hexdocs.pm/mobile_number_format).