NIF interface to Google's libphonenumber C++ library.
This module provides NIF bindings for phone number parsing, formatting, and validation using libphonenumber. The NIF is always compiled and loaded as a required dependency.
Requires the libphonenumber C++ library to be installed on
the system. On macOS: brew install libphonenumber.
Summary
Functions
Returns whether the NIF backend is available.
Formats a phone number in the specified format.
Parses a phone number string into its component parts.
Returns whether a phone number is a possible number.
Returns the territory code for a phone number.
Returns the phone number type.
Returns whether a phone number is valid.
Returns whether a phone number is valid for a specific territory.
Functions
@spec available?() :: boolean()
Returns whether the NIF backend is available.
Returns
trueif the NIF shared library was loaded successfully.falseif the NIF is not compiled or libphonenumber libraries are missing.
Examples
iex> is_boolean(Localize.PhoneNumber.Nif.available?())
true
Formats a phone number in the specified format.
Arguments
native_binaryis the opaque binary from a parsed phone number.format_typeis one of"e164","international","national", or"rfc3966".
Returns
{:ok, formatted_string}on success.{:error, reason}on failure.
@spec parse(String.t(), String.t()) :: {:ok, pos_integer(), pos_integer(), String.t(), String.t(), binary()} | {:error, String.t()}
Parses a phone number string into its component parts.
Arguments
number_stringis the phone number string to parse.default_territoryis the ISO 3166-1 alpha-2 territory code used when the number is not in international format.
Returns
{:ok, country_code, national_number, extension, raw_input, native_binary}on successful parse.{:error, reason}if the number cannot be parsed.
Returns whether a phone number is a possible number.
A possible number has the right length for its type and territory but may not actually be allocated.
Arguments
native_binaryis the opaque binary from a parsed phone number.
Returns
trueif the phone number is possible.falseotherwise.
Returns the territory code for a phone number.
Arguments
native_binaryis the opaque binary from a parsed phone number.
Returns
- A two-letter ISO 3166-1 alpha-2 territory code string
(e.g.,
"US","GB"), or an empty string if the territory cannot be determined.
Returns the phone number type.
Arguments
native_binaryis the opaque binary from a parsed phone number.
Returns
- A string representing the type, such as
"mobile","fixed_line","fixed_line_or_mobile","toll_free","premium_rate","shared_cost","voip","personal_number","pager","uan","voicemail", or"unknown".
Returns whether a phone number is valid.
Arguments
native_binaryis the opaque binary from a parsed phone number.
Returns
trueif the phone number is valid.falseotherwise.
Returns whether a phone number is valid for a specific territory.
Arguments
native_binaryis the opaque binary from a parsed phone number.territoryis the ISO 3166-1 alpha-2 territory code.
Returns
trueif the phone number is valid for the given territory.falseotherwise.