Packages

A Gleam implementation of CUID2, the secure, collision-resistant ids optimized for horizontal scaling and performance.

Current section

Files

Jump to
glecuid src glecuid@cuid2.erl
Raw

src/glecuid@cuid2.erl

-module(glecuid@cuid2).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/glecuid/cuid2.gleam").
-export([create_id/0, generate/1, is_cuid/1, new/0, with_counter/2, with_random_counter/1, with_fingerprint/2, with_length/2, with_randomizer/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/glecuid/cuid2.gleam", 21).
?DOC(
" Generates a CUIDv2 using the default generator.\n"
" \n"
" ## Examples\n"
" \n"
" ```gleam\n"
" cuid2.create_id()\n"
" // -> \"avu4793cnw6ljhov1s7Oxidg\" \n"
" ```\n"
).
-spec create_id() -> binary().
create_id() ->
glecuid@internals@core:generate(
{generator, none, none, 24, fun rand:uniform/0},
fun glecuid@internals@crypto:hash/1
).
-file("src/glecuid/cuid2.gleam", 39).
?DOC(
" Generates a CUIDv2 using custom generator.\n"
" \n"
" ## Examples\n"
" \n"
" ```gleam\n"
" cuid2.new()\n"
" |> cuid2.with_length(10)\n"
" |> cuid2.with_fingerprint(\"my_machine\")\n"
" |> cuid2.with_counter(fn() { int.random(100) })\n"
" |> cuid2.with_randomizer(fn() { 0.5 })\n"
" |> cuid2.generate()\n"
" // -> \"av77nekw5e\"\n"
" ```\n"
).
-spec generate(glecuid@internals@core:generator()) -> binary().
generate(G) ->
glecuid@internals@core:generate(G, fun glecuid@internals@crypto:hash/1).
-file("src/glecuid/cuid2.gleam", 45).
?DOC(" Determines whether or not the string is a valid CUIDv2.\n").
-spec is_cuid(binary()) -> boolean().
is_cuid(Input) ->
glecuid@internals@core:is_cuid(Input).
-file("src/glecuid/cuid2.gleam", 53).
?DOC(" Creates a `Generator` with default values to be customized.\n").
-spec new() -> glecuid@internals@core:generator().
new() ->
{generator, none, none, 24, fun rand:uniform/0}.
-file("src/glecuid/cuid2.gleam", 62).
?DOC(
" Sets a custom counter to be used by the `Generator`.\n"
" \n"
" Counter is a function that returns an incrementing `Int` \n"
" every time it is called.\n"
).
-spec with_counter(glecuid@internals@core:generator(), fun(() -> integer())) -> glecuid@internals@core:generator().
with_counter(G, Counter) ->
glecuid@internals@core:with_counter(G, Counter).
-file("src/glecuid/cuid2.gleam", 70).
?DOC(
" Sets a random counter to be used by the `Generator`.\n"
" \n"
" This counter returns a random `Int` instead of an incrementing value.\n"
).
-spec with_random_counter(glecuid@internals@core:generator()) -> glecuid@internals@core:generator().
with_random_counter(G) ->
glecuid@internals@core:with_random_counter(G).
-file("src/glecuid/cuid2.gleam", 79).
?DOC(
" Sets a custom fingerprint to be used by the `Generator`.\n"
" \n"
" Fingerprint is a unique `String` to help prevent collision \n"
" when generating ids in a distributed system.\n"
).
-spec with_fingerprint(glecuid@internals@core:generator(), binary()) -> glecuid@internals@core:generator().
with_fingerprint(G, Fingerprint) ->
glecuid@internals@core:with_fingerprint(G, Fingerprint).
-file("src/glecuid/cuid2.gleam", 85).
?DOC(" Sets the length of the ids generated by the `Generator`.\n").
-spec with_length(glecuid@internals@core:generator(), integer()) -> glecuid@internals@core:generator().
with_length(G, Length) ->
glecuid@internals@core:with_length(G, Length).
-file("src/glecuid/cuid2.gleam", 94).
?DOC(
" Sets a custom randomizer to be used by the `Generator`.\n"
" \n"
" Randomizer is a function that returns a random `Float` \n"
" between zero (inclusive) and one (exclusive).\n"
).
-spec with_randomizer(glecuid@internals@core:generator(), fun(() -> float())) -> glecuid@internals@core:generator().
with_randomizer(G, Randomizer) ->
glecuid@internals@core:with_randomizer(G, Randomizer).