provides error handling functions inspired by rust like Some, None, Ok, Err
npm install rlx-jsbash
npm install rlx-js
`
Or update package.json to include a dependency on
`json
"dependencies": {
"rlx-js": "0.2.x"
}
`
Samples
$3
`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);
`
$3
`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);
``