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.
You can install Resulta using npm:
npm install resulta
Or using yarn:
yarn add resulta
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);
}
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();
Returns an object representing an Ok result.
Returns an object representing an Err result.
Executes a provided asynchronous function and returns a Result
type.
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 }
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' }
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] }
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 }
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); // { isSome: true, value: 42 }
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); // { isSome: true, value: 4 }
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
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 }
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 }
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' }
This project is licensed under the MIT License - see the LICENSE file for details.