Etop.Utils (Etop v0.5.3)

Utility helpers for Etop.

Link to this section Summary

Functions

Center a string in the given length.

Round a number and convert to a string.

Returns the server's local naive datetime with the microsecond field truncated to the given precision (:microsecond, :millisecond or :second).

Pad (leading) the given string with spaces for the given length.

Pad (trailing) the given string with spaces for the given length.

Scale a number into xb unit with label.

Scale a number into xb unit with label.

Configurable sort.

Get the server's timezone offset in seconds.

Link to this section Functions

Link to this function

center(item, len, char \\ " ")

Specs

center(any(), integer(), String.t()) :: String.t()

Center a string in the given length.

Return a string of length >= the given length with the given string centered.

The returned string is padded (leading and trailing) with the given padding (default " ")

Examples

iex> Etop.Utils.center("Test", 8)
"  Test  "

iex> Etop.Utils.center('Test', 7, "-")
"-Test--"

iex> Etop.Utils.center("test", 2)
"test"
Link to this function

float_to_string(size, rnd)

Specs

float_to_string(number(), integer()) :: String.t()

Round a number and convert to a string.

iex> Etop.Utils.float_to_string(1.125, 2)
"1.13"

iex> Etop.Utils.float_to_string(1.125, 1)
"1.1"

iex> Etop.Utils.float_to_string(1.5, 0)
"2"

iex> Etop.Utils.float_to_string(100, 0)
"100"
Link to this function

local_time(datetime \\ NaiveDateTime.utc_now(), precision \\ :second)

Specs

local_time(DateTime.t() | NaiveDateTime.t(), atom()) :: NaiveDateTime.t()

Returns the server's local naive datetime with the microsecond field truncated to the given precision (:microsecond, :millisecond or :second).

Arguments

  • datetime (default utc_now)
  • precision (default :second)

Examples

iex> datetime = Etop.Utils.local_time()
iex> datetime.year >= 2020
true

iex> datetime = Etop.Utils.local_time(:millisecond)
iex> elem(datetime.microsecond, 1)
3

iex> datetime = NaiveDateTime.utc_now()
iex> expected = NaiveDateTime.add(datetime, Etop.Utils.timezone_offset())
iex> Etop.Utils.local_time(datetime) == %{expected | microsecond: {0, 0}}
true

iex> datetime = NaiveDateTime.utc_now()
iex> expected = NaiveDateTime.add(datetime, Etop.Utils.timezone_offset())
iex> Etop.Utils.local_time(datetime, :microsecond) == expected
true
Link to this function

pad(string, len, char \\ " ")

Specs

pad(any(), integer(), String.t()) :: String.t()

Pad (leading) the given string with spaces for the given length.

Examples

iex> Etop.Utils.pad("Test", 8)
"    Test"

iex> Etop.Utils.pad("Test", 2)
"Test"

iex> Etop.Utils.pad(100, 4, "0")
"0100"
Link to this function

pad_t(string, len, char \\ " ")

Specs

pad_t(any(), integer(), String.t()) :: String.t()

Pad (trailing) the given string with spaces for the given length.

Examples

iex> Etop.Utils.pad_t("Test", 8)
"Test    "

iex> Etop.Utils.pad_t("Test", 2)
"Test"

iex> Etop.Utils.pad_t(10.1, 5, "0")
"10.10"
Link to this function

size_string_b(size, rnd \\ 2)

Specs

size_string_b(number(), integer()) :: String.t()

Scale a number into xb unit with label.

Examples

iex> Etop.Utils.size_string_b(100.123) "100.12B"

iex> Etop.Utils.size_string_b(10.5, 0) "11B"

iex> Etop.Utils.size_string_b(1500) "1.46KB"

Link to this function

size_string_kb(size, rnd \\ 2)

Specs

size_string_kb(number(), integer()) :: String.t()

Scale a number into xb unit with label.

Examples

iex> Etop.Utils.size_string_kb(0.253)
"0.25KB"

iex> Etop.Utils.size_string_kb(0.253, 1)
"0.3KB"

iex> Etop.Utils.size_string_kb(1500)
"1.46MB"

iex> Etop.Utils.size_string_kb(1024 * 1024 * 3)
"3.0GB"

iex> Etop.Utils.size_string_kb(1024 * 1024 * 1024 * 2.5)
"2.5TB"

iex> Etop.Utils.size_string_kb(1024 * 1024 * 1024 * 1024 * 1.5, 0)
"2PB"

iex> Etop.Utils.size_string_kb(1024 * 1024 * 1024 * 1024 * 1024, 0)
"1EB"
Link to this function

sort(list, field, opts \\ [])

Configurable sort.

Arguments

  • list - the enumerable to be sorted.
  • field (:reductions_diff) - the field to be sorted on.
  • field_fn (fn field -> &elem(&1, 1)[field] end) - function to get the field.
  • sorter_fn (&>/2) -> Sort comparator (default descending)

Examples

iex> data = [one: %{a: 3, b: 2}, two: %{a: 1, b: 3}]
iex> Etop.Utils.sort(data, :b)
[two: %{a: 1, b: 3}, one: %{a: 3, b: 2}]

iex> data = [one: %{a: 3, b: 2}, two: %{a: 1, b: 3}]
iex> Etop.Utils.sort(data, :a, sorter: &<=/2)
[two: %{a: 1, b: 3}, one: %{a: 3, b: 2}]

iex> data = [%{a: 1, b: 2}, %{a: 2, b: 3}]
iex> Etop.Utils.sort(data, :a, mapper: & &1[:a])
[%{a: 2, b: 3}, %{a: 1, b: 2}]

iex> data = [x: %{a: 1, b: 1}, z: %{a: 2, b: 0}, y: %{a: 1, b: 2}]
iex> Etop.Utils.sort(data, :a, secondary: :b)
[z: %{a: 2, b: 0}, y: %{a: 1, b: 2}, x: %{a: 1, b: 1}]

iex> data = [w: %{a: 1, b: 3}, x: %{a: 1, b: 1}, z: %{a: 2, b: 0}, y: %{a: 1, b: 2}]
iex> data |> Etop.Utils.sort(:a, secondary: :b, mapper: &elem(&1, 1)) |> Keyword.keys()
[:z, :w, :y, :x]
Link to this function

timezone_offset()

Specs

timezone_offset() :: integer()

Get the server's timezone offset in seconds.