Skip to content

👇Given is a library written in Gleam to make it safe and easy to early return.

License

Notifications You must be signed in to change notification settings

inoas/gleam-given

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

👇Given for Gleam

Package <a href="https://github.com/inoas/gleam-given/releases"><img src="https://img.shields.io/github/release/inoas/gleam-given" alt="GitHub release"></a> Version Erlang-compatible JavaScript Compatible Hex Docs Discord CI Test



Given Logo


👇Given is a library written in Gleam to make it safe and easy to early return.



Installation

gleam add given@1

Usage

import given.{given, not_given}

pub fn given_example() {
  let user_understood = False
  use <- given(user_understood, return: fn() { "💡 Bright!" })

  // …else handle case where user did not understand here…

  "🤯 Woof!"
}

pub fn not_given_example() {
  let has_admin_role = True
  // Fetch this from some database
  use <- not_given(has_admin_role, return: fn() { "✋ Denied!" })

  // …else handle case where they are admin here…

  "👌 Access granted..."
}

pub fn given_ok_in_example() {
  let a_result = Ok("Hello Joe!")
  use ok_value <- given.ok_in(a_result, else_return: fn(error_value) {
    error_value
  })

  // …handle Ok value here…

  ok_value
}

pub fn given_error_in_example() {
  let a_result = Error("Memory exhausted!")
  use error_value <- given.error_in(a_result, else_return: fn(ok_value) {
    ok_value
  })
  // …handle Error value here…

  error_value
}

import gleam/option.{None, Some}

pub fn given_some_in_example() {
  let an_option = Some("One Penny")
  use some_value <- given.some_in(an_option, else_return: fn() { "Woof!" })

  // …handle Some value here…

  some_value
}

pub fn given_none_in_example() {
  let an_option = None
  use <- given.none_in(an_option, else_return: fn(some_value) { some_value })

  // …handle None here…

  "Nothing at all"
}

pub fn given_none_in_another_example() {
  let an_option = None
  use else_some_value <- given.none_in(an_option, return: fn() { "None here" })

  // …handle Some value here…

  else_some_value
}

import gleam/io

pub fn main() {
  given_example() |> io.debug()
  // "🤯 Woof!"

  not_given_example() |> io.debug()
  // "👌 Access granted..."

  given_ok_in_example() |> io.debug()
  // "Hello Joe!"

  given_error_in_example() |> io.debug()
  // "Memory exhausted!"

  given_some_in_example() |> io.debug()
  // "One Penny"

  given_none_in_example() |> io.debug()
  // "Nothing at all"
}

Further documentation can be found at https://hexdocs.pm/given.

Development

gleam run   # Run the project
gleam test  # Run the tests

Run examples

gleam run --module given/internal/examples

About

👇Given is a library written in Gleam to make it safe and easy to early return.

Resources

License

Stars

Watchers

Forks

Packages

No packages published