0.0.7 • Published 4 months ago

resulta v0.0.7

Weekly downloads
-
License
MIT
Repository
-
Last release
4 months ago

Resulta

Resulta is a TypeScript library that provides a Result type for handling success and error values in a functional way. It is inspired by the Result type in Rust.

Installation

You can install Resulta using npm:

npm install resulta

Or using yarn:

yarn add resulta

Usage

Basic Usage

import { ok, err, Result } from 'resulta';

function hello(message = ''): Result<string, Error> {
    if (!message) {
        return err(new Error('hello without world'));
    }

    return ok(`hello ${message}`);
}

const result = hello();

if (result.ok) {
    console.log(result.value);
} else {
    console.error(result.error);
}

Async Functions

import { match } from 'resulta';

async function fetchData(): Promise<string> {
    // Simulate an async operation
    return "data";
}

async function main() {
    const result = await match(fetchData);

    if (result.ok) {
        console.log(result.value);
    } else {
        console.error(result.error);
    }
}

main();

API

ok<T>(value: T): Ok<T>

Returns an object representing an Ok result.

err<E>(error: E): Err<E>

Returns an object representing an Err result.

match<T, E>(fn: () => Promise<T>): Promise<Result<T, E>>

Executes a provided asynchronous function and returns a Result type.

License

This project is licensed under the MIT License - see the LICENSE file for details.

0.0.7

4 months ago

0.0.6

4 months ago

0.0.5

4 months ago

0.0.4

5 months ago

0.0.3

5 months ago

0.0.2

5 months ago

0.0.1

5 months ago