cuid2_gleam

Types

pub opaque type Builder(counter, randomiser, fingerprint, length)
pub type CustomCounter
pub type CustomFingerprint
pub type CustomLength
pub type CustomRandomiser
pub type DefaultCounter
pub type DefaultFingerprint
pub type DefaultLength
pub type DefaultRandomiser
pub opaque type Generator

Randomiser used to create entropy, needs to return between [0, 1[

pub type Randomiser =
  fn() -> Float

Functions

pub fn build(builder: Builder(a, b, c, d)) -> Generator

Build a Generator from a Builder!

pub fn create(generator: Generator) -> String

Returns a cuid following the Generator configuration.

pub fn default() -> Generator

Returns a new Generator with Default values same as createId from JS implementation.

pub fn is_cuid_like(maybe_cuid: String) -> Bool

Returns true if the given string looks like a cuid. It’s just a regex check, so “thisisacuid” is true.

pub fn is_valid(generator: Generator, maybe_cuid: String) -> Bool

Returns true if the given string has been generated by this generator

pub fn new() -> Builder(
  DefaultCounter,
  DefaultRandomiser,
  DefaultFingerprint,
  DefaultLength,
)

Creates a Builder with default options so you can customise them as needed

pub fn with_counter(
  builder: Builder(DefaultCounter, a, b, c),
  counter: Counter,
) -> Builder(CustomCounter, a, b, c)

Generation uses a counter that is expected to increment on each create call. You can customise that, I don’t fully understand all of it, so do this at your own risks.

pub fn with_fingerprint(
  builder: Builder(a, b, DefaultFingerprint, c),
  fingerprint: String,
) -> Builder(a, b, CustomFingerprint, c)

The fingerprint is to allow to differentiate even more on distributed system, I think… not 100% sure. Default is random

pub fn with_length(
  builder: Builder(a, b, c, DefaultLength),
  length: Int,
) -> Builder(a, b, c, CustomLength)

Will change the length of the created ids (default is 24)

pub fn with_randomiser(
  builder: Builder(a, DefaultRandomiser, b, c),
  randomiser: fn() -> Float,
) -> Builder(a, CustomRandomiser, b, c)

The randomiser used can also be custom if needed

Search Document