glecuid/cuid2

Types

Type to represent CUIDv2 generator configuration.

pub opaque type Generator

Values

pub fn create_id() -> String

Generates a CUIDv2 using the default generator.

Examples

cuid2.create_id()
// -> "avu4793cnw6ljhov1s7Oxidg" 
pub const default_length: Int

Constant representing CUIDv2 default length.

pub fn generate(g: Generator) -> String

Generates a CUIDv2 using custom generator.

Examples

cuid2.new()
|> cuid2.with_length(10)
|> cuid2.with_fingerprint("my_machine")
|> cuid2.with_counter(fn() { int.random(100) })
|> cuid2.with_randomizer(fn() { 0.5 })
|> cuid2.generate()
// -> "av77nekw5e"
pub fn is_cuid(input: String) -> Bool

Determines whether or not the string is a valid CUIDv2.

pub fn new() -> Generator

Creates a Generator with default values to be customized.

pub fn with_counter(
  g: Generator,
  counter: fn() -> Int,
) -> Generator

Sets a custom counter to be used by the Generator.

Counter is a function that returns an incrementing Int every time it is called.

pub fn with_fingerprint(
  g: Generator,
  fingerprint: String,
) -> Generator

Sets a custom fingerprint to be used by the Generator.

Fingerprint is a unique String to help prevent collision when generating ids in a distributed system.

pub fn with_length(g: Generator, length: Int) -> Generator

Sets the length of the ids generated by the Generator.

pub fn with_random_counter(g: Generator) -> Generator

Sets a random counter to be used by the Generator.

This counter returns a random Int instead of an incrementing value.

pub fn with_randomizer(
  g: Generator,
  randomizer: fn() -> Float,
) -> Generator

Sets a custom randomizer to be used by the Generator.

Randomizer is a function that returns a random Float between zero (inclusive) and one (exclusive).

Search Document