0.2.5 • Published 12 months ago

slosh-js v0.2.5

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
12 months ago

slosh-js

npm License

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.

0.2.5

12 months ago

0.2.4

1 year ago

0.2.3

1 year ago

0.2.2

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago