View Source Numscriptex.Utilities (numscriptex v0.2.5)

Numscriptex.Utilities module contain reusable code that are common in more than one module of this library.

Summary

Functions

Gets the decimal places from an asset if has any (e.g. decimal places for "USD/2" would be 2). You can choose a default value (the second argument), but if you don't choose any the default will be 2.

Converts an integer value to float

Convert map keys to the desired type. Can go from string to atom and vice versa.

Functions

decimal_places_from_asset(asset, default \\ 2)

@spec decimal_places_from_asset(bitstring(), pos_integer()) :: pos_integer()

Gets the decimal places from an asset if has any (e.g. decimal places for "USD/2" would be 2). You can choose a default value (the second argument), but if you don't choose any the default will be 2.

iex> Utilities.decimal_places_from_asset("USD/2")
2

iex> Utilities.decimal_places_from_asset("USD/4")
4

iex> Utilities.decimal_places_from_asset("USD")
2

iex> Utilities.decimal_places_from_asset("USD", 3)
3

integer_to_decimal(amount, decimal_places \\ 2)

@spec integer_to_decimal(pos_integer(), pos_integer() | nil) :: float()

Converts an integer value to float

iex> Utilities.integer_to_decimal(1000, 2)
10.0

iex> Utilities.integer_to_decimal(1000, 3)
1.0

iex> Utilities.integer_to_decimal(1000, 4)
0.1

iex> Utilities.integer_to_decimal(1000, 5)
0.01

normalize_keys(map, atom)

@spec normalize_keys(map(), :string | :atom) :: map()

Convert map keys to the desired type. Can go from string to atom and vice versa.

iex> map = %{"foo" => 100}
...> Utilities.normalize_keys(map, :atom)
%{foo: 100}

iex> map = %{foo: 100}
...> Utilities.normalize_keys(map, :string)
%{"foo" => 100}