BeamLabCountries (beamlab_countries v1.1.0)

View Source

Module for providing countries related functions.

Summary

Functions

Returns all countries.

Returns the total number of countries.

Returns all EU member countries.

Checks if country for specific attribute and value exists.

Filters countries by given attribute.

Returns one country given its alpha2 country code, or nil if not found.

Returns one country given its alpha2 country code, or raises if not found.

Returns one country matching the given attribute and value, or nil if not found.

Returns one country given its alpha3 country code, or nil if not found.

Functions

all()

@spec all() :: [BeamLabCountries.Country.t()]

Returns all countries.

count()

@spec count() :: non_neg_integer()

Returns the total number of countries.

Examples

iex> BeamLabCountries.count()
250

eu_members()

@spec eu_members() :: [BeamLabCountries.Country.t()]

Returns all EU member countries.

Examples

iex> eu = BeamLabCountries.eu_members()
iex> length(eu)
27
iex> Enum.map(eu, &Map.get(&1, :alpha2)) |> Enum.take(3)
["AT", "BE", "BG"]

exists?(attribute, value)

@spec exists?(atom(), term()) :: boolean()

Checks if country for specific attribute and value exists.

Returns boolean.

Examples

iex> BeamLabCountries.exists?(:name, "Poland")
true

iex> BeamLabCountries.exists?(:name, "Polande")
false

filter_by(criteria)

@spec filter_by(keyword()) :: [BeamLabCountries.Country.t()]

filter_by(attribute, value)

@spec filter_by(atom(), term()) :: [BeamLabCountries.Country.t()]

Filters countries by given attribute.

Returns a list of BeamLabCountries.Country structs

Single attribute

iex> countries = BeamLabCountries.filter_by(:region, "Europe")
iex> Enum.count(countries)
51
iex> Enum.map(countries, &Map.get(&1, :alpha2)) |> Enum.take(5)
["AD", "AL", "AT", "AX", "BA"]

iex> countries = BeamLabCountries.filter_by(:unofficial_names, "Reino Unido")
iex> Enum.count(countries)
1
iex> Enum.map(countries, &Map.get(&1, :name)) |> List.first
"United Kingdom of Great Britain and Northern Ireland"

Multiple attributes

iex> countries = BeamLabCountries.filter_by(region: "Europe", eu_member: true)
iex> length(countries)
26

iex> countries = BeamLabCountries.filter_by(region: "Europe", eea_member: true)
iex> length(countries)
30

get(country_code)

@spec get(String.t()) :: BeamLabCountries.Country.t() | nil

Returns one country given its alpha2 country code, or nil if not found.

Examples

iex> %BeamLabCountries.Country{name: name} = BeamLabCountries.get("PL")
iex> name
"Poland"

iex> BeamLabCountries.get("INVALID")
nil

get!(country_code)

Returns one country given its alpha2 country code, or raises if not found.

Examples

iex> %BeamLabCountries.Country{name: name} = BeamLabCountries.get!("PL")
iex> name
"Poland"

get_by(attribute, value)

@spec get_by(atom(), term()) :: BeamLabCountries.Country.t() | nil

Returns one country matching the given attribute and value, or nil if not found.

Examples

iex> %BeamLabCountries.Country{alpha2: alpha2} = BeamLabCountries.get_by(:name, "Poland")
iex> alpha2
"PL"

iex> BeamLabCountries.get_by(:name, "Atlantis")
nil

get_by_alpha3(country_code)

@spec get_by_alpha3(String.t()) :: BeamLabCountries.Country.t() | nil

Returns one country given its alpha3 country code, or nil if not found.

Examples

iex> %BeamLabCountries.Country{name: name} = BeamLabCountries.get_by_alpha3("POL")
iex> name
"Poland"

iex> BeamLabCountries.get_by_alpha3("INVALID")
nil