Aphora v1.0.0 Aphora.Helper View Source

Aphora.Helper provides many utility methods, which are meant to facilitate the handling and encoding of Aphora.id/0.

Link to this section Summary

Functions

Decodes the Aphora.id/0 into a binary/0.

Encodes the binary/0 into a Aphora.id/0.

Extracts the Aphora.timestamp/0 out of the given Aphora.id/0.

Link to this section Functions

Link to this function

decode(id) View Source (since 0.2.0)
decode(Aphora.id()) :: <<_::72>>

Decodes the Aphora.id/0 into a binary/0.

It basically is just a reverse implementation of encode/1, so please ensure to read the warnings of it.

Examples

iex> Aphora.Helper.decode("0eHHz1--abHr")
<<6, 164, 146, 252, 32, 0, 154, 116, 183>>
Link to this function

encode(binary) View Source (since 0.2.0)
encode(<<_::72>>) :: Aphora.id()

Encodes the binary/0 into a Aphora.id/0.

It uses a custom alphabet inspired by Firebase's push ID

encode/1 completely ignores the usual Base64 padding. This is absolutely fine in case of an Aphora.id/0, because its size of 72 Bit is divisible by 24.
Make sure to not encode anything, which isn't exactly divisible by 24, otherwise you'll lose data!

Examples

iex> Aphora.Helper.encode(<<6, 164, 146, 252, 32, 0, 154, 116, 183>>)
"0eHHz1--abHr"
Link to this function

get_timestamp(id, atom) View Source (since 0.4.0)
get_timestamp(Aphora.id(), :relative) :: Aphora.timestamp()
get_timestamp(Aphora.id(), :absolute) :: Aphora.timestamp()

Extracts the Aphora.timestamp/0 out of the given Aphora.id/0.

If you select :absolute, get_timestamp/2 will calculate the actual Aphora.timestamp/0.
If you instead want a Aphora.timestamp/0 relative to your Aphora.epoch/0, use :relative.

Examples

iex> Aphora.Helper.get_timestamp("0eHHz1--abHr", :relative)
912_988_800_000

iex> Aphora.Helper.get_timestamp("-0dqbzfF----", :absolute)
1_560_508_218_202