npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

throwless

v1.0.1

Published

The Result and Option type from Rust ported to TypeScript.

Downloads

14

Readme

Throwless Build Status Coverage Status

Description

The Result and Option type from Rust ported to TypeScript.

Installation

npm install --save throwless

Usage

Result

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

const result = ok(42);

result.isOk(); // => true
result.isErr(); // => false

result.unwrap(); // => 42

result.map((i) => i * 2).unwrap(); // => 84


const anotherResult = err(new Error("Something went wrong"));

if(anotherResult.isErr()){
    // handle error here....
}

anotherResult.unwrap(); // => Throws Error "Something went wrong"

const mappedResult = anotherResult.mapErr(
    (e) => new Error("MyError: " + e.message)
);
    
mappedResult.unwrap(); // => Throws Error "MyError: Something went wrong"

Option

import {some, none, Option} from 'throwless';

const result = some(42);

result.isSome(); // => true
...


const result2 = none<number>();

console.log(result.xor(result2)); // => Some(42)

Helper Functions for Result and Option

export const ok = <T, E>(value: T): Result<T, E> => new Ok(value);
export const err = <T, E>(error: E): Result<T, E> => new Err(error);

export const emptyOk = <E>(): Result<void, E> => new Ok(undefined);
export const emptyErr = <T>(): Result<T, void> => new Err(undefined);

export const some = <T>(value: T): Option<T> => new Some(value);
export const none = <T>(): Option<T> => new None();

Interfaces

Result<T, E>

interface Result<T, E> {
    isErr(): boolean;
    isOk(): boolean;

    ok(): Option<T>;
    err(): Option<E>;

    map<U>(fn: (value: T) => U): Result<U, E>;
    mapErr<F>(fn: (error: E) => F): Result<T, F>;

    mapOrElse<U>(fnMap: (value: T) => U, fnOrElse: (error: E) => U): U;

    and<U>(res: Result<U, E>): Result<U, E>;
    andThen<U>(fn: (value: T) => Result<U, E>): Result<U, E>;

    or<F>(res: Result<T, F>): Result<T, F>;
    orElse<F>(fn: (error: E) =>  Result<T, F>): Result<T, F>;

    expectErr(error: any): E;
    expect(error: any): T;

    unwrap(): T;
    unwrapErr(): E;

    unwrapOr(value: T): T;
    unwrapOrElse(fn: (error: E) => T): T;
}

Option<T>

interface Option<T> {
    isSome(): boolean;
    isNone(): boolean;

    expect(error: any): T;
    unwrap(): T;
    unwrapOr(value: T): T;
    unwrapOrElse(fn: () => T): T;

    map<U>(fn: (value: T) => U): Option<U>;
    mapOr<U>(value: U, fn: (value: T) => U): U;
    mapOrElse<U>(fnMap: (value: T) => U, fnOrElse: () => U): U;

    okOr<E>(error: E): Result<T, E>;
    okOrElse<E>(fn: () => E): Result<T, E>;

    and<U>(optb: Option<U>): Option<U>;
    andThen<U>(fn: (value: T) => Option<U>): Option<U>;

    filter(fn: (value: T) => boolean): Option<T>;

    or(optb: Option<T>): Option<T>;
    orElse(fn: () => Option<T>): Option<T>;

    xor(optb: Option<T>): Option<T>;
}