# optionem

> A library for people with options and results.

Latest version **0.2.5** (published 2021-08-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install optionem
pnpm add optionem
yarn add optionem
bun add optionem
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.5 |
| Published | 2021-08-20 |
| First published | 2021-08-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 15.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Fraol Lemecha |
| Maintainers | fraol |
| Keywords | option, result, either |

## Links

- npm: https://www.npmjs.com/package/optionem
- Repository: https://github.com/fraol0912/optionem
- Homepage: https://github.com/fraol0912/optionem#readme
- Issues: https://github.com/fraol0912/optionem/issues
- npm.io page: https://npm.io/package/optionem

## Recent versions

- 0.2.5 (latest) — 2021-08-20
- 0.2.4 — 2021-08-20
- 0.2.3 — 2021-08-19
- 0.2.2 — 2021-08-19
- 0.2.1 — 2021-08-19
- 0.2.0 — 2021-08-19
- 0.1.8 — 2021-08-19
- 0.1.7 — 2021-08-15
- 0.1.6 — 2021-08-15
- 0.1.5 — 2021-08-14
- 0.1.4 — 2021-08-14
- 0.1.3 — 2021-08-14
- 0.1.2 — 2021-08-14
- 0.0.1 — 2021-08-13

## README

![npm](https://img.shields.io/npm/v/optionem)
![npm bundle size](https://img.shields.io/bundlephobia/min/optionem)
![NPM](https://img.shields.io/npm/l/optionem)
![npm](https://img.shields.io/npm/dt/optionem)

# Optionem

A library for people with options and results.

## Install

```sh
npm install optionem
yarn add optionem
```

### Library Name

**Optionem** = **Option in Latin**

## Example

Using option

```typescript
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

```typescript
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](/docs)

# License

This project is licensed under the [MIT](/LICENSE) license.

# Todo

- [x] Implement Option
- [ ] Support async functions in methods of option
- [x] Implement Result

---
_Source: https://npm.io/package/optionem · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
