# resultt

> Type-safe runtime wrapping library

Latest version **2.0.6** (published 2022-06-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install resultt
pnpm add resultt
yarn add resultt
bun add resultt
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.6 |
| Published | 2022-06-18 |
| First published | 2021-12-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 79.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Simon Nozaki |
| Maintainers | snozaki |
| Keywords | Kotlin, Result |

## Links

- npm: https://www.npmjs.com/package/resultt
- Repository: https://github.com/simonNozaki/ResultT
- Homepage: https://github.com/simonNozaki/ResultT#readme
- Issues: https://github.com/simonNozaki/ResultT/issues
- npm.io page: https://npm.io/package/resultt

## Dependencies (1)

- [fp-ts](https://npm.io/package/fp-ts.md) ^2.12.1

## Recent versions

- 2.0.6 (latest) — 2022-06-18
- 2.0.5 — 2022-06-12
- 2.0.4 — 2022-05-28
- 2.0.3 — 2022-05-01
- 2.0.2 — 2022-05-01
- 2.0.1 — 2022-01-29
- 2.0.0 — 2022-01-15
- 1.0.6 — 2022-01-04
- 1.0.5 — 2022-01-01
- 1.0.4 — 2022-01-01
- 1.0.3 — 2021-12-31
- 1.0.2 — 2021-12-30
- 1.0.1 — 2021-12-30
- 1.0.0 — 2021-12-30

## README

[![CircleCI](https://circleci.com/gh/simonNozaki/ResultT/tree/main.svg?style=svg)](https://circleci.com/gh/simonNozaki/ResultT/tree/main)

# ResultT
ts/js users cannot use try-catch as expression so that we have to struggle with complicated runtimes.

ResultT is type-safe runtime wrapping library. Strongly inspired by [Kotlin Result](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-result/) implementation.


## Install
From npm registry...
```
npm i resultt
```
type declaration is included.

class import...
```typescript
import { Resultt } from 'resultt';
```

## Usage
Try to start from `runCatching` method. This wraps the result of success or failure.
```typescript
// sample logic and response interface
interface Response {
    data: string
}
class Service {
    execute(value: string): Response {
    return {data: value};
    }
}

// execute some function and wrap by "runCatching"
const result: Result<Response> = Result.runCatching(() => {
        return new Service().execute('execution');
    })
    .onSuccess((v) => {
        console.log(`response => ${v}`);
        return v
    });
    .onFailure((it: Error) => {
        console.error(it);
        return {
            data: 'DEFAULT'
        };
    });

// You may get the value of execute by "get" functions as declarative.
const v1 = result.getOrThrow();  // => success ... { data: execution }
const v2 = result.getOrDefault({
    data: 'OTHER'
});

// You can process as commandly.
let v;
if (result.isSuccess()) {
    v = result.getOrThrow();
}

// Map the result to another map by fold.
const folded: number = Result.runCatching(() => {
        return new Service().execute('execution');
    })
    .fold(
        (data: Response) => {
            console.log(data);
            return data.data.length;
        },
        (it: Error) => {
            console.log(it);
            return 0;
        },
    );
console.log(folded);  // => 9

// Or, shorthand for fold with getOrElse
const n: number = Result.runCatching(() => {
        return new Service().execute('execution');
    })
    .getOrElse((it: Error) => {
            console.log(it);
            return 0;
        },
    );
console.log(n);  // => 9
```


## For more info...
Full class documentation is here: [docs](https://github.com/simonNozaki/ResultT/blob/main/docs/classes/Resultt.md)

---
_Source: https://npm.io/package/resultt · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
