View Source UspsEx.Util (usps_ex v2023.1.27)

Utils for UspsEx.

Link to this section Summary

Functions

Returns true for nil, empty strings, and strings only containing whitespace. Returns false otherwise.

Converts centimeters to inches.

Converts inches to centimeters.

Converts kilograms to pounds.

Converts pounds to kilograms.

Takes a price and multiplies it by 100. Accepts nil, floats, integers, and strings.

Takes a price and divides it by 100, returning a string representation. This is used for API calls that require dollars instead of cents. Unlike price_to_cents, this only accepts integers and nil. Otherwise, it will raise an exception.

Returns the given map with keys converted to strings, and the values trimmed (if the values are also strings).

Removes accents/ligatures from letters.

Link to this section Functions

@spec blank?(term()) :: boolean()

Returns true for nil, empty strings, and strings only containing whitespace. Returns false otherwise.

iex> Util.blank?(nil)
true
iex> Util.blank?("")
true
iex> Util.blank?("   ")
true
iex> Util.blank?(" \t\r\n ")
true
iex> Util.blank?("Test")
false
iex> Util.blank?(100)
false
@spec cm_to_inches(number()) :: float()

Converts centimeters to inches.

iex> Util.cm_to_inches(10)
3.9
iex> Util.cm_to_inches(0)
0.0
Link to this function

create_hash(string, min_len \\ 5)

View Source
@spec inches_to_cm(number()) :: float()

Converts inches to centimeters.

iex> Util.inches_to_cm(10)
25.4
iex> Util.inches_to_cm(0)
0.0
@spec kgs_to_lbs(number()) :: float()

Converts kilograms to pounds.

iex> Util.kgs_to_lbs(10)
22.0
iex> Util.kgs_to_lbs(0)
0.0
@spec lbs_to_kgs(number()) :: float()

Converts pounds to kilograms.

iex> Util.lbs_to_kgs(10)
4.5
iex> Util.lbs_to_kgs(0)
0.0
@spec price_to_cents(nil | number() | String.t()) :: integer()

Takes a price and multiplies it by 100. Accepts nil, floats, integers, and strings.

iex> Util.price_to_cents(nil)
0
iex> Util.price_to_cents(0)
0
iex> Util.price_to_cents(28.00)
2800
iex> Util.price_to_cents("28.00")
2800
iex> Util.price_to_cents("28")
2800
iex> Util.price_to_cents(28)
2800
Link to this function

price_to_dollars(integer)

View Source
@spec price_to_dollars(integer()) :: String.t() | none()

Takes a price and divides it by 100, returning a string representation. This is used for API calls that require dollars instead of cents. Unlike price_to_cents, this only accepts integers and nil. Otherwise, it will raise an exception.

iex> Util.price_to_dollars(nil)
"0.00"
iex> Util.price_to_dollars(200_00)
"200"
iex> Util.price_to_dollars("20000")
** (FunctionClauseError) no function clause matching in UspsEx.Util.price_to_dollars/1
Link to this function

services_country?(country)

View Source
Link to this function

state_without_country(map)

View Source
Link to this function

stringify_and_trim(params)

View Source
@spec stringify_and_trim(map()) :: map()

Returns the given map with keys converted to strings, and the values trimmed (if the values are also strings).

iex> Util.stringify_and_trim(%{foo: "  bar  "})
%{"foo" => "bar"}
@spec unaccent(String.t()) :: String.t()

Removes accents/ligatures from letters.

iex> Util.unaccent("Curaçao")
"Curacao"
iex> Util.unaccent("Republic of Foo (the)")
"Republic of Foo (the)"
iex> Util.unaccent("Åland Islands")
"Aland Islands"
Link to this function

weight_in_ounces(data, weight_unit \\ :lbs)

View Source