JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 15
  • Score
    100M100P100Q83518F
  • License MIT

resulta is a TypeScript utility for handling results in an expressive way, inspired by Rust's Result type.

Package Exports

  • resulta
  • resulta/dist/index.js
  • resulta/dist/index.mjs

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (resulta) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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 { tryCatchAsync } from 'resulta';

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

async function main() {
    const result = await tryCatchAsync(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.

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

Executes a provided asynchronous function and returns a Result type.

Additional Features

map<T, U, E>(result: Result<T, E>, fn: (value: T) => U): Result<U, E>

Transforms the value of a successful Result using the provided function.

Example:

import { map, ok } from 'resulta';

const result = ok(2);
const mappedResult = map(result, (value) => value * 2);

console.log(mappedResult); // { ok: true, value: 4 }

mapErr<T, E, F>(result: Result<T, E>, fn: (error: E) => F): Result<T, F>

Transforms the error of a failed Result using the provided function.

Example:

import { mapErr, err } from 'resulta';

const result = err('Error occurred');
const mappedError = mapErr(result, (error) => `Mapped: ${error}`);

console.log(mappedError); // { ok: false, error: 'Mapped: Error occurred' }

combine<T, E>(results: Result<T, E>[]): Result<T[], E>

Combines multiple Result objects into a single Result containing an array of values.

Example:

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

const results = [ok(1), ok(2), ok(3)];
const combined = combine(results);

console.log(combined); // { ok: true, value: [1, 2, 3] }

optionToResult<T, E>(option: Option<T>, error: E): Result<T, E>

Converts an Option to a Result, using the provided error if the Option is None.

Example:

import { optionToResult, some, none } from 'resulta';

const option = some(42);
const result = optionToResult(option, 'No value');

console.log(result); // { ok: true, value: 42 }

resultToOption<T, E>(result: Result<T, E>): Option<T>

Converts a Result to an Option, discarding the error if present.

Example:

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

const result = ok(42);
const option = resultToOption(result);

console.log(option); // { is_some: true, value: 42 }

mapOption<T, U>(option: Option<T>, fn: (value: T) => U): Option<U>

Transforms the value of an Option using the provided function.

Example:

import { mapOption, some } from 'resulta';

const option = some(2);
const mappedOption = mapOption(option, (value) => value * 2);

console.log(mappedOption); // { is_some: true, value: 4 }

unwrapOption<T>(option: Option<T>, defaultValue: T): T

Unwraps the value of an Option, returning a default value if it is None.

Example:

import { unwrapOption, none } from 'resulta';

const option = none();
const value = unwrapOption(option, 0);

console.log(value); // 0

flatMap<T, U, E>(result: Result<T, E>, fn: (value: T) => Result<U, E>): Result<U, E>

Transforms the value of a successful Result using the provided function, allowing chaining.

Example:

import { flatMap, ok } from 'resulta';

const result = ok(2);
const flatMapped = flatMap(result, (value) => ok(value * 2));

console.log(flatMapped); // { ok: true, value: 4 }

validate<T, E>(value: T, predicate: (value: T) => boolean, error: E): Result<T, E>

Validates a value using a predicate function, returning a Result.

Example:

import { validate } from 'resulta';

const result = validate(10, (value) => value > 5, 'Value must be greater than 5');

console.log(result); // { ok: true, value: 10 }

fromPromise<T, E>(promise: Promise<T>, errorHandler: (error: unknown) => E): Promise<Result<T, E>>

Converts a Promise to a Result, using an error handler for rejected Promises.

Example:

import { fromPromise } from 'resulta';

async function fetchData() {
    return 'data';
}

const result = await fromPromise(fetchData(), (error) => `Error: ${error}`);

console.log(result); // { ok: true, value: 'data' }

License

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