Skip to content

xxlabaza/result

Repository files navigation

Overview

Build Status

Result - is a value that represents either a success or a failure, including an associated value in each case.

Usage

installation:

$> npm install --save @xxlabaza/result

Create Success or Failure instances with the success and failure functions.

import { success, failure } from '@xxlabaza/result'

// something awesome happend

const yesss = success(someAesomeValue)

// moments later ...

const mappedYes = yesss.map(doingSuperUsefulStuff)

// @xxlabaza/result uses type-guards to differentiate between Success and Failure instances
// Mode info: https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types
if (mappedYes.isSuccess()) {
  // using type guards, we can access an Success instance's `value` field
  doStuffWith(mappedYes.value)
} else {
  // because of type guards
  // typescript knows that mappedYes is an Failure instance and thus has a `error` field
  doStuffWith(mappedYes.error)
}

Result is defined as follows:

type Result<T, E> = Success<T, E> | Failure<T, E>

Ok<T, E>: contains the success value of type T

Err<T, E>: contains the failure value of type E

Development

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

Building

To build the project, do the following:

$> npm run build
...

Running the tests

To run the project's test, do the following:

$> npm test

...

Test Suites: 2 passed, 2 total
Tests:       12 passed, 12 total
Snapshots:   0 total
Time:        1.232 s, estimated 2 s
Ran all test suites.

Changelog

To see what has changed in recent versions of the project, see the changelog file.

Contributing

Please read contributing file for details on my code of conduct, and the process for submitting pull requests to me.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

License

This project is licensed under the Apache License 2.0 License - see the license file for details