Localize.PhoneNumber.Nif (Localize.PhoneNumber v0.2.0)

Copy Markdown View Source

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

available?()

@spec available?() :: boolean()

Returns whether the NIF backend is available.

Returns

  • true if the NIF shared library was loaded successfully.

  • false if the NIF is not compiled or libphonenumber libraries are missing.

Examples

iex> is_boolean(Localize.PhoneNumber.Nif.available?())
true

format(native_binary, format_type)

@spec format(binary(), String.t()) :: {:ok, String.t()} | {:error, String.t()}

Formats a phone number in the specified format.

Arguments

  • native_binary is the opaque binary from a parsed phone number.

  • format_type is one of "e164", "international", "national", or "rfc3966".

Returns

  • {:ok, formatted_string} on success.

  • {:error, reason} on failure.

parse(number_string, default_territory)

@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_string is the phone number string to parse.

  • default_territory is 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.

possible?(native_binary)

@spec possible?(binary()) :: boolean()

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_binary is the opaque binary from a parsed phone number.

Returns

  • true if the phone number is possible.

  • false otherwise.

territory(native_binary)

@spec territory(binary()) :: String.t()

Returns the territory code for a phone number.

Arguments

  • native_binary is 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.

type(native_binary)

@spec type(binary()) :: String.t()

Returns the phone number type.

Arguments

  • native_binary is 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".

valid?(native_binary)

@spec valid?(binary()) :: boolean()

Returns whether a phone number is valid.

Arguments

  • native_binary is the opaque binary from a parsed phone number.

Returns

  • true if the phone number is valid.

  • false otherwise.

valid_for_territory?(native_binary, territory)

@spec valid_for_territory?(binary(), String.t()) :: boolean()

Returns whether a phone number is valid for a specific territory.

Arguments

  • native_binary is the opaque binary from a parsed phone number.

  • territory is the ISO 3166-1 alpha-2 territory code.

Returns

  • true if the phone number is valid for the given territory.

  • false otherwise.