qworks-io/monads


👻 Type safe Option and Result type – for TypeScript and JavaScript

License: ISC

Language: TypeScript

Keywords: javascript, monads, nodejs, option, result, rust, types, typescript


CircleCI codecov npm version GuardRails Security Responsible Disclosure

Monads

Type safe Option and Result types – inspired by Rust.

Prereqs & Install

  • Node >=9.10.0
  • npm >=6.1.0

Please note that the TypeScript target is ES6.

npm install @usefultools/monads

Documentation

Usage

Option<T>

See full Option<T> API documentation here.

import { Option, Some, None } from "@usefultools/monads"

function divide(numerator: number, denominator: number): Option<number> {
  if (denominator === 0) {
    return None
  } else {
    return Some(numerator / denominator)
  }
};

// The return value of the function is an option
const result = divide(2.0, 3.0)

// Pattern match to retrieve the value
const message = result.match({
  some: res => `Result: ${res}`,
  none: "Cannot divide by 0",
})

console.log(message) // "Result: 0.6666666666666666"

More Option<T> examples here.

Result<T, E>

See full Result<T, E> API documentation here.

import { Result, Ok, Err } from "@usefultools/monads"

function getIndex(values: string[], value: string): Result<number, string> {
  const index = values.indexOf(value)

  switch (index) {
    case -1:
      return Err("Value not found")
  default:
    return Ok(index)
  }
}

console.log(getIndex(["a", "b", "c"], "b")) // Ok(1)
console.log(getIndex(["a", "b", "c"], "z")) // Err("Value not found")

More Result<T, E> examples here.

Contributing

If you have comments, complaints, or ideas for improvements, feel free to open an issue or a pull request! See Contributing guide for details about project setup, testing, etc.

Author and license

This library was created by @qworks.io. Main author and maintainer is Slavo Vojacek.

Contributors: Slavo Vojacek

@usefultools/monads is available under the ISC license. See the LICENSE file for more info.

Project Statistics

Sourcerank 9
Repository Size 485 KB
Stars 109
Forks 6
Watchers 5
Open issues 1
Dependencies 430
Contributors 8
Tags 48
Created
Last updated
Last pushed

Top Contributors See all

renovate[bot] Anas AIT ALI Slavo Vojacek Renovate Bot

Packages Referencing this Repo

@usefultools/monads
Type safe Option and Result types – inspired by Rust
Latest release 3.0.0 - Updated - 109 stars
tsp-monads
Option and Result types for JavaScript; TypeScript first
Latest release 13.1.0 - Updated - 109 stars
@threestup/monads
Option and Result types for JavaScript; TypeScript first
Latest release 1.1.0 - Updated - 109 stars

Recent Tags See all

v2.1.0 July 15, 2019
v2.0.5 December 02, 2018
v2.0.5-0 December 02, 2018
v2.0.5-0 December 02, 2018
v2.0.4-0 December 02, 2018
v2.0.4-0 December 02, 2018
v2.0.3-0 December 02, 2018
v2.0.2-0 December 02, 2018
v2.0.1-0 December 02, 2018
v2.0.0-0 December 02, 2018
v1.2.0 December 01, 2018
v1.1.2 October 03, 2018
v1.0.4 May 02, 2018
v1.0.3 May 02, 2018
v1.0.2 May 02, 2018

Something wrong with this page? Make a suggestion

Last synced: 2019-11-07 13:12:09 UTC

Login to resync this repository