gulid
ULID Implementation in Gleam
Create
new()
: Non-monotonic.new_monotonic(Ulid)
: Monotonic using previousUlid
value- Parse ULID from a string:
let from_string = from_string_function()
from_string(new()) |> io.debug
from_parts(Int, Int)
: Create from a timestamp milliseconds from Epoch and random.
Convert to String
let to_string = to_string_function()
let ulid = new()
io.println("Ulid: " <> to_string(ulid))
Types
Ulid module errors to be returned in a Result
pub type UlidError {
DecodeError(mesage: String)
InvalidLength(message: String)
}
Constructors
-
DecodeError(mesage: String)
Returned when failed to decode
Ulid
from aString
-
InvalidLength(message: String)
Returned when input string is of incorrect length
Functions
pub fn from_bitarray(array: BitArray) -> Result(Ulid, UlidError)
Builds a Ulid
value from given BitArray
. Returns Ok
with Ulid
value
on success or Error(UlidError)
otherwise.
pub fn from_parts(
timestamp timestamp: Int,
random random: Int,
) -> Ulid
Returns a Ulid
value, build from a given integer timestamp (millis from
Epoch) and random values
pub fn from_string_function() -> fn(String) ->
Result(Ulid, UlidError)
Returns a function that decodes a Ulid
value from a given string. It
returns Ok
with the Ulid
value or UlidError
.
Examples
let from_string = from_string_function()
let ulid_str = "01J9P2J2B0S4T4DFJAJ6RTV1DE"
let ulid = from_string(ulid_str)
io.debug(ulid)
pub fn from_tuple(parts: #(Int, Int)) -> Ulid
Returns a Ulid
value, built from a given #(timestamp, random)
tuple.
pub fn new_as_string() -> String
Returns a non-monotonic ULID value as a string. Note, this is a shortcut, not very good for performance.
pub fn new_monotonic(prev_ulid: Ulid) -> Ulid
Returns a new Ulid
value based on given previous one according to the
behavior, described in the ULID spec, but basically, if the previous Ulid
has the same timestamp then increment the least significant bit of its
random value by 1 (with carry) to produce a new Ulid
(with the same
timestamp).
pub fn to_bitarray(ulid: Ulid) -> BitArray
Returns a binary representation of given Ulid
value
pub fn to_parts(ulid: Ulid) -> #(Int, Int)
Returns Ulid
components in a #(timestamp, random)
tuple.
pub fn to_string_function() -> fn(Ulid) -> String
Returns a function than converts a Ulid
value to string.
Eamples
let to_string = to_string_function()
let ulid = new()
io.debug(to_string(ulid))