slosh-js v0.2.5
slosh-js
slosh-js is a TypeScript utility library providing constructs for managing optional values (Option
) and error states (Result
).
Table of Contents
Installation
You can install the library via npm:
npm install slosh-js
Usage
This library mainly consists of two utility structures, Option
and Result
, which help to handle optional values and error states in a functional way.
Option
The Option
construct provides methods for handling cases where a value might be null
or undefined
. Here's how you can use it:
import { Option } from 'slosh-js';
let maybeValue = Option(getPotentiallyNullableValue());
let value = maybeValue.unwrapOr(defaultValue);
Result
The Result
construct provides a way to handle cases where a function might return an error. Here's a sample usage:
import { Result, Err } from 'slosh-js';
function mightFail(): Result<string, Error> {
// something that might fail
if (failed) {
return new Err(new Error('Failure'));
} else {
return 'Success';
}
}
let result = Result(mightFail());
result.match({
Ok: (value) => console.log('Success: ', value),
Err: (err) => console.error('Error: ', err),
});
Contributing
We welcome contributions to this library. To contribute, you can open an issue or submit a pull request here.
License
slosh-js is licensed under the Apache License 2.0. See the LICENSE
file for more information.