result

deno land deno doc GitHub release (latest by date) codecov GitHub

test NPM standard-readme compliant semantic-release: angular

Minimum result type port of Rust.

Option version.

Table of Contents

Install

deno.land:

import * as mod from "https://deno.land/x/result_js/mod.ts";

npm:

npm i @miyauci/result

Usage

Type Result represents an success or failure.

import {
  Err,
  Ok,
  type Result,
  unwrap,
} from "https://deno.land/x/result_js/mod.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";

function divide(
  numerator: number,
  denominator: number,
): Result<number, string> {
  if (!denominator) return Err("divide by 0");

  return Ok(numerator / denominator);
}

const opt = divide(100, 0);
assertThrows(() => unwrap(opt));

All operators for Result are separated from prototype.

API

All APIs can be found in the deno doc.

Acknowledgements

Contributing

See contribution.

License

MIT © 2023 Tomoki Miyauchi