https://github.com/joncloud/rlx-js
provides error handling functions inspired by rust like Some, None, Ok, Err.
https://github.com/joncloud/rlx-js
error-handling functional null option result rustlang
Last synced: about 2 months ago
JSON representation
provides error handling functions inspired by rust like Some, None, Ok, Err.
- Host: GitHub
- URL: https://github.com/joncloud/rlx-js
- Owner: joncloud
- License: mit
- Created: 2017-11-14T01:29:04.000Z (over 8 years ago)
- Default Branch: publish
- Last Pushed: 2023-07-20T01:52:18.000Z (over 2 years ago)
- Last Synced: 2025-03-06T04:36:32.119Z (11 months ago)
- Topics: error-handling, functional, null, option, result, rustlang
- Language: JavaScript
- Homepage:
- Size: 166 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# RlxJS
[](https://www.npmjs.com/package/rlx-js)
## Description
RlxJS provides error handling functions inspired by rust like Some, None, Ok, Err.
## Licensing
Released under the MIT License. See the [LICENSE][] file for further details.
[license]: LICENSE.md
## Installation
In shell execute
```bash
npm install rlx-js
```
Or update `package.json` to include a dependency on
```json
"dependencies": {
"rlx-js": "0.2.x"
}
```
## Samples
### Sample echo program
```javascript
import { Some, None } from 'rlx-js';
const getArg = () => process.argv.length > 2 ? Some(process.argv[2]) : None();
const message = getArg().mapOrElse(() => "Missing Argument", msg => `Echo: ${msg}`);
console.log(message);
```
### Promise helpers
```javascript
import { Ok, Err, FlattenResult } from 'rlx-js';
const fetchData = async (url) => {
const res = await fetch(url);
if (res.ok) {
return Ok(res);
}
return Err(`Invalid status ${res.status}`);
};
const validateContentType = (res) => {
const contentType = res.headers.get('content-type');
if (contentType.includes('application/json')) {
return Ok(res);
}
return Err(`Invalid content type ${contentType}`);
};
const loadText = async (res) => {
const text = await res.text();
return text;
};
const text = await FlattenResult(fetchData('...'))
.andThen(validateContentType)
.map(loadText)
.unwrapOrElse(err => err);
console.log(text);
```
### Additional usage
For additional usage see [Option][] and [Result][] tests.
[Option]: option.spec.js
[Result]: result.spec.js