https://github.com/frectonz/optionem
A library for people with options and results.
https://github.com/frectonz/optionem
either option result
Last synced: 7 months ago
JSON representation
A library for people with options and results.
- Host: GitHub
- URL: https://github.com/frectonz/optionem
- Owner: frectonz
- License: mit
- Created: 2021-08-13T11:45:26.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-01-18T20:08:14.000Z (over 3 years ago)
- Last Synced: 2025-02-14T05:05:10.696Z (8 months ago)
- Topics: either, option, result
- Language: TypeScript
- Homepage:
- Size: 250 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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 {
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 {
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
- etcThis 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` interface
- Some
- None## Result
A result has two variants that implement the `Result` 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