BeamLabCountries (beamlab_countries v2.0.1)
View SourceModule for providing countries related functions.
Lookup functions (get/1, get_by_alpha3/1) return nil for anything that is
not a known code, including non-string input. get_by/2, filter_by/1,2 and
exists?/2 raise ArgumentError for attributes that are not BeamLabCountries.Country
fields, so a typo does not silently produce an empty result.
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
@spec all() :: [BeamLabCountries.Country.t()]
Returns all countries.
@spec count() :: non_neg_integer()
Returns the total number of countries.
Examples
iex> BeamLabCountries.count()
250
@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"]
Checks if country for specific attribute and value exists.
Returns boolean.
Examples
iex> BeamLabCountries.exists?(:name, "Poland")
true
iex> BeamLabCountries.exists?(:name, "Polande")
false
@spec filter_by(keyword()) :: [BeamLabCountries.Country.t()]
@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)
29
@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
@spec get!(String.t()) :: BeamLabCountries.Country.t()
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"
@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
@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