-module(glecuid@cuid2). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/glecuid/cuid2.gleam"). -export([is_cuid/1, with_counter/2, with_random_counter/1, with_fingerprint/2, with_length/2, with_randomizer/2, new/0, generate/1, create_id/0]). -export_type([generator/0]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. -opaque generator() :: {generator, gleam@option:option(fun(() -> integer())), gleam@option:option(binary()), integer(), fun(() -> float())}. -file("src/glecuid/cuid2.gleam", 107). ?DOC(" Determines whether or not the string is a valid CUIDv2.\n"). -spec is_cuid(binary()) -> boolean(). is_cuid(Input) -> Length = begin _pipe = Input, string:length(_pipe) end, Regex@1 = case gleam@regexp:from_string(<<"^[a-z][0-9a-z]+$"/utf8>>) of {ok, Regex} -> Regex; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"glecuid/cuid2"/utf8>>, function => <<"is_cuid"/utf8>>, line => 109, value => _assert_fail, start => 2424, 'end' => 2485, pattern_start => 2435, pattern_end => 2444}) end, case gleam@regexp:check(Regex@1, Input) of true when (2 =< Length) andalso (Length =< 32) -> true; _ -> false end. -file("src/glecuid/cuid2.gleam", 130). ?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(generator(), fun(() -> integer())) -> generator(). with_counter(G, Counter) -> {generator, begin _pipe = Counter, {some, _pipe} end, erlang:element(3, G), erlang:element(4, G), erlang:element(5, G)}. -file("src/glecuid/cuid2.gleam", 138). ?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(generator()) -> generator(). with_random_counter(G) -> {generator, {some, fun() -> gleam@int:random(32) end}, erlang:element(3, G), erlang:element(4, G), erlang:element(5, G)}. -file("src/glecuid/cuid2.gleam", 147). ?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(generator(), binary()) -> generator(). with_fingerprint(G, Fingerprint) -> {generator, erlang:element(2, G), begin _pipe = Fingerprint, {some, _pipe} end, erlang:element(4, G), erlang:element(5, G)}. -file("src/glecuid/cuid2.gleam", 153). ?DOC(" Sets the length of the ids generated by the `Generator`.\n"). -spec with_length(generator(), integer()) -> generator(). with_length(G, Length) -> {generator, erlang:element(2, G), erlang:element(3, G), Length, erlang:element(5, G)}. -file("src/glecuid/cuid2.gleam", 162). ?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(generator(), fun(() -> float())) -> generator(). with_randomizer(G, Randomizer) -> {generator, erlang:element(2, G), erlang:element(3, G), erlang:element(4, G), Randomizer}. -file("src/glecuid/cuid2.gleam", 121). ?DOC(" Creates a `Generator` with default values to be customized.\n"). -spec new() -> generator(). new() -> {generator, none, none, 24, fun rand:uniform/0}. -file("src/glecuid/cuid2.gleam", 69). ?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(generator()) -> binary(). generate(G) -> {generator, Counter, Fingerprint, Length, Randomizer} = G, First_letter = glecuid@internals@util:random_letter(Randomizer), Time = begin _pipe = gleam@time@timestamp:system_time(), _pipe@1 = gleam@time@timestamp:to_unix_seconds_and_nanoseconds(_pipe), _pipe@2 = gleam@pair:first(_pipe@1), gleam@int:to_base36(_pipe@2) end, Count = begin _pipe@4 = case Counter of {some, X} -> X(); none -> _pipe@3 = glecuid@internals@util:random_int( Randomizer, 476782367 ), glecuid_ffi:bump_or_initialize_counter(_pipe@3) end, gleam@int:to_base36(_pipe@4) end, Fingerprint@1 = case Fingerprint of {some, X@1} -> X@1; none -> glecuid@internals@util:create_fingerprint(Randomizer) end, Salt = glecuid@internals@util:create_entropy(Randomizer, Length), <>/binary, Count/binary>>/binary, Fingerprint@1/binary>>), _pipe@6 = glecuid@internals@util:hash(_pipe@5), _pipe@7 = gleam@string:slice(_pipe@6, 1, Length - 1), string:lowercase(_pipe@7) end)/binary>>. -file("src/glecuid/cuid2.gleam", 51). ?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() -> generate({generator, none, none, 24, fun rand:uniform/0}).