# result-class

> Port the Result and Option classes from Rust to Typescript.

Latest version **1.4.0** (published 2018-05-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install result-class
pnpm add result-class
yarn add result-class
bun add result-class
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.4.0 |
| Published | 2018-05-07 |
| First published | 2017-10-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 25.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Maintainers | vincentpang |

## Links

- npm: https://www.npmjs.com/package/result-class
- Repository: https://github.com/Vincent-Pang/result-class
- npm.io page: https://npm.io/package/result-class

## Recent versions

- 1.4.0 (latest) — 2018-05-07
- 1.3.1 — 2017-10-28
- 1.3.0 — 2017-10-28
- 1.2.0 — 2017-10-15
- 1.1.5 — 2017-10-13
- 1.1.4 — 2017-10-10
- 1.1.3 — 2017-10-09
- 1.1.2 — 2017-10-09
- 1.1.1 — 2017-10-09
- 1.1.0 — 2017-10-09
- 1.0.0 — 2017-10-09

## README

# result-class

Port the Result and Option classes from Rust to Typescript.

## Installation

```
yarn add result-class
```

## API
Please refer to the rust document  
https://doc.rust-lang.org/std/result/enum.Result.html  
https://doc.rust-lang.org/std/option/enum.Option.html

## Usage

### `Option<T>`, `Some<T>`, `None`
```
function getUserName(userId: number): Option<string>
{
    return 1 === userId ? new Some('Vincent') : None.getInstance();
}

getUserName(1).match({
    Some: v => console.log(`User found = ${v}`),
    None: () => console.log('User cannot be found')
});

or

const name = getUserName(1).match({
    Some: v => v,
    None: () => 'Guest'
});

console.log(name);  // Vincent
```

### `Result<T, E>`, `Ok<T>`, `Err<E>`
```
function division(dividend: number, divisor: number): Result<number, string>
{
    if (divisor === 0)
    {
        return new Err('Division by zero');
    }
    else
    {
        return new Ok(dividend / divisor);
    }
}

division(dividend, divisor).match({
    Ok: v => console.log(`Answer = ${v}`),
    Err: e => console.log(`Error = ${e}`)
});
```

## Contributing

1. Fork it!
2. Create your feature branch: `git checkout -b my-new-feature`
3. Commit your changes: `git commit -am 'Add some feature'`
4. Push to the branch: `git push origin my-new-feature`
5. Submit a pull request :D

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details

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