0.2.5 • Published 3 years ago

optionem v0.2.5

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

npm npm bundle size NPM npm

Optionem

A library for people with options and results.

Install

npm install optionem
yarn add optionem

Library Name

Optionem = Option in Latin

Example

Using option

import { Option, None, Some } from "optionem";

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

const result = divide(
  Math.floor(Math.random() * 10),
  Math.floor(Math.random() * 10)
);

result.match({
  Some(x) {
    console.log("Result ", x);
  },
  None() {
    console.log("Can not divide by", 0);
  },
});

Using result

import { Result, Err, Ok } from "optionem";

function divide(
  numerator: number,
  denominator: number
): Result<number, string> {
  if (denominator === 0) {
    return new Err("divide by zero error");
  } else {
    return new Ok(numerator / denominator);
  }
}

const result = divide(
  Math.floor(Math.random() * 10),
  Math.floor(Math.random() * 10)
);

result.match({
  Ok(x) {
    console.log("Result ", x);
  },
  Err(err) {
    console.log("ERROR", err);
  },
});

The api of this library is made to resemble the one in rust.

This library can be used for

  • Network Requests
  • Database Gateways
  • etc

This can be used in places where in normal circumstances you would use null or undefined. Null is bad enough but in javascript we also have undefined. It is time to eliminate null and undefined checks from your codebase.

Option

An option has two variants that implement the Option<T> interface

  • Some
  • None

Result

A result has two variants that implement the Result<T, E> interface

  • Ok
  • Err

API

Check out the API

License

This project is licensed under the MIT license.

Todo

  • Implement Option
  • Support async functions in methods of option
  • Implement Result
0.2.1

3 years ago

0.2.0

3 years ago

0.1.8

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.5

3 years ago

0.2.4

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.0.1

3 years ago