Skip to content

Latest commit

 

History

History
71 lines (51 loc) · 2.5 KB

README.md

File metadata and controls

71 lines (51 loc) · 2.5 KB

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