friendly_id

Types

pub type EncodingError {
  OutOfBounds
}

Constructors

  • OutOfBounds

This record contains the objects and predicates arrays, needed to generate a friendly ID. Should only be initialized once, then passed as a dependency.

pub opaque type Generator
pub type GeneratorError {
  NegativePredicateCount
}

Constructors

  • NegativePredicateCount

Values

pub fn encode_int(
  generator: Generator,
  id: Int,
) -> Result(String, EncodingError)

Encodes an int to a Friendly ID. This is a deterministic operation, which means an int will always return the same ID, and guarantees no collisions.

Examples

Create a generator with defaults, then generate an ID by encoding an int

let generator = friendly_id.new_generator()
echo friendly_id.encode_int(generator, 23)
pub fn encoder_max_int(generator: Generator) -> Int
pub fn generate(generator: Generator) -> String

Generates a friendly ID from a Generator record.

Examples

Create a generator with defaults, then generate an ID

let generator = friendly_id.new_generator()
echo friendly_id.generate(generator)
pub fn new_generator() -> Generator

Create a Generator record with the following defaults:

  • Predicate count: 1
  • No separator
  • No transformation
  • Provided word lists

Examples

let generator = friendly_id.new_generator()
pub fn new_generator_with_words(
  objects objects: glearray.Array(String),
  predicates predicates: glearray.Array(String),
) -> Generator

Create a Generator record with the word lists passed to it and the following defaults:

  • Predicate count: 1
  • No separator
  • No transformation

Examples

let objects = glearray.from_list(["apple, potato"])
let predicates = glearray.from_list(["brave, insightful"])
let generator = friendly_id.new_generator_with_words(objects:, predicates:)
pub fn set_generator_predicate_count(
  generator: Generator,
  predicate_count: Int,
) -> Result(Generator, GeneratorError)
pub fn set_generator_separator(
  generator: Generator,
  separator: String,
) -> Generator
pub fn set_generator_transform_fn(
  generator: Generator,
  transform_fn: fn(String) -> String,
) -> Generator
Search Document