@frank-mayer/opsult
TypeScript icon, indicating that this package has built-in type declarations

2.4.0 • Public • Published

opsult

Basic overfiew

A implementation of Option, Result and Future types in TypeScript. If you know Rust, you will feel right at home.

Option

import { Option } from '@frank-mayer/opsult/Option';

const a: Option<number> = some(1);
const b: Option<number> = none();
const c: Option<number> = some(2);

a.andThen((x: number) => c.map((y: number) => x + y)); // some(3)
b.andThen((x: number) => c.map((y: number) => x + y)); // none()

Result

import { Result } from '@frank-mayer/opsult/Result';

const a: Result<number, string> = ok(1);
const b: Result<number, string> = err('error');
const c: Result<number, string> = ok(2);

a.andThen((x: number) => c.map((y: number) => x + y)); // ok(3)
b.andThen((x: number) => c.map((y: number) => x + y)); // err('error')

Future

import { Future } from '@frank-mayer/opsult/Future';

const fut: Future<number, string> = new Future<number, string>((ok, err) => {
    setTimeout(() => err("timeout"), 1000);

    complexAsyncOperation((x: number) => {
        ok(x);
    });
})

const res: Result<number, string> = await fut;

res.match({
    ok: (x: number) => console.log(x),
    err: (e: string) => console.error(e)
});

Read the docs to learn on how to use it.

Installation

npm i @frank-mayer/opsult

Readme

Keywords

none

Package Sidebar

Install

npm i @frank-mayer/opsult

Weekly Downloads

9

Version

2.4.0

License

MIT

Unpacked Size

54.4 kB

Total Files

15

Last publish

Collaborators

  • frank-mayer