Note: This library is currently NOT in use and was replaced by @sniptt/monads which provides similar functionality.
A simple library for error handling, used in the JValue projects.
Use ResultLike
to express Success
and Failure
of an operation.
const s = success(123);
const f = failure('My custom failure message');
This enables error handling without try/catch.
const result = someOperationReturningAResultLike();
if (result.isSuccess()) {
const data = result.data();
// do sth with the data
} else {
const errorMsgs = result.errors();
// do sth with the error messages
}
All recoverable cases should be expressed explicitly as a ResultLike
.
Use OptionLike
to express Some
and None
of a type.
const s = some(123);
const n = none();
This enables using syntactic sugar for default values.
const option = someOperationReturningAnOptionLike();
const result = option.orElse('Default value');
ResultLike
and OptionLike
both support the methods map
, flatMap
, and filter
.
TODO
- Download dependencies:
npm install
- Linting:
npm run lint
- One test run:
npm test
- Start tests in watch mode:
npm run test:watch
We propose to use the following VSCode plugin so benefit from our preset configs out of the box:
- ESLint:
dbaeumer.vscode-eslint
- Prettier:
esbenp.prettier-vscode
- Editorconfig:
editorconfig.editorconfig
Recommended config (paste into /.vscode/settings.json
):
{
"eslint.validate": ["typescript"],
"typescript.preferences.importModuleSpecifier": "relative",
"[javascript]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[json]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[jsonc]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescript]": {
"editor.formatOnSave": false,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": ["source.fixAll.format", "source.fixAll.eslint"]
}
}
Copyright 2022 Friedrich-Alexander University Erlangen-Nürnberg (FAU). This work (source code) is licensed under Apache-2.0.